package parse

import "text/template/parse"

Package parse 为 text/template 和 html/template 定义的模板构建解析树。 客户端应该使用这些包来构造模板,而不是这个包, 这个包提供了共享的内部数据结构,不打算供一般使用。

Index

Functions

func IsEmptyTree

func IsEmptyTree(n Node) bool

IsEmptyTree 报告此树(节点)是否除了空格或注释之外为空。

func Parse

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

Parse 返回从模板名称到 Tree 的映射,通过解析参数字符串中描述的模板创建。 顶级模板将被给予指定的名称。如果遇到错误,解析停止并返回空 map 和错误。

Types

type ActionNode

type ActionNode struct {
	NodeType
	Pos

	Line int       // 输入中的行号。已弃用:为兼容性保留。
	Pipe *PipeNode // 动作中的管道。
	// contains filtered or unexported fields
}

ActionNode 持有动作(由分隔符界定的内容)。 控制动作有自己的节点;ActionNode 表示简单的动作,如字段求值和带括号的管道。

func (*ActionNode) Copy

func (a *ActionNode) Copy() Node

func (*ActionNode) String

func (a *ActionNode) String() string

type BoolNode

type BoolNode struct {
	NodeType
	Pos

	True bool // 布尔常量的值。
	// contains filtered or unexported fields
}

BoolNode 持有布尔常量。

func (*BoolNode) Copy

func (b *BoolNode) Copy() Node

func (*BoolNode) String

func (b *BoolNode) String() string

type BranchNode

type BranchNode struct {
	NodeType
	Pos

	Line     int       // 输入中的行号。已弃用:为兼容性保留。
	Pipe     *PipeNode // 要求值的管道。
	List     *ListNode // 如果值非空则执行的内容。
	ElseList *ListNode // 如果值为空则执行的内容(如果不存在则为 nil)。
	// contains filtered or unexported fields
}

BranchNode 是 if、range 和 with 的公共表示。

func (*BranchNode) Copy

func (b *BranchNode) Copy() Node

func (*BranchNode) String

func (b *BranchNode) String() string

type BreakNode

type BreakNode struct {
	NodeType
	Pos
	Line int
	// contains filtered or unexported fields
}

BreakNode 表示 {{break}} 动作。

func (*BreakNode) Copy

func (b *BreakNode) Copy() Node

func (*BreakNode) String

func (b *BreakNode) String() string

type ChainNode

type ChainNode struct {
	NodeType
	Pos

	Node  Node
	Field []string // 按词法顺序的标识符。
	// contains filtered or unexported fields
}

ChainNode 持有后面跟着字段链的项(以 '.' 开头的标识符)。 名称可以链式调用('.x.y')。 每个标识符中的句点被丢弃。

func (*ChainNode) Add

func (c *ChainNode) Add(field string)

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

func (*ChainNode) Copy

func (c *ChainNode) Copy() Node

func (*ChainNode) String

func (c *ChainNode) String() string

type CommandNode

type CommandNode struct {
	NodeType
	Pos

	Args []Node // 按词法顺序的参数:Identifier、field 或 constant。
	// contains filtered or unexported fields
}

CommandNode 持有命令(求值动作中的管道)。

func (*CommandNode) Copy

func (c *CommandNode) Copy() Node

func (*CommandNode) String

func (c *CommandNode) String() string

type CommentNode

type CommentNode struct {
	NodeType
	Pos

	Text string // 注释文本。
	// contains filtered or unexported fields
}

CommentNode 持有注释。

func (*CommentNode) Copy

func (c *CommentNode) Copy() Node

func (*CommentNode) String

func (c *CommentNode) String() string

type ContinueNode

type ContinueNode struct {
	NodeType
	Pos
	Line int
	// contains filtered or unexported fields
}

ContinueNode 表示 {{continue}} 动作。

func (*ContinueNode) Copy

func (c *ContinueNode) Copy() Node

func (*ContinueNode) String

func (c *ContinueNode) String() string

type DotNode

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

DotNode 持有特殊标识符 '.'。

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

