parse

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 20, 2016 License: MIT Imports: 7 Imported by: 2

Documentation

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

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type 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.
}

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

func (a *ActionNode) Copy() Node

func (*ActionNode) String

func (a *ActionNode) String() string

type BoolNode

type BoolNode struct {
	NodeType
	Pos
	True bool // The value of the boolean constant.
}

BoolNode holds a boolean constant.

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       // 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).
}

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

func (*BranchNode) String

func (b *BranchNode) String() string

type ChainNode

type ChainNode struct {
	NodeType
	Pos
	Node  Node
	Field []string // The identifiers in lexical order.
}

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

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

func (c *ChainNode) Copy() Node

func (*ChainNode) String

func (c *ChainNode) String() string

type CommandNode

type CommandNode struct {
	NodeType
	Pos
	Args []Node // Arguments in lexical order: Identifier, field, or constant.
}

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

type DefineNode

type DefineNode struct {
	NodeType
	Pos
	Line   int       // The line number in the input.
	Name   string    // The name of the template (unquoted).
	Parent string    // The name of the parent template (unquoted).
	List   *ListNode // Contents of the template.
	// contains filtered or unexported fields
}

DefineNode represents a {{define}} action.

func (*DefineNode) Copy

func (d *DefineNode) Copy() Node

func (*DefineNode) CopyDefine

func (d *DefineNode) CopyDefine() *DefineNode

func (*DefineNode) ErrorContext

func (d *DefineNode) ErrorContext(n Node) (location, context string)

ErrorContext returns a textual representation of the location of the node in the input text.

func (*DefineNode) String

func (d *DefineNode) String() string

type DotNode

type DotNode struct {
	Pos
}

DotNode holds the special identifier '.'.

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 // The identifiers in lexical order.
}

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

func (*FieldNode) Copy

func (f *FieldNode) Copy() Node

func (*FieldNode) String

func (f *FieldNode) String() string

type FillNode

type FillNode struct {
	NodeType
	Pos
	Line int       // The line number in the input.
	Name string    // The name of the fill (unquoted).
	List *ListNode // Contents of the fill.
}

FillNode represents a {{fill}} action.

func (*FillNode) Copy

func (f *FillNode) Copy() Node

func (*FillNode) CopyFill

func (f *FillNode) CopyFill() *FillNode

func (*FillNode) String

func (f *FillNode) String() string

type IdentifierNode

type IdentifierNode struct {
	NodeType
	Pos
	Ident string // The identifier's name.
}

IdentifierNode holds an identifier.

func NewIdentifier

func NewIdentifier(ident string) *IdentifierNode

NewIdentifier returns a new IdentifierNode with the given identifier name.

func (*IdentifierNode) Copy

func (i *IdentifierNode) Copy() Node

func (*IdentifierNode) SetPos

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

func (i *IdentifierNode) String() string

type IfNode

type IfNode struct {
	BranchNode
}

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

func (*IfNode) Copy

func (i *IfNode) Copy() Node

type ListNode

type ListNode struct {
	NodeType
	Pos
	Nodes []Node // The element nodes in lexical order.
}

ListNode holds a sequence of nodes.

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

type NilNode struct {
	Pos
}

NilNode holds the special identifier 'nil' representing an untyped nil constant.

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 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
}

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

type NodeType int

NodeType identifies the type of a parse tree node.

const (
	NodeText    NodeType = iota // 0  Plain text.
	NodeAction                  // 1  A non-control action such as a field evaluation.
	NodeBool                    // 2  A boolean constant.
	NodeChain                   // 3  A sequence of field accesses.
	NodeCommand                 // 4  An element of a pipeline.
	NodeDefine                  // 5  A template definition.
	NodeDot                     // 6  The cursor, dot.

	NodeField      // 9  A field or method name.
	NodeFill       // 10 A fill action.
	NodeIdentifier // 11 An identifier; always a function name.
	NodeIf         // 12 An if action.
	NodeList       // 13 A list of Nodes.
	NodeNil        // 14 An untyped nil constant.
	NodeNumber     // 15 A numerical constant.
	NodePipe       // 16 A pipeline of commands.
	NodeRange      // 17 A range action.
	NodeSlot       // 18 A slot action.
	NodeString     // 19 A string constant.
	NodeTemplate   // 20 A template invocation action.
	NodeTree       // 21 A tree of define nodes.
	NodeVariable   // 22 A $ variable.
	NodeWith       // 23 A with action.
)

func (NodeType) Type

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

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

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

type PipeNode

type PipeNode struct {
	NodeType
	Pos
	Line int             // The line number in the input (deprecated; kept for compatibility)
	Decl []*VariableNode // Variable declarations in lexical order.
	Cmds []*CommandNode  // The commands in lexical order.
}

PipeNode holds a pipeline with optional declaration

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 represents a byte position in the original input text from which this template was parsed.

func (Pos) Position

func (p Pos) Position() Pos

type RangeNode

type RangeNode struct {
	BranchNode
}

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

func (*RangeNode) Copy

func (r *RangeNode) Copy() Node

type SlotNode

type SlotNode struct {
	NodeType
	Pos
	Line int       // The line number in the input.
	Name string    // The name of the slot (unquoted).
	List *ListNode // Contents of the fill.
}

SlotNode represents a {{slot}} action.

func (*SlotNode) Copy

func (s *SlotNode) Copy() Node

func (*SlotNode) String

func (s *SlotNode) String() string

type StringNode

type StringNode struct {
	NodeType
	Pos
	Quoted string // The original text of the string, with quotes.
	Text   string // The string, after quote processing.
}

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

type TemplateNode

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

TemplateNode represents a {{template}} action.

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 // The text; may span newlines.
}

TextNode holds plain text.

func (*TextNode) Copy

func (t *TextNode) Copy() Node

func (*TextNode) String

func (t *TextNode) String() string

type Tree

type Tree map[string]*DefineNode

Tree stores a collection of DefineNode's.

func Parse

func Parse(name, text, leftDelim, rightDelim string, funcs ...map[string]interface{}) (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.

func (Tree) Add

func (t Tree) Add(node *DefineNode) error

Add adds a node to the tree.

func (Tree) AddTree

func (t Tree) AddTree(t2 Tree) error

AddTree adds all nodes from the given tree to this tree.

func (Tree) Copy

func (t Tree) Copy() Tree

Copy returns a deep copy of the tree.

func (Tree) String

func (t Tree) String() string

String returns a parseable representation of all templates in the tree.

type VariableNode

type VariableNode struct {
	NodeType
	Pos
	Ident []string // Variable name and fields in lexical order.
}

VariableNode holds a list of variable names, possibly with chained field accesses. 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

type WithNode

type WithNode struct {
	BranchNode
}

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

func (*WithNode) Copy

func (w *WithNode) Copy() Node

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL