parse

package
v1.0.6 Latest Latest
Warning

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

Go to latest
Published: Apr 1, 2023 License: MIT Imports: 9 Imported by: 4

Documentation

Overview

Package parse handles transforming Stick source code into AST for further processing.

Index

Constants

View Source
const (
	OpUnaryNot      = "not"
	OpUnaryPositive = "+"
	OpUnaryNegative = "-"

	OpBinaryOr           = "or"
	OpBinaryAnd          = "and"
	OpBinaryBitwiseOr    = "b-or"
	OpBinaryBitwiseXor   = "b-xor"
	OpBinaryBitwiseAnd   = "b-and"
	OpBinaryEqual        = "=="
	OpBinaryNotEqual     = "!="
	OpBinaryLessThan     = "<"
	OpBinaryLessEqual    = "<="
	OpBinaryGreaterThan  = ">"
	OpBinaryGreaterEqual = ">="
	OpBinaryNotIn        = "not in"
	OpBinaryIn           = "in"
	OpBinaryMatches      = "matches"
	OpBinaryStartsWith   = "starts with"
	OpBinaryEndsWith     = "ends with"
	OpBinaryRange        = ".."
	OpBinaryAdd          = "+"
	OpBinarySubtract     = "-"
	OpBinaryConcat       = "~"
	OpBinaryMultiply     = "*"
	OpBinaryDivide       = "/"
	OpBinaryFloorDiv     = "//"
	OpBinaryModulo       = "%"
	OpBinaryIs           = "is"
	OpBinaryIsNot        = "is not"
	OpBinaryPower        = "**"
)

Built-in operators.

Variables

This section is empty.

Functions

This section is empty.

Types

type ArrayExpr

type ArrayExpr struct {
	Pos
	Elements []Expr
}

func NewArrayExpr

func NewArrayExpr(pos Pos, els ...Expr) *ArrayExpr

NewArrayExpr returns a ArrayExpr.

func (*ArrayExpr) All

func (exp *ArrayExpr) All() []Node

All returns all the child Nodes in a ArrayExpr.

func (*ArrayExpr) String

func (exp *ArrayExpr) String() string

String returns a string representation of a ArrayExpr.

type BinaryExpr

type BinaryExpr struct {
	Pos
	Left  Expr   // Left side expression.
	Op    string // Binary operation in string form.
	Right Expr   // Right side expression.
}

BinaryExpr represents a binary operation, such as "x + y"

func NewBinaryExpr

func NewBinaryExpr(left Expr, op string, right Expr, pos Pos) *BinaryExpr

NewBinaryExpr returns a BinaryExpr.

func (*BinaryExpr) All

func (exp *BinaryExpr) All() []Node

All returns all the child Nodes in a BinaryExpr.

func (*BinaryExpr) String

func (exp *BinaryExpr) String() string

String returns a string representation of the BinaryExpr.

type BlockNode

type BlockNode struct {
	Pos
	TrimmableNode
	Name   string // Name of the block.
	Body   Node   // Body of the block.
	Origin string // The name where this block is originally defined.
}

BlockNode represents a block statement

func NewBlockNode

func NewBlockNode(name string, body Node, p Pos) *BlockNode

NewBlockNode returns a BlockNode.

func (*BlockNode) All

func (t *BlockNode) All() []Node

All returns all the child Nodes in a BlockNode.

func (*BlockNode) String

func (t *BlockNode) String() string

String returns a string representation of a BlockNode.

type BodyNode

type BodyNode struct {
	Pos
	Nodes []Node
}

BodyNode represents a list of nodes.

func NewBodyNode

func NewBodyNode(pos Pos, nodes ...Node) *BodyNode

NewBodyNode returns a BodyNode.

func (*BodyNode) All

func (l *BodyNode) All() []Node

All returns all the child Nodes in a BodyNode.

func (*BodyNode) Append

func (l *BodyNode) Append(n Node)

Append a Node to the BodyNode.

func (*BodyNode) String

func (l *BodyNode) String() string

String returns a string representation of a BodyNode.

type BoolExpr

