예제 #1
0
func TestParseExpr(t *testing.T) {
	positives := []string{
		`-> "String"`,
		"-> false",
		`== "bla"`,
		`-> eq($string, "bla")`,
		`-> 1`,
		`-> 1.0`,
		`-> -1.0`,
	}
	negatives := []string{
		"a",
		`= "bla"`,
	}
	for _, in := range positives {
		_, err := parser.NewParser().ParseExpr(in)
		if err != nil {
			t.Errorf("%s results in error: %s", in, err)
		}
	}
	for _, in := range negatives {
		_, err := parser.NewParser().ParseExpr(in)
		if err == nil {
			t.Errorf("%s results in no error", in)
		}
	}
}
예제 #2
0
파일: name.go 프로젝트: katydid/katydid
//FuncToName decompiles a function back into a name expression, if possible.
func FuncToName(f funcs.Bool) *ast.NameExpr {
	exprStr := funcs.Sprint(f)
	expr, err := relapseparser.NewParser().ParseExpr(exprStr)
	if err != nil {
		panic(err)
	}
	return exprToName(expr)
}
예제 #3
0
func TestNoEqualError(t *testing.T) {
	exprStr := Sprint(StringEq(ElemStrings(NewListOfString([]String{StringVar()}), IntConst(3)), StringConst("0123456789")))
	t.Logf(exprStr)
	e, err := parser.NewParser().ParseExpr(exprStr)
	if err != nil {
		t.Fatal(err)
	}
	b, err := compose.NewBool(e)
	if err != nil {
		t.Fatal(err)
	}
	f, err := compose.NewBoolFunc(b)
	if err != nil {
		t.Fatal(err)
	}
	if v, err := f.Eval(debug.NewStringValue("0")); err != nil {
		t.Fatal(err)
	} else if v {
		t.Fatalf("expected false")
	}
}
예제 #4
0
func TestParse(t *testing.T) {
	patternDecls := []string{
		`->hasQuotes("string_lit")`,
		`a->hasQuotes("string_lit")`,
		`@ref1
		#ref1 = a->eq($int, 123)
		`,
		`@main`,
		`A [
			a->eq($string, "123"), 
			b->eq($string, "456")
		]`,
		`A [
			a->eq($string, "aa"),
			b->eq($int, 123)
		]`,
		`A[
			*,
			[
				a->eq($string, "aa"),
				*
			]
		]`,
		`A: (a->eq($string, "aa"))*`,
		`Desc [
			*,
			[
				Src->contains($string, "1") ,
				[
					Src->contains($string, "2") ,
					(!(Src): *)*
				]
			]
		]`,
		`(MyParent->any() &  MyParent->any())`,
		`A :[
			*,
			a->eq($string, "aa"),
			*
		]`,
		`A: (
			* &
			a -> eq($string, "aa") &
			*
		)`,
		`A: (
			* |
			a -> eq($string, "aa") |
			* |
			!(*) |
			<empty>
		)`,
		`[
			*,
			a -> eq($string, "aa") ,
			*,
			( 
			  b -> contains($string, "bb") 
			  | (
			  	c -> contains($string, "cc")  &
			  	c -> contains($string, "see")  &
			  	(
			  		c -> contains($string, "sea")  |
			  		c -> contains($string, "ocean") 
			  	)
			  )
			  | d -> contains($string, "dd") 
			  | d -> contains($string, "dd") 
			  | d -> contains($string, "dd") 
			)
		]`,
		`_->greaterThanOne(1) `,
		`[
			(_ -> eq($int, 1) )*,
			bla -> any() 
		]`,
		`( a|b ) -> eq($int, 1) `,
		`( a|_|!(b) ) -> eq($int, 1) `,
		`"\"a" -> any() `,
		`(
			.a->any() |
			.b->any()
		)`,
		`(
			.a->any() &
			.b [ a->any(), b->any() ] &
			.c->any() &
			.d->any()
		)`,
		`a.b->any()`,
		`.a._.D.b123->any()`,
		`"Whats Up" == "E"`,
		`(* & * & *)`,
		`(* | * | * | ( * & * & * ))`,
		`[ * , * , *]`,
		`[ * , * , *,]`,
		`[(*)*, == "a", > 1, :: $string]`,
		`[(*)*, ->eq($string, "a"), > 1, :: $string]`,
		`[*,_ ->eq($string, "a"),_ > 1,_ :: $string]`,
		`[0 ->eq($string, "a"),1 > 1,2 :: $string]`,
		`(0|1) == "a"`,
		`(*)?`,
		`{a:* ; b:*}`,
		`{
			a:* ; 
			b:*;
		}`,
	}
	p := parser.NewParser()
	for i, patternDecl := range patternDecls {
		t.Logf("parsing %d", i)
		st, err := p.ParseGrammar(patternDecl)
		if err != nil {
			t.Errorf("err = %v, input = %s", err, patternDecl)
		} else {
			s := st.String()
			if s != patternDecl {
				t.Errorf("String function output = %s\n expected output = %s\n", s, patternDecl)
			}
		}
	}
}