version: 1.10

package parse

import "text/template/parse"

Overview

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.

Index

Package files

lex.go node.go parse.go

func IsEmptyTree

  1. func IsEmptyTree(n Node) bool

IsEmptyTree reports whether this tree (node) is empty of everything but space.

func Parse

  1. func Parse(name, text, leftDelim, rightDelim string, funcs ...map[string]interface{}) (map[string]*Tree, error)

Parse returns a map from template name to parse.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.

type ActionNode

  1. type ActionNode struct {
  2. NodeType
  3. Pos
  4.  
  5. Line int // The line number in the input. Deprecated: Kept for compatibility.
  6. Pipe *PipeNode // The pipeline in the action.
  7. // contains filtered or unexported fields
  8. }

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) Copy

  1. func (a *ActionNode) Copy() Node

func (*ActionNode) String

  1. func (a *ActionNode) String() string

type BoolNode

  1. type BoolNode struct {
  2. NodeType
  3. Pos
  4.  
  5. True bool // The value of the boolean constant.
  6. // contains filtered or unexported fields
  7. }

BoolNode holds a boolean constant.

func (*BoolNode) Copy

  1. func (b *BoolNode) Copy() Node

func (*BoolNode) String

  1. func (b *BoolNode) String() string

type BranchNode

  1. type BranchNode struct {
  2. NodeType
  3. Pos
  4.  
  5. Line int // The line number in the input. Deprecated: Kept for compatibility.
  6. Pipe *PipeNode // The pipeline to be evaluated.
  7. List *ListNode // What to execute if the value is non-empty.
  8. ElseList *ListNode // What to execute if the value is empty (nil if absent).
  9. // contains filtered or unexported fields
  10. }

BranchNode is the common representation of if, range, and with.

func (*BranchNode) Copy

  1. func (b *BranchNode) Copy() Node

func (*BranchNode) String

  1. func (b *BranchNode) String() string

type ChainNode

  1. type ChainNode struct {
  2. NodeType
  3. Pos
  4.  
  5. Node Node
  6. Field []string // The identifiers in lexical order.
  7. // contains filtered or unexported fields
  8. }

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.

func (*ChainNode) Add

  1. func (c *ChainNode) Add(field string)

Add adds the named field (which should start with a period) to the end of the chain.

func (*ChainNode) Copy

  1. func (c *ChainNode) Copy() Node

func (*ChainNode) String

  1. func (c *ChainNode) String() string

type CommandNode

  1. type CommandNode struct {
  2. NodeType
  3. Pos
  4.  
  5. Args []Node // Arguments in lexical order: Identifier, field, or constant.
  6. // contains filtered or unexported fields
  7. }

CommandNode holds a command (a pipeline inside an evaluating action).

func (*CommandNode) Copy

  1. func (c *CommandNode) Copy() Node

func (*CommandNode) String

  1. func (c *CommandNode) String() string

type DotNode

  1. type DotNode struct {
  2. NodeType
  3. Pos
  4. // contains filtered or unexported fields
  5. }

DotNode holds the special identifier ‘.’.

func (*DotNode) Copy

  1. func (d *DotNode) Copy() Node

func (*DotNode) String

  1. func (d *DotNode) String() string

func (*DotNode) Type

  1. func (d *DotNode) Type() NodeType

type FieldNode

  1. type FieldNode struct {
  2. NodeType
  3. Pos
  4.  
  5. Ident []string // The identifiers in lexical order.
  6. // contains filtered or unexported fields
  7. }

FieldNode holds a field (identifier starting with ‘.’). The names may be chained (‘.x.y’). The period is dropped from each ident.

func (*FieldNode) Copy

  1. func (f *FieldNode) Copy() Node

func (*FieldNode) String

  1. func (f *FieldNode) String() string

type IdentifierNode

  1. type IdentifierNode struct {
  2. NodeType
  3. Pos
  4.  
  5. Ident string // The identifier's name.
  6. // contains filtered or unexported fields
  7. }