type BoolExpr struct {
	Pos
	Value bool // The raw boolean value.
}

BoolExpr represents a boolean literal.

func NewBoolExpr

func NewBoolExpr(value bool, pos Pos) *BoolExpr

NewBoolExpr returns a BoolExpr.

func (*BoolExpr) All

func (exp *BoolExpr) All() []Node

All returns all the child Nodes in a UseNode.

func (*BoolExpr) String

func (exp *BoolExpr) String() string

String returns a string representation of the BoolExpr.

type CommentNode

type CommentNode struct {
	*TextNode
	TrimmableNode
}

CommentNode represents a comment.

func NewCommentNode

func NewCommentNode(data string, p Pos) *CommentNode

NewCommentNode returns a CommentNode.

type DebugError

type DebugError interface {
	error
	Debug() string // Debug returns extra information about the error.
}

A DebugError is emitted when the package is built with the debug flag.

type DoNode

type DoNode struct {
	Pos
	TrimmableNode
	X Expr // The expression to evaluate.
}

DoNode simply executes the expression it contains.

func NewDoNode

func NewDoNode(expr Expr, pos Pos) *DoNode

NewDoNode returns a DoNode.

func (*DoNode) All

func (t *DoNode) All() []Node

All returns all the child Nodes in a DoNode.

func (*DoNode) String

func (t *DoNode) String() string

String returns a string representation of an DoNode.

type EmbedNode

type EmbedNode struct {
	*IncludeNode
	Blocks map[string]*BlockNode // Blocks inside the embed body.
}

EmbedNode is a special include statement.

func NewEmbedNode

func NewEmbedNode(tmpl Expr, with Expr, only bool, blocks map[string]*BlockNode, pos Pos) *EmbedNode

NewEmbedNode returns a EmbedNode.

func (*EmbedNode) All

func (t *EmbedNode) All() []Node

All returns all the child Nodes in a EmbedNode.

func (*EmbedNode) String

func (t *EmbedNode) String() string

String returns a string representation of an EmbedNode.

type Expr

type Expr interface {
	Node
}

Expr represents a special type of Node that represents an expression.

type ExtendsNode

type ExtendsNode struct {
	Pos
	TrimmableNode
	Tpl Expr // Name of the template being extended.
}

ExtendsNode represents an extends statement

func NewExtendsNode

func NewExtendsNode(tplRef Expr, p Pos) *ExtendsNode

NewExtendsNode returns a ExtendsNode.

func (*ExtendsNode) All

func (t *ExtendsNode) All() []Node

All returns all the child Nodes in a ExtendsNode.

func (*ExtendsNode) String

func (t *ExtendsNode) String() string

String returns a string representation of an ExtendsNode.

type FilterExpr

type FilterExpr struct {
	*FuncExpr
}

FilterExpr represents a filter application.

func NewFilterExpr

func NewFilterExpr(name string, args []Expr, pos Pos) *FilterExpr

NewFilterExpr returns a FilterExpr.

func (*FilterExpr) String

func (exp *FilterExpr) String() string

String returns a string representation of the FilterExpr.

type FilterNode

type FilterNode struct {
	Pos
	TrimmableNode
	Filters []string // Filters to apply to Body.
	Body    Node     // Body of the filter tag.
}

FilterNode represents a block of filtered data.

func NewFilterNode

func NewFilterNode(filters []string, body Node, p Pos) *FilterNode

NewFilterNode creates a FilterNode.

func (*FilterNode) All

func (t *FilterNode) All() []Node

All returns all the child Nodes in a FilterNode.

func (*FilterNode) String

func (t *FilterNode) String() string

String returns a string representation of a FilterNode.

type ForNode

type ForNode struct {
	Pos
	TrimmableNode
	Key  string // Name of key variable, or empty string.
	Val  string // Name of val variable.
	X    Expr   // Expression to iterate over.
	Body Node   // Body of the for loop.
	Else Node   // Body of the else section if X is empty.
}

ForNode represents a for loop construct.

func NewForNode

func NewForNode(k, v string, expr Expr, body, els Node, p Pos) *ForNode

NewForNode returns a ForNode.

