Package parse

import "text/template/parse"
Overview
Index

Overview ▾

包解析为text / template和html / template定义的模板构建解析树. 客户应使用这些包而不是此包来构造模板,后者提供了不用于一般用途的共享内部数据结构.

Index ▾

func IsEmptyTree(n Node) bool
func Parse(name, text, leftDelim, rightDelim string, funcs ...map[string]interface{}) (map[string]*Tree, error)
type ActionNode
    func (a *ActionNode) Copy() Node
    func (a *ActionNode) String() string
type BoolNode
    func (b *BoolNode) Copy() Node
    func (b *BoolNode) String() string
type BranchNode
    func (b *BranchNode) Copy() Node
    func (b *BranchNode) String() string
type ChainNode
    func (c *ChainNode) Add(field string)
    func (c *ChainNode) Copy() Node
    func (c *ChainNode) String() string
type CommandNode
    func (c *CommandNode) Copy() Node
    func (c *CommandNode) String() string
type DotNode
    func (d *DotNode) Copy() Node
    func (d *DotNode) String() string
    func (d *DotNode) Type() NodeType
type FieldNode
    func (f *FieldNode) Copy() Node
    func (f *FieldNode) String() string
type IdentifierNode
    func NewIdentifier(ident string) *IdentifierNode
    func (i *IdentifierNode) Copy() Node
    func (i *IdentifierNode) SetPos(pos Pos) *IdentifierNode
    func (i *IdentifierNode) SetTree(t *Tree) *IdentifierNode
    func (i *IdentifierNode) String() string
type IfNode
    func (i *IfNode) Copy() Node
type ListNode
    func (l *ListNode) Copy() Node
    func (l *ListNode) CopyList() *ListNode
    func (l *ListNode) String() string
type NilNode
    func (n *NilNode) Copy() Node
    func (n *NilNode) String() string
    func (n *NilNode) Type() NodeType
type Node
type NodeType
    func (t NodeType) Type() NodeType
type NumberNode
    func (n *NumberNode) Copy() Node
    func (n *NumberNode) String() string
type PipeNode
    func (p *PipeNode) Copy() Node
    func (p *PipeNode) CopyPipe() *PipeNode
    func (p *PipeNode) String() string
type Pos
    func (p Pos) Position() Pos
type RangeNode
    func (r *RangeNode) Copy() Node
type StringNode
    func (s *StringNode) Copy() Node
    func (s *StringNode) String() string
type TemplateNode
    func (t *TemplateNode) Copy() Node
    func (t *TemplateNode) String() string
type TextNode
    func (t *TextNode) Copy() Node
    func (t *TextNode) String() string
type Tree
    func New(name string, funcs ...map[string]interface{}) *Tree
    func (t *Tree) Copy() *Tree
    func (t *Tree) ErrorContext(n Node) (location, context string)
    func (t *Tree) Parse(text, leftDelim, rightDelim string, treeSet map[string]*Tree, funcs ...map[string]interface{}) (tree *Tree, err error)
type VariableNode
    func (v *VariableNode) Copy() Node
    func (v *VariableNode) String() string
type WithNode
    func (w *WithNode) Copy() Node

Package files

lex.go node.go parse.go

func IsEmptyTree

func IsEmptyTree(n Node) bool

IsEmptyTree报告此树(节点)是否除空间外什么都没有.

func Parse

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

Parse返回从模板名称到parse.Tree的映射,该映射是通过解析参数字符串中描述的模板而创建的. 将为顶层模板指定名称. 如果遇到错误,解析将停止,并返回带有错误的空映射.

type ActionNode

ActionNode持有一个动作(由定界符限制). 控制动作有自己的节点; ActionNode代表简单的变量,例如字段评估和带括号的管道.

type ActionNode struct {
    NodeType
    Pos

    Line int       // The line number in the input. Deprecated: Kept for compatibility.
    Pipe *PipeNode // The pipeline in the action.
    // contains filtered or unexported fields
}

func (*ActionNode) Copy

func (a *ActionNode) Copy() Node

func (*ActionNode) String

func (a *ActionNode) String() string

type BoolNode

BoolNode拥有一个布尔常量.

type BoolNode struct {
    NodeType
    Pos

    True bool // The value of the boolean constant.
    // contains filtered or unexported fields
}

func (*BoolNode) Copy

func (b *BoolNode) Copy() Node

func (*BoolNode) String

func (b *BoolNode) String() string

type BranchNode

BranchNode是if,range和with的通用表示形式.

type BranchNode struct {
    NodeType
    Pos

    Line     int       // 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
}

func (*BranchNode) Copy 1.4

