Documentation
¶
Overview ¶
Package expr provides an abstract syntax tree implementation for an expression language.
Status in this fork ¶
Nothing in this repository uses it. It is inherited from upstream's `experimental/` tree, promoted to the top level by Track C (#44), and upstream is in the same position: bufbuild/protocompile's only importer of expr is experimental/internal/exprx, which nothing imports in turn. Wiring it to a consumer is upstream's design question, not this fork's.
It said "shared by various parts of the compiler" until #199, which is how an audit came to read the package as dead code and propose deleting it. It is not dead; it is unfinished, and upstream's.
Two things follow for anyone reading it here. The package is not covered by this repository's test suite, so nothing pins the behaviour described below. And it is NOT the implementation of RFC-001's `expression` parameter type, despite the name: that type is defined as an opaque text payload validated by the configured engine at run time, never parsed by protocompile (see ir.AnnotationParam.IsExpression). The grammar here — `func`, `for ... in`, `break`/`continue`/`return`, and the assignment operators — describes a general-purpose imperative language, which is more than either a validation predicate or a computed default needs.
Node Types ¶
Most types in this package represent AST nodes of some kind. Each such type contains a grammar for the lenient extension of the Protobuf grammar that it represents. The grammar is notated using regular expression syntax, with the following modifications:
- Whitespace is ignored.
- Literal strings (except in character classes) are represented with Go strings.
- Unquoted names refer to other productions.
Productions that represent a type from this package, a token.Kind, or a production used by a different type, are in PascalCase; all other productions are in camelCase and are scoped to that type.
These grammars are provided for illustration only: the complete grammar is ambiguous in the absence of greediness decisions, which, except where otherwise noted, neither affect the parsing of correct Protobuf files, nor are they specified and are subject to change.
Pointer-like Types ¶
Virtually all AST nodes in this library are "pointer-like" types, in that although they are not Go pointers, they do refer to something stored in a Context somewhere. Internally, they contain a pointer to a Context and a pointer to the compressed node representation inside the Context. This is done so that we can avoid spending an extra eight or twelve bytes per node-at-rest, and to minimize GC churn by avoiding pointer cycles in the in-memory representation of the AST.
All pointer-like types have a bool-returning IsZero method, which checks for the zero value. Pointer-like types should generally be passed by value, not by pointer; all of them have value receivers.
Index ¶
- type Block
- type BlockArgs
- type Call
- type CallArgs
- type Case
- type CaseArgs
- type Context
- type Control
- type ControlArgs
- type Error
- type Expr
- func (e Expr) AsBlock() Block
- func (e Expr) AsCall() Call
- func (e Expr) AsControl() Control
- func (e Expr) AsError() Error
- func (e Expr) AsFor() For
- func (e Expr) AsFunc() Func
- func (e Expr) AsIf() If
- func (e Expr) AsOp() Op
- func (e Expr) AsRecord() Record
- func (e Expr) AsSwitch() Switch
- func (e Expr) AsToken() Token
- func (e Expr) Span() source.Span
- type For
- type ForArgs
- type Func
- type FuncArgs
- type If
- type IfArgs
- type Kind
- type Nodes
- func (n *Nodes) Context() *Context
- func (n *Nodes) NewBlock(args BlockArgs) Block
- func (n *Nodes) NewCall(args CallArgs) Call
- func (n *Nodes) NewCase(args CaseArgs) Case
- func (n *Nodes) NewControl(args ControlArgs) Control
- func (n *Nodes) NewError(span source.Span) Error
- func (n *Nodes) NewFor(args ForArgs) For
- func (n *Nodes) NewIf(args IfArgs) If
- func (n *Nodes) NewOp(args OpArgs) Op
- func (n *Nodes) NewRecord(args RecordArgs) Record
- func (n *Nodes) NewSwitch(args SwitchArgs) Switch
- type Op
- type OpArgs
- type Param
- type Params
- func (p Params) AppendComma(v Param, comma token.Token)
- func (p Params) At(n int) Param
- func (p Params) Brackets() token.Token
- func (p Params) Comma(n int) token.Token
- func (p Params) Delete(n int)
- func (p Params) Insert(n int, v Param)
- func (p Params) InsertComma(n int, v Param, comma token.Token)
- func (p Params) Len() int
- func (p Params) SetAt(n int, v Param)
- func (p Params) SetBrackets(brackets token.Token)
- func (p Params) Span() source.Span
- type ParamsArgs
- type Record
- type RecordArgs
- type Switch
- type SwitchArgs
- type Token
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Block ¶
Block := `{` (Expr (`;` | `\n`))* Expr? `}`.
type BlockArgs ¶
BlockArgs is arguments for Nodes.NewBlock.
type Call ¶
Call is a function call/indexing expression, consisting of an expression followed by bracketed Params.
Grammar ¶
Call := Expr (`(` Params `)` | `[` Params `]` | `{` Params `}`)
type CallArgs ¶
CallArgs is arguments for Nodes.NewCall.
type Case ¶
Case is a case block within a Switch.
Grammar ¶
Case := (`case` (Expr `,`)* Expr? | `else`) `:`
func (Case) KeywordToken ¶
KeywordToken returns this switch's "case" or "else" keyword.
type CaseArgs ¶
CaseArgs is arguments for Nodes.NewCase.
type Context ¶
type Context struct {
// contains filtered or unexported fields
}
Context is book-keeping for all of the expressions parsed from a particular token.Stream.
This type does not represent an AST node; instead, it is intended to be embedded into the top-level struct for one of the other AST packages in the compiler.
func (*Context) FromID ¶
FromID implements id.Context.
type Control ¶
Control is a control flow expression, such as a break or continue, with an optional condition at the end.
Grammar ¶
Switch := (`break` | `continue` | `return`) Params (`if` Expr)?
func (Control) Condition ¶
Condition returns the condition expression for this expression.
This will be zero if the expression is not conditioned.
func (Control) IfToken ¶
IfToken returns the token for an `if` after this expression, if it has an associated condition.
func (Control) KeywordToken ¶
KeywordToken returns the token for the keyword for this expression.
type ControlArgs ¶
ControlArgs is arguments for Nodes.NewControl.
type Error ¶
Error is an expression produced due to a parse failure, where a non-zero expression was still needed.
Note that Error is not an [error].
type Expr ¶
Expr is any expression type in this package.
Values of this type can be obtained by calling an AsAny method on a expression type, such as Token.AsAny. It can be type-asserted back to any of the concrete expression types using its own As* methods.
This type is used in lieu of a putative Expr interface type to avoid heap allocations in functions that would return one of many different expression types.
Note that the expression and type grammars for the language are the same, so Expr appears in places you might expect to see a "Type" node.
func (Expr) AsBlock ¶
AsBlock converts an Expr into a Block, if that is its concrete type.
Otherwise, returns zero.
func (Expr) AsCall ¶
AsFor converts an Expr into a Call, if that is its concrete type.
Otherwise, returns zero.
func (Expr) AsControl ¶
AsControl converts an Expr into a Control, if that is its concrete type.
Otherwise, returns zero.
func (Expr) AsError ¶
AsError converts an Expr into an Error, if that is its concrete type.
Otherwise, returns zero.
func (Expr) AsFor ¶
AsFor converts an Expr into a For, if that is its concrete type.
Otherwise, returns zero.
func (Expr) AsFunc ¶
AsFunc converts an Expr into a Func, if that is its concrete type.
Otherwise, returns zero.
func (Expr) AsIf ¶
AsIf converts an Expr into a If, if that is its concrete type.
Otherwise, returns zero.
func (Expr) AsOp ¶
AsOp converts an Expr into a Op, if that is its concrete type.
Otherwise, returns zero.
func (Expr) AsRecord ¶
AsRecord converts an Expr into a Record, if that is its concrete type.
Otherwise, returns zero.
func (Expr) AsSwitch ¶
AsSwitch converts an Expr into a Switch, if that is its concrete type.
Otherwise, returns zero.
type For ¶
For is a loop which assigns items out of an iterator to variables in a Params.
Grammar ¶
For := `for` Params `in` Expr Block
type ForArgs ¶
ForArgs is arguments for Nodes.NewFor.
type Func ¶
Func is a function literal or definition. A function definition is simply a function that includes a name after the func keyword.
Grammar ¶
Func := `func` Ident? `(` Params `)` (`->` Expr Block? | Expr)?
type FuncArgs ¶
type FuncArgs struct {
Func token.Token
Name token.Token
Params Params
Arrow token.Token
Return Expr
Body Expr
}
FuncArgs is arguments for [Nodes.NewFunc].
type If ¶
If is a chain of if conditions.
Each If represents one of the blocks in the chain, which may be either an if, an else if, or an else. Only ifs and else ifs have conditions.
Grammar ¶
If := `if` Expr Block (`else` (If | Block))?
func (If) Condition ¶
Condition returns the condition expression for this if.
This will be zero if it is a final else block.
type IfArgs ¶
IfArgs is arguments for Nodes.NewIf.
type Kind ¶
type Kind int8
Kind is a kind of expression. There is one value of Kind for each expression type in this package.
func (Kind) DecodeDynID ¶
type Nodes ¶
type Nodes Context
Nodes provides storage for the various AST node types, and can be used to construct new ones.
func (*Nodes) NewControl ¶
func (n *Nodes) NewControl(args ControlArgs) Control
NewCase constructs a new Case in this context.
type Op ¶
Op is an operator expression, consisting of one or two expressions and an operator. This subsumes all of the operator expressions in the grammar, including property, assignment, and range expressions.
Unary do not have a left-hand side.
Grammar ¶
ExprToken := Expr Op Expr
Here, Op is any of the following tokens, ordered from least to greatest precedence (later tokens bind more tightly):
= := += -= *= /= %= , or and == != < > <= >= .. ..= to + - * / % - not (unary) .
Note that ExprCall has higher precedence than ., so -x.f() will group as -((x.f)()).
func (Op) Left ¶
Left returns the left-hand side of this expression. Unary operators do not have a left-hand side.
func (Op) OperatorToken ¶
OperatorToken returns the token for this expression's operator.
type OpArgs ¶
OpArgs is arguments for Nodes.NewOp.
type Param ¶
type Param struct {
// The name for this parameter. In a map context, this would be the key;
// in a function parameter context, this would be the argument name.
Name Expr
Colon token.Token
// The expression for this parameter. In a map context, this would be the value;
// in a function parameter context, this would be the argument type.
Expr Expr
If token.Token
// A condition for the parameter; not allowed in all productions.
Cond Expr
}
Param is a parameter in a Params.
type Params ¶
Params is a parameter list, which is used for all expression lists in the grammar.
Grammar ¶
Params := (Param `,`)* Param? Param := Expr (`:` Expr)? (`if` Expr)
func (Params) AppendComma ¶
AppendComma implements [Commas].
func (Params) Brackets ¶
Brackets returns the token tree for the brackets wrapping the argument list.
May be zero, if the user forgot to include brackets.
func (Params) InsertComma ¶
InsertComma implements [Commas].
func (Params) SetBrackets ¶
SetBrackets sets the token tree for the brackets wrapping the argument list.
type ParamsArgs ¶
ParamsArgs is arguments for [Nodes.NewParams].
type Record ¶
Record is a bracketed sequence of expressions, represented as a Params.
Grammar ¶
Record := `(` Params `)` | `[` Params `]` | `{` Params `}`
type RecordArgs ¶
type RecordArgs struct {
Entries Params
}
RecordArgs is arguments for Nodes.NewRecord.
type Switch ¶
Switch selects one of several blocks to execute based on a number of cases.
Grammar ¶
Switch := `switch` Expr? `{` Case* `}`
func (Switch) SwitchToken ¶
SwitchToken returns this expression's switch token.
type SwitchArgs ¶
SwitchArgs is arguments for Nodes.NewSwitch.
type Token ¶
type Token struct {
ExprContext *Context
// The token backing this expression. Must be [token.String], [token.Number],
// or [token.Ident].
token.Token
}
Token is an expression corresponding to a single token: a string, a number, or an identifier.
Grammar ¶
Token := token.Number | token.String | token.Ident
func (Token) Context ¶
Context returns this token's context.
This returns a Context rather than a token.Stream, which would otherwise be returned because Token embeds token.Token.