예제 #1
0
func bench(b *testing.B, grammar *ast.Grammar, parsers []testsuite.ResetParser, record bool) {
	num := len(parsers)
	if *bN != 0 {
		b.N = *bN
	}
	var m *mem.Mem
	var err error
	if record {
		m, err = mem.NewRecord(grammar)
	} else {
		m, err = mem.New(grammar)
	}
	if err != nil {
		b.Fatal(err)
	}
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		if err := parsers[i%num].Reset(); err != nil {
			b.Fatal(err)
		}
		if _, err := m.Validate(parsers[i%num]); err != nil {
			b.Fatal(err)
		}
	}
}
예제 #2
0
func TestInject(t *testing.T) {
	grammar := injectNumber.Grammar()
	m, err := mem.New(grammar)
	if err != nil {
		t.Fatal(err)
	}
	c := &funcs.Context{Value: int64(0)}
	m.SetContext(c)
	c.Value = int64(456)
	if !testInject(t, m) {
		t.Fatalf("expected match")
	}
	c.Value = int64(123)
	if testInject(t, m) {
		t.Fatalf("expected non match")
	}
}
예제 #3
0
func test(t *testing.T, g *ast.Grammar, p parser.Interface, expected bool, desc string, record bool) {
	if interp.HasRecursion(g) {
		t.Skipf("interp was not designed to handle left recursion")
	}
	var m *mem.Mem
	var err error
	if record {
		m, err = mem.NewRecord(g)
	} else {
		m, err = mem.New(g)
	}
	if err != nil {
		t.Fatal(err)
	}
	match, err := m.Validate(p)
	if err != nil {
		t.Fatal(err)
	}
	if match != expected {
		t.Fatalf("Expected %v on given \n%s\n on \n%s", expected, g.String(), desc)
	}
}
예제 #4
0
파일: compile.go 프로젝트: katydid/katydid
func compile(g *ast.Grammar, record bool) (*Auto, error) {
	var m *mem.Mem
	var err error
	if record {
		m, err = mem.NewRecord(g)
	} else {
		m, err = mem.New(g)
	}
	if err != nil {
		return nil, err
	}
	if err := m.Compile(); err != nil {
		return nil, err
	}
	a := &Auto{
		calls:           m.Calls,
		returns:         newReturns(m.Returns),
		escapables:      m.Escapables,
		start:           m.Start,
		stateToNullable: m.StateToNullable,
		accept:          m.Accept,
	}
	return a, nil
}
예제 #5
0
파일: relapse.go 프로젝트: katydid/katydid
//Prepare creates a memoizing object given the grammar.
//The memoizing object is used to memorize any previous states created from previous validations.
//This results in a more efficient execution each time the memoizing object is used to validate a parser.
func Prepare(g *ast.Grammar) (*mem.Mem, error) {
	return mem.New(g)
}