Documentation ¶
Index ¶
- func ColumnRowFromPos(pos int, input string) (column, row int)
- func Inspect(node Node, f func(Node) bool) error
- func Walk(v Visitor, node Node) error
- type BinOperatorNode
- type BinOperatorType
- type BinaryExpressionNode
- type BlockSet
- type BoolNode
- type CallNode
- type CommentNode
- type Config
- type DataNode
- type DictNode
- type Expression
- type FilterCall
- type FilteredExpression
- type FloatNode
- type GetItemNode
- type InlineIfExpressionNode
- type Inspector
- type IntegerNode
- type Lexer
- type ListNode
- type MacroNode
- type NameNode
- type NegationNode
- type Node
- type OutputNode
- type PairNode
- type Parser
- func (p *Parser) Consume()
- func (p *Parser) Current() *Token
- func (p *Parser) End() bool
- func (p *Parser) Match(types ...TokenType) *Token
- func (p *Parser) MatchName(names ...string) *Token
- func (p *Parser) Next() *Token
- func (p *Parser) Parse() (*TemplateNode, error)
- func (p *Parser) ParseComment() *CommentNode
- func (p *Parser) ParseExpression() Expression
- func (p *Parser) ParseExpressionNode() Node
- func (p *Parser) ParseFilter() *FilterCall
- func (p *Parser) ParseFilterExpression(expr Expression) Expression
- func (p *Parser) ParseInlineIf(expr Expression) Expression
- func (p *Parser) ParseMath() Expression
- func (p *Parser) ParseOptionalExpression() Expression
- func (p *Parser) ParseStatement() Statement
- func (p *Parser) ParseStatementBlock() *StatementBlockNode
- func (p *Parser) ParseTemplate() (tpl *TemplateNode, err error)
- func (p *Parser) ParseTest(expr Expression) Expression
- func (p *Parser) ParseVariable() Expression
- func (p *Parser) ParseVariableOrLiteral() Expression
- func (p *Parser) Peek(types ...TokenType) *Token
- func (p *Parser) PeekName(names ...string) *Token
- func (p *Parser) Pop() *Token
- func (p *Parser) SkipUntil(names ...string)
- func (p *Parser) WrapUntil(names ...string) (*WrapperNode, *Parser)
- type Pos
- type Position
- type Statement
- type StatementBlockNode
- type StatementParser
- type Stream
- type StringNode
- type TemplateNode
- type TemplateParseFn
- type TestCall
- type TestExpression
- type Token
- type TokenIterator
- type TokenType
- type Trim
- type TupleNode
- type UnaryExpressionNode
- type Visitor
- type WrapperNode
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ColumnRowFromPos ¶
ColumnRowFromPos returns the column and row for a given character offset of an input string.
Types ¶
type BinOperatorNode ¶
type BinOperatorNode struct { Token *Token Type BinOperatorType }
BinOperatorNode represents a binary operator node `{{ 1 + 2 }}`.
func (BinOperatorNode) Position ¶
func (bo BinOperatorNode) Position() *Token
Position returns the start token of the Node.
func (BinOperatorNode) String ¶
func (bo BinOperatorNode) String() string
type BinOperatorType ¶
type BinOperatorType int
const ( OperatorInvalid BinOperatorType = iota OperatorAnd OperatorOr OperatorNot OperatorIs OperatorIn OperatorEq OperatorNe OperatorGt OperatorGteq OperatorLt OperatorLteq OperatorAdd OperatorSub OperatorMul OperatorDiv OperatorFloordiv OperatorMod OperatorPower OperatorConcat )
func (BinOperatorType) String ¶
func (t BinOperatorType) String() string
type BinaryExpressionNode ¶
type BinaryExpressionNode struct { Left Expression Right Expression Operator *BinOperatorNode }
BinaryExpressionNode represents a binary expression node `{{ 1 + 2 }}`.
func (*BinaryExpressionNode) Position ¶
func (be *BinaryExpressionNode) Position() *Token
Position returns the start token of the Node.
func (*BinaryExpressionNode) String ¶
func (be *BinaryExpressionNode) String() string
type BlockSet ¶
type BlockSet map[string]*WrapperNode
BlockSet is a map of block names to block implementations.
type BoolNode ¶
BoolNode represents a boolean literal node `{{ true }}`.
type CallNode ¶
type CallNode struct { Location *Token Func Node Args []Expression Kwargs map[string]Expression }
CallNode represents a function call node `{{ func() }}`.
type CommentNode ¶
type CommentNode struct { Start *Token // Opening token Text string // Comment text End *Token // Closing token Trim *Trim }
CommentNode represents a single comment node `{# comment #}`.
func (*CommentNode) Position ¶
func (c *CommentNode) Position() *Token
Position returns the start token of the Node.
func (*CommentNode) String ¶
func (c *CommentNode) String() string
func (c *Comment) End() token.Pos { return token.Pos(int(c.Slash) + len(c.Text)) }
type Config ¶
type Config struct { // BlockStartString marks the beginning of a block. Defaults to '{%' BlockStartString string // BlockEndString marks the end of a block. Defaults to '%}'. BlockEndString string // VariableStartString marks the the beginning of a print statement. Defaults to '{{'. VariableStartString string // VariableEndString marks the end of a print statement. Defaults to '}}'. VariableEndString string // CommentStartString marks the beginning of a comment. Defaults to '{#'. CommentStartString string // CommentEndString marks the end of a comment. Defaults to '#}'. CommentEndString string // LineStatementPrefix will be used as prefix for line based statements, if // given and a string. LineStatementPrefix string // LineCommentPrefix will be used as prefix for line based comments, if given // and a string. LineCommentPrefix string }
Config is a configuration for the parser and lexer.
type DataNode ¶
type DataNode struct {
Data *Token // data token
}
DataNode represents a raw data (non-template text) a node.
type DictNode ¶
DictNode represents a dictionary literal node `{{ {'k1': 'v', 'k2': 'v'} }}`.
type Expression ¶
type Expression interface { Node }
Expression represents an evaluable expression part `{{ expr }}`.
type FilterCall ¶
type FilterCall struct { Token *Token Name string Args []Expression Kwargs map[string]Expression }
type FilteredExpression ¶
type FilteredExpression struct { Expression Expression Filters []*FilterCall }
func (*FilteredExpression) Position ¶
func (expr *FilteredExpression) Position() *Token
func (*FilteredExpression) String ¶
func (expr *FilteredExpression) String() string
type FloatNode ¶
FloatNode represents a float literal node `{{ 1.0 }}`.
type GetItemNode ¶
GetItemNode represents a node for looking up items from a list or dictionary `{{ obj[key] }}`.
func (*GetItemNode) Position ¶
func (g *GetItemNode) Position() *Token
Position returns the start token of the Node.
func (*GetItemNode) String ¶
func (g *GetItemNode) String() string
type InlineIfExpressionNode ¶
type InlineIfExpressionNode struct { Location *Token Condition Expression TrueExpr Expression FalseExpr Expression }
InlineIfExpressionNode represents an inline if expression node `{{ foo if foo is defined else bar }}`.
func (InlineIfExpressionNode) Position ¶
func (ii InlineIfExpressionNode) Position() *Token
Position returns the start token of the Node.
func (InlineIfExpressionNode) String ¶
func (ii InlineIfExpressionNode) String() string
type IntegerNode ¶
IntegerNode represents an integer literal node `{{ 1 }}`.
func (*IntegerNode) Position ¶
func (i *IntegerNode) Position() *Token
Position returns the start token of the Node.
func (*IntegerNode) String ¶
func (i *IntegerNode) String() string
type Lexer ¶
type Lexer struct { Input string // the string being scanned. Start int // start position of this item. Pos int // current position in the input. Width int // width of last rune read from input. Line int // Current line in the input Col int // Current position in the line // Position Position // Current lexing position in the input Config *Config // The lexer configuration Tokens chan *Token // channel of scanned tokens. RawStatements rawStmt // contains filtered or unexported fields }
Lexer holds the state of the scanner.
type ListNode ¶
type ListNode struct { Location *Token Val []Expression }
ListNode represents a list node `{{ [1, 2, 3] }}`.
type MacroNode ¶
type MacroNode struct { Location *Token Name string Args []string Kwargs []*PairNode Wrapper *WrapperNode }
MacroNode represents a macro definition node `{{% macro foo() }}`.
type NameNode ¶
type NameNode struct {
Name *Token
}
NameNode represents a variable name node `{{ my_var }}`.
type NegationNode ¶
type NegationNode struct { Term Expression Operator *Token }
NegationNode represents a unary negation node `{{ not true }}`.
func (*NegationNode) Position ¶
func (n *NegationNode) Position() *Token
Position returns the start token of the Node.
func (*NegationNode) String ¶
func (n *NegationNode) String() string
type Node ¶
type Node interface { fmt.Stringer // Position returns the start token of the Node. Position() *Token }
Node represents a token (or series of tokens) with associated data.
All nodes contain position information marking the beginning of the corresponding source text segment; it is accessible via the Pos accessor method. Nodes may contain additional position info for language constructs where comments may be found between parts of the construct (typically any larger, parenthesized subpart). That position information is needed to properly position comments when printing the construct.
type OutputNode ¶
type OutputNode struct { Start *Token Expression Expression End *Token Trim *Trim }
OutputNode represents a printable expression node `{{ expr }}`.
func (*OutputNode) Position ¶
func (o *OutputNode) Position() *Token
Position returns the start token of the Node.
func (*OutputNode) String ¶
func (o *OutputNode) String() string
type PairNode ¶
type PairNode struct { Key Expression Value Expression }
PairNode represents a key/value pair node `{{ {'key': 'value'} }}`
type Parser ¶
type Parser struct { Stream *Stream Config *Config Template *TemplateNode Statements map[string]StatementParser Level int8 TemplateParseFn TemplateParseFn }
Parser provides the means to parse a template document. The parser works on a token list and creates a node tree.
func (*Parser) Consume ¶
func (p *Parser) Consume()
Consume consumes the current token, thereby removing it from the stream.
func (*Parser) Match ¶
Match returns and consumes the current token if it matches one of the given types.
func (*Parser) MatchName ¶
MatchName returns and advances the current token if it matches one of the given names.
func (*Parser) Parse ¶
func (p *Parser) Parse() (*TemplateNode, error)
Parse is an alias for ParseTemplate.
func (*Parser) ParseComment ¶
func (p *Parser) ParseComment() *CommentNode
ParseComment parses a comment and returns a CommentNode.
func (*Parser) ParseExpression ¶
func (p *Parser) ParseExpression() Expression
ParseExpression parses an expression.
func (*Parser) ParseExpressionNode ¶
ParseExpressionNode parses an expression node.
func (*Parser) ParseFilter ¶
func (p *Parser) ParseFilter() *FilterCall
ParseFilter parses a filter.
func (*Parser) ParseFilterExpression ¶
func (p *Parser) ParseFilterExpression(expr Expression) Expression
ParseFilterExpression parses an optional filter expression.
func (*Parser) ParseInlineIf ¶
func (p *Parser) ParseInlineIf(expr Expression) Expression
ParseInlineIf parses an inline if-statement.
func (*Parser) ParseMath ¶
func (p *Parser) ParseMath() Expression
ParseMath parses a math expression.
func (*Parser) ParseOptionalExpression ¶
func (p *Parser) ParseOptionalExpression() Expression
ParseExpression parses an expression.
func (*Parser) ParseStatement ¶
Tag = "{%" IDENT ARGS "%}"
func (*Parser) ParseStatementBlock ¶
func (p *Parser) ParseStatementBlock() *StatementBlockNode
func (*Parser) ParseTemplate ¶
func (p *Parser) ParseTemplate() (tpl *TemplateNode, err error)
ParseTemplate parses a template and returns the root node of the AST.
func (*Parser) ParseTest ¶
func (p *Parser) ParseTest(expr Expression) Expression
func (*Parser) ParseVariable ¶
func (p *Parser) ParseVariable() Expression
ParseVariable parses a variable.
func (*Parser) ParseVariableOrLiteral ¶
func (p *Parser) ParseVariableOrLiteral() Expression
ParseVariableOrLiteral parses a variable or a literal.
func (*Parser) Peek ¶
Peek returns the next token without consuming the current one if it matches one of the given types.
func (*Parser) PeekName ¶
PeekName returns the next token without consuming the current one if it matches one of the given names.
func (*Parser) SkipUntil ¶
SkipUntil skips all nodes between starting tag and "{% endtag %}". Errors are returned as panics.
func (*Parser) WrapUntil ¶
func (p *Parser) WrapUntil(names ...string) (*WrapperNode, *Parser)
WrapUntil wraps all nodes between starting tag and "{% endtag %}" and provides one simple interface to execute the wrapped It returns a parser to process provided arguments to the tag. Errors are returned as panics.
type Position ¶
type Position struct { Filename string // filename, if any Offset int // offset, starting at 0 Line int // line number, starting at 1 Column int // column number, starting at 1 (byte count) }
Position describes an arbitrary source position including the file, line, and column location. A Position is valid if the line number is > 0.
func (Position) String ¶
String returns a string in one of several forms:
file:line:column valid position with file name file:line valid position with file name but no column (column == 0) line:column valid position without file name line valid position without file name and no column (column == 0) file invalid position with file name - invalid position without file name
type Statement ¶
type Statement interface { Node }
Statement represents a statement block `{% stmt %}`.
type StatementBlockNode ¶
type StatementBlockNode struct { Location *Token Name string Stmt Statement Trim *Trim LStrip bool }
StatementBlockNode represents a statement block node `{% 1; 2; 3 %}`.
func (StatementBlockNode) Position ¶
func (sb StatementBlockNode) Position() *Token
Position returns the start token of the Node.
func (StatementBlockNode) String ¶
func (sb StatementBlockNode) String() string
type StatementParser ¶
type Stream ¶
type Stream struct {
// contains filtered or unexported fields
}
Stream represents a stream of tokens that can be navigated sequentially. The stream allows to access each token one after the other and also goings back and forth.
type StringNode ¶
func (*StringNode) Position ¶
func (s *StringNode) Position() *Token
func (*StringNode) String ¶
func (s *StringNode) String() string
type TemplateNode ¶
type TemplateNode struct { Name string Nodes []Node Blocks BlockSet Macros map[string]*MacroNode Parent *TemplateNode }
TemplateNode is the root node of any template AST.
func Parse ¶
func Parse(input string) (*TemplateNode, error)
Parse parses the given template string and returns the root node of the AST.
func (*TemplateNode) GetBlocks ¶
func (tpl *TemplateNode) GetBlocks(name string) []*WrapperNode
GetBlocks returns the blocks with the given name.
func (*TemplateNode) Position ¶
func (tpl *TemplateNode) Position() *Token
Position returns the start token of the Node.
func (*TemplateNode) String ¶
func (tpl *TemplateNode) String() string
type TemplateParseFn ¶
type TemplateParseFn func(string) (*TemplateNode, error)
TemplateParseFn is a function that parses a template string and returns a node tree.
type TestCall ¶
type TestCall struct { Token *Token Name string Args []Expression Kwargs map[string]Expression }
type TestExpression ¶
type TestExpression struct { Expression Expression Test *TestCall }
func (*TestExpression) Position ¶
func (expr *TestExpression) Position() *Token
func (*TestExpression) String ¶
func (expr *TestExpression) String() string
type Token ¶
Token represents a unit of lexing
func (Token) ErrorToken ¶
ErrorToken converts the Token into an errors.Token.
type TokenIterator ¶
type TokenIterator interface {
Next() *Token
}
TokenIterator is an interface for token providers.
func ChanIterator ¶
func ChanIterator(input chan *Token) TokenIterator
ChanIterator creates a new channel iterator.
func SliceIterator ¶
func SliceIterator(input []*Token) TokenIterator
SliceIterator creates a new slice iterator.
type TokenType ¶
type TokenType int
TokenType identifies the type of token.
const ( TokenError TokenType = iota TokenAdd TokenAssign TokenColon TokenComma TokenDiv TokenDot TokenEq TokenFloordiv TokenGt TokenGteq TokenLbrace TokenLbracket TokenLparen TokenLt TokenLteq TokenMod TokenMul TokenNe TokenPipe TokenPow TokenRbrace TokenRbracket TokenRparen TokenSemicolon TokenSub TokenTilde TokenWhitespace TokenFloat TokenInteger TokenName TokenString TokenOperator TokenBlockBegin TokenBlockEnd TokenVariableBegin TokenVariableEnd TokenRawBegin TokenRawEnd TokenCommentBegin TokenCommentEnd TokenComment TokenLinestatementBegin TokenLinestatementEnd TokenLinecommentBegin TokenLinecommentEnd TokenLinecomment TokenData TokenInitial TokenEOF )
Known tokens
type TupleNode ¶
type TupleNode struct { Location *Token Val []Expression }
TupleNode represents a tuple literal node `{{ ('a', 'b') }}`.
type UnaryExpressionNode ¶
type UnaryExpressionNode struct { Location *Token Term Expression Negative bool }
UnaryExpressionNode represents a unary expression node `{{ -1 }}`.
func (*UnaryExpressionNode) Position ¶
func (ue *UnaryExpressionNode) Position() *Token
Position returns the start token of the Node.
func (*UnaryExpressionNode) String ¶
func (ue *UnaryExpressionNode) String() string
type WrapperNode ¶
WrapperNode wraps one or more nodes.
func (WrapperNode) Position ¶
func (w WrapperNode) Position() *Token
Position returns the start token of the Node.
func (WrapperNode) String ¶
func (w WrapperNode) String() string