Documentation
¶
Overview ¶
Package ast declares the types used to represent syntax trees for Next source code.
Index ¶
- func Inspect(node Node, f func(Node) bool)
- func TrimComments(comments []string) []string
- func Walk(v Visitor, node Node)
- type Annotation
- type AnnotationGroup
- type AnnotationParam
- type ArrayType
- type BadExpr
- type BadStmt
- type BasicLit
- type BinaryExpr
- type CallExpr
- type Comment
- type CommentGroup
- type CommentMap
- type ConstDecl
- type Decl
- type EnumDecl
- type EnumMember
- type EnumType
- type Expr
- type ExprStmt
- type FieldList
- type File
- type GenDecl
- type Ident
- type ImportDecl
- type InterfaceDecl
- type InterfaceType
- type List
- type MapType
- type MemberList
- type Method
- type MethodList
- type MethodParam
- type MethodParamList
- type Node
- type ParenExpr
- type SelectorExpr
- type Stmt
- type StructDecl
- type StructField
- type StructType
- type Type
- type UnaryExpr
- type VectorType
- 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).
func TrimComments ¶
TrimComments removes comment markers and trims whitespace from a slice of comment strings.
Types ¶
type Annotation ¶
type Annotation struct { At token.Pos // position of "@" Name *Ident // annotation name Lparen token.Pos // position of "(" if any Params []*AnnotationParam // annotation parameters; or nil Rparen token.Pos // position of ")" if any }
Annotation represents an annotation in the source code.
func (*Annotation) End ¶
func (a *Annotation) End() token.Pos
End returns the position of the character immediately after the annotation.
func (*Annotation) Pos ¶
func (a *Annotation) Pos() token.Pos
Pos returns the position of the first character in the annotation.
type AnnotationGroup ¶
type AnnotationGroup struct {
List []*Annotation // len(List) > 0
}
AnnotationGroup represents a group of annotations.
func (*AnnotationGroup) End ¶
func (g *AnnotationGroup) End() token.Pos
End returns the position of the character immediately after the last annotation in the group.
func (*AnnotationGroup) Pos ¶
func (g *AnnotationGroup) Pos() token.Pos
Pos returns the position of the first character in the first annotation of the group.
type AnnotationParam ¶
type AnnotationParam struct { Name *Ident // name of parameter AssignPos token.Pos // position of "=" if any Value Node // parameter value (Expr or Type), or nil }
func (*AnnotationParam) End ¶
func (p *AnnotationParam) End() token.Pos
End returns the position of the character immediately after the annotation parameter.
func (*AnnotationParam) Pos ¶
func (p *AnnotationParam) Pos() token.Pos
Pos returns the position of the first character in the annotation parameter.
type ArrayType ¶
type ArrayType struct { Array token.Pos // position of token.ARRAY T Type // element type N Expr // size of the array GT token.Pos // position of ">" }
ArrayType represents an fixed-size array type.
type BasicLit ¶
type BasicLit struct { ValuePos token.Pos // literal position 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` }
BasicLit represents a literal of basic type.
type BinaryExpr ¶
type BinaryExpr struct { X Expr // left operand OpPos token.Pos // position of Op Op token.Token // operator Y Expr // right operand }
BinaryExpr represents a binary expression.
func (*BinaryExpr) End ¶
func (x *BinaryExpr) End() token.Pos
func (*BinaryExpr) Pos ¶
func (x *BinaryExpr) Pos() token.Pos
type CallExpr ¶
type CallExpr struct { Fun Expr // function expression Lparen token.Pos // position of "(" Args []Expr // function arguments; or nil Rparen token.Pos // position of ")" }
CallExpr represents an expression followed by an argument list.
type Comment ¶
type Comment struct { Slash token.Pos // position of "/" starting the comment Text string // comment text (excluding '\n' for //-style comments) }
Comment represents a single //-style or /*-style comment.
type CommentGroup ¶
type CommentGroup struct {
List []*Comment // len(List) > 0
}
CommentGroup represents a sequence of comments with no other tokens and no empty lines between.
func (*CommentGroup) End ¶
func (g *CommentGroup) End() token.Pos
End returns the position of the character immediately after the last comment in the group.
func (*CommentGroup) Pos ¶
func (g *CommentGroup) Pos() token.Pos
Pos returns the position of the first character in the first comment in the group.
func (*CommentGroup) Text ¶
func (g *CommentGroup) Text() string
Text returns the text of the comment group. Comment markers (//, /*, and */), the first space of a line comment, and leading and trailing empty lines are removed. Multiple empty lines are reduced to one, and trailing space on lines is trimmed. Unless the result is empty, it is newline-terminated.
func (*CommentGroup) TrimComments ¶
func (g *CommentGroup) TrimComments() []string
TrimComments returns the trimmed text of the comments in the group.
type CommentMap ¶
type CommentMap map[Node][]*CommentGroup
CommentMap maps an AST node to a list of comment groups associated with it. See NewCommentMap for a description of the association.
func NewCommentMap ¶
func NewCommentMap(fset *token.FileSet, node Node, comments []*CommentGroup) CommentMap
NewCommentMap creates a new comment map by associating comment groups of the comments list with the nodes of the AST specified by node.
A comment group g is associated with a node n if:
- g starts on the same line as n ends
- g starts on the line immediately following n, and there is at least one empty line after g and before the next node
- g starts before n and is not associated to the node before n via the previous rules
NewCommentMap tries to associate a comment group to the "largest" node possible: For instance, if the comment is a line comment trailing an assignment, the comment is associated with the entire assignment rather than just the last operand in the assignment.
func (CommentMap) Comments ¶
func (cmap CommentMap) Comments() []*CommentGroup
Comments returns the list of comment groups in the comment map. The result is sorted in source order.
func (CommentMap) Filter ¶
func (cmap CommentMap) Filter(node Node) CommentMap
Filter returns a new comment map consisting of only those entries of cmap for which a corresponding node exists in the AST specified by node.
func (CommentMap) String ¶
func (cmap CommentMap) String() string
String returns a string representation of the CommentMap.
func (CommentMap) Update ¶
func (cmap CommentMap) Update(old, new Node) Node
Update replaces an old node in the comment map with the new node and returns the new node. Comments that were associated with the old node are associated with the new node.
type EnumMember ¶
type EnumMember struct { Doc *CommentGroup // associated documentation; or nil Annotations *AnnotationGroup // associated annotations; or nil Name *Ident // value name AssignPos token.Pos // position of "=" if any Value Expr // value; or nil Comment *CommentGroup // line comments; or nil }
EnumMember represents an enum member declaration.
func (*EnumMember) End ¶
func (m *EnumMember) End() token.Pos
End returns the position of the character immediately after the enum member.
func (*EnumMember) Pos ¶
func (m *EnumMember) Pos() token.Pos
Pos returns the position of the first character in the enum member.
type EnumType ¶
type EnumType struct {
Members *MemberList // list of enum members
}
EnumType represents an enum type.
type Expr ¶
type Expr interface { Node // contains filtered or unexported methods }
Expr represents any expression node in the abstract syntax tree.
type ExprStmt ¶
type ExprStmt struct {
X Expr // expression
}
ExprStmt represents a standalone expression in a statement list.
type FieldList ¶
type FieldList = List[*StructField]
FieldList represents a list of fields, enclosed by parentheses, curly braces, or square brackets.
type File ¶
type File struct { Doc *CommentGroup // associated documentation; or nil Annotations *AnnotationGroup // associated annotations; or nil Package token.Pos // position of "package" keyword Name *Ident // package name Decls []Decl // top-level declarations except imports Stmts []Stmt // top-level statements; or nil FileStart, FileEnd token.Pos // start and end of entire file Imports []*ImportDecl // imports in this file Comments []*CommentGroup // list of all comments in the source file }
File represents a Next source file.
type GenDecl ¶
type GenDecl[T Node] struct { Doc *CommentGroup // associated documentation; or nil Annotations *AnnotationGroup // associated annotations; or nil Tok token.Token // CONST, ENUM, STRUCT or INTERFACE TokPos token.Pos // position of Tok Name *Ident // value name Spec T // spec of the declaration Comment *CommentGroup // line comments const declarations; or nil }
GenDecl represents a general declaration node.
type ImportDecl ¶
type ImportDecl struct { Doc *CommentGroup // associated documentation; or nil ImportPos token.Pos // position of "import" keyword Path *BasicLit // import path Comment *CommentGroup // line comments; or nil EndPos token.Pos // end of spec (overrides Path.Pos if nonzero) }
ImportDecl represents a single package import.
func (*ImportDecl) End ¶
func (s *ImportDecl) End() token.Pos
func (*ImportDecl) Pos ¶
func (s *ImportDecl) Pos() token.Pos
Pos and End implementations for decl nodes.
func (*ImportDecl) Token ¶
func (x *ImportDecl) Token() token.Token
type InterfaceDecl ¶
type InterfaceDecl = GenDecl[*InterfaceType]
InterfaceDecl represents an interface declaration.
type InterfaceType ¶
type InterfaceType struct {
Methods *MethodList // list of methods
}
InterfaceType represents an interface type.
func (*InterfaceType) End ¶
func (x *InterfaceType) End() token.Pos
func (*InterfaceType) Pos ¶
func (x *InterfaceType) Pos() token.Pos
type List ¶
type List[T Node] struct { Opening token.Pos // position of opening parenthesis/brace/bracket, if any List []T // field list; or nil Closing token.Pos // position of closing parenthesis/brace/bracket, if any }
List represents a generic list of nodes.
type MapType ¶
type MapType struct { Map token.Pos // position of token.MAP K Type // key type V Type // value type GT token.Pos // position of ">" }
MapType represents a map type.
type Method ¶
type Method struct { Doc *CommentGroup // associated documentation; or nil Annotations *AnnotationGroup // associated annotations; or nil Name *Ident // method name Params *MethodParamList // method parameters Result Type // method result type; or nil Comment *CommentGroup // line comments; or nil }
Method represents a method declaration.
type MethodList ¶
MethodList represents a list of method declarations.
type MethodParam ¶
type MethodParam struct { Doc *CommentGroup // associated documentation; or nil Annotations *AnnotationGroup // associated annotations; or nil Type Type // parameter type Name *Ident // parameter name Comment *CommentGroup // line comments; or nil }
MethodParam represents a method parameter declaration.
func (*MethodParam) End ¶
func (p *MethodParam) End() token.Pos
End returns the position of the character immediately after the method parameter.
func (*MethodParam) Pos ¶
func (p *MethodParam) Pos() token.Pos
Pos returns the position of the first character in the method parameter.
type MethodParamList ¶
type MethodParamList = List[*MethodParam]
MethodParamList represents a list of function parameters.
type Node ¶
type Node interface { Pos() token.Pos // position of first character belonging to the node End() token.Pos // position of first character immediately after the node }
Node represents any node in the abstract syntax tree.
type ParenExpr ¶
type ParenExpr struct { Lparen token.Pos // position of "(" X Expr // parenthesized expression Rparen token.Pos // position of ")" }
ParenExpr represents a parenthesized expression.
type SelectorExpr ¶
SelectorExpr 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 }
Stmt represents any statement node in the abstract syntax tree.
type StructDecl ¶
type StructDecl = GenDecl[*StructType]
StructDecl represents a struct declaration.
type StructField ¶
type StructField struct { Doc *CommentGroup // associated documentation; or nil Annotations *AnnotationGroup // associated annotations; or nil Type Type // field/method/parameter type; or nil Name *Ident // field/method/(type) parameter names; or nil Comment *CommentGroup // line comments; or nil }
StructField represents a field in a struct type
func (*StructField) End ¶
func (f *StructField) End() token.Pos
End returns the position of the character immediately after the struct field.
func (*StructField) Pos ¶
func (f *StructField) Pos() token.Pos
Pos returns the position of the first character in the struct field.
type StructType ¶
type StructType struct {
Fields *FieldList // list of field declarations
}
StructType represents a struct type.
func (*StructType) End ¶
func (x *StructType) End() token.Pos
func (*StructType) Pos ¶
func (x *StructType) Pos() token.Pos
type Type ¶
type Type interface { Node // contains filtered or unexported methods }
Type represents any type node in the abstract syntax tree.
type UnaryExpr ¶
type UnaryExpr struct { OpPos token.Pos // position of Op Op token.Token // operator X Expr // operand }
UnaryExpr represents a unary expression. Unary "*" expressions are represented via StarExpr nodes.
type VectorType ¶
type VectorType struct { Vector token.Pos // position of token.VECTOR T Type // element type GT token.Pos // position of ">" }
VectorType represents a vector type.
func (*VectorType) End ¶
func (x *VectorType) End() token.Pos
func (*VectorType) Pos ¶
func (x *VectorType) Pos() token.Pos