type FieldNode struct {
	NodeType
	Pos

	Ident []string // 按词法顺序的标识符。
	// contains filtered or unexported fields
}

FieldNode 持有字段(以 '.' 开头的标识符)。 名称可以链式调用('.x.y')。 每个标识符中的句点被丢弃。

func (*FieldNode) Copy

func (f *FieldNode) Copy() Node

func (*FieldNode) String

func (f *FieldNode) String() string

type IdentifierNode

type IdentifierNode struct {
	NodeType
	Pos

	Ident string // 标识符的名称。
	// contains filtered or unexported fields
}

IdentifierNode 持有标识符。

func NewIdentifier

func NewIdentifier(ident string) *IdentifierNode

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

func (*IdentifierNode) Copy

func (i *IdentifierNode) Copy() Node

func (*IdentifierNode) SetPos

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

SetPos 设置位置。NewIdentifier 是一个公共方法,所以我们不能修改其签名。 为方便起见进行链式调用。 TODO: 有一天修复?

func (*IdentifierNode) SetTree

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

SetTree 为节点设置父树。NewIdentifier 是一个公共方法,所以我们不能修改其签名。 为方便起见进行链式调用。 TODO: 有一天修复?

func (*IdentifierNode) String

func (i *IdentifierNode) String() string

type IfNode

type IfNode struct {
	BranchNode
}

IfNode 表示 {{if}} 动作及其命令。

func (*IfNode) Copy

func (i *IfNode) Copy() Node

type ListNode

type ListNode struct {
	NodeType
	Pos

	Nodes []Node // 按词法顺序的元素节点。
	// contains filtered or unexported fields
}

ListNode 持有节点序列。

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 Mode

type Mode uint

Mode 值是一组标志(或 0)。模式控制解析器行为。

const (
	ParseComments Mode = 1 << iota // 解析注释并将其添加到 AST
	SkipFuncCheck                  // 不检查函数是否已定义
)

type NilNode

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

NilNode 持有特殊标识符 'nil',表示无类型的 nil 常量。

func (*NilNode) Copy

func (n *NilNode) Copy() Node

func (*NilNode) String

func (n *NilNode) String() string

func (*NilNode) Type

func (n *NilNode) Type() NodeType

type Node

type Node interface {
	Type() NodeType
	String() string
	// Copy 对 Node 及其所有组件进行深层复制。
	// 为了避免类型断言,一些 XxxNode 还有专门的 CopyXxx 方法返回 *XxxNode。
	Copy() Node
	Position() Pos // 节点在完整原始输入字符串中的字节位置
	// contains filtered or unexported methods
}

Node 是解析树中的一个元素。接口很简单。 接口包含一个未导出的方法,因此只有此包本地的类型可以满足它。

type NodeType

type NodeType int

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

const (
	NodeText    NodeType = iota // 纯文本。
	NodeAction                  // 非控制动作,如字段求值。
	NodeBool                    // 布尔常量。
	NodeChain                   // 字段访问序列。
	NodeCommand                 // 管道的一个元素。
	NodeDot                     // 游标,点。

	NodeField      // 字段或方法名。
	NodeIdentifier // 标识符;始终是函数名。
	NodeIf         // if 动作。
	NodeList       // Node 列表。
	NodeNil        // 无类型的 nil 常量。
	NodeNumber     // 数字常量。
	NodePipe       // 命令管道。
	NodeRange      // range 动作。
	NodeString     // 字符串常量。
	NodeTemplate   // 模板调用动作。
	NodeVariable   // $ 变量。
	NodeWith       // with 动作。
	NodeComment    // 注释。
	NodeBreak      // break 动作。
	NodeContinue   // continue 动作。
)

func (NodeType) Type

func (t NodeType) Type() NodeType

Type 返回自身,并为嵌入到 Node 中提供简单的默认实现。 嵌入在所有非平凡 Node 中。

type NumberNode

