package parse

Import Path
	text/template/parse (on go.dev)

Dependency Relation
	imports 7 packages, and imported by 2 packages

Involved Source Files lex.go node.go Package parse builds parse trees for templates as defined by text/template and html/template. Clients should use those packages to construct templates rather than this one, which provides shared internal data structures not intended for general use.
Package-Level Type Names (total 27)
/* sort by: | */
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. // The line number in the input. Deprecated: Kept for compatibility. NodeType NodeType // The pipeline in the action. Pos Pos (*ActionNode) Copy() Node ( ActionNode) Position() Pos (*ActionNode) String() string Type returns itself and provides an easy default implementation for embedding in a Node. Embedded in all non-trivial Nodes. *ActionNode : Node *ActionNode : expvar.Var *ActionNode : fmt.Stringer
BoolNode holds a boolean constant. NodeType NodeType Pos Pos // The value of the boolean constant. (*BoolNode) Copy() Node ( BoolNode) Position() Pos (*BoolNode) String() string Type returns itself and provides an easy default implementation for embedding in a Node. Embedded in all non-trivial Nodes. *BoolNode : Node *BoolNode : expvar.Var *BoolNode : fmt.Stringer
BranchNode is the common representation of if, range, and with. // What to execute if the value is empty (nil if absent). // The line number in the input. Deprecated: Kept for compatibility. // What to execute if the value is non-empty. NodeType NodeType // The pipeline to be evaluated. Pos Pos (*BranchNode) Copy() Node ( BranchNode) Position() Pos (*BranchNode) String() string Type returns itself and provides an easy default implementation for embedding in a Node. Embedded in all non-trivial Nodes. *BranchNode : Node *BranchNode : expvar.Var *BranchNode : fmt.Stringer
BreakNode represents a {{break}} action. Line int NodeType NodeType Pos Pos (*BreakNode) Copy() Node ( BreakNode) Position() Pos (*BreakNode) String() string Type returns itself and provides an easy default implementation for embedding in a Node. Embedded in all non-trivial Nodes. *BreakNode : Node *BreakNode : expvar.Var *BreakNode : fmt.Stringer
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. // The identifiers in lexical order. Node Node NodeType NodeType Pos Pos Add adds the named field (which should start with a period) to the end of the chain. (*ChainNode) Copy() Node ( ChainNode) Position() Pos (*ChainNode) String() string Type returns itself and provides an easy default implementation for embedding in a Node. Embedded in all non-trivial Nodes. *ChainNode : Node *ChainNode : expvar.Var *ChainNode : fmt.Stringer
CommandNode holds a command (a pipeline inside an evaluating action). // Arguments in lexical order: Identifier, field, or constant. NodeType NodeType Pos Pos (*CommandNode) Copy() Node ( CommandNode) Position() Pos (*CommandNode) String() string Type returns itself and provides an easy default implementation for embedding in a Node. Embedded in all non-trivial Nodes. *CommandNode : Node *CommandNode : expvar.Var *CommandNode : fmt.Stringer
CommentNode holds a comment. NodeType NodeType Pos Pos // Comment text. (*CommentNode) Copy() Node ( CommentNode) Position() Pos (*CommentNode) String() string Type returns itself and provides an easy default implementation for embedding in a Node. Embedded in all non-trivial Nodes. *CommentNode : Node *CommentNode : expvar.Var *CommentNode : fmt.Stringer
ContinueNode represents a {{continue}} action. Line int NodeType NodeType Pos Pos (*ContinueNode) Copy() Node ( ContinueNode) Position() Pos (*ContinueNode) String() string Type returns itself and provides an easy default implementation for embedding in a Node. Embedded in all non-trivial Nodes. *ContinueNode : Node *ContinueNode : expvar.Var *ContinueNode : fmt.Stringer
DotNode holds the special identifier '.'. NodeType NodeType Pos Pos (*DotNode) Copy() Node ( DotNode) Position() Pos (*DotNode) String() string (*DotNode) Type() NodeType *DotNode : Node *DotNode : expvar.Var *DotNode : fmt.Stringer
FieldNode holds a field (identifier starting with '.'). The names may be chained ('.x.y'). The period is dropped from each ident. // Variable name and fields in lexical order. NodeType NodeType Pos Pos (*FieldNode) Copy() Node ( FieldNode) Position() Pos (*FieldNode) String() string Type returns itself and provides an easy default implementation for embedding in a Node. Embedded in all non-trivial Nodes. *FieldNode : Node *FieldNode : expvar.Var *FieldNode : fmt.Stringer
IdentifierNode holds an identifier. // The identifier's name. NodeType NodeType Pos Pos (*IdentifierNode) Copy() Node ( IdentifierNode) Position() Pos SetPos sets the position. [NewIdentifier] is a public method so we can't modify its signature. Chained for convenience. TODO: fix one day? SetTree sets the parent tree for the node. [NewIdentifier] is a public method so we can't modify its signature. Chained for convenience. TODO: fix one day? (*IdentifierNode) String() string Type returns itself and provides an easy default implementation for embedding in a Node. Embedded in all non-trivial Nodes. *IdentifierNode : Node *IdentifierNode : expvar.Var *IdentifierNode : fmt.Stringer func NewIdentifier(ident string) *IdentifierNode func (*IdentifierNode).SetPos(pos Pos) *IdentifierNode func (*IdentifierNode).SetTree(t *Tree) *IdentifierNode
IfNode represents an {{if}} action and its commands. BranchNode BranchNode // What to execute if the value is empty (nil if absent). // The line number in the input. Deprecated: Kept for compatibility. // What to execute if the value is non-empty. BranchNode.NodeType NodeType // The pipeline to be evaluated. BranchNode.Pos Pos (*IfNode) Copy() Node ( IfNode) Position() Pos (*IfNode) String() string Type returns itself and provides an easy default implementation for embedding in a Node. Embedded in all non-trivial Nodes. *IfNode : Node *IfNode : expvar.Var *IfNode : fmt.Stringer
ListNode holds a sequence of nodes. NodeType NodeType // The element nodes in lexical order. Pos Pos (*ListNode) Copy() Node (*ListNode) CopyList() *ListNode ( ListNode) Position() Pos (*ListNode) String() string Type returns itself and provides an easy default implementation for embedding in a Node. Embedded in all non-trivial Nodes. *ListNode : Node *ListNode : expvar.Var *ListNode : fmt.Stringer func (*ListNode).CopyList() *ListNode
A mode value is a set of flags (or 0). Modes control parser behavior. const ParseComments const SkipFuncCheck
NilNode holds the special identifier 'nil' representing an untyped nil constant. NodeType NodeType Pos Pos (*NilNode) Copy() Node ( NilNode) Position() Pos (*NilNode) String() string (*NilNode) Type() NodeType *NilNode : Node *NilNode : expvar.Var *NilNode : fmt.Stringer
A Node is an element in the parse tree. The interface is trivial. The interface contains an unexported method so that only types local to this package can satisfy it. Copy does a deep copy of the Node and all its components. To avoid type assertions, some XxxNodes also have specialized CopyXxx methods that return *XxxNode. // byte position of start of node in full original input string ( Node) String() string ( Node) Type() NodeType *ActionNode *BoolNode *BranchNode *BreakNode *ChainNode *CommandNode *CommentNode *ContinueNode *DotNode *FieldNode *IdentifierNode *IfNode *ListNode *NilNode *NumberNode *PipeNode *RangeNode *StringNode *TemplateNode *TextNode *VariableNode *WithNode Node : expvar.Var Node : fmt.Stringer func (*ActionNode).Copy() Node func (*BoolNode).Copy() Node func (*BranchNode).Copy() Node func (*BreakNode).Copy() Node func (*ChainNode).Copy() Node func (*CommandNode).Copy() Node func (*CommentNode).Copy() Node func (*ContinueNode).Copy() Node func (*DotNode).Copy() Node func (*FieldNode).Copy() Node func (*IdentifierNode).Copy() Node func (*IfNode).Copy() Node func (*ListNode).Copy() Node func (*NilNode).Copy() Node func Node.Copy() Node func (*NumberNode).Copy() Node func (*PipeNode).Copy() Node func (*RangeNode).Copy() Node func (*StringNode).Copy() Node func (*TemplateNode).Copy() Node func (*TextNode).Copy() Node func (*VariableNode).Copy() Node func (*WithNode).Copy() Node func IsEmptyTree(n Node) bool func (*Tree).ErrorContext(n Node) (location, context string)
NodeType identifies the type of a parse tree node. Type returns itself and provides an easy default implementation for embedding in a Node. Embedded in all non-trivial Nodes. func (*DotNode).Type() NodeType func (*NilNode).Type() NodeType func Node.Type() NodeType func NodeType.Type() NodeType const NodeAction const NodeBool const NodeBreak const NodeChain const NodeCommand const NodeComment const NodeContinue const NodeDot const NodeField const NodeIdentifier const NodeIf const NodeList const NodeNil const NodeNumber const NodePipe const NodeRange const NodeString const NodeTemplate const NodeText const NodeVariable const NodeWith
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. // The complex value. // The floating-point value. // The signed integer value. // Number is complex. // Number has a floating-point value. // Number has an integral value. // Number has an unsigned integral value. NodeType NodeType Pos Pos // The original textual representation from the input. // The unsigned integer value. (*NumberNode) Copy() Node ( NumberNode) Position() Pos (*NumberNode) String() string Type returns itself and provides an easy default implementation for embedding in a Node. Embedded in all non-trivial Nodes. *NumberNode : Node *NumberNode : expvar.Var *NumberNode : fmt.Stringer
PipeNode holds a pipeline with optional declaration // The commands in lexical order. // Variables in lexical order. // The variables are being assigned, not declared. // The line number in the input. Deprecated: Kept for compatibility. NodeType NodeType Pos Pos (*PipeNode) Copy() Node (*PipeNode) CopyPipe() *PipeNode ( PipeNode) Position() Pos (*PipeNode) String() string Type returns itself and provides an easy default implementation for embedding in a Node. Embedded in all non-trivial Nodes. *PipeNode : Node *PipeNode : expvar.Var *PipeNode : fmt.Stringer func (*PipeNode).CopyPipe() *PipeNode
Pos represents a byte position in the original input text from which this template was parsed. ( Pos) Position() Pos func Node.Position() Pos func Pos.Position() Pos func (*IdentifierNode).SetPos(pos Pos) *IdentifierNode
RangeNode represents a {{range}} action and its commands. BranchNode BranchNode // What to execute if the value is empty (nil if absent). // The line number in the input. Deprecated: Kept for compatibility. // What to execute if the value is non-empty. BranchNode.NodeType NodeType // The pipeline to be evaluated. BranchNode.Pos Pos (*RangeNode) Copy() Node ( RangeNode) Position() Pos (*RangeNode) String() string Type returns itself and provides an easy default implementation for embedding in a Node. Embedded in all non-trivial Nodes. *RangeNode : Node *RangeNode : expvar.Var *RangeNode : fmt.Stringer
StringNode holds a string constant. The value has been "unquoted". NodeType NodeType Pos Pos // The original text of the string, with quotes. // The string, after quote processing. (*StringNode) Copy() Node ( StringNode) Position() Pos (*StringNode) String() string Type returns itself and provides an easy default implementation for embedding in a Node. Embedded in all non-trivial Nodes. *StringNode : Node *StringNode : expvar.Var *StringNode : fmt.Stringer
TemplateNode represents a {{template}} action. // The line number in the input. Deprecated: Kept for compatibility. // The name of the template (unquoted). NodeType NodeType // The command to evaluate as dot for the template. Pos Pos (*TemplateNode) Copy() Node ( TemplateNode) Position() Pos (*TemplateNode) String() string Type returns itself and provides an easy default implementation for embedding in a Node. Embedded in all non-trivial Nodes. *TemplateNode : Node *TemplateNode : expvar.Var *TemplateNode : fmt.Stringer
TextNode holds plain text. NodeType NodeType Pos Pos // The text; may span newlines. (*TextNode) Copy() Node ( TextNode) Position() Pos (*TextNode) String() string Type returns itself and provides an easy default implementation for embedding in a Node. Embedded in all non-trivial Nodes. *TextNode : Node *TextNode : expvar.Var *TextNode : fmt.Stringer
Tree is the representation of a single parsed template. // parsing mode. // name of the template represented by the tree. // name of the top-level template during parsing, for error messages. // top-level root of the tree. Copy returns a copy of the [Tree]. Any parsing state is discarded. 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. Parse parses the template definition string to construct a representation of the template for execution. If either action delimiter string is empty, the default ("{{" or "}}") is used. Embedded template definitions are added to the treeSet map. func New(name string, funcs ...map[string]any) *Tree func Parse(name, text, leftDelim, rightDelim string, funcs ...map[string]any) (map[string]*Tree, error) func (*Tree).Copy() *Tree func (*Tree).Parse(text, leftDelim, rightDelim string, treeSet map[string]*Tree, funcs ...map[string]any) (tree *Tree, err error) func (*IdentifierNode).SetTree(t *Tree) *IdentifierNode func (*Tree).Parse(text, leftDelim, rightDelim string, treeSet map[string]*Tree, funcs ...map[string]any) (tree *Tree, err error) func text/template.(*Template).AddParseTree(name string, tree *Tree) (*template.Template, error) func html/template.(*Template).AddParseTree(name string, tree *Tree) (*template.Template, error)
VariableNode holds a list of variable names, possibly with chained field accesses. The dollar sign is part of the (first) name. // Variable name and fields in lexical order. NodeType NodeType Pos Pos (*VariableNode) Copy() Node ( VariableNode) Position() Pos (*VariableNode) String() string Type returns itself and provides an easy default implementation for embedding in a Node. Embedded in all non-trivial Nodes. *VariableNode : Node *VariableNode : expvar.Var *VariableNode : fmt.Stringer
WithNode represents a {{with}} action and its commands. BranchNode BranchNode // What to execute if the value is empty (nil if absent). // The line number in the input. Deprecated: Kept for compatibility. // What to execute if the value is non-empty. BranchNode.NodeType NodeType // The pipeline to be evaluated. BranchNode.Pos Pos (*WithNode) Copy() Node ( WithNode) Position() Pos (*WithNode) String() string Type returns itself and provides an easy default implementation for embedding in a Node. Embedded in all non-trivial Nodes. *WithNode : Node *WithNode : expvar.Var *WithNode : fmt.Stringer
Package-Level Functions (total 4)
IsEmptyTree reports whether this tree (node) is empty of everything but space or comments.
New allocates a new parse tree with the given name.
NewIdentifier returns a new [IdentifierNode] with the given identifier name.
Parse returns a map from template name to [Tree], created by parsing the templates described in the argument string. The top-level template will be given the specified name. If an error is encountered, parsing stops and an empty map is returned with the error.
Package-Level Constants (total 23)
const NodeAction NodeType = 1 // A non-control action such as a field evaluation.
const NodeBool NodeType = 2 // A boolean constant.
const NodeBreak NodeType = 21 // A break action.
const NodeChain NodeType = 3 // A sequence of field accesses.
const NodeCommand NodeType = 4 // An element of a pipeline.
const NodeComment NodeType = 20 // A comment.
const NodeContinue NodeType = 22 // A continue action.
const NodeDot NodeType = 5 // The cursor, dot.
const NodeField NodeType = 8 // A field or method name.
const NodeIdentifier NodeType = 9 // An identifier; always a function name.
const NodeIf NodeType = 10 // An if action.
const NodeList NodeType = 11 // A list of Nodes.
const NodeNil NodeType = 12 // An untyped nil constant.
const NodeNumber NodeType = 13 // A numerical constant.
const NodePipe NodeType = 14 // A pipeline of commands.
const NodeRange NodeType = 15 // A range action.
const NodeString NodeType = 16 // A string constant.
const NodeTemplate NodeType = 17 // A template invocation action.
const NodeText NodeType = 0 // Plain text.
const NodeVariable NodeType = 18 // A $ variable.
const NodeWith NodeType = 19 // A with action.
const ParseComments Mode = 1 // parse comments and add them to AST
const SkipFuncCheck Mode = 2 // do not check that functions are defined