Documentation
¶
Overview ¶
Package ast defines the abstract syntax tree (AST) node types produced by the parser and consumed by the interpreter.
The tree is organized into two interface hierarchies — Expr for expressions and Stmt for statements — both extending the common Node interface. Every node carries the source position of its first token via Pos, and the position just past its last token via End, enabling precise error reporting.
The shape follows the ESTree specification loosely, adapted to idiomatic Go: concrete struct types with exported fields, small marker methods to seal the interfaces, and no visitor indirection (the interpreter type-switches).
ECMA-262 Reference: §13–§16 (Expressions, Statements, Functions, Programs).
Index ¶
- func Dump(n Node) string
- type ArrayLit
- type ArrowFunc
- type AssignExpr
- type AssignPattern
- type AwaitExpr
- type BigIntLit
- type BinaryExpr
- type BlockStmt
- type BoolLit
- type BreakStmt
- type CallExpr
- type CatchClause
- type ClassDecl
- type ClassDef
- type ClassExpr
- type ClassMember
- type ConditionalExpr
- type ContinueStmt
- type DebuggerStmt
- type DoWhileStmt
- type EmptyStmt
- type ExportSpecifier
- type ExportStmt
- type Expr
- type ExprStmt
- type ForInStmt
- type ForStmt
- type FuncDecl
- type FuncDef
- type FuncExpr
- type Ident
- type IfStmt
- type ImportCall
- type LabeledStmt
- type LogicalExpr
- type MemberExpr
- type NewExpr
- type Node
- type NullLit
- type NumberLit
- type ObjectLit
- type PrivateIdent
- type Program
- type Property
- type PropertyKind
- type RegexLit
- type RestElement
- type ReturnStmt
- type SequenceExpr
- type SpreadElement
- type Stmt
- type StringLit
- type SuperExpr
- type SwitchCase
- type SwitchStmt
- type TaggedTemplateExpr
- type TemplateElement
- type TemplateLit
- type ThisExpr
- type ThrowStmt
- type TryStmt
- type UnaryExpr
- type UpdateExpr
- type VarDecl
- type VarDeclarator
- type WhileStmt
- type WithStmt
- type YieldExpr
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Dump ¶
Dump returns a human-readable, indented tree rendering of an AST node. It is intended for debugging and for the CLI's --ast mode; the format is not stable and should not be parsed.
The dumper is reflection-based so it automatically covers every node type without a per-type visitor: for each node it prints the concrete type name followed by its exported fields, recursing into nested Node values, slices, and pointers.
Types ¶
type ArrayLit ¶
type ArrayLit struct {
Lbracket token.Pos
Rbracket token.Pos
Elements []Expr // element may be nil (hole) or *SpreadElement
// TrailingComma is set when the literal ends with a comma (e.g. `[a,]`). It
// is benign for an array expression but, when the literal is refined into an
// ArrayAssignmentPattern, an elision/comma may not follow a rest element.
TrailingComma bool
}
ArrayLit is an array literal such as [1, 2, ...rest]. A nil element denotes an elision (a hole), e.g. [1, , 3].
type ArrowFunc ¶
type ArrowFunc struct {
Start token.Pos
Params []Expr // Ident, patterns, or *RestElement
Body Node // *BlockStmt or Expr
Async bool
Expression bool // true when Body is an expression (concise body)
Strict bool // strict-mode code (own directive or lexically nested in strict code)
// Source is the exact source text of the arrow function, returned by
// Function.prototype.toString ([[SourceText]], §20.2.3.5).
Source string
}
ArrowFunc is an arrow function (params) => body. Body is either a *BlockStmt or an expression (when Expression is true).
type AssignExpr ¶
type AssignExpr struct {
Target Expr // assignment target (Ident, MemberExpr, or destructuring pattern)
OpPos token.Pos
Op token.Type
Value Expr
}
AssignExpr is an assignment such as x = y or x += y. Op is token.ASSIGN for a plain assignment, or a compound-assignment token otherwise.
func (*AssignExpr) End ¶
func (e *AssignExpr) End() token.Pos
func (*AssignExpr) Pos ¶
func (e *AssignExpr) Pos() token.Pos
type AssignPattern ¶
AssignPattern is a binding with a default value, target = default, used in parameter lists and destructuring patterns.
func (*AssignPattern) End ¶
func (e *AssignPattern) End() token.Pos
func (*AssignPattern) Pos ¶
func (e *AssignPattern) Pos() token.Pos
type BigIntLit ¶
type BigIntLit struct {
ValuePos token.Pos
Raw string // full spelling including trailing 'n'
Digits string // normalized decimal digits
}
BigIntLit is a BigInt literal such as 123n. Digits holds the decimal digit string without the trailing 'n'.
type BinaryExpr ¶
BinaryExpr is a binary operation such as a + b, a < b, a instanceof b. It does NOT include the short-circuiting logical operators (see LogicalExpr).
func (*BinaryExpr) End ¶
func (e *BinaryExpr) End() token.Pos
func (*BinaryExpr) Pos ¶
func (e *BinaryExpr) Pos() token.Pos
type BreakStmt ¶
type BreakStmt struct {
Keyword token.Pos
Label *Ident // nil for an unlabeled break
EndPos token.Pos
}
BreakStmt is a break statement, optionally with a label.
type CallExpr ¶
type CallExpr struct {
Callee Expr
Arguments []Expr // may contain *SpreadElement
Rparen token.Pos
Optional bool
}
CallExpr is a function call callee(args). When Optional is true it is an optional call (fn?.()).
type CatchClause ¶
type CatchClause struct {
Keyword token.Pos
Param Expr // Ident or destructuring pattern, may be nil
Body *BlockStmt
}
CatchClause is the catch (param) { ... } part of a try statement. Param is nil for an optional-catch-binding form (catch { ... }).
type ClassDef ¶
type ClassDef struct {
Name *Ident // nil for anonymous class expressions
SuperClass Expr // nil when the class has no `extends` clause
Members []*ClassMember
Lbrace token.Pos
Rbrace token.Pos
}
ClassDef holds the shared shape of a class declaration or expression.
type ClassMember ¶
type ClassMember struct {
KeyPos token.Pos
Key Expr // Ident, StringLit, NumberLit, PrivateIdent, or computed
Value Expr // *FuncExpr for methods, initializer Expr for fields
Kind PropertyKind // PropInit (field/method), PropGet, or PropSet
Static bool
Computed bool
// Field reports whether this member is a class field (as opposed to a
// method or accessor).
Field bool
// StaticBlock holds the body of a `static { ... }` initialization block.
// When non-nil the member is a static block (Static is true and Key is nil);
// it is neither a field nor a method.
StaticBlock *BlockStmt
}
ClassMember is a single element of a class body: a method, accessor, or field.
func (*ClassMember) End ¶
func (m *ClassMember) End() token.Pos
func (*ClassMember) Pos ¶
func (m *ClassMember) Pos() token.Pos
type ConditionalExpr ¶
ConditionalExpr is the ternary operator test ? cons : alt.
func (*ConditionalExpr) End ¶
func (e *ConditionalExpr) End() token.Pos
func (*ConditionalExpr) Pos ¶
func (e *ConditionalExpr) Pos() token.Pos
type ContinueStmt ¶
type ContinueStmt struct {
Keyword token.Pos
Label *Ident // nil for an unlabeled continue
EndPos token.Pos
}
ContinueStmt is a continue statement, optionally with a label.
func (*ContinueStmt) End ¶
func (s *ContinueStmt) End() token.Pos
func (*ContinueStmt) Pos ¶
func (s *ContinueStmt) Pos() token.Pos
type DebuggerStmt ¶
DebuggerStmt is a debugger statement.
func (*DebuggerStmt) End ¶
func (s *DebuggerStmt) End() token.Pos
func (*DebuggerStmt) Pos ¶
func (s *DebuggerStmt) Pos() token.Pos
type DoWhileStmt ¶
DoWhileStmt is a do/while loop.
func (*DoWhileStmt) End ¶
func (s *DoWhileStmt) End() token.Pos
func (*DoWhileStmt) Pos ¶
func (s *DoWhileStmt) Pos() token.Pos
type ExportSpecifier ¶
ExportSpecifier is one entry of an `export { local as exported }` clause.
type ExportStmt ¶
type ExportStmt struct {
Keyword token.Pos
Decl Stmt // exported declaration (*VarDecl/*FuncDecl/*ClassDecl); nil otherwise
DefaultExpr Expr // `export default <expr>`; nil otherwise
Specifiers []*ExportSpecifier // `export { … }` entries
Default bool // true for `export default …`
// Source is the decoded `from` module specifier of a re-export
// (`export { … } from 'src'`, `export * from 'src'`), or "" when absent.
Source string
// Star is true for `export * [as name] from 'src'`; StarName holds the
// namespace alias of `export * as name from 'src'` ("" for a bare `export *`).
Star bool
StarName string
EndPos token.Pos
}
ExportStmt is an ES-module export declaration. It appears only in module code. The variants it captures are:
- export var/let/const/function/class … (Decl set, Default false)
- export default function/class … (Decl set, Default true)
- export default AssignmentExpression; (DefaultExpr set, Default true)
- export { a, b as c }; (Specifiers set)
func (*ExportStmt) End ¶
func (s *ExportStmt) End() token.Pos
func (*ExportStmt) Pos ¶
func (s *ExportStmt) Pos() token.Pos
type Expr ¶
type Expr interface {
Node
// contains filtered or unexported methods
}
Expr is the interface implemented by all expression nodes.
type ExprStmt ¶
type ExprStmt struct {
X Expr
// Directive holds the string value when this statement is a directive
// prologue entry such as "use strict"; otherwise it is empty.
Directive string
}
ExprStmt is an expression used in statement position.
type ForInStmt ¶
type ForInStmt struct {
Keyword token.Pos
Left Node // *VarDecl or Expr
Right Expr
Body Stmt
Of bool // true for for-of, false for for-in
Await bool // true for `for await (... of ...)`
}
ForInStmt is a for-in or for-of loop. Of distinguishes the two. Left is a *VarDecl or an assignment target Expr.
type ForStmt ¶
type ForStmt struct {
Keyword token.Pos
Init Node // *VarDecl, Expr, or nil
Test Expr
Update Expr
Body Stmt
}
ForStmt is a C-style for loop: for (init; test; update) body. Any of Init, Test, Update may be nil. Init is either a *VarDecl or an Expr.
type FuncDef ¶
type FuncDef struct {
Name *Ident // nil for anonymous function expressions
Params []Expr // Ident, patterns, AssignPattern, or *RestElement
Body *BlockStmt
Async bool
Generator bool
// Strict reports whether this function's body runs in strict mode, either
// because it carries its own "use strict" directive prologue or because it
// is lexically nested in strict code (a strict script, module, or function).
Strict bool
// Source is the exact source text of the whole function/method definition —
// the [[SourceText]] returned by Function.prototype.toString (§20.2.3.5). It
// is empty for functions with no available source (the Function constructor
// builds its own), in which case toString uses the NativeFunction form.
Source string
}
FuncDef holds the shared shape of a function: its (optional) name, parameter list, and body. It is embedded by FuncDecl, FuncExpr, and object/class methods so the interpreter can treat all callable definitions uniformly.
type FuncExpr ¶
FuncExpr is a function expression: function (params) { body } or a named function expression. It shares its shape with function declarations via the embedded FuncDef.
type Ident ¶
type Ident struct {
NamePos token.Pos
Name string
// Parenthesized marks an identifier that appeared as the immediate content of
// a ParenthesizedExpression, e.g. `(fn)`. Such an operand is a
// CoverParenthesizedExpression, whose IsIdentifierRef is false (§13.2.5.2), so
// it does not trigger NamedEvaluation: `(fn) = function(){}` leaves the
// function anonymous even though a bare `fn = function(){}` would name it.
Parenthesized bool
}
Ident is an identifier reference such as a variable name.
type ImportCall ¶
type ImportCall struct {
Keyword token.Pos
Specifier Expr
Options Expr // the optional second argument; nil when absent
Rparen token.Pos
}
ImportCall is a dynamic import() expression (ECMA-262 ES2020): import(Specifier) or import(Specifier, Options). It evaluates to a Promise for the imported module's namespace object. It is distinct from an import declaration and from the import.meta meta-property.
func (*ImportCall) End ¶
func (e *ImportCall) End() token.Pos
func (*ImportCall) Pos ¶
func (e *ImportCall) Pos() token.Pos
type LabeledStmt ¶
LabeledStmt is a labeled statement label: stmt.
func (*LabeledStmt) End ¶
func (s *LabeledStmt) End() token.Pos
func (*LabeledStmt) Pos ¶
func (s *LabeledStmt) Pos() token.Pos
type LogicalExpr ¶
LogicalExpr is a short-circuiting logical operation: &&, ||, or ?? (nullish coalescing). It is distinct from BinaryExpr because evaluation order and short-circuit semantics differ.
func (*LogicalExpr) End ¶
func (e *LogicalExpr) End() token.Pos
func (*LogicalExpr) Pos ¶
func (e *LogicalExpr) Pos() token.Pos
type MemberExpr ¶
type MemberExpr struct {
Object Expr
Property Expr // Ident (dot) / PrivateIdent, or arbitrary Expr (computed)
EndPos token.Pos
Computed bool
Optional bool
}
MemberExpr is a property access a.b or a[b]. When Optional is true the access is part of an optional chain (a?.b).
func (*MemberExpr) End ¶
func (e *MemberExpr) End() token.Pos
func (*MemberExpr) Pos ¶
func (e *MemberExpr) Pos() token.Pos
type Node ¶
type Node interface {
// Pos returns the position of the node's first token.
Pos() token.Pos
// End returns the position immediately after the node's last token.
End() token.Pos
}
Node is the interface implemented by every AST node.
type NumberLit ¶
NumberLit is a numeric literal. Value holds the parsed IEEE-754 value; Raw preserves the original spelling (e.g. "0xff", "1_000").
type PrivateIdent ¶
PrivateIdent is a private class member name such as #count. It is only valid inside a class body (as a member key or in a `#x in obj` check).
func (*PrivateIdent) End ¶
func (e *PrivateIdent) End() token.Pos
func (*PrivateIdent) Pos ¶
func (e *PrivateIdent) Pos() token.Pos
type Program ¶
type Program struct {
Source string // source name (filename or "<eval>")
Body []Stmt // top-level statements
// Strict reports whether the program body began with a "use strict"
// directive prologue.
Strict bool
// EndPos is the position of the trailing EOF token.
EndPos token.Pos
}
Program is the root node of a parsed source file (a Script goal symbol). It holds the top-level statement list plus source metadata.
type Property ¶
type Property struct {
KeyPos token.Pos
Key Expr // Ident, StringLit, NumberLit, or computed Expr
Value Expr // value expression (nil for spread's separate handling)
Kind PropertyKind
// Computed reports whether the key was written as [expr].
Computed bool
// Shorthand reports whether this was written as { x } rather than { x: x }.
Shorthand bool
// Method reports whether the value is a concise method definition.
Method bool
}
Property is a single member of an object literal.
type PropertyKind ¶
type PropertyKind int
PropertyKind classifies an object-literal or class member.
const ( // PropInit is a normal data property (key: value or shorthand). PropInit PropertyKind = iota // PropGet is a getter accessor (get key() {...}). PropGet // PropSet is a setter accessor (set key(v) {...}). PropSet // PropSpread is a spread element (...expr). PropSpread )
type RestElement ¶
RestElement is a rest binding ...target used in parameter lists and destructuring patterns.
func (*RestElement) End ¶
func (e *RestElement) End() token.Pos
func (*RestElement) Pos ¶
func (e *RestElement) Pos() token.Pos
type ReturnStmt ¶
ReturnStmt is a return statement. Argument is nil for a bare return.
func (*ReturnStmt) End ¶
func (s *ReturnStmt) End() token.Pos
func (*ReturnStmt) Pos ¶
func (s *ReturnStmt) Pos() token.Pos
type SequenceExpr ¶
type SequenceExpr struct {
Exprs []Expr
}
SequenceExpr is a comma expression (a, b, c) evaluating to its last operand.
func (*SequenceExpr) End ¶
func (e *SequenceExpr) End() token.Pos
func (*SequenceExpr) Pos ¶
func (e *SequenceExpr) Pos() token.Pos
type SpreadElement ¶
SpreadElement is a spread/rest element ...expr used in array/call/object contexts.
func (*SpreadElement) End ¶
func (e *SpreadElement) End() token.Pos
func (*SpreadElement) Pos ¶
func (e *SpreadElement) Pos() token.Pos
type Stmt ¶
type Stmt interface {
Node
// contains filtered or unexported methods
}
Stmt is the interface implemented by all statement nodes.
type StringLit ¶
StringLit is a string literal. Value holds the decoded contents; Raw includes the surrounding quotes.
type SuperExpr ¶
SuperExpr is the `super` keyword (valid only as a member/call base inside class methods).
type SwitchCase ¶
SwitchCase is one case/default clause. Test is nil for the default clause.
type SwitchStmt ¶
SwitchStmt is a switch statement.
func (*SwitchStmt) End ¶
func (s *SwitchStmt) End() token.Pos
func (*SwitchStmt) Pos ¶
func (s *SwitchStmt) Pos() token.Pos
type TaggedTemplateExpr ¶
type TaggedTemplateExpr struct {
Tag Expr
Quasi *TemplateLit
}
TaggedTemplateExpr is a tagged template such as tag`a${x}b`.
func (*TaggedTemplateExpr) End ¶
func (e *TaggedTemplateExpr) End() token.Pos
func (*TaggedTemplateExpr) Pos ¶
func (e *TaggedTemplateExpr) Pos() token.Pos
type TemplateElement ¶
type TemplateElement struct {
Pos token.Pos
Cooked string // interpreted value (with escapes processed)
Raw string // exact source text
// CookedInvalid reports that this segment contained an escape with no cooked
// value (a legacy octal, \8/\9, or malformed hex/unicode escape). It is an
// early SyntaxError in an untagged template; a tagged template yields an
// undefined cooked value for the segment (ECMA-262 §12.9.6).
CookedInvalid bool
}
TemplateElement is one cooked/raw string segment of a template literal.
type TemplateLit ¶
TemplateLit is a template literal `a${x}b`. Quasis holds the literal string segments; Exprs holds the interpolated expressions. len(Quasis) is always len(Exprs)+1.
func (*TemplateLit) End ¶
func (e *TemplateLit) End() token.Pos
func (*TemplateLit) Pos ¶
func (e *TemplateLit) Pos() token.Pos
type TryStmt ¶
type TryStmt struct {
Keyword token.Pos
Block *BlockStmt
Handler *CatchClause // nil when there is no catch
Finalizer *BlockStmt // nil when there is no finally
}
TryStmt is a try/catch/finally statement. Handler and Finalizer may be nil, but at least one is always present.
type UpdateExpr ¶
type UpdateExpr struct {
OpPos token.Pos
Op token.Type // token.INC or token.DEC
Operand Expr
Prefix bool
}
UpdateExpr is an increment/decrement (++x, x--). Prefix distinguishes ++x from x++.
func (*UpdateExpr) End ¶
func (e *UpdateExpr) End() token.Pos
func (*UpdateExpr) Pos ¶
func (e *UpdateExpr) Pos() token.Pos
type VarDecl ¶
type VarDecl struct {
Keyword token.Pos
Kind token.Type // token.VAR, token.LET, or token.CONST
Decls []*VarDeclarator
EndPos token.Pos
}
VarDecl is a variable declaration: var/let/const with one or more declarators.
type VarDeclarator ¶
type VarDeclarator struct {
Target Expr // Ident or a destructuring pattern (ArrayLit/ObjectLit)
Init Expr // may be nil
}
VarDeclarator is a single name = init binding within a VarDecl.
func (*VarDeclarator) End ¶
func (d *VarDeclarator) End() token.Pos
func (*VarDeclarator) Pos ¶
func (d *VarDeclarator) Pos() token.Pos