func (*ForNode) All

func (t *ForNode) All() []Node

All returns all the child Nodes in a ForNode.

func (*ForNode) String

func (t *ForNode) String() string

String returns a string representation of a ForNode.

type FromNode

type FromNode struct {
	Pos
	TrimmableNode
	Tpl     Expr              // Evaluates to the name of the template to include.
	Imports map[string]string // Imports to fetch from the included template.
}

FromNode represents an alternative form of importing macros.

func NewFromNode

func NewFromNode(tpl Expr, imports map[string]string, p Pos) *FromNode

NewFromNode returns a FromNode.

func (*FromNode) All

func (t *FromNode) All() []Node

All returns all the child Nodes in a FromNode.

func (*FromNode) String

func (t *FromNode) String() string

String returns a string representation of a FromNode.

type FuncExpr

type FuncExpr struct {
	Pos
	Name string // The name of the function.
	Args []Expr // Arguments to be passed to the function.
}

FuncExpr represents a function call.

func NewFuncExpr

func NewFuncExpr(name string, args []Expr, pos Pos) *FuncExpr

NewFuncExpr returns a FuncExpr.

func (*FuncExpr) All

func (exp *FuncExpr) All() []Node

All returns all the child Nodes in a FuncExpr.

func (*FuncExpr) String

func (exp *FuncExpr) String() string

String returns a string representation of a FuncExpr.

type GetAttrExpr

type GetAttrExpr struct {
	Pos
	Cont Expr   // Container to get attribute from.
	Attr Expr   // Attribute to get.
	Args []Expr // Args to pass to attribute, if its a method.
}

GetAttrExpr represents an attempt to retrieve an attribute from a value.

func NewGetAttrExpr

func NewGetAttrExpr(cont Expr, attr Expr, args []Expr, pos Pos) *GetAttrExpr

NewGetAttrExpr returns a GetAttrExpr.

func (*GetAttrExpr) All

func (exp *GetAttrExpr) All() []Node

All returns all the child Nodes in a GetAttrExpr.

func (*GetAttrExpr) String

func (exp *GetAttrExpr) String() string

String returns a string representation of a GetAttrExpr.

type GroupExpr

type GroupExpr struct {
	Pos
	X Expr // Expression to be evaluated.
}

GroupExpr represents an arbitrary wrapper around an inner expression.

func NewGroupExpr

func NewGroupExpr(inner Expr, pos Pos) *GroupExpr

NewGroupExpr returns a GroupExpr.

func (*GroupExpr) All

func (exp *GroupExpr) All() []Node

All returns all the child Nodes in a GroupExpr.

func (*GroupExpr) String

func (exp *GroupExpr) String() string

String returns a string representation of a GroupExpr.

type HashExpr

type HashExpr struct {
	Pos
	Elements []*KeyValueExpr
}

func NewHashExpr

func NewHashExpr(pos Pos, elements ...*KeyValueExpr) *HashExpr

NewHashExpr returns a HashExpr.

func (*HashExpr) All

func (exp *HashExpr) All() []Node

All returns all the child Nodes in a HashExpr.

func (*HashExpr) String

func (exp *HashExpr) String() string

String returns a string representation of a HashExpr.

type IfNode

type IfNode struct {
	Pos
	TrimmableNode
	Cond Expr // Condition to test.
	Body Node // Body to evaluate if Cond is true.
	Else Node // Body if Cond is false.
}

IfNode represents an if statement

func NewIfNode

func NewIfNode(cond Expr, body Node, els Node, p Pos) *IfNode

NewIfNode returns a IfNode.

func (*IfNode) All

func (t *IfNode) All() []Node

All returns all the child Nodes in a IfNode.

func (*IfNode) String

func (t *IfNode) String() string

String returns a string representation of an IfNode.

type ImportNode

type ImportNode struct {
	Pos
	TrimmableNode
	Tpl   Expr   // Evaluates to the name of the template to include.
	Alias string // Name of the var to be used as the base for any macros.
}

ImportNode represents importing macros from another template.

func NewImportNode

