ast

package
v0.0.0-...-c922539 Latest Latest
Warning

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

Go to latest
Published: Jan 23, 2024 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddComment

func AddComment(n Node, cg *CommentGroup)

AddComment adds the given comment to the node if it supports it. If a node does not support comments, such as for CommentGroup or Comment, this call has no effect.

func IsValidIdent

func IsValidIdent(ident string) bool

IsValidIdent reports whether str is a valid identifier.

func SetComments

func SetComments(n Node, cgs []*CommentGroup)

SetComments replaces all comments of n with the given set of comments. If a node does not support comments, such as for CommentGroup or Comment, this call has no effect.

func SetRelPos

func SetRelPos(n Node, p token.RelPos)

SetRelPos sets the relative position of a node without modifying its file position. Setting it to token.NoRelPos allows a node to adopt default formatting.

func Walk

func Walk(node Node, before func(Node) bool, after func(Node))

Walk traverses an AST in depth-first order: It starts by calling f(node); node must not be nil. If before returns true, Walk invokes f recursively for each of the non-nil children of node, followed by a call of after. Both functions may be nil. If before is nil, it is assumed to always return true.

Types

type BadDecl

type BadDecl struct {
	From, To token.Pos // position range of bad declaration
	// contains filtered or unexported fields
}

A BadDecl node is a placeholder for declarations containing syntax errors for which no correct declaration nodes can be created.

func (*BadDecl) AddComment

func (c *BadDecl) AddComment(cg *CommentGroup)

func (*BadDecl) Comments

func (c *BadDecl) Comments() []*CommentGroup

func (*BadDecl) End

func (d *BadDecl) End() token.Pos

func (BadDecl) IsDecl

func (BadDecl) IsDecl() bool

func (*BadDecl) Pos

func (d *BadDecl) Pos() token.Pos

func (*BadDecl) SetComments

func (c *BadDecl) SetComments(cgs []*CommentGroup)

type BadExpr

type BadExpr struct {
	From, To token.Pos // position range of bad expression
	// contains filtered or unexported fields
}

A BadExpr node is a placeholder for expressions containing syntax errors for which no correct expression nodes can be created.

func (*BadExpr) AddComment

func (c *BadExpr) AddComment(cg *CommentGroup)

func (*BadExpr) Comments

func (c *BadExpr) Comments() []*CommentGroup

func (*BadExpr) End

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

func (BadExpr) IsExpr

func (BadExpr) IsExpr() bool

func (*BadExpr) Pos

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

func (*BadExpr) SetComments

func (c *BadExpr) SetComments(cgs []*CommentGroup)

type BasicLit

type BasicLit struct {
	ValuePos token.Pos   // literal position
	Kind     token.Token // INT, FLOAT, or STRING
	Value    string      // literal string; e.g. 42, 0x7f, 3.14, 1_234_567, 1e-9, 2.4i, 'a', '\x7f', "foo", or '\m\n\o'
	// contains filtered or unexported fields
}

A BasicLit node represents a literal of basic type.

func (*BasicLit) AddComment

func (c *BasicLit) AddComment(cg *CommentGroup)

func (*BasicLit) Comments

func (c *BasicLit) Comments() []*CommentGroup

func (*BasicLit) End

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

func (BasicLit) IsExpr

func (BasicLit) IsExpr() bool

func (BasicLit) IsLabel

func (l BasicLit) IsLabel() bool

func (*BasicLit) Pos

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

func (*BasicLit) SetComments

func (c *BasicLit) SetComments(cgs []*CommentGroup)

type BinaryExpr

type BinaryExpr struct {
	X     Expr        // left operand
	OpPos token.Pos   // position of Op
	Op    token.Token // operator
	Y     Expr        // right operand
	// contains filtered or unexported fields
}

A BinaryExpr node represents a binary expression.

func (*BinaryExpr) AddComment

func (c *BinaryExpr) AddComment(cg *CommentGroup)

func (*BinaryExpr) Comments

func (c *BinaryExpr) Comments() []*CommentGroup

func (*BinaryExpr) End

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

func (BinaryExpr) IsExpr

func (BinaryExpr) IsExpr() bool

func (*BinaryExpr) Pos

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

func (*BinaryExpr) SetComments

func (c *BinaryExpr) SetComments(cgs []*CommentGroup)

type CallExpr

type CallExpr struct {
	Fun    Expr      // function expression
	Lparen token.Pos // position of "("
	Args   []Decl    // function arguments; or nil
	Rparen token.Pos // position of ")"
	// contains filtered or unexported fields
}

A CallExpr node represents an expression followed by an argument list.

func (*CallExpr) AddComment

func (c *CallExpr) AddComment(cg *CommentGroup)

func (*CallExpr) Comments

func (c *CallExpr) Comments() []*CommentGroup

func (*CallExpr) End

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

func (CallExpr) IsExpr

func (CallExpr) IsExpr() bool

func (*CallExpr) Pos

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

func (*CallExpr) SetComments

func (c *CallExpr) SetComments(cgs []*CommentGroup)

type Clause

type Clause interface {
	Node
	IsClause() bool
}

Clause nodes are part of comprehensions.

type Comment

type Comment struct {
	Slash token.Pos // position of "/" starting the comment
	Text  string    // comment text (excluding '\n' for //-style comments)
}

A Comment node represents a single //-style

func (*Comment) AddComment

func (c *Comment) AddComment(*CommentGroup)

func (*Comment) Comments

func (c *Comment) Comments() []*CommentGroup

func (*Comment) End

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

func (*Comment) Pos

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

type CommentGroup

type CommentGroup struct {
	// TODO: remove and use the token position of the first comment.
	Doc  bool
	Line bool // true if it is on the same line as the node's end pos.

	// Position indicates where a comment should be attached if a node has
	// multiple tokens. 0 means before the first token, 1 means before the
	// second, etc. For instance, for a field, the positions are:
	//    <0> Label <1> ":" <2> Expr <3> "," <4>
	Position int8
	List     []*Comment // len(List) > 0
	// contains filtered or unexported fields
}

A CommentGroup represents a sequence of comments with no other tokens and no empty lines between.

func Comments

func Comments(n Node) []*CommentGroup

Comments returns all comments associated with a given node.

func (*CommentGroup) AddComment

func (g *CommentGroup) AddComment(*CommentGroup)

func (*CommentGroup) Comments

func (g *CommentGroup) Comments() []*CommentGroup

func (*CommentGroup) End

func (g *CommentGroup) End() token.Pos

func (CommentGroup) IsDecl

func (CommentGroup) IsDecl() bool

func (*CommentGroup) Pos

func (g *CommentGroup) Pos() token.Pos

func (*CommentGroup) Text

func (g *CommentGroup) Text() string

Text returns the text of the comment. 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.

type Decl

type Decl interface {
	Node
	IsDecl() bool
}

A Decl node is implemented by all declarations.

type DefaultExpr

type DefaultExpr struct {
	Default token.Pos // position of default token
	X       Expr      // expression to apply default to
	// contains filtered or unexported fields
}

func (*DefaultExpr) AddComment

func (c *DefaultExpr) AddComment(cg *CommentGroup)

func (*DefaultExpr) Comments

func (c *DefaultExpr) Comments() []*CommentGroup

func (*DefaultExpr) End

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

func (DefaultExpr) IsExpr

func (DefaultExpr) IsExpr() bool

func (*DefaultExpr) Pos

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

func (*DefaultExpr) SetComments

func (c *DefaultExpr) SetComments(cgs []*CommentGroup)

type Else

type Else struct {
	Else   token.Pos
	If     *If
	Struct *StructLit
	// contains filtered or unexported fields
}

An Else node represents an else or else if expression after an if expression

func (*Else) AddComment

func (c *Else) AddComment(cg *CommentGroup)

func (*Else) Comments

func (c *Else) Comments() []*CommentGroup

func (*Else) End

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

func (Else) IsExpr

func (Else) IsExpr() bool

func (*Else) Pos

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

func (*Else) SetComments

func (c *Else) SetComments(cgs []*CommentGroup)

type EmbedDecl

type EmbedDecl struct {
	Expr Expr
	// contains filtered or unexported fields
}

An EmbedDecl node represents a single expression used as a declaration. The expressions in this declaration is what will be emitted as configuration output.

An EmbedDecl may only appear at the top level.

func (*EmbedDecl) AddComment

func (c *EmbedDecl) AddComment(cg *CommentGroup)

func (*EmbedDecl) Comments

func (c *EmbedDecl) Comments() []*CommentGroup

func (*EmbedDecl) End

func (d *EmbedDecl) End() token.Pos

func (EmbedDecl) IsDecl

func (EmbedDecl) IsDecl() bool

func (*EmbedDecl) Pos

func (d *EmbedDecl) Pos() token.Pos

func (*EmbedDecl) SetComments

func (c *EmbedDecl) SetComments(cgs []*CommentGroup)

type Expr

type Expr interface {
	Node
	IsExpr() bool
}