func (b *BranchNode) Copy() Node

func (*BranchNode) String

func (b *BranchNode) String() string

type ChainNode 1.1

ChainNode持有一个术语,后跟一连串的字段访问(以"."开头的标识符). 名称可以链接(" .x.y"). 期间从每个标识中删除.

type ChainNode struct {
    NodeType
    Pos

    Node  Node
    Field []string // The identifiers in lexical order.
    // contains filtered or unexported fields
}

func (*ChainNode) Add 1.1

func (c *ChainNode) Add(field string)

Add将命名字段(应以句点开头)添加到链的末尾.

func (*ChainNode) Copy 1.1

func (c *ChainNode) Copy() Node

func (*ChainNode) String 1.1

func (c *ChainNode) String() string

type CommandNode

CommandNode保留命令(评估动作中的管道).

type CommandNode struct {
    NodeType
    Pos

    Args []Node // Arguments in lexical order: Identifier, field, or constant.
    // contains filtered or unexported fields
}

func (*CommandNode) Copy

func (c *CommandNode) Copy() Node

func (*CommandNode) String

func (c *CommandNode) String() string

type DotNode 1.1

DotNode保留特殊标识符".".

type DotNode struct {
    NodeType
    Pos
    // contains filtered or unexported fields
}

func (*DotNode) Copy

func (d *DotNode) Copy() Node

func (*DotNode) String

func (d *DotNode) String() string

func (*DotNode) Type

func (d *DotNode) Type() NodeType

type FieldNode

FieldNode保存一个字段(以"."开头的标识符). 名称可以链接(" .x.y"). 该期间从每个标识中删除.

type FieldNode struct {
    NodeType
    Pos

    Ident []string // The identifiers in lexical order.
    // contains filtered or unexported fields
}

func (*FieldNode) Copy

func (f *FieldNode) Copy() Node

func (*FieldNode) String

func (f *FieldNode) String() string

type IdentifierNode

IdentifierNode包含一个标识符.

type IdentifierNode struct {
    NodeType
    Pos

    Ident string // The identifier's name.
    // contains filtered or unexported fields
}

func NewIdentifier

func NewIdentifier(ident string) *IdentifierNode

NewIdentifier返回具有给定标识符名称的新IdentifierNode.

func (*IdentifierNode) Copy

func (i *IdentifierNode) Copy() Node

func (*IdentifierNode) SetPos 1.1

func (i *IdentifierNode) SetPos(pos Pos) *IdentifierNode

SetPos设置位置. NewIdentifier是公共方法,因此我们无法修改其签名. 链接起来很方便. TODO:定一天吗?

func (*IdentifierNode) SetTree 1.4

func (i *IdentifierNode) SetTree(t *Tree) *IdentifierNode

SetTree设置节点的父树. NewIdentifier是公共方法,因此我们无法修改其签名. 链接起来很方便. TODO:定一天吗?

func (*IdentifierNode) String

func (i *IdentifierNode) String() string

type IfNode

IfNode表示一个{{if}}动作及其命令.

type IfNode struct {
    BranchNode
}

func (*IfNode) Copy

func (i *IfNode) Copy() Node

type ListNode

ListNode包含一系列节点.

type ListNode struct {
    NodeType
    Pos

    Nodes []Node // The element nodes in lexical order.
    // contains filtered or unexported fields
}

func (*ListNode) Copy

func (l *ListNode) Copy() Node

func (*ListNode) CopyList

func (l *ListNode) CopyList() *ListNode

func (*ListNode) String

func (l *ListNode) String() string

type NilNode 1.1

NilNode保留特殊标识符" nil",该标识符表示未类型化的nil常数.

type NilNode struct {
    NodeType
    Pos
    // contains filtered or unexported fields
}

func (*NilNode) Copy 1.1

func (n *NilNode) Copy() Node

func (*NilNode) String 1.1

func (n *NilNode) String() string

func (*NilNode) Type 1.1

func (n *NilNode) Type() NodeType

type Node 1.1

节点是解析树中的元素. 该接口是微不足道的. 该接口包含一个未导出的方法,因此只有此程序包本地的类型才能满足它.

type Node interface {
    Type() NodeType
    String() 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() Node
    Position() Pos // byte position of start of node in full original input string
    // contains filtered or unexported methods
}

type NodeType

NodeType标识解析树节点的类型.

type NodeType int
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.
    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.
)

func (NodeType) Type

func (t NodeType) Type() NodeType

类型返回自身,并提供一个简单的默认实现以嵌入到Node中. 嵌入所有非平凡节点中.

type NumberNode

