parse

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Jun 17, 2026 License: BSD-3-Clause Imports: 7 Imported by: 0

Documentation

Overview

Package parse builds parse trees for Go templates using the text/template grammar. It is a fork of the standard library's text/template/parse, trimmed and adapted for gotmplfumpt's formatting needs.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ActionNode

type ActionNode struct {
	NodeType
	Pipe *PipeNode
	Pos
	Trim trim
	Line int
	// contains filtered or unexported fields
}

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

func (a *ActionNode) String() string

String renders the action as source.

type BoolNode

type BoolNode struct {
	NodeType
	Pos
	Line int
	True bool
	// contains filtered or unexported fields
}

BoolNode holds a boolean constant.

func (*BoolNode) String

func (b *BoolNode) String() string

String renders "true" or "false".

type BranchNode

type BranchNode struct {
	NodeType
	Pipe    *PipeNode
	List    *ListNode
	End     *EndNode
	Keyword string
	Elses   []*ElseNode
	Pos
	Trim trim
	Line int
	// contains filtered or unexported fields
}

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

func (*BranchNode) String

func (b *BranchNode) String() string

String renders the branch as source.

type ChainNode

type ChainNode struct {
	Node Node
	NodeType
	Field []string
	Pos
	Line int
	// 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.

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

func (c *ChainNode) String() string

String renders the chain term followed by its field chain.

type CommandNode

type CommandNode struct {
	NodeType
	Args []Node
	Pos
	Trim trim
	Line int
	// contains filtered or unexported fields
}

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

func (*CommandNode) String

func (c *CommandNode) String() string

String renders the command as source.

type CommentNode

type CommentNode struct {
	NodeType
	Text string
	Pos
	Trim trim
	Line int
	// contains filtered or unexported fields
}

CommentNode holds a comment.

func (*CommentNode) String

func (c *CommentNode) String() string

String renders the comment as source.

type DotNode

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

DotNode holds the special identifier '.'.

func (*DotNode) String

func (d *DotNode) String() string

String renders the dot.

func (*DotNode) Type

func (d *DotNode) Type() NodeType

Type overrides the embedded NodeType for API compatibility.

type ElseNode

type ElseNode struct {
	NodeType
	Pipe    *PipeNode
	List    *ListNode
	Keyword string
	Pos
	Trim trim
	Line int
	// contains filtered or unexported fields
}

ElseNode represents an {{else}}, {{else if}}, or {{else with}} action. Does not appear in the final tree.

func (*ElseNode) String

func (e *ElseNode) String() string

String renders the {{else}} action as source.

func (*ElseNode) Type

func (e *ElseNode) Type() NodeType

Type overrides the embedded NodeType to expose the (unexported) nodeElse value for API compatibility.

type EndNode

type EndNode struct {
	NodeType
	Pos
	Trim trim
	Line int
	// contains filtered or unexported fields
}

EndNode represents an {{end}} action.

func (*EndNode) String

func (e *EndNode) String() string

String renders the {{end}} action.

type FieldNode

type FieldNode struct {
	NodeType
	Ident []string
	Pos
	Line int
	// 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.

func (*FieldNode) String

func (f *FieldNode) String() string

String renders the field chain with a leading dot per element.

type IdentifierNode

type IdentifierNode struct {
	NodeType
	Ident string
	Pos
	Line int
	// contains filtered or unexported fields
}

IdentifierNode holds an identifier.

func NewIdentifier

func NewIdentifier(ident string) *IdentifierNode

NewIdentifier returns a new IdentifierNode with the given identifier name.

func (*IdentifierNode) SetLine

func (i *IdentifierNode) SetLine(line int) *IdentifierNode

SetLine sets the line number. NewIdentifier is a public method so we can't modify its signature. Chained for convenience.

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.

func (*IdentifierNode) SetTree

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.

func (*IdentifierNode) String

func (i *IdentifierNode) String() string

String returns the identifier name.

type ListNode

type ListNode struct {
	NodeType
	Nodes []Node
	Pos
	// contains filtered or unexported fields
}

ListNode holds a sequence of nodes.

func Parse

func Parse(text string) (*ListNode, error)

Parse parses the given template source and returns the root list node. Returns the error from (*Tree).Parse if parsing fails.

func (*ListNode) HasIgnoreAll

func (l *ListNode) HasIgnoreAll() bool

HasIgnoreAll reports whether the first comment in the list is a gotmplfumpt-ignore-all directive.

func (*ListNode) String

func (l *ListNode) String() string

String renders the list as source.

type Mode

type Mode uint

Mode is a set of flags (or 0) controlling parser behavior.

const (
	// ParseComments adds comment nodes to the AST.
	ParseComments Mode = 1 << iota
	// SkipFuncCheck disables function-presence checks.
	SkipFuncCheck
)

Mode flags. ParseComments adds {{/* … */}} comments to the AST; SkipFuncCheck disables function-presence checks (unused here but preserved for API compatibility with text/template/parse).

type NilNode

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

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

func (*NilNode) String

func (n *NilNode) String() string

String renders the literal "nil".

func (*NilNode) Type

func (n *NilNode) Type() NodeType

Type overrides the embedded NodeType for API compatibility.

type Node

type Node interface {
	Type() NodeType
	String() string
	Position() Pos // byte position of start of node in full original input
	// contains filtered or unexported methods
}

Node is an element in the parse tree. 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 // 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.
	NodeBranch     // A branch-y action.
	NodeList       // A list of Nodes.
	NodeNil        // An untyped nil constant.
	NodeNumber     // A numerical constant.
	NodePipe       // A pipeline of commands.
	NodeString     // A string constant.
	NodeTemplate   // A template invocation action.
	NodeVariable   // A $ variable.
	NodeComment    // A comment.
)

NodeType values identify the kind of a parse tree node.

func (NodeType) Type

func (t NodeType) Type() NodeType

Type returns the NodeType. Provides a default implementation suitable for embedding in concrete Node types.

type NumberNode

type NumberNode struct {
	NodeType
	Text       string
	Complex128 complex128
	Int64      int64
	Uint64     uint64
	Float64    float64
	Pos
	Line      int
	IsInt     bool
	IsUint    bool
	IsFloat   bool
	IsComplex bool
	// 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) String

func (n *NumberNode) String() string

String returns the original textual representation of the number.

type PipeNode

type PipeNode struct {
	NodeType
	Decl []*VariableNode
	Cmds []*CommandNode
	Pos
	Line     int
	IsAssign bool
	// contains filtered or unexported fields
}

PipeNode holds a pipeline with optional declaration.

func (*PipeNode) String

func (p *PipeNode) String() string

String renders the pipeline as source.

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

Position returns the byte position.

type StringNode

type StringNode struct {
	NodeType
	Quoted string
	Text   string
	Pos
	Line int
	// contains filtered or unexported fields
}

StringNode holds a string constant. The value has been "unquoted".

func (*StringNode) String

func (s *StringNode) String() string

String returns the original quoted representation of the string.

type TextNode

type TextNode struct {
	NodeType
	Text string
	Pos
	Line int
	// contains filtered or unexported fields
}

TextNode holds plain text.

func (*TextNode) String

func (t *TextNode) String() string

String returns the verbatim text.

type Tree

type Tree struct {
	Root *ListNode
	// contains filtered or unexported fields
}

Tree is the representation of a single parsed template.

func (*Tree) ErrorContext

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

func (t *Tree) Parse(text string) (err error)

Parse parses the template definition string to construct a representation of the template for formatting.

type VariableNode

type VariableNode struct {
	NodeType
	Ident []string
	Pos
	Line int
	// contains filtered or unexported fields
}

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

func (*VariableNode) String

func (v *VariableNode) String() string

String renders the variable and its field chain.

Jump to

Keyboard shortcuts

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