Documentation ¶
Overview ¶
Package parse handles transforming Stick source code into AST for further processing.
Index ¶
- Constants
- type ArrayExpr
- type BinaryExpr
- type BlockNode
- type BodyNode
- type BoolExpr
- type CommentNode
- type DebugError
- type DoNode
- type EmbedNode
- type Expr
- type ExtendsNode
- type FilterExpr
- type FilterNode
- type ForNode
- type FromNode
- type FuncExpr
- type GetAttrExpr
- type GroupExpr
- type HashExpr
- type IfNode
- type ImportNode
- type IncludeNode
- type KeyValueExpr
- type MacroNode
- type ModuleNode
- type MultipleExtendsError
- type NameExpr
- type Node
- type NodeVisitor
- type NullExpr
- type NumberExpr
- type ParsingError
- type Pos
- type PrintNode
- type SetNode
- type StringExpr
- type TernaryIfExpr
- type TestExpr
- type TextNode
- type Tree
- type TrimmableNode
- type UnaryExpr
- type UnclosedTagError
- type UnexpectedEOFError
- type UnexpectedTokenError
- type UnexpectedValueError
- type UseNode
Constants ¶
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 ¶
func NewArrayExpr ¶
NewArrayExpr returns 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 ¶
NewBlockNode returns a BlockNode.
type BodyNode ¶
BodyNode represents a list of nodes.
func NewBodyNode ¶
NewBodyNode returns a BodyNode.
type BoolExpr ¶
BoolExpr represents a boolean literal.
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.
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.
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 ¶
NewForNode returns 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 ¶
NewFromNode returns 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 ¶
NewFuncExpr returns 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 ¶
GroupExpr represents an arbitrary wrapper around an inner expression.
func NewGroupExpr ¶
NewGroupExpr returns a GroupExpr.
type HashExpr ¶
type HashExpr struct { Pos Elements []*KeyValueExpr }
func NewHashExpr ¶
func NewHashExpr(pos Pos, elements ...*KeyValueExpr) *HashExpr
NewHashExpr returns 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
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 ¶
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 ¶
NewMacroNode returns 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 ¶
NameExpr represents an identifier, such as a variable.
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.
type NumberExpr ¶
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 PrintNode ¶
type PrintNode struct { Pos TrimmableNode X Expr // Expression to print. }
PrintNode represents a print statement
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 ¶
NewSetNode returns a SetNode.
type StringExpr ¶
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 ¶
NewTestExpr returns a TestExpr.
type TextNode ¶
TextNode represents raw, non Stick source code, like plain HTML.
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 ¶
NewNamedTree is an alternative constructor which creates a Tree with a name
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 ¶
NewUnaryExpr returns a new 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 ¶
NewUseNode returns a UseNode.