Example #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)
		}
	}
}
Example #2
0
func testInject(t *testing.T, m *mem.Mem) bool {
	parser := reflectparser.NewReflectParser()
	parser.Init(reflect.ValueOf(&Number{Num: 456}))
	res, err := m.Validate(parser)
	if err != nil {
		t.Fatal(err)
	}
	return res
}
Example #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)
	}
}
Example #4
0
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
}
Example #5
0
//Validate validates the parser with the given memoizing object, containing the grammar, for efficiency.
func Validate(m *mem.Mem, p parser.Interface) (bool, error) {
	return m.Validate(p)
}