Documentation
¶
Overview ¶
Package ast declares data structures useful for parsed and checked abstract syntax trees
Index ¶
- func CheckedASTToCheckedExpr(ast *CheckedAST) (*exprpb.CheckedExpr, error)
- func ConstantToVal(c *exprpb.Constant) (ref.Val, error)
- func ReferenceInfoToReferenceExpr(info *ReferenceInfo) (*exprpb.Reference, error)
- func ValToConstant(v ref.Val) (*exprpb.Constant, error)
- type CheckedAST
- type ExprKind
- type ExprMatcher
- type NavigableCallExpr
- type NavigableComprehensionExpr
- type NavigableEntry
- type NavigableExpr
- type NavigableField
- type NavigableListExpr
- type NavigableMapExpr
- type NavigableSelectExpr
- type NavigableStructExpr
- type ReferenceInfo
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CheckedASTToCheckedExpr ¶
func CheckedASTToCheckedExpr(ast *CheckedAST) (*exprpb.CheckedExpr, error)
CheckedASTToCheckedExpr converts a CheckedAST to a CheckedExpr protobouf.
func ConstantToVal ¶
ConstantToVal converts a protobuf Constant to a CEL-native ref.Val.
func ReferenceInfoToReferenceExpr ¶
func ReferenceInfoToReferenceExpr(info *ReferenceInfo) (*exprpb.Reference, error)
ReferenceInfoToReferenceExpr converts a ReferenceInfo instance to a protobuf Reference suitable for serialization.
Types ¶
type CheckedAST ¶
type CheckedAST struct {
Expr *exprpb.Expr
SourceInfo *exprpb.SourceInfo
TypeMap map[int64]*types.Type
ReferenceMap map[int64]*ReferenceInfo
}
CheckedAST contains a protobuf expression and source info along with CEL-native type and reference information.
func CheckedExprToCheckedAST ¶
func CheckedExprToCheckedAST(checked *exprpb.CheckedExpr) (*CheckedAST, error)
CheckedExprToCheckedAST converts a CheckedExpr protobuf to a CheckedAST instance.
type ExprKind ¶
type ExprKind int
ExprKind represents the expression node kind.
const ( // UnspecifiedKind represents an unset expression with no specified properties. UnspecifiedKind ExprKind = iota // LiteralKind represents a primitive scalar literal. LiteralKind // IdentKind represents a simple variable, constant, or type identifier. IdentKind // SelectKind represents a field selection expression. SelectKind // CallKind represents a function call. CallKind // ListKind represents a list literal expression. ListKind // MapKind represents a map literal expression. MapKind // StructKind represents a struct literal expression. StructKind // ComprehensionKind represents a comprehension expression generated by a macro. ComprehensionKind )
type ExprMatcher ¶
type ExprMatcher func(NavigableExpr) bool
ExprMatcher takes a NavigableExpr in and indicates whether the value is a match.
This function type should be use with the `Match` and `MatchList` calls.
func AllMatcher ¶
func AllMatcher() ExprMatcher
AllMatcher returns true for all descendants of a NavigableExpr, effectively flattening them into a list.
Such a result would work well with subsequent MatchList calls.
func ConstantValueMatcher ¶
func ConstantValueMatcher() ExprMatcher
ConstantValueMatcher returns an ExprMatcher which will return true if the input NavigableExpr is comprised of all constant values, such as a simple literal or even list and map literal.
func FunctionMatcher ¶
func FunctionMatcher(funcName string) ExprMatcher
FunctionMatcher returns an ExprMatcher which will match NavigableExpr nodes of CallKind type whose function name is equal to `funcName`.
func KindMatcher ¶
func KindMatcher(kind ExprKind) ExprMatcher
KindMatcher returns an ExprMatcher which will return true if the input NavigableExpr.Kind() matches the specified `kind`.
type NavigableCallExpr ¶
type NavigableCallExpr interface {
FunctionName() string
Target() NavigableExpr
Args() []NavigableExpr
ReturnType() *types.Type
// contains filtered or unexported methods
}
NavigableCallExpr defines an interface for inspecting a function call and its arugments.
type NavigableComprehensionExpr ¶
type NavigableComprehensionExpr interface {
IterRange() NavigableExpr
IterVar() string
AccuVar() string
AccuInit() NavigableExpr
LoopCondition() NavigableExpr
LoopStep() NavigableExpr
Result() NavigableExpr
// contains filtered or unexported methods
}
NavigableComprehensionExpr defines an interface for inspecting a comprehension expression.
type NavigableEntry ¶
type NavigableEntry interface {
Key() NavigableExpr
Value() NavigableExpr
IsOptional() bool
// contains filtered or unexported methods
}
NavigableEntry defines an interface for inspecting a map entry.
type NavigableExpr ¶
type NavigableExpr interface {
ID() int64
Kind() ExprKind
Type() *types.Type
Parent() (NavigableExpr, bool)
Children() []NavigableExpr
ToExpr() *exprpb.Expr
//
// The Kind() must be equal to a CallKind for the conversion to be well-defined.
AsCall() NavigableCallExpr
//
// The Kind() must be equal to a ComprehensionKind for the conversion to be well-defined.
AsComprehension() NavigableComprehensionExpr
//
// The Kind() must be equal to an IdentKind for the conversion to be well-defined.
AsIdent() string
//
// The Kind() must be equal to a LiteralKind for the conversion to be well-defined.
AsLiteral() ref.Val
//
// The Kind() must be equal to a ListKind for the conversion to be well-defined.
AsList() NavigableListExpr
//
// The Kind() must be equal to a MapKind for the conversion to be well-defined.
AsMap() NavigableMapExpr
//
// The Kind() must be equal to a SelectKind for the conversion to be well-defined.
AsSelect() NavigableSelectExpr
//
// The Kind() must be equal to a StructKind for the conversion to be well-defined.
AsStruct() NavigableStructExpr
// contains filtered or unexported methods
}
NavigableExpr represents the base navigable expression value.
Depending on the `Kind()` value, the NavigableExpr may be converted to a concrete expression types as indicated by the `As<Kind>` methods.
NavigableExpr values and their concrete expression types should be nil-safe. Conversion of an expr to the wrong kind should produce a nil value.
func MatchDescendants ¶
func MatchDescendants(expr NavigableExpr, matcher ExprMatcher) []NavigableExpr
MatchDescendants takes a NavigableExpr and ExprMatcher and produces a list of NavigableExpr values of the descendants which match.
func MatchSubset ¶
func MatchSubset(exprs []NavigableExpr, matcher ExprMatcher) []NavigableExpr
MatchSubset applies an ExprMatcher to a list of NavigableExpr values and their descendants, producing a subset of NavigableExpr values which match.
func NavigateCheckedAST ¶
func NavigateCheckedAST(ast *CheckedAST) NavigableExpr
NavigateCheckedAST converts a CheckedAST to a NavigableExpr
type NavigableField ¶
type NavigableField interface {
FieldName() string
Value() NavigableExpr
IsOptional() bool
// contains filtered or unexported methods
}
NavigableField defines an interface for inspecting a struct field initialization.
type NavigableListExpr ¶
type NavigableListExpr interface {
Elements() []NavigableExpr
OptionalIndices() []int32
Size() int
// contains filtered or unexported methods
}
NavigableListExpr defines an interface for inspecting a list literal expression.
type NavigableMapExpr ¶
type NavigableMapExpr interface {
Entries() []NavigableEntry
Size() int
// contains filtered or unexported methods
}
NavigableMapExpr defines an interface for inspecting a map expression.
type NavigableSelectExpr ¶
type NavigableSelectExpr interface {
Operand() NavigableExpr
FieldName() string
IsTestOnly() bool
// contains filtered or unexported methods
}
NavigableSelectExpr defines an interface for inspecting a select expression.
type NavigableStructExpr ¶
type NavigableStructExpr interface {
TypeName() string
Fields() []NavigableField
// contains filtered or unexported methods
}
NavigableStructExpr defines an interfaces for inspecting a struct and its field initializers.
type ReferenceInfo ¶
ReferenceInfo contains a CEL native representation of an identifier reference which may refer to either a qualified identifier name, a set of overload ids, or a constant value from an enum.
func NewFunctionReference ¶
func NewFunctionReference(overloads ...string) *ReferenceInfo
NewFunctionReference creates a ReferenceInfo instance for a set of function overloads.
func NewIdentReference ¶
func NewIdentReference(name string, value ref.Val) *ReferenceInfo
NewIdentReference creates a ReferenceInfo instance for an identifier with an optional constant value.
func ReferenceExprToReferenceInfo ¶
func ReferenceExprToReferenceInfo(ref *exprpb.Reference) (*ReferenceInfo, error)
ReferenceExprToReferenceInfo converts a protobuf Reference into a CEL-native ReferenceInfo instance.
func (*ReferenceInfo) AddOverload ¶
func (r *ReferenceInfo) AddOverload(overloadID string)
AddOverload appends a function overload ID to the ReferenceInfo.
func (*ReferenceInfo) Equals ¶
func (r *ReferenceInfo) Equals(other *ReferenceInfo) bool
Equals returns whether two references are identical to each other.