type NumberNode struct {
	NodeType
	Pos

	IsInt      bool       // 数字有整数值。
	IsUint     bool       // 数字有无符号整数值。
	IsFloat    bool       // 数字有浮点值。
	IsComplex  bool       // 数字是复数。
	Int64      int64      // 有符号整数值。
	Uint64     uint64     // 无符号整数值。
	Float64    float64    // 浮点值。
	Complex128 complex128 // 复数值。
	Text       string     // 输入中的原始文本表示。
	// contains filtered or unexported fields
}

NumberNode 持有数字:有符号或无符号整数、浮点数或复数。 值被解析并存储在所有可以表示该值的类型下。 这用少量代码模拟了 Go 理想常量的行为。

func (*NumberNode) Copy

func (n *NumberNode) Copy() Node

func (*NumberNode) String

func (n *NumberNode) String() string

type PipeNode

type PipeNode struct {
	NodeType
	Pos

	Line     int             // 输入中的行号。已弃用:为兼容性保留。
	IsAssign bool            // 变量被赋值而非声明。
	Decl     []*VariableNode // 按词法顺序的变量。
	Cmds     []*CommandNode  // 按词法顺序的命令。
	// contains filtered or unexported fields
}

PipeNode 持有带有可选声明的管道。

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

type Pos int

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

func (Pos) Position

func (p Pos) Position() Pos

type RangeNode

type RangeNode struct {
	BranchNode
}

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

func (*RangeNode) Copy

func (r *RangeNode) Copy() Node

type StringNode

type StringNode struct {
	NodeType
	Pos

	Quoted string // 字符串的原始文本,带引号。
	Text   string // 字符串,在引号处理之后。
	// contains filtered or unexported fields
}

StringNode 持有字符串常量。值已被"去引号"。

func (*StringNode) Copy

func (s *StringNode) Copy() Node

func (*StringNode) String

func (s *StringNode) String() string

type TemplateNode

type TemplateNode struct {
	NodeType
	Pos

	Line int       // 输入中的行号。已弃用:为兼容性保留。
	Name string    // 模板的名称(不带引号)。
	Pipe *PipeNode // 作为模板的 dot 求值的命令。
	// contains filtered or unexported fields
}

TemplateNode 表示 {{template}} 动作。

func (*TemplateNode) Copy

func (t *TemplateNode) Copy() Node

func (*TemplateNode) String

func (t *TemplateNode) String() string

type TextNode

type TextNode struct {
	NodeType
	Pos

	Text []byte // 文本;可以跨越换行符。
	// contains filtered or unexported fields
}

TextNode 持有纯文本。

func (*TextNode) Copy

func (t *TextNode) Copy() Node

func (*TextNode) String

func (t *TextNode) String() string

type Tree

type Tree struct {
	Name      string    // 树表示的模板的名称。
	ParseName string    // 解析期间顶级模板的名称,用于错误消息。
	Root      *ListNode // 树的顶层根节点。
	Mode      Mode      // 解析模式。
	// contains filtered or unexported fields
}

Tree 是单个解析模板的表示。

func New

func New(name string, funcs ...map[string]any) *Tree

New 分配具有给定名称的新解析树。

func (*Tree) Copy

func (t *Tree) Copy() *Tree

Copy 返回 Tree 的副本。丢弃任何解析状态。

func (*Tree) ErrorContext

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]any) (tree *Tree, err error)

Parse 解析模板定义字符串以构造用于执行的模板表示。 如果任一动作分隔符字符串为空,则使用默认值("{{" 或 "}}")。 嵌入式模板定义被添加到 treeSet map。

type VariableNode

type VariableNode struct {
	NodeType
	Pos

	Ident []string // 变量名和字段,按词法顺序。
	// contains filtered or unexported fields
}

VariableNode 持有变量名列表,可能带有链式字段访问。 美元符号是(第一个)名称的一部分。

func (*VariableNode) Copy

func (v *VariableNode) Copy() Node

func (*VariableNode) String

func (v *VariableNode) String() string

type WithNode

type WithNode struct {
	BranchNode
}

WithNode 表示 {{with}} 动作及其命令。

func (*WithNode) Copy

func (w *WithNode) Copy() Node