コード例 #1
0
ファイル: protonum.go プロジェクト: katydid/katydid
//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
}
コード例 #2
0
ファイル: format_test.go プロジェクト: katydid/katydid
func TestFormatGrammar(t *testing.T) {
	format(t, " #main =  *", "#main = *")
	format(t,
		`//attachedcomment
	  *`,
		`//attachedcomment
*`)
	format(t,
		`//unattachedcomment

		*`,
		`//unattachedcomment
*`)
	//2 pattern declarations
	format(t,
		`#main = *
			#other = a->any()`,
		`#main = *
#other = a->any()`)
	//2 pattern declarations without a new line
	format(t,
		`#main = *	#other = a->any()`,
		`#main = * #other = a->any()`)
	//3 pattern declarations
	format(t,
		`*
			#other = a->any()

			#more = (*)*`,
		`*
#other = a->any()
#more = (*)*`)
	//treenode
	format(t,
		`#main =   "a":*`,
		`#main = a:*`)
	format(t,
		ast.NewGrammar(map[string]*ast.Pattern{"main": ast.NewReference("ref1"), "ref1": ast.NewReference("main")}).String(),
		`@ref1
#ref1=@main`)
}
コード例 #3
0
ファイル: simplify.go プロジェクト: katydid/katydid
func (this *simplifier) Grammar() *ast.Grammar {
	for name, p := range this.refs {
		this.refs[name] = this.Simplify(p)
	}
	return ast.NewGrammar(this.refs)
}
コード例 #4
0
ファイル: deriv.go プロジェクト: katydid/katydid
func simps(refs ast.RefLookup, patterns []*ast.Pattern) []*ast.Pattern {
	for i := range patterns {
		patterns[i] = NewSimplifier(ast.NewGrammar(refs)).Simplify(patterns[i])
	}
	return patterns
}