ast

package
v0.2.8 Latest Latest
Warning

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

Go to latest
Published: Nov 4, 2024 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package ast declares the types used to represent syntax trees for Next source code.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Inspect

func Inspect(node Node, f func(Node) bool)

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

func TrimComments(comments []string) []string

TrimComments removes comment markers and trims whitespace from a slice of comment strings.

func Walk

func Walk(v Visitor, node Node)

Walk traverses an AST in depth-first order: It starts by calling v.Visit(node); node must not be nil. If the visitor w returned by v.Visit(node) is not nil, Walk is invoked recursively with visitor w for each of the non-nil children of node, followed by a call of w.Visit(nil).

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.

func (*ArrayType) End

func (x *ArrayType) End() token.Pos

func (*ArrayType) Pos

func (x *ArrayType) Pos() token.Pos

type BadExpr

type BadExpr struct {
	From, To token.Pos // position range of bad expression
}

BadExpr represents an incorrect or unparseable expression.

func (*BadExpr) End

func (x *BadExpr) End() token.Pos

func (*BadExpr) Pos

func (x *BadExpr) Pos() token.Pos

type BadStmt

type BadStmt struct {
	From, To token.Pos // position range of bad statement
}

BadStmt represents an invalid statement.

func (*BadStmt) End

func (s *BadStmt) End() token.Pos

func (*BadStmt) Pos

func (s *BadStmt) Pos() token.Pos

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.

func (*BasicLit) End

func (x *BasicLit) End() token.Pos

func (*BasicLit) Pos

func (x *BasicLit) Pos() token.Pos

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.

func (*CallExpr) End

func (x *CallExpr) End() token.Pos

func (*CallExpr) Pos

func (x *CallExpr) Pos() token.Pos

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.

func (*Comment) End

func (c *Comment) End() token.Pos

End returns the position of the character immediately after the comment.

func (*Comment) Pos

func (c *Comment) Pos() token.Pos

Pos returns the position of the first character in the 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 ConstDecl

type ConstDecl = GenDecl[Expr]

ConstDecl represents a constant declaration.

type Decl

type Decl interface {
	Node
	Token() token.Token
	// contains filtered or unexported methods
}

Decl represents a declaration in the Next source code.

type EnumDecl

type EnumDecl = GenDecl[*EnumType]

EnumDecl represents an enum declaration.

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.

func (*EnumType) End

func (x *EnumType) End() token.Pos

func (*EnumType) Pos

func (x *EnumType) Pos() token.Pos

type Expr

type Expr interface {
	Node
	// contains filtered or unexported methods
}

Expr represents any expression node in the abstract syntax tree.

func Unparen

func Unparen(e Expr) Expr

Unparen returns the expression with any enclosing parentheses removed.

type ExprStmt

type ExprStmt struct {
	X Expr // expression
}

ExprStmt represents a standalone expression in a statement list.

func (*ExprStmt) End

func (s *ExprStmt) End() token.Pos

func (*ExprStmt) Pos

func (s *ExprStmt) Pos() token.Pos

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.

func (*File) End

func (f *File) End() token.Pos

End returns the end of the last declaration in the file.

func (*File) Pos

func (f *File) Pos() token.Pos

Pos returns the position of the package declaration.

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.

func (*GenDecl[T]) End

func (s *GenDecl[T]) End() token.Pos

func (*GenDecl[T]) Pos

func (s *GenDecl[T]) Pos() token.Pos

func (*GenDecl[T]) Token

func (x *GenDecl[T]) Token() token.Token

type Ident

type Ident struct {
	NamePos token.Pos // identifier position
	Name    string    // identifier name
}

Ident represents an identifier.

func (*Ident) End

func (x *Ident) End() token.Pos

func (*Ident) Pos

func (x *Ident) Pos() token.Pos

func (*Ident) String

func (id *Ident) String() string

String returns the identifier name.

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.

func (*List[T]) End

func (l *List[T]) End() token.Pos

End returns the position of the character immediately after the list.

func (*List[T]) NumFields

func (l *List[T]) NumFields() int

NumFields returns the number of fields in the list.

func (*List[T]) Pos

func (l *List[T]) Pos() token.Pos

Pos returns the position of the first character in the list.

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.

func (*MapType) End

func (x *MapType) End() token.Pos

func (*MapType) Pos

func (x *MapType) Pos() token.Pos

type MemberList

type MemberList = List[*EnumMember]

MemberList represents a list of enum members.

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.

func (*Method) End

func (m *Method) End() token.Pos

End returns the position of the character immediately after the method declaration.

func (*Method) Pos

func (m *Method) Pos() token.Pos

Pos returns the position of the first character in the method declaration.

type MethodList

type MethodList = List[*Method]

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.

func (*ParenExpr) End

func (x *ParenExpr) End() token.Pos

func (*ParenExpr) Pos

func (x *ParenExpr) Pos() token.Pos

type SelectorExpr

type SelectorExpr struct {
	X   Expr   // expression
	Sel *Ident // field selector
}

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.

func (*UnaryExpr) End

func (x *UnaryExpr) End() token.Pos

func (*UnaryExpr) Pos

func (x *UnaryExpr) Pos() token.Pos

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

type Visitor

type Visitor interface {
	Visit(node Node) (w Visitor)
}

Visitor represents an interface for traversing an AST. The Visit method is invoked for each node encountered by Walk. If the result visitor w is not nil, Walk visits each of the children of node with the visitor w, followed by a call of w.Visit(nil).

Jump to

Keyboard shortcuts

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