ast

package
v0.17.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 19, 2023 License: Apache-2.0, BSD-3-Clause Imports: 5 Imported by: 27

Documentation

Overview

Package ast declares data structures useful for parsed and checked abstract syntax trees

Index

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

func ConstantToVal(c *exprpb.Constant) (ref.Val, error)

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.

func ValToConstant

func ValToConstant(v ref.Val) (*exprpb.Constant, error)

ValToConstant converts a CEL-native ref.Val to a protobuf Constant.

Only simple scalar types are supported by this method.

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 interface {
	// FunctionName returns the name of the function.
	FunctionName() string

	// Target returns the target of the expression if one is present.
	Target() NavigableExpr

	// Args returns the list of call arguments, excluding the target.
	Args() []NavigableExpr

	// ReturnType returns the result type of the call.
	ReturnType() *types.Type
	// contains filtered or unexported methods
}

NavigableCallExpr defines an interface for inspecting a function call and its arugments.

type NavigableComprehensionExpr interface {
	// IterRange returns the iteration range expression.
	IterRange() NavigableExpr

	// IterVar returns the iteration variable name.
	IterVar() string

	// AccuVar returns the accumulation variable name.
	AccuVar() string

	// AccuInit returns the accumulation variable initialization expression.
	AccuInit() NavigableExpr

	// LoopCondition returns the loop condition expression.
	LoopCondition() NavigableExpr

	// LoopStep returns the loop step expression.
	LoopStep() NavigableExpr

	// Result returns the comprehension result expression.
	Result() NavigableExpr
	// contains filtered or unexported methods
}

NavigableComprehensionExpr defines an interface for inspecting a comprehension expression.

type NavigableEntry interface {
	// Key returns the map entry key expression.
	Key() NavigableExpr

	// Value returns the map entry value expression.
	Value() NavigableExpr

	// IsOptional returns whether the entry is optional.
	IsOptional() bool
	// contains filtered or unexported methods
}

NavigableEntry defines an interface for inspecting a map entry.

type NavigableExpr interface {
	// ID of the expression as it appears in the AST
	ID() int64

	// Kind of the expression node. See ExprKind for the valid enum values.
	Kind() ExprKind

	// Type of the expression node.
	Type() *types.Type

	// Parent returns the parent expression node, if one exists.
	Parent() (NavigableExpr, bool)

	// Children returns a list of child expression nodes.
	Children() []NavigableExpr

	// ToExpr adapts this NavigableExpr to a protobuf representation.
	ToExpr() *exprpb.Expr

	// AsCall adapts the expr into a NavigableCallExpr
	//
	// The Kind() must be equal to a CallKind for the conversion to be well-defined.
	AsCall() NavigableCallExpr

	// AsComprehension adapts the expr into a NavigableComprehensionExpr.
	//
	// The Kind() must be equal to a ComprehensionKind for the conversion to be well-defined.
	AsComprehension() NavigableComprehensionExpr

	// AsIdent adapts the expr into an identifier string.
	//
	// The Kind() must be equal to an IdentKind for the conversion to be well-defined.
	AsIdent() string

	// AsLiteral adapts the expr into a constant ref.Val.
	//
	// The Kind() must be equal to a LiteralKind for the conversion to be well-defined.
	AsLiteral() ref.Val

	// AsList adapts the expr into a NavigableListExpr.
	//
	// The Kind() must be equal to a ListKind for the conversion to be well-defined.
	AsList() NavigableListExpr

	// AsMap adapts the expr into a NavigableMapExpr.
	//
	// The Kind() must be equal to a MapKind for the conversion to be well-defined.
	AsMap() NavigableMapExpr

	// AsSelect adapts the expr into a NavigableSelectExpr.
	//
	// The Kind() must be equal to a SelectKind for the conversion to be well-defined.
	AsSelect() NavigableSelectExpr

	// AsStruct adapts the expr into a NavigableStructExpr.
	//
	// 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(ast *CheckedAST) NavigableExpr

NavigateCheckedAST converts a CheckedAST to a NavigableExpr

type NavigableField interface {
	// FieldName returns the name of the field.
	FieldName() string

	// Value returns the field initialization expression.
	Value() NavigableExpr

	// IsOptional returns whether the field is optional.
	IsOptional() bool
	// contains filtered or unexported methods
}

NavigableField defines an interface for inspecting a struct field initialization.

type NavigableListExpr interface {
	// Elements returns the list elements as navigable expressions.
	Elements() []NavigableExpr

	// OptionalIndicies returns the list of optional indices in the list literal.
	OptionalIndices() []int32

	// Size returns the number of elements in the list.
	Size() int
	// contains filtered or unexported methods
}

NavigableListExpr defines an interface for inspecting a list literal expression.

type NavigableMapExpr interface {
	// Entries returns the map key value pairs as NavigableEntry values.
	Entries() []NavigableEntry

	// Size returns the number of entries in the map.
	Size() int
	// contains filtered or unexported methods
}

NavigableMapExpr defines an interface for inspecting a map expression.

type NavigableSelectExpr interface {
	// Operand returns the selection operand expression.
	Operand() NavigableExpr

	// FieldName returns the field name being selected from the operand.
	FieldName() string

	// IsTestOnly indicates whether the select expression is a presence test generated by a macro.
	IsTestOnly() bool
	// contains filtered or unexported methods
}

NavigableSelectExpr defines an interface for inspecting a select expression.

type NavigableStructExpr interface {
	// TypeName returns the struct type name.
	TypeName() string

	// Fields returns the set of field initializers in the struct expression as NavigableField values.
	Fields() []NavigableField
	// contains filtered or unexported methods
}

NavigableStructExpr defines an interfaces for inspecting a struct and its field initializers.

type ReferenceInfo

type ReferenceInfo struct {
	Name        string
	OverloadIDs []string
	Value       ref.Val
}

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL