Documentation
¶
Overview ¶
Package parse builds parse trees for Go templates using the text/template grammar. It is a fork of the standard library's text/template/parse, trimmed and adapted for gotmplfumpt's formatting needs.
Index ¶
- type ActionNode
- type BoolNode
- type BranchNode
- type ChainNode
- type CommandNode
- type CommentNode
- type DotNode
- type ElseNode
- type EndNode
- type FieldNode
- type IdentifierNode
- type ListNode
- type Mode
- type NilNode
- type Node
- type NodeType
- type NumberNode
- type PipeNode
- type Pos
- type StringNode
- type TextNode
- type Tree
- type VariableNode
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ActionNode ¶
type ActionNode struct {
NodeType
Pipe *PipeNode
Pos
Trim trim
Line int
// contains filtered or unexported fields
}
ActionNode holds an action (something bounded by delimiters). Control actions have their own nodes; ActionNode represents simple ones such as field evaluations and parenthesized pipelines.
func (*ActionNode) String ¶
func (a *ActionNode) String() string
String renders the action as source.
type BranchNode ¶
type BranchNode struct {
NodeType
Pipe *PipeNode
List *ListNode
End *EndNode
Keyword string
Elses []*ElseNode
Pos
Trim trim
Line int
// contains filtered or unexported fields
}
BranchNode is the common representation of if, range, and with.
func (*BranchNode) String ¶
func (b *BranchNode) String() string
String renders the branch as source.
type ChainNode ¶
type ChainNode struct {
Node Node
NodeType
Field []string
Pos
Line int
// contains filtered or unexported fields
}
ChainNode holds a term followed by a chain of field accesses (identifier starting with '.'). The names may be chained ('.x.y'). The periods are dropped from each ident.
type CommandNode ¶
type CommandNode struct {
NodeType
Args []Node
Pos
Trim trim
Line int
// contains filtered or unexported fields
}
CommandNode holds a command (a pipeline inside an evaluating action).
func (*CommandNode) String ¶
func (c *CommandNode) String() string
String renders the command as source.
type CommentNode ¶
type CommentNode struct {
NodeType
Text string
Pos
Trim trim
Line int
// contains filtered or unexported fields
}
CommentNode holds a comment.
func (*CommentNode) String ¶
func (c *CommentNode) String() string
String renders the comment as source.
type ElseNode ¶
type ElseNode struct {
NodeType
Pipe *PipeNode
List *ListNode
Keyword string
Pos
Trim trim
Line int
// contains filtered or unexported fields
}
ElseNode represents an {{else}}, {{else if}}, or {{else with}} action. Does not appear in the final tree.
type FieldNode ¶
type FieldNode struct {
NodeType
Ident []string
Pos
Line int
// contains filtered or unexported fields
}
FieldNode holds a field (identifier starting with '.'). The names may be chained ('.x.y'). The period is dropped from each ident.
type IdentifierNode ¶
type IdentifierNode struct {
NodeType
Ident string
Pos
Line int
// contains filtered or unexported fields
}
IdentifierNode holds an identifier.
func NewIdentifier ¶
func NewIdentifier(ident string) *IdentifierNode
NewIdentifier returns a new IdentifierNode with the given identifier name.
func (*IdentifierNode) SetLine ¶
func (i *IdentifierNode) SetLine(line int) *IdentifierNode
SetLine sets the line number. NewIdentifier is a public method so we can't modify its signature. Chained for convenience.
func (*IdentifierNode) SetPos ¶
func (i *IdentifierNode) SetPos(pos Pos) *IdentifierNode
SetPos sets the position. NewIdentifier is a public method so we can't modify its signature. Chained for convenience.
func (*IdentifierNode) SetTree ¶
func (i *IdentifierNode) SetTree(t *Tree) *IdentifierNode
SetTree sets the parent tree for the node. NewIdentifier is a public method so we can't modify its signature. Chained for convenience.
func (*IdentifierNode) String ¶
func (i *IdentifierNode) String() string
String returns the identifier name.
type ListNode ¶
ListNode holds a sequence of nodes.
func Parse ¶
Parse parses the given template source and returns the root list node. Returns the error from (*Tree).Parse if parsing fails.
func (*ListNode) HasIgnoreAll ¶
HasIgnoreAll reports whether the first comment in the list is a gotmplfumpt-ignore-all directive.
type Mode ¶
type Mode uint
Mode is a set of flags (or 0) controlling parser behavior.
const ( // ParseComments adds comment nodes to the AST. ParseComments Mode = 1 << iota // SkipFuncCheck disables function-presence checks. SkipFuncCheck )
Mode flags. ParseComments adds {{/* … */}} comments to the AST; SkipFuncCheck disables function-presence checks (unused here but preserved for API compatibility with text/template/parse).
type Node ¶
type Node interface {
Type() NodeType
String() string
Position() Pos // byte position of start of node in full original input
// contains filtered or unexported methods
}
Node is an element in the parse tree. The interface contains an unexported method so that only types local to this package can satisfy it.
type NodeType ¶
type NodeType int
NodeType identifies the type of a parse tree node.
const ( NodeText NodeType = iota // Plain text. NodeAction // A non-control action such as a field evaluation. NodeBool // A boolean constant. NodeChain // A sequence of field accesses. NodeCommand // An element of a pipeline. NodeDot // The cursor, dot. NodeField // A field or method name. NodeIdentifier // An identifier; always a function name. NodeBranch // A branch-y action. NodeList // A list of Nodes. NodeNil // An untyped nil constant. NodeNumber // A numerical constant. NodePipe // A pipeline of commands. NodeString // A string constant. NodeTemplate // A template invocation action. NodeVariable // A $ variable. NodeComment // A comment. )
NodeType values identify the kind of a parse tree node.
type NumberNode ¶
type NumberNode struct {
NodeType
Text string
Complex128 complex128
Int64 int64
Uint64 uint64
Float64 float64
Pos
Line int
IsInt bool
IsUint bool
IsFloat bool
IsComplex bool
// contains filtered or unexported fields
}
NumberNode holds a number: signed or unsigned integer, float, or complex. The value is parsed and stored under all the types that can represent the value. This simulates in a small amount of code the behavior of Go's ideal constants.
func (*NumberNode) String ¶
func (n *NumberNode) String() string
String returns the original textual representation of the number.
type PipeNode ¶
type PipeNode struct {
NodeType
Decl []*VariableNode
Cmds []*CommandNode
Pos
Line int
IsAssign bool
// contains filtered or unexported fields
}
PipeNode holds a pipeline with optional declaration.
type Pos ¶
type Pos int
Pos represents a byte position in the original input text from which this template was parsed.
type StringNode ¶
type StringNode struct {
NodeType
Quoted string
Text string
Pos
Line int
// contains filtered or unexported fields
}
StringNode holds a string constant. The value has been "unquoted".
func (*StringNode) String ¶
func (s *StringNode) String() string
String returns the original quoted representation of the string.
type TextNode ¶
type TextNode struct {
NodeType
Text string
Pos
Line int
// contains filtered or unexported fields
}
TextNode holds plain text.
type Tree ¶
type Tree struct {
Root *ListNode
// contains filtered or unexported fields
}
Tree is the representation of a single parsed template.
func (*Tree) ErrorContext ¶
ErrorContext returns a textual representation of the location of the node in the input text. The receiver is only used when the node does not have a pointer to the tree inside, which can occur in old code.
type VariableNode ¶
type VariableNode struct {
NodeType
Ident []string
Pos
Line int
// contains filtered or unexported fields
}
VariableNode holds a list of variable names, possibly with chained field accesses. The dollar sign is part of the (first) name.
func (*VariableNode) String ¶
func (v *VariableNode) String() string
String renders the variable and its field chain.