func NewImportNode(tpl Expr, alias string, p Pos) *ImportNode

NewImportNode returns a ImportNode.

func (*ImportNode) All

func (t *ImportNode) All() []Node

All returns all the child Nodes in a ImportNode.

func (*ImportNode) String

func (t *ImportNode) String() string

String returns a string representation of a ImportNode.

type IncludeNode

type IncludeNode struct {
	Pos
	TrimmableNode
	Tpl  Expr // Expression evaluating to the name of the template to include.
	With Expr // Explicit list of variables to include in the included template.
	Only bool // If true, only vars defined in With will be passed.
}

IncludeNode is an include statement.

func NewIncludeNode

func NewIncludeNode(tmpl Expr, with Expr, only bool, pos Pos) *IncludeNode

NewIncludeNode returns a IncludeNode.

func (*IncludeNode) All

func (t *IncludeNode) All() []Node

All returns all the child Nodes in a IncludeNode.

func (*IncludeNode) String

func (t *IncludeNode) String() string

String returns a string representation of an IncludeNode.

type KeyValueExpr

type KeyValueExpr struct {
	Pos
	Key   Expr
	Value Expr
}

func NewKeyValueExpr

func NewKeyValueExpr(k, v Expr, pos Pos) *KeyValueExpr

NewKeyValueExpr returns a KeyValueExpr.

func (*KeyValueExpr) All

func (exp *KeyValueExpr) All() []Node

All returns all the child Nodes in a KeyValueExpr.

func (*KeyValueExpr) String

func (exp *KeyValueExpr) String() string

String returns a string representation of a KeyValueExpr.

type MacroNode

type MacroNode struct {
	Pos
	TrimmableNode
	Name   string    // Name of the macro.
	Args   []string  // Args the macro receives.
	Body   *BodyNode // Body of the macro.
	Origin string    // The name where this macro is originally defined.
}

MacroNode represents a reusable macro.

func NewMacroNode

func NewMacroNode(name string, args []string, body *BodyNode, p Pos) *MacroNode

NewMacroNode returns a MacroNode.

func (*MacroNode) All

func (t *MacroNode) All() []Node

All returns all the child Nodes in a MacroNode.

func (*MacroNode) String

func (t *MacroNode) String() string

String returns a string representation of a MacroNode.

type ModuleNode

type ModuleNode struct {
	*BodyNode
	Parent *ExtendsNode // Parent template reference.
	Origin string       // The name where this module is originally defined.
}

ModuleNode represents a root node in the AST.

func NewModuleNode

func NewModuleNode(name string, nodes ...Node) *ModuleNode

NewModuleNode returns a ModuleNode.

func (*ModuleNode) String

func (l *ModuleNode) String() string

String returns a string representation of a ModuleNode.

type MultipleExtendsError

type MultipleExtendsError struct {
	// contains filtered or unexported fields
}

MultipleExtendsError describes an attempt to extend from multiple parent templates.

func (*MultipleExtendsError) Error

func (e *MultipleExtendsError) Error() string

type NameExpr

type NameExpr struct {
	Pos
	Name string // Name of the identifier.
}

NameExpr represents an identifier, such as a variable.

func NewNameExpr

func NewNameExpr(name string, pos Pos) *NameExpr

NewNameExpr returns a NameExpr.

func (*NameExpr) All

func (exp *NameExpr) All() []Node

All returns all the child Nodes in a NameExpr.

func (*NameExpr) String

func (exp *NameExpr) String() string

String returns a string representation of the NameExpr.

type Node

type Node interface {
	String() string // String representation of the Node, for debugging.
	Start() Pos     // The position of the Node in the source code.
	All() []Node    // All children of the Node.
}

Node is an item in the AST.

type NodeVisitor

type NodeVisitor interface {
	Enter(Node) // Enter is called before the node is traversed.
	Leave(Node) // Exit is called before leaving the given Node.
}

A NodeVisitor can be used to modify node contents and structure.

type NullExpr

type NullExpr struct {
	Pos
}

NullExpr represents a null literal.

func NewNullExpr

func NewNullExpr(pos Pos) *NullExpr

NewNullExpr returns a NullExpr.

func (*NullExpr) All

func (exp *NullExpr) All() []Node

All returns all the child Nodes in a NullExpr.

func (*NullExpr) String

func (exp *NullExpr) String() string

String returnsa string representation of the NullExpr.

type NumberExpr

type NumberExpr struct {
	Pos
	Value string // The string representation of the number.
}

NumberExpr represents a number literal.

func NewNumberExpr

func NewNumberExpr(val string, pos Pos) *NumberExpr

NewNumberExpr returns a NumberExpr.

func (*NumberExpr) All

func (exp *NumberExpr) All() []Node

All returns all the child Nodes in a NumberExpr.

func (*NumberExpr) String

func (exp *NumberExpr) String() string

String returns a string representation of the NumberExpr.

type ParsingError

type ParsingError interface {
	error
	Pos() Pos     // Pos returns the position where the error originated.
	Name() string // Name returns the name of the template this error occurred in.
	// contains filtered or unexported methods
}

A ParsingError represents an error originating from parsing.

type Pos

type Pos struct {
	Line   int
	Offset int
}

Pos is used to track line and offset in a given string.

func (Pos) Start

func (p Pos) Start() Pos

Start returns the start position of the node.

func (Pos) String

func (p Pos) String() string

String returns a string representation of a pos.

type PrintNode

type PrintNode struct {
	Pos
	TrimmableNode
	X Expr // Expression to print.
}

PrintNode represents a print statement

func NewPrintNode

func NewPrintNode(exp Expr, p Pos) *PrintNode

NewPrintNode returns a PrintNode.

func (*PrintNode) All

func (t *PrintNode) All() []Node

All returns all the child Nodes in a PrintNode.

func (*PrintNode) String

func (t *PrintNode) String() string

String returns a string representation of a PrintNode.

type SetNode

type SetNode struct {
	Pos
	TrimmableNode
	Name string // Name of the var to set.
	X    Node   // Value of the var.
}

SetNode is a set operation on the given varName.

func NewSetNode

func NewSetNode(varName string, expr Node, pos Pos) *SetNode

NewSetNode returns a SetNode.

func (*SetNode) All

func (t *SetNode) All() []Node

All returns all the child Nodes in a SetNode.

func (*SetNode) String

func (t *SetNode) String() string

String returns a string representation of an SetNode.

type StringExpr

type StringExpr struct {
	Pos
	Text string // The text contained within the literal.
}

StringExpr represents a string literal.

func NewStringExpr

func NewStringExpr(text string, pos Pos) *StringExpr

NewStringExpr returns a StringExpr.

func (*StringExpr) All

func (exp *StringExpr) All() []Node

All returns all the child Nodes in a StringExpr.

func (*StringExpr) String

func (exp *StringExpr) String() string

String returns a string representation of the StringExpr.

type TernaryIfExpr

type TernaryIfExpr struct {
	Pos
	Cond   Expr // Condition to test.
	TrueX  Expr // Expression if Cond is true.
	FalseX Expr // Expression if Cond is false.
}

TernaryIfExpr represents an attempt to retrieve an attribute from a value.

func NewTernaryIfExpr

func NewTernaryIfExpr(cond, tx, fx Expr, pos Pos) *TernaryIfExpr

NewTernaryIfExpr returns a TernaryIfExpr.

func (*TernaryIfExpr) All

func (exp *TernaryIfExpr) All() []Node

All returns all the child Nodes in a TernaryIfExpr.

func (*TernaryIfExpr) String

func (exp *TernaryIfExpr) String() string

String returns a string representation of a TernaryIfExpr.

type TestExpr

type TestExpr struct {
	*FuncExpr
}

TestExpr represents a boolean test expression.

func NewTestExpr

func NewTestExpr(name string, args []Expr, pos Pos) *TestExpr

NewTestExpr returns a TestExpr.

func (*TestExpr) String

func (exp *TestExpr) String() string

String returns a string representation of the TestExpr.

type TextNode

type TextNode struct {
	Pos
	Data string // Textual data in the node.
}

