Beispiel #1
0
//FieldNamesToNumbers rewrites field names contained in the grammar to their respective field numbers found in the protocol buffer filedescriptorset.
//This allows for more speedy field comparisons in validation when used in conjunction with the ProtoNumParser.
func FieldNamesToNumbers(pkgName, msgName string, desc *descriptor.FileDescriptorSet, grammar *ast.Grammar) (*ast.Grammar, error) {
	g := grammar.Clone()
	descMap, err := proto.NewDescriptorMap(pkgName, msgName, desc)
	if err != nil {
		return nil, err
	}
	root := descMap.GetRoot()
	refs := ast.NewRefLookup(g)
	nameToNumber := &nameToNumber{
		refs:    make(map[string]*context),
		descMap: descMap,
	}
	nameToNumber.refs["main"] = &context{root, false}
	newRefs := make(map[string]*ast.Pattern)
	oldContexts := 0
	newContexts := 1
	for oldContexts != newContexts {
		oldContexts = newContexts
		for name, context := range nameToNumber.refs {
			newp, err := nameToNumber.translate(context, refs[name])
			if err != nil {
				return nil, err
			}
			newRefs[name] = newp
		}
		newContexts = len(nameToNumber.refs)
	}
	return ast.NewGrammar(newRefs), nil
}
Beispiel #2
0
func test(t *testing.T, name string, g *ast.Grammar, p parser.Interface, expected bool, desc string, record bool) {
	if interp.HasRecursion(g) {
		t.Skipf("convert was not designed to handle recursion")
	}
	if strings.HasPrefix(name, "GoBigOr") {
		t.Skipf("too big to fail: the number of Ors creates a state space explosion")
	}
	var a *auto.Auto
	var err error
	if record {
		a, err = auto.CompileRecord(g)
	} else {
		a, err = auto.Compile(g)
	}
	if err != nil {
		t.Fatal(err)
	}
	match, err := a.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)
	}
}
Beispiel #3
0
func test(t *testing.T, g *ast.Grammar, p parser.Interface, expected bool, desc string) {
	if interp.HasRecursion(g) {
		t.Skipf("interp was not designed to handle left recursion")
	}
	p = debug.NewLogger(p, debug.NewLineLogger())
	match, err := interp.Interpret(g, 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)
	}
}
Beispiel #4
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)
	}
}