IdentifierNode holds an identifier.

func NewIdentifier

  1. func NewIdentifier(ident string) *IdentifierNode

NewIdentifier returns a new IdentifierNode with the given identifier name.

func (*IdentifierNode) Copy

  1. func (i *IdentifierNode) Copy() Node

func (*IdentifierNode) SetPos

  1. 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. TODO: fix one day?

func (*IdentifierNode) SetTree

  1. 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

  1. func (i *IdentifierNode) String() string

type IfNode

  1. type IfNode struct {
  2. BranchNode
  3. }

IfNode represents an {{if}} action and its commands.

func (*IfNode) Copy

  1. func (i *IfNode) Copy() Node

type ListNode

  1. type ListNode struct {
  2. NodeType
  3. Pos
  4.  
  5. Nodes []Node // The element nodes in lexical order.
  6. // contains filtered or unexported fields
  7. }

ListNode holds a sequence of nodes.

func (*ListNode) Copy

  1. func (l *ListNode) Copy() Node

func (*ListNode) CopyList

  1. func (l *ListNode) CopyList() *ListNode

func (*ListNode) String

  1. func (l *ListNode) String() string

type NilNode

  1. type NilNode struct {
  2. NodeType
  3. Pos
  4. // contains filtered or unexported fields
  5. }

NilNode holds the special identifier ‘nil’ representing an untyped nil constant.

func (*NilNode) Copy

  1. func (n *NilNode) Copy() Node

func (*NilNode) String

  1. func (n *NilNode) String() string

func (*NilNode) Type

  1. func (n *NilNode) Type() NodeType

type Node

  1. type Node interface {
  2. Type() NodeType
  3. String() string
  4. // Copy does a deep copy of the Node and all its components.
  5. // To avoid type assertions, some XxxNodes also have specialized
  6. // CopyXxx methods that return *XxxNode.
  7. Copy() Node
  8. Position() Pos // byte position of start of node in full original input string
  9. // contains filtered or unexported methods
  10. }

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.

type NodeType

  1. type NodeType int

NodeType identifies the type of a parse tree node.

  1. const (
  2. NodeText NodeType = iota // Plain text.
  3. NodeAction // A non-control action such as a field evaluation.
  4. NodeBool // A boolean constant.
  5. NodeChain // A sequence of field accesses.
  6. NodeCommand // An element of a pipeline.
  7. NodeDot // The cursor, dot.
  8.  
  9. NodeField // A field or method name.
  10. NodeIdentifier // An identifier; always a function name.
  11. NodeIf // An if action.
  12. NodeList // A list of Nodes.
  13. NodeNil // An untyped nil constant.
  14. NodeNumber // A numerical constant.
  15. NodePipe // A pipeline of commands.
  16. NodeRange // A range action.
  17. NodeString // A string constant.
  18. NodeTemplate // A template invocation action.
  19. NodeVariable // A $ variable.
  20. NodeWith // A with action.
  21. )

func (NodeType) Type

  1. func (t NodeType) Type() NodeType

Type returns itself and provides an easy default implementation for embedding in a Node. Embedded in all non-trivial Nodes.

type NumberNode

  1. type NumberNode struct {
  2. NodeType
  3. Pos
  4.  
  5. IsInt bool // Number has an integral value.
  6. IsUint bool // Number has an unsigned integral value.
  7. IsFloat bool // Number has a floating-point value.
  8. IsComplex bool // Number is complex.
  9. Int64 int64 // The signed integer value.
  10. Uint64 uint64 // The unsigned integer value.
  11. Float64 float64 // The floating-point value.
  12. Complex128 complex128 // The complex value.
  13. Text string // The original textual representation from the input.
  14. // contains filtered or unexported fields
  15. }

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

  1. func (n *NumberNode) Copy() Node

func (*NumberNode) String

  1. func (n *NumberNode) String() string

type PipeNode

  1. type PipeNode struct {
  2. NodeType
  3. Pos
  4.  
  5. Line int // The line number in the input. Deprecated: Kept for compatibility.
  6. Decl []*VariableNode // Variable declarations in lexical order.
  7. Cmds []*CommandNode // The commands in lexical order.
  8. // contains filtered or unexported fields
  9. }

PipeNode holds a pipeline with optional declaration

func (*PipeNode) Copy

  1. func (p *PipeNode) Copy() Node

func (*PipeNode) CopyPipe

  1. func (p *PipeNode) CopyPipe() *PipeNode

func (*PipeNode) String

  1. func (p *PipeNode) String() string

type Pos

  1. type Pos int

Pos represents a byte position in the original input text from which this template was parsed.

func (Pos) Position

  1. func (p Pos) Position() Pos

type RangeNode

  1. type RangeNode struct {
  2. BranchNode
  3. }

RangeNode represents a {{range}} action and its commands.

func (*RangeNode) Copy

  1. func (r *RangeNode) Copy() Node

type StringNode

  1. type StringNode struct {
  2. NodeType
  3. Pos
  4.  
  5. Quoted string // The original text of the string, with quotes.
  6. Text string // The string, after quote processing.
  7. // contains filtered or unexported fields
  8. }

StringNode holds a string constant. The value has been “unquoted”.

func (*StringNode) Copy

  1. func (s *StringNode) Copy() Node

func (*StringNode) String

  1. func (s *StringNode) String() string

type TemplateNode

  1. type TemplateNode struct {
  2. NodeType
  3. Pos
  4.  
  5. Line int // The line number in the input. Deprecated: Kept for compatibility.
  6. Name string // The name of the template (unquoted).
  7. Pipe *PipeNode // The command to evaluate as dot for the template.
  8. // contains filtered or unexported fields
  9. }

TemplateNode represents a {{template}} action.

func (*TemplateNode) Copy

  1. func (t *TemplateNode) Copy() Node

func (*TemplateNode) String

  1. func (t *TemplateNode) String() string

type TextNode

  1. type TextNode struct {
  2. NodeType
  3. Pos
  4.  
  5. Text []byte // The text; may span newlines.
  6. // contains filtered or unexported fields
  7. }

TextNode holds plain text.

func (*TextNode) Copy

  1. func (t *TextNode) Copy() Node

func (*TextNode) String

  1. func (t *TextNode) String() string

type Tree

  1. type Tree struct {
  2. Name string // name of the template represented by the tree.
  3. ParseName string // name of the top-level template during parsing, for error messages.
  4. Root *ListNode // top-level root of the tree.
  5. // contains filtered or unexported fields
  6. }

Tree is the representation of a single parsed template.

func New

  1. func New(name string, funcs ...map[string]interface{}) *Tree

New allocates a new parse tree with the given name.

func (*Tree) Copy

  1. func (t *Tree) Copy() *Tree

Copy returns a copy of the Tree. Any parsing state is discarded.

func (*Tree) ErrorContext

  1. func (t *Tree) ErrorContext(n Node) (location, context string)

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

  1. func (t *Tree) Parse(text, leftDelim, rightDelim string, treeSet map[string]*Tree, funcs ...map[string]interface{}) (tree *Tree, err error)

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.

type VariableNode

  1. type VariableNode struct {
  2. NodeType
  3. Pos
  4.  
  5. Ident []string // Variable name and fields in lexical order.
  6. // contains filtered or unexported fields
  7. }

VariableNode holds a list of variable names, possibly with chained field accesses. The dollar sign is part of the (first) name.

func (*VariableNode) Copy

  1. func (v *VariableNode) Copy() Node

func (*VariableNode) String

  1. func (v *VariableNode) String() string

type WithNode

  1. type WithNode struct {
  2. BranchNode
  3. }

WithNode represents a {{with}} action and its commands.

func (*WithNode) Copy

  1. func (w *WithNode) Copy() Node