An Expr is implemented by all expression nodes.

type Field

type Field struct {
	Label      Label
	Constraint token.Token // token.ILLEGAL (no constraint), token.OPTION
	Colon      token.Pos
	// Match is set to the position of the match token if this is a regexp matching field and Label will be a string
	// token that should be interpreted as a regexp
	Match token.Pos
	Value Expr
	// contains filtered or unexported fields
}

A Field represents a field declaration in a struct.

func (*Field) AddComment

func (c *Field) AddComment(cg *CommentGroup)

func (*Field) Comments

func (c *Field) Comments() []*CommentGroup

func (*Field) End

func (d *Field) End() token.Pos

func (Field) IsDecl

func (Field) IsDecl() bool

func (*Field) Pos

func (d *Field) Pos() token.Pos

func (*Field) SetComments

func (c *Field) SetComments(cgs []*CommentGroup)

type File

type File struct {
	Filename string
	Decls    []Decl // top-level declarations; or nil
	// contains filtered or unexported fields
}

A File node represents a Go source file.

The Comments list contains all comments in the source file in order of appearance, including the comments that are pointed to from other nodes via Doc and Comment fields.

func (*File) AddComment

func (c *File) AddComment(cg *CommentGroup)

func (*File) Comments

func (c *File) Comments() []*CommentGroup

func (*File) End

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

func (*File) Pos

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

func (*File) SetComments

func (c *File) SetComments(cgs []*CommentGroup)

type For

type For struct {
	For    token.Pos
	Clause *ForClause
	Struct *StructLit
	Else   *Else
	// contains filtered or unexported fields
}

A For node represents a for expression

func (*For) AddComment

func (c *For) AddComment(cg *CommentGroup)

func (*For) Comments

func (c *For) Comments() []*CommentGroup

func (*For) End

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

func (For) IsExpr

func (For) IsExpr() bool

func (*For) Pos

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

func (*For) SetComments

func (c *For) SetComments(cgs []*CommentGroup)

type ForClause

type ForClause struct {
	Key    *Ident
	Comma  token.Pos
	Value  *Ident
	In     token.Pos
	Source Expr
	// contains filtered or unexported fields
}

A ForClause node represents a for clause in a comprehension.

func (*ForClause) AddComment

func (c *ForClause) AddComment(cg *CommentGroup)

func (*ForClause) Comments

func (c *ForClause) Comments() []*CommentGroup

func (*ForClause) End

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

func (ForClause) IsClause

func (ForClause) IsClause() bool

func (*ForClause) Pos

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

func (*ForClause) SetComments

func (c *ForClause) SetComments(cgs []*CommentGroup)

type Func

type Func struct {
	Func       token.Pos // position of "function"
	Body       *StructLit
	ReturnType Expr
	// contains filtered or unexported fields
}

A Func node represents a function expression.

func (*Func) AddComment

func (c *Func) AddComment(cg *CommentGroup)

func (*Func) Comments

func (c *Func) Comments() []*CommentGroup

func (*Func) End

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

func (Func) IsExpr

func (Func) IsExpr() bool

func (*Func) Pos

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

func (*Func) SetComments

func (c *Func) SetComments(cgs []*CommentGroup)

type Ident

type Ident struct {
	NamePos token.Pos // identifier position

	Name string
	// contains filtered or unexported fields
}

An Ident node represents an left-hand side identifier,

func (*Ident) AddComment

func (c *Ident) AddComment(cg *CommentGroup)

func (*Ident) Comments

func (c *Ident) Comments() []*CommentGroup

func (*Ident) End

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

func (Ident) IsExpr

func (Ident) IsExpr() bool

func (Ident) IsLabel

func (l Ident) IsLabel() bool

func (*Ident) Pos

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

func (*Ident) SetComments

func (c *Ident) SetComments(cgs []*CommentGroup)

func (*Ident) String

func (x *Ident) String() string

type If

type If struct {
	If        token.Pos
	Condition *IfClause
	Struct    *StructLit
	Else      *Else
	// contains filtered or unexported fields
}

A If node represents an if expression

func (*If) AddComment

func (c *If) AddComment(cg *CommentGroup)

func (*If) Comments

func (c *If) Comments() []*CommentGroup

func (*If) End

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

func (If) IsExpr

func (If) IsExpr() bool

func (*If) Pos

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

func (*If) SetComments

func (c *If) SetComments(cgs []*CommentGroup)

type IfClause

type IfClause struct {
	Condition Expr
	// contains filtered or unexported fields
}

A IfClause node represents an if guard clause in a comprehension.

func (*IfClause) AddComment

func (c *IfClause) AddComment(cg *CommentGroup)

func (*IfClause) Comments

func (c *IfClause) Comments() []*CommentGroup

func (*IfClause) End

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

func (IfClause) IsClause

func (IfClause) IsClause() bool

func (*IfClause) Pos

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

func (*IfClause) SetComments

func (c *IfClause) SetComments(cgs []*CommentGroup)

type IndexExpr

type IndexExpr struct {
	X      Expr      // expression
	Lbrack token.Pos // position of "["
	Index  Expr      // index expression
	Rbrack token.Pos // position of "]"
	// contains filtered or unexported fields
}

An IndexExpr node represents an expression followed by an index.

func (*IndexExpr) AddComment

func (c *IndexExpr) AddComment(cg *CommentGroup)

func (*IndexExpr) Comments

func (c *IndexExpr) Comments() []*CommentGroup

func (*IndexExpr) End

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

func (IndexExpr) IsExpr

func (IndexExpr) IsExpr() bool

func (*IndexExpr) Pos

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

func (*IndexExpr) SetComments

func (c *IndexExpr) SetComments(cgs []*CommentGroup)

type Interpolation

type Interpolation struct {
	Elts []Expr // interleaving of strings and expressions.
	// contains filtered or unexported fields
}

func (*Interpolation) AddComment

func (c *Interpolation) AddComment(cg *CommentGroup)

func (*Interpolation) Comments

func (c *Interpolation) Comments() []*CommentGroup

func (*Interpolation) End

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

func (Interpolation) IsExpr

func (Interpolation) IsExpr() bool

func (Interpolation) IsLabel

func (l Interpolation) IsLabel() bool

func (*Interpolation) Pos

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

func (*Interpolation) SetComments

func (c *Interpolation) SetComments(cgs []*CommentGroup)

type Label

type Label interface {
	Node

	IsLabel() bool
}

A Label is any production that can be used as a LHS label.

type Lambda

type Lambda struct {
	Lambda token.Pos // position of "lambda"
	Colon  token.Pos // position of "colon"
	Idents []*Ident
	Expr   Expr
	// contains filtered or unexported fields
}

A Lambda node represents a lambda function expression.

func (*Lambda) AddComment

func (c *Lambda) AddComment(cg *CommentGroup)

func (*Lambda) Comments

func (c *Lambda) Comments() []*CommentGroup

func (*Lambda) End

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

func (Lambda) IsExpr

func (Lambda) IsExpr() bool

func (*Lambda) Pos

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

func (*Lambda) SetComments

func (c *Lambda) SetComments(cgs []*CommentGroup)

type LetClause

type LetClause struct {
	Let   token.Pos
	Ident *Ident
	Colon token.Pos
	Expr  Expr
	// contains filtered or unexported fields
}

A LetClause node represents a let clause in a comprehension.

func (*LetClause) AddComment

func (c *LetClause) AddComment(cg *CommentGroup)

func (*LetClause) Comments

func (c *LetClause) Comments() []*CommentGroup

func (*LetClause) End

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

func (LetClause) IsClause

func (LetClause) IsClause() bool

func (LetClause) IsDecl

func (LetClause) IsDecl() bool

func (*LetClause) Pos

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

func (*LetClause) SetComments

func (c *LetClause) SetComments(cgs []*CommentGroup)

type ListComprehension

type ListComprehension struct {
	Lbrack token.Pos // position of "["
	For    token.Pos
	Rbrack token.Pos // position of "]"
	Clause *ForClause
	Value  Expr
	// contains filtered or unexported fields
}

A ListComprehension node represents literal list with and embedded for expression

func (*ListComprehension) AddComment

func (c *ListComprehension) AddComment(cg *CommentGroup)

func (*ListComprehension) Comments

func (c *ListComprehension) Comments() []*CommentGroup

func (*ListComprehension) End

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

func (ListComprehension) IsExpr

func (ListComprehension) IsExpr() bool

func (*ListComprehension) Pos

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

func (*ListComprehension) SetComments

func (c *ListComprehension) SetComments(cgs []*CommentGroup)

type ListLit

type ListLit struct {
	Lbrack token.Pos // position of "["
	Elts   []Expr    // list of composite elements; or nil
	Rbrack token.Pos // position of "]"
	// contains filtered or unexported fields
}

A ListLit node represents a literal list.

func (*ListLit) AddComment

func (c *ListLit) AddComment(cg *CommentGroup)

func (*ListLit) Comments

func (c *ListLit) Comments() []*CommentGroup

func (*ListLit) End

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

func (ListLit) IsExpr

func (ListLit) IsExpr() bool

func (*ListLit) Pos

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

func (*ListLit) SetComments

func (c *ListLit) SetComments(cgs []*CommentGroup)

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
	// contains filtered or unexported methods
}

A 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 ")"
	// contains filtered or unexported fields
}

A ParenExpr node represents a parenthesized expression.

func (*ParenExpr) AddComment

func (c *ParenExpr) AddComment(cg *CommentGroup)

func (*ParenExpr) Comments

func (c *ParenExpr) Comments() []*CommentGroup

func (*ParenExpr) End

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

func (ParenExpr) IsExpr

func (ParenExpr) IsExpr() bool

func (*ParenExpr) Pos

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

func (*ParenExpr) SetComments

func (c *ParenExpr) SetComments(cgs []*CommentGroup)

type SchemaLit

type SchemaLit struct {
	Schema token.Pos // position of token.SCHEMA
	Decl   Decl
	// contains filtered or unexported fields
}

A SchemaLit node represents a literal schema definition.

func (*SchemaLit) AddComment

func (c *SchemaLit) AddComment(cg *CommentGroup)

func (*SchemaLit) Comments

func (c *SchemaLit) Comments() []*CommentGroup

func (*SchemaLit) End

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

func (SchemaLit) IsExpr

func (SchemaLit) IsExpr() bool

func (*SchemaLit) Pos

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

func (*SchemaLit) SetComments

func (c *SchemaLit) SetComments(cgs []*CommentGroup)

type SelectorExpr

type SelectorExpr struct {
	X   Expr  // expression
	Sel Label // field selector
	// contains filtered or unexported fields
}

A SelectorExpr node represents an expression followed by a selector.

func (*SelectorExpr) AddComment

func (c *SelectorExpr) AddComment(cg *CommentGroup)

func (*SelectorExpr) Comments

func (c *SelectorExpr) Comments() []*CommentGroup

func (*SelectorExpr) End

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

func (SelectorExpr) IsExpr

func (SelectorExpr) IsExpr() bool

func (*SelectorExpr) Pos

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

func (*SelectorExpr) SetComments

func (c *SelectorExpr) SetComments(cgs []*CommentGroup)

type SliceExpr

type SliceExpr struct {
	X      Expr      // expression
	Lbrack token.Pos // position of "["
	Low    Expr      // begin of slice range; or nil
	High   Expr      // end of slice range; or nil
	Rbrack token.Pos // position of "]"
	// contains filtered or unexported fields
}

An SliceExpr node represents an expression followed by slice indices.

func (*SliceExpr) AddComment

func (c *SliceExpr) AddComment(cg *CommentGroup)

func (*SliceExpr) Comments

func (c *SliceExpr) Comments() []*CommentGroup

func (*SliceExpr) End

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

func (SliceExpr) IsExpr

func (SliceExpr) IsExpr() bool

func (*SliceExpr) Pos

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

func (*SliceExpr) SetComments

func (c *SliceExpr) SetComments(cgs []*CommentGroup)

type StructLit

type StructLit struct {
	Lbrace token.Pos // position of "{"
	Elts   []Decl    // list of elements; or nil
	Rbrace token.Pos // position of "}"
	// contains filtered or unexported fields
}

A StructLit node represents a literal struct.

func (*StructLit) AddComment

func (c *StructLit) AddComment(cg *CommentGroup)

func (*StructLit) Comments

func (c *StructLit) Comments() []*CommentGroup

func (*StructLit) End

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

func (StructLit) IsExpr

func (StructLit) IsExpr() bool

func (*StructLit) Pos

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

func (*StructLit) SetComments

func (c *StructLit) SetComments(cgs []*CommentGroup)

type UnaryExpr

type UnaryExpr struct {
	OpPos token.Pos   // position of Op
	Op    token.Token // operator
	X     Expr        // operand
	// contains filtered or unexported fields
}

A UnaryExpr node represents a unary expression.

func (*UnaryExpr) AddComment

func (c *UnaryExpr) AddComment(cg *CommentGroup)

func (*UnaryExpr) Comments

func (c *UnaryExpr) Comments() []*CommentGroup

func (*UnaryExpr) End

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

func (UnaryExpr) IsExpr

func (UnaryExpr) IsExpr() bool

func (*UnaryExpr) Pos

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

func (*UnaryExpr) SetComments

func (c *UnaryExpr) SetComments(cgs []*CommentGroup)

Jump to

Keyboard shortcuts

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