Documentation
¶
Index ¶
- func Inspect(node Node, f func(Node) bool)
- func Preorder(root Node) iter.Seq[Node]
- func PreorderStack(root Node, stack []Node, f func(n Node, stack []Node) bool)
- func Walk(v Visitor, node Node)
- type ArrayLit
- type BasicLit
- type BinaryExpr
- type BlockStmt
- type BranchStmt
- type CallExpr
- type Comment
- type CommentGroup
- type EmptyStmt
- type Expr
- type ExprStmt
- type File
- type ForStmt
- type FuncDecl
- type FuncLit
- type Ident
- type IfStmt
- type ImportDecl
- type ImportSpec
- type IndexExpr
- type LabeledStmt
- type Node
- type Object
- type ParenExpr
- type ReturnStmt
- type SelectorExpr
- type Stmt
- type UnaryExpr
- type ValueDecl
- type Visitor
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Inspect ¶
Inspect traverses an AST in depth-first order: It starts by calling f(node); node must not be nil. If f returns true, Inspect invokes f recursively for each of the non-nil children of node, followed by a call of f(nil).
In many cases it may be more convenient to use Preorder, which returns an iterator over the sequence of nodes, or PreorderStack, which (like Inspect) provides control over descent into subtrees, but additionally reports the stack of enclosing nodes.
func Preorder ¶
Preorder returns an iterator over all the nodes of the syntax tree beneath (and including) the specified root, in depth-first preorder.
For greater control over the traversal of each subtree, use Inspect or PreorderStack.
func PreorderStack ¶
PreorderStack traverses the tree rooted at root, calling f before visiting each node.
Each call to f provides the current node and traversal stack, consisting of the original value of stack appended with all nodes from root to n, excluding n itself. (This design allows calls to PreorderStack to be nested without double counting.)
If f returns false, the traversal skips over that subtree. Unlike Inspect, no second call to f is made after visiting node n. (In practice, the second call is nearly always used only to pop the stack, and it is surprisingly tricky to do this correctly.)
Types ¶
type ArrayLit ¶
type ArrayLit struct {
Lbrack token.Pos // position of "["
Values []Expr // array elements; or nil
Rbrack token.Pos // position of "]"
}
An ArrayLit node represents an array literal.
type BasicLit ¶
type BasicLit struct {
ValuePos token.Pos // literal position
ValueEnd token.Pos // position immediately after the literal
Kind token.Token // token.INT, token.FLOAT, token.IMAG, token.CHAR, or token.STRING
Value string // literal string; e.g. 42, 0x7f, 3.14, 1e-9, 2.4i, 'a', '\x7f', "foo" or `\m\n\o`
}
type BinaryExpr ¶
type BinaryExpr struct {
X Expr // left operand
OpPos token.Pos // position of Op
Op token.Token // operator
Y Expr // right operand
}
A BinaryExpr node represents a binary expression.
func (*BinaryExpr) End ¶
func (x *BinaryExpr) End() token.Pos
func (*BinaryExpr) Pos ¶
func (x *BinaryExpr) Pos() token.Pos
type BlockStmt ¶
type BlockStmt struct {
Lbrace token.Pos // position of "{"
List []Stmt
Rbrace token.Pos // position of "}", if any (may be absent due to syntax error)
}
A BlockStmt node represents a braced statement list.
type BranchStmt ¶
type BranchStmt struct {
TokPos token.Pos // position of Tok
Tok token.Token // keyword token (BREAK, CONTINUE)
Label *Ident // label name; or nil
}
A BranchStmt node represents a break or continue statement.
func (*BranchStmt) End ¶
func (s *BranchStmt) End() token.Pos
func (*BranchStmt) Pos ¶
func (s *BranchStmt) Pos() token.Pos
type CallExpr ¶
type CallExpr struct {
Fun Expr // function expression
Lparen token.Pos // position of "("
Args []Expr // function arguments; or nil
Ellipsis token.Pos // position of "..." (token.NoPos if there is no "...")
Rparen token.Pos // position of ")"
}
A CallExpr node represents an expression followed by an argument list.
type Comment ¶
A Comment node represents a single //-style or /*-style comment.
The Text field contains the comment text without carriage returns (\r) that may have been present in the source. Because a comment's end position is computed using len(Text), the position reported by [Comment.End] does not match the true source end position for comments containing carriage returns.
type CommentGroup ¶
type CommentGroup = ast.CommentGroup
A CommentGroup represents a sequence of comments with no other tokens and no empty lines between.
type EmptyStmt ¶
type EmptyStmt struct {
Semicolon token.Pos // position of following ";"
Implicit bool // if set, ";" was omitted in the source
}
An EmptyStmt node represents an empty statement. The "position" of the empty statement is the position of the immediately following (explicit or implicit) semicolon.
type Expr ¶
type Expr interface {
Node
// contains filtered or unexported methods
}
All expression nodes implement the Expr interface.
type ExprStmt ¶
type ExprStmt struct {
X Expr // expression
}
An ExprStmt node represents a (stand-alone) expression in a statement list.
type File ¶
type File struct {
Doc *CommentGroup // associated documentation; or nil
Stmts []Stmt // top-level statements; or nil
Comments []*CommentGroup // comments in the file, in lexical order
}
A File node represents a JavaScript source file.
type ForStmt ¶
type ForStmt struct {
For token.Pos // position of "for" keyword
Init Stmt // initialization statement; or nil
Cond Expr // condition; or nil
Post Stmt // post iteration statement; or nil
Body *BlockStmt
}
A ForStmt represents a for statement.
type FuncDecl ¶
type FuncDecl struct {
Doc *CommentGroup // associated documentation; or nil
Func token.Pos // position of "function" keyword
Name *Ident // function/method name
Opening token.Pos // position of (
Params []*Ident // parameters
Closing token.Pos // position of )
Body *BlockStmt // function body
}
A FuncDecl node represents a function declaration.
function Name(Params) { Body }
type FuncLit ¶
type FuncLit struct {
Opening token.Pos // position of (
Params []*Ident // parameters
Closing token.Pos // position of )
Body *BlockStmt // function body
}
A FuncLit node represents a function literal.
type Ident ¶
type Ident struct {
NamePos token.Pos // identifier position
Name string // identifier name
Obj *Object // denoted object, or nil.
}
An Ident node represents an identifier.
type IfStmt ¶
type IfStmt struct {
If token.Pos // position of "if" keyword
Cond Expr // condition
Body *BlockStmt
Else Stmt // else branch; or nil
}
An IfStmt node represents an if statement.
type ImportDecl ¶
type ImportDecl struct {
Import token.Pos // position of "import" keyword
Specs []*ImportSpec // import specifications
Path *BasicLit // import path
}
An ImportDecl represents an import declaration.
import { Name1, Name2 as AliasName2, type Name3 } from "path"
func (*ImportDecl) End ¶
func (i *ImportDecl) End() token.Pos
func (*ImportDecl) Pos ¶
func (i *ImportDecl) Pos() token.Pos
type ImportSpec ¶
type ImportSpec struct {
Type token.Pos // position of "type"; or token.NoPos
Name *Ident // symbol name or type name to import
Alias *Ident // alias name; or nil
}
An ImportSpec represents an import specification.
func (*ImportSpec) End ¶
func (i *ImportSpec) End() token.Pos
func (*ImportSpec) Pos ¶
func (i *ImportSpec) Pos() token.Pos
type IndexExpr ¶
type IndexExpr struct {
X Expr // expression
Lbrack token.Pos // position of "["
Index Expr // index expression
Rbrack token.Pos // position of "]"
}
An IndexExpr node represents an expression followed by an index.
type LabeledStmt ¶
A LabeledStmt node represents a labeled statement.
func (*LabeledStmt) End ¶
func (s *LabeledStmt) End() token.Pos
func (*LabeledStmt) Pos ¶
func (s *LabeledStmt) Pos() token.Pos
type ParenExpr ¶
type ParenExpr struct {
Lparen token.Pos // position of "("
X Expr // parenthesized expression
Rparen token.Pos // position of ")"
}
A ParenExpr node represents a parenthesized expression.
type ReturnStmt ¶
type ReturnStmt struct {
Return token.Pos // position of "return" keyword
Result Expr // result expression; or nil
}
A ReturnStmt node represents a return statement.
func (*ReturnStmt) End ¶
func (s *ReturnStmt) End() token.Pos
func (*ReturnStmt) Pos ¶
func (s *ReturnStmt) Pos() token.Pos
type SelectorExpr ¶
type SelectorExpr struct {
X Expr // expression; or nil (for an implicit package name)
Sel *Ident // field selector
}
A SelectorExpr node represents an expression followed by a selector.
func (*SelectorExpr) End ¶
func (x *SelectorExpr) End() token.Pos
func (*SelectorExpr) Pos ¶
func (x *SelectorExpr) Pos() token.Pos
type Stmt ¶
type Stmt interface {
Node
// contains filtered or unexported methods
}
All statement nodes implement the Stmt interface.
type UnaryExpr ¶
type UnaryExpr struct {
OpPos token.Pos // position of Op
Op token.Token // operator
X Expr // operand
}
A UnaryExpr node represents a unary expression.
type ValueDecl ¶
type ValueDecl struct {
Doc *CommentGroup // associated documentation; or nil
TokPos token.Pos // position of Tok
Tok token.Token // VAR (let) or CONST (const)
Name *Ident // name
Value Expr // value; or nil
}
A ValueDecl node represents a variable or constant declaration.
let Name = Value const Name = Value