parse
packagestandard libraryThis package is not in the latest version of its module.
Details
Validgo.mod file
The Go module system was introduced in Go 1.11 and is the official dependency management solution for Go.
Redistributable license
Redistributable licenses place minimal restrictions on how software can be used, modified, and redistributed.
Tagged version
Modules with tagged versions give importers more predictable builds.
Stable version
When a project reaches major version v1 it is considered stable.
- Learn more about best practices
Repository
Links
Documentation¶
Overview¶
Package parse builds parse trees for templates as defined by text/templateand html/template. Clients should use those packages to construct templatesrather than this one, which provides shared internal data structures notintended for general use.
Index¶
- func IsEmptyTree(n Node) bool
- func Parse(name, text, leftDelim, rightDelim string, funcs ...map[string]any) (map[string]*Tree, error)
- type ActionNode
- type BoolNode
- type BranchNode
- type BreakNode
- type ChainNode
- type CommandNode
- type CommentNode
- type ContinueNode
- type DotNode
- type FieldNode
- type IdentifierNode
- type IfNode
- type ListNode
- type Mode
- type NilNode
- type Node
- type NodeType
- type NumberNode
- type PipeNode
- type Pos
- type RangeNode
- type StringNode
- type TemplateNode
- type TextNode
- type Tree
- type VariableNode
- type WithNode
Constants¶
This section is empty.
Variables¶
This section is empty.
Functions¶
funcIsEmptyTree¶
IsEmptyTree reports whether this tree (node) is empty of everything but space or comments.
funcParse¶
func Parse(name, text, leftDelim, rightDelimstring, funcs ...map[string]any) (map[string]*Tree,error)
Parse returns a map from template name toTree, created by parsing thetemplates described in the argument string. The top-level template will begiven the specified name. If an error is encountered, parsing stops and anempty map is returned with the error.
Types¶
typeActionNode¶
type ActionNode struct {NodeTypePosLineint// The line number in the input. Deprecated: Kept for compatibility.Pipe *PipeNode// The pipeline in the action.// contains filtered or unexported fields}
ActionNode holds an action (something bounded by delimiters).Control actions have their own nodes; ActionNode represents simpleones such as field evaluations and parenthesized pipelines.
func (*ActionNode)Copy¶
func (a *ActionNode) Copy()Node
func (*ActionNode)String¶
func (a *ActionNode) String()string
typeBoolNode¶
type BoolNode struct {NodeTypePosTruebool// The value of the boolean constant.// contains filtered or unexported fields}
BoolNode holds a boolean constant.
typeBranchNode¶
type BranchNode struct {NodeTypePosLineint// The line number in the input. Deprecated: Kept for compatibility.Pipe *PipeNode// The pipeline to be evaluated.List *ListNode// What to execute if the value is non-empty.ElseList *ListNode// What to execute if the value is empty (nil if absent).// contains filtered or unexported fields}
BranchNode is the common representation of if, range, and with.
func (*BranchNode)Copy¶added ingo1.4
func (b *BranchNode) Copy()Node
func (*BranchNode)String¶
func (b *BranchNode) String()string
typeChainNode¶added ingo1.1
type ChainNode struct {NodeTypePosNodeNodeField []string// The identifiers in lexical order.// 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.
typeCommandNode¶
type CommandNode struct {NodeTypePosArgs []Node// Arguments in lexical order: Identifier, field, or constant.// contains filtered or unexported fields}
CommandNode holds a command (a pipeline inside an evaluating action).
func (*CommandNode)Copy¶
func (c *CommandNode) Copy()Node
func (*CommandNode)String¶
func (c *CommandNode) String()string
typeCommentNode¶added ingo1.16
type CommentNode struct {NodeTypePosTextstring// Comment text.// contains filtered or unexported fields}
CommentNode holds a comment.
func (*CommentNode)Copy¶added ingo1.16
func (c *CommentNode) Copy()Node
func (*CommentNode)String¶added ingo1.16
func (c *CommentNode) String()string
typeContinueNode¶added ingo1.18
ContinueNode represents a {{continue}} action.
func (*ContinueNode)Copy¶added ingo1.18
func (c *ContinueNode) Copy()Node
func (*ContinueNode)String¶added ingo1.18
func (c *ContinueNode) String()string
typeFieldNode¶
type FieldNode struct {NodeTypePosIdent []string// The identifiers in lexical order.// 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.
typeIdentifierNode¶
type IdentifierNode struct {NodeTypePosIdentstring// The identifier's name.// contains filtered or unexported fields}
IdentifierNode holds an identifier.
funcNewIdentifier¶
func NewIdentifier(identstring) *IdentifierNode
NewIdentifier returns a newIdentifierNode with the given identifier name.
func (*IdentifierNode)Copy¶
func (i *IdentifierNode) Copy()Node
func (*IdentifierNode)SetPos¶added ingo1.1
func (i *IdentifierNode) SetPos(posPos) *IdentifierNode
SetPos sets the position.NewIdentifier is a public method so we can't modify its signature.Chained for convenience.TODO: fix one day?
func (*IdentifierNode)SetTree¶added ingo1.4
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.TODO: fix one day?
func (*IdentifierNode)String¶
func (i *IdentifierNode) String()string
typeListNode¶
type ListNode struct {NodeTypePosNodes []Node// The element nodes in lexical order.// contains filtered or unexported fields}
ListNode holds a sequence of nodes.
typeMode¶added ingo1.16
type Modeuint
A mode value is a set of flags (or 0). Modes control parser behavior.
typeNilNode¶added ingo1.1
NilNode holds the special identifier 'nil' representing an untyped nil constant.
typeNode¶
type Node interface {Type()NodeTypeString()string// 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.Copy()NodePosition()Pos// byte position of start of node in full original input string// contains filtered or unexported methods}
A Node is an element in the parse tree. The interface is trivial.The interface contains an unexported method so that onlytypes local to this package can satisfy it.
typeNodeType¶
type NodeTypeint
NodeType identifies the type of a parse tree node.
const (NodeTextNodeType =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.NodeIf// An if action.NodeList// A list of Nodes.NodeNil// An untyped nil constant.NodeNumber// A numerical constant.NodePipe// A pipeline of commands.NodeRange// A range action.NodeString// A string constant.NodeTemplate// A template invocation action.NodeVariable// A $ variable.NodeWith// A with action.NodeComment// A comment.NodeBreak// A break action.NodeContinue// A continue action.)
typeNumberNode¶
type NumberNode struct {NodeTypePosIsIntbool// Number has an integral value.IsUintbool// Number has an unsigned integral value.IsFloatbool// Number has a floating-point value.IsComplexbool// Number is complex.Int64int64// The signed integer value.Uint64uint64// The unsigned integer value.Float64float64// The floating-point value.Complex128complex128// The complex value.Textstring// The original textual representation from the input.// 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)Copy¶
func (n *NumberNode) Copy()Node
func (*NumberNode)String¶
func (n *NumberNode) String()string
typePipeNode¶
type PipeNode struct {NodeTypePosLineint// The line number in the input. Deprecated: Kept for compatibility.IsAssignbool// The variables are being assigned, not declared.Decl []*VariableNode// Variables in lexical order.Cmds []*CommandNode// The commands in lexical order.// contains filtered or unexported fields}
PipeNode holds a pipeline with optional declaration
typePos¶added ingo1.1
type Posint
Pos represents a byte position in the original input text from whichthis template was parsed.
typeRangeNode¶
type RangeNode struct {BranchNode}
RangeNode represents a {{range}} action and its commands.
typeStringNode¶
type StringNode struct {NodeTypePosQuotedstring// The original text of the string, with quotes.Textstring// The string, after quote processing.// contains filtered or unexported fields}
StringNode holds a string constant. The value has been "unquoted".
func (*StringNode)Copy¶
func (s *StringNode) Copy()Node
func (*StringNode)String¶
func (s *StringNode) String()string
typeTemplateNode¶
type TemplateNode struct {NodeTypePosLineint// The line number in the input. Deprecated: Kept for compatibility.Namestring// The name of the template (unquoted).Pipe *PipeNode// The command to evaluate as dot for the template.// contains filtered or unexported fields}
TemplateNode represents a {{template}} action.
func (*TemplateNode)Copy¶
func (t *TemplateNode) Copy()Node
func (*TemplateNode)String¶
func (t *TemplateNode) String()string
typeTextNode¶
type TextNode struct {NodeTypePosText []byte// The text; may span newlines.// contains filtered or unexported fields}
TextNode holds plain text.
typeTree¶
type Tree struct {Namestring// name of the template represented by the tree.ParseNamestring// name of the top-level template during parsing, for error messages.Root *ListNode// top-level root of the tree.ModeMode// parsing mode.// contains filtered or unexported fields}
Tree is the representation of a single parsed template.
func (*Tree)ErrorContext¶added ingo1.1
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.
func (*Tree)Parse¶
func (t *Tree) Parse(text, leftDelim, rightDelimstring, treeSet map[string]*Tree, funcs ...map[string]any) (tree *Tree, errerror)
Parse parses the template definition string to construct a representation ofthe template for execution. If either action delimiter string is empty, thedefault ("{{" or "}}") is used. Embedded template definitions are added tothe treeSet map.
typeVariableNode¶
type VariableNode struct {NodeTypePosIdent []string// Variable name and fields in lexical order.// contains filtered or unexported fields}
VariableNode holds a list of variable names, possibly with chained fieldaccesses. The dollar sign is part of the (first) name.
func (*VariableNode)Copy¶
func (v *VariableNode) Copy()Node
func (*VariableNode)String¶
func (v *VariableNode) String()string