TextNode represents raw, non Stick source code, like plain HTML.

func NewTextNode

func NewTextNode(data string, p Pos) *TextNode

NewTextNode returns a TextNode.

func (*TextNode) All

func (t *TextNode) All() []Node

All returns all the child Nodes in a TextNode.

func (*TextNode) String

func (t *TextNode) String() string

String returns a string representation of a TextNode.

type Tree

type Tree struct {
	Name string // A name identifying this tree; the template name.

	Visitors []NodeVisitor
	// contains filtered or unexported fields
}

Tree represents the state of a parser.

func NewNamedTree

func NewNamedTree(name string, input io.Reader) *Tree

NewNamedTree is an alternative constructor which creates a Tree with a name

func NewTree

func NewTree(input io.Reader) *Tree

NewTree creates a new parser Tree, ready for use.

func Parse

func Parse(input string) (*Tree, error)

Parse parses the given input.

func (*Tree) Blocks

func (t *Tree) Blocks() map[string]*BlockNode

Blocks returns a map of blocks in this tree.

func (*Tree) Macros

func (t *Tree) Macros() map[string]*MacroNode

Macros returns a map of macros defined in this tree.

func (*Tree) Parse

func (t *Tree) Parse() error

Parse begins parsing, returning an error, if any.

func (*Tree) Root

func (t *Tree) Root() *ModuleNode

Root returns the root module node.

type TrimmableNode

type TrimmableNode struct {
	TrimBefore bool // True if whitespace before the node should be removed.
	TrimAfter  bool // True if whitespace after the node should be removed.
}

A TrimmableNode contains information on whether preceding or trailing whitespace should be removed when executing the template.

type UnaryExpr

type UnaryExpr struct {
	Pos
	Op string // The operation, in string form.
	X  Expr   // Expression to be evaluated.
}

UnaryExpr represents a unary operation, such as "not x"

func NewUnaryExpr

func NewUnaryExpr(op string, expr Expr, pos Pos) *UnaryExpr

NewUnaryExpr returns a new UnaryExpr.

func (*UnaryExpr) All

func (exp *UnaryExpr) All() []Node

All returns all the child Nodes in a UnaryExpr.

func (*UnaryExpr) String

func (exp *UnaryExpr) String() string

String returns a string representation of a UnaryExpr.

type UnclosedTagError

type UnclosedTagError struct {
	// contains filtered or unexported fields
}

UnclosedTagError is generated when a tag is not properly closed.

func (*UnclosedTagError) Error

func (e *UnclosedTagError) Error() string

type UnexpectedEOFError

type UnexpectedEOFError struct {
	// contains filtered or unexported fields
}

UnexpectedEOFError describes an unexpected end of input.

func (*UnexpectedEOFError) Error

func (e *UnexpectedEOFError) Error() string

type UnexpectedTokenError

type UnexpectedTokenError struct {
	// contains filtered or unexported fields
}

UnexpectedTokenError is generated when the current token is not of the expected type.

func (*UnexpectedTokenError) Error

func (e *UnexpectedTokenError) Error() string

type UnexpectedValueError

type UnexpectedValueError struct {
	// contains filtered or unexported fields
}

UnexpectedValueError describes an invalid or unexpected value inside a token.

func (*UnexpectedValueError) Error

func (e *UnexpectedValueError) Error() string

type UseNode

type UseNode struct {
	Pos
	TrimmableNode
	Tpl     Expr              // Evaluates to the name of the template to include.
	Aliases map[string]string // Aliases for included block names, if any.
}

A UseNode represents the inclusion of blocks from another template. It is also possible to specify aliases for the imported blocks to avoid naming conflicts.

{% use '::blocks.html.twig' with main as base_main, left as base_left %}

func NewUseNode

func NewUseNode(tpl Expr, aliases map[string]string, pos Pos) *UseNode

NewUseNode returns a UseNode.

func (*UseNode) All

func (t *UseNode) All() []Node

All returns all the child Nodes in a UseNode.

func (*UseNode) String

func (t *UseNode) String() string

String returns a string representation of a UseNode.

Jump to

Keyboard shortcuts

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