NumberNode拥有一个数字:有符号或无符号整数,浮点数或复数. 解析值并将其存储在可以代表该值的所有类型下. 这用少量的代码模拟了Go的理想常数的行为.

type NumberNode struct {
    NodeType
    Pos

    IsInt      bool       // Number has an integral value.
    IsUint     bool       // Number has an unsigned integral value.
    IsFloat    bool       // Number has a floating-point value.
    IsComplex  bool       // Number is complex.
    Int64      int64      // The signed integer value.
    Uint64     uint64     // The unsigned integer value.
    Float64    float64    // The floating-point value.
    Complex128 complex128 // The complex value.
    Text       string     // The original textual representation from the input.
    // contains filtered or unexported fields
}

func (*NumberNode) Copy

func (n *NumberNode) Copy() Node

func (*NumberNode) String

func (n *NumberNode) String() string

type PipeNode

PipeNode使用可选声明保存管道

type PipeNode struct {
    NodeType
    Pos

    Line     int             // The line number in the input. Deprecated: Kept for compatibility.
    IsAssign bool            // The variables are being assigned, not declared; added in Go 1.11
    Decl     []*VariableNode // Variables in lexical order.
    Cmds     []*CommandNode  // The commands in lexical order.
    // contains filtered or unexported fields
}

func (*PipeNode) Copy

func (p *PipeNode) Copy() Node

func (*PipeNode) CopyPipe

func (p *PipeNode) CopyPipe() *PipeNode

func (*PipeNode) String

func (p *PipeNode) String() string

type Pos 1.1

Pos表示原始输入文本中从中解析此模板的字节位置.

type Pos int

func (Pos) Position 1.1

func (p Pos) Position() Pos

type RangeNode

RangeNode表示{{range}}动作及其命令.

type RangeNode struct {
    BranchNode
}

func (*RangeNode) Copy

func (r *RangeNode) Copy() Node

type StringNode

StringNode保存一个字符串常量. 该值已被"取消引用".

type StringNode struct {
    NodeType
    Pos

    Quoted string // The original text of the string, with quotes.
    Text   string // The string, after quote processing.
    // contains filtered or unexported fields
}

func (*StringNode) Copy

func (s *StringNode) Copy() Node

func (*StringNode) String

func (s *StringNode) String() string

type TemplateNode

TemplateNode代表一个{{template}}动作.

type TemplateNode struct {
    NodeType
    Pos

    Line int       // The line number in the input. Deprecated: Kept for compatibility.
    Name string    // The name of the template (unquoted).
    Pipe *PipeNode // The command to evaluate as dot for the template.
    // contains filtered or unexported fields
}

func (*TemplateNode) Copy

func (t *TemplateNode) Copy() Node

func (*TemplateNode) String

func (t *TemplateNode) String() string

type TextNode

TextNode保存纯文本.

type TextNode struct {
    NodeType
    Pos

    Text []byte // The text; may span newlines.
    // contains filtered or unexported fields
}

func (*TextNode) Copy

func (t *TextNode) Copy() Node

func (*TextNode) String

func (t *TextNode) String() string

type Tree

树是单个已解析模板的表示.

type Tree struct {
    Name      string    // name of the template represented by the tree.
    ParseName string    // name of the top-level template during parsing, for error messages; added in Go 1.1
    Root      *ListNode // top-level root of the tree.
    // contains filtered or unexported fields
}

func New

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

New使用给定名称分配新的解析树.

func (*Tree) Copy 1.2

func (t *Tree) Copy() *Tree

复制返回树的副本. 任何解析状态都将被丢弃.

func (*Tree) ErrorContext 1.1

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

ErrorContext返回输入文本中节点位置的文本表示形式. 仅当节点内部没有指向树的指针时才使用接收器,这可能会在旧代码中出现.

func (*Tree) Parse

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

Parse解析模板定义字符串,以构造要执行的模板的表示形式. 如果任何一个动作分隔符字符串为空,则使用默认值(" {{"或"}}"). 嵌入式模板定义将添加到treeSet映射中.

type VariableNode

AssignNode包含变量名列表,可能具有链接的字段访问. 美元符号是(名字)名称的一部分.

type VariableNode struct {
    NodeType
    Pos

    Ident []string // Variable name and fields in lexical order.
    // contains filtered or unexported fields
}

func (*VariableNode) Copy

func (v *VariableNode) Copy() Node

func (*VariableNode) String

func (v *VariableNode) String() string

type WithNode

WithNode代表{{with}}动作及其命令.

type WithNode struct {
    BranchNode
}

func (*WithNode) Copy

func (w *WithNode) Copy() Node

by  ICOPY.SITE