コード例 #1
0
ファイル: name.go プロジェクト: katydid/katydid
//NameToFunc compiles a parsed name expression into a function.
func NameToFunc(n *ast.NameExpr) funcs.Bool {
	typ := n.GetValue()
	switch v := typ.(type) {
	case *ast.Name:
		if v.DoubleValue != nil {
			return funcs.DoubleEq(funcs.DoubleVar(), funcs.DoubleConst(v.GetDoubleValue()))
		} else if v.IntValue != nil {
			return funcs.IntEq(funcs.IntVar(), funcs.IntConst(v.GetIntValue()))
		} else if v.UintValue != nil {
			return funcs.UintEq(funcs.UintVar(), funcs.UintConst(v.GetUintValue()))
		} else if v.BoolValue != nil {
			return funcs.BoolEq(funcs.BoolVar(), funcs.BoolConst(v.GetBoolValue()))
		} else if v.StringValue != nil {
			return funcs.StringEq(funcs.StringVar(), funcs.StringConst(v.GetStringValue()))
		} else if v.BytesValue != nil {
			return funcs.BytesEq(funcs.BytesVar(), funcs.BytesConst(v.GetBytesValue()))
		}
		panic(fmt.Sprintf("unknown name expr name %#v", v))
	case *ast.AnyName:
		return funcs.BoolConst(true)
	case *ast.AnyNameExcept:
		return funcs.Not(NameToFunc(v.GetExcept()))
	case *ast.NameChoice:
		return funcs.Or(NameToFunc(v.GetLeft()), NameToFunc(v.GetRight()))
	}
	panic(fmt.Sprintf("unknown name expr typ %T", typ))
}
コード例 #2
0
ファイル: walk_test.go プロジェクト: katydid/katydid
func topDownName(n *ast.NameExpr, f func(*ast.NameExpr)) {
	f(n)
	typ := n.GetValue()
	switch v := typ.(type) {
	case *ast.Name, *ast.AnyName:
		//do nothing
	case *ast.AnyNameExcept:
		topDownName(v.GetExcept(), f)
	case *ast.NameChoice:
		topDownName(v.GetLeft(), f)
		topDownName(v.GetRight(), f)
	default:
		panic(fmt.Sprintf("unknown name typ %T", typ))
	}
}
コード例 #3
0
ファイル: protonum.go プロジェクト: katydid/katydid
func (this *nameToNumber) translateName(current *context, name *ast.NameExpr, child *ast.Pattern) (*ast.Pattern, error) {
	switch n := name.GetValue().(type) {
	case *ast.Name:
		if current.index {
			if n.IntValue == nil {
				return nil, &errExpectedArray{name.String(), current}
			}
			c := &context{current.msg, false}
			newp, err := this.translate(c, child)
			if err != nil {
				return nil, err
			}
			return ast.NewTreeNode(name, newp), nil
		}
		if n.StringValue == nil {
			return nil, &errExpectedField{name.String(), current}
		}
		f := getField(this.descMap.LookupFields(current.msg), n.GetStringValue())
		if f == nil {
			return nil, &errUnknownField{name.String(), current}
		}
		msg := this.descMap.LookupMessage(f)
		c := &context{msg, f.IsRepeated()}
		newp, err := this.translate(c, child)
		if err != nil {
			return nil, err
		}
		newName := ast.NewUintName(uint64(f.GetNumber()))
		return ast.NewTreeNode(newName, newp), nil
	case *ast.AnyName:
		if current.index {
			c := &context{current.msg, false}
			newp, err := this.translate(c, child)
			if err != nil {
				return nil, err
			}
			return ast.NewTreeNode(name, newp), nil
		} else {
			return nil, &errAnyFieldNotSupported{name.String()}
		}
	case *ast.AnyNameExcept:
		return nil, &errAnyNameExceptNotSupported{name.String()}
	case *ast.NameChoice:
		l, err1 := this.translateName(current, n.GetLeft(), child)
		r, err2 := this.translateName(current, n.GetRight(), child)
		return ast.NewOr(l, r), anyErr(err1, err2)
	}
	panic(fmt.Sprintf("unknown name typ %T", name))
}