parse

package
v0.0.0-...-b160839 Latest Latest
Warning

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

Go to latest
Published: May 29, 2023 License: MIT Imports: 9 Imported by: 2

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ColumnRowFromPos

func ColumnRowFromPos(pos int, input string) (column, row int)

ColumnRowFromPos returns the column and row for a given character offset of an input string.

func Inspect

func Inspect(node Node, f func(Node) bool) error

Inspect traverses an AST in depth-first order: It starts by calling f(node); node must not be nil. If f returns true, Inspect invokes f recursively for each of the non-nil children of node, followed by a call of f(nil).

func Walk

func Walk(v Visitor, node Node) error

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.

func (BlockSet) Exists

func (bs BlockSet) Exists(name string) bool

Exists indicates if the given block is already registered.

func (*BlockSet) Register

func (bs *BlockSet) Register(name string, w *WrapperNode) error

Register registers a new block. An error will be returned, if there's already a block with the same name registered.

func (*BlockSet) Replace

func (bs *BlockSet) Replace(name string, w *WrapperNode) error

Replace replaces an already registered block with a new implementation.

type BoolNode

type BoolNode struct {
	Location *Token
	Val      bool
}

BoolNode represents a boolean literal node `{{ true }}`.

func (*BoolNode) Position

func (b *BoolNode) Position() *Token

Position returns the start token of the Node.

func (*BoolNode) String

func (b *BoolNode) String() string

type CallNode

type CallNode struct {
	Location *Token
	Func     Node
	Args     []Expression
	Kwargs   map[string]Expression
}

CallNode represents a function call node `{{ func() }}`.

func (*CallNode) Position

func (c *CallNode) Position() *Token

Position returns the start token of the Node.

func (*CallNode) String

func (c *CallNode) String() string

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.

func NewConfig

func NewConfig() *Config

func (Config) Inherit

func (cfg Config) Inherit() *Config

type DataNode

type DataNode struct {
	Data *Token // data token
}

DataNode represents a raw data (non-template text) a node.

func (*DataNode) Position

func (d *DataNode) Position() *Token

func (*DataNode) String

func (d *DataNode) String() string

type DictNode

type DictNode struct {
	Token *Token
	Pairs []*PairNode
}

DictNode represents a dictionary literal node `{{ {'k1': 'v', 'k2': 'v'} }}`.

func (*DictNode) Position

func (d *DictNode) Position() *Token

Position returns the start token of the Node.

func (*DictNode) String

func (d *DictNode) String() string

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

type FloatNode struct {
	Location *Token
	Val      float64
}

FloatNode represents a float literal node `{{ 1.0 }}`.

func (*FloatNode) Position

func (f *FloatNode) Position() *Token

Position returns the start token of the Node.

func (*FloatNode) String

func (f *FloatNode) String() string

type GetItemNode

type GetItemNode struct {
	Location *Token
	Node     Node
	Arg      string
	Index    int
}

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 Inspector

type Inspector func(Node) bool

func (Inspector) Visit

func (f Inspector) Visit(node Node) (Visitor, error)

type IntegerNode

type IntegerNode struct {
	Location *Token
	Val      int
}

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.

func NewLexer

func NewLexer(input string, cfg *Config) *Lexer

NewLexer creates a new scanner for the input string.

func (*Lexer) Current

func (l *Lexer) Current() string

Current returns the current token.

func (*Lexer) Position

func (l *Lexer) Position() *Position

Position return the current position in the input

func (*Lexer) Run

func (l *Lexer) Run()

Run lexes the input by executing state functions until the state is nil.

type ListNode

type ListNode struct {
	Location *Token
	Val      []Expression
}

ListNode represents a list node `{{ [1, 2, 3] }}`.

func (*ListNode) Position

func (l *ListNode) Position() *Token

Position returns the start token of the Node.

func (*ListNode) String

func (l *ListNode) String() string

type MacroNode

type MacroNode struct {
	Location *Token
	Name     string
	Args     []string
	Kwargs   []*PairNode
	Wrapper  *WrapperNode
}

MacroNode represents a macro definition node `{{% macro foo() }}`.

func (*MacroNode) Position

func (m *MacroNode) Position() *Token

Position returns the start token of the Node.

func (*MacroNode) String

func (m *MacroNode) String() string

type NameNode

type NameNode struct {
	Name *Token
}

NameNode represents a variable name node `{{ my_var }}`.

func (*NameNode) Position

func (n *NameNode) Position() *Token

Position returns the start token of the Node.

func (*NameNode) String

func (n *NameNode) String() string

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

func (*PairNode) Position

func (p *PairNode) Position() *Token

Position returns the start token of the Node.

func (*PairNode) String

func (p *PairNode) String() string

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 NewParser

func NewParser(cfg *Config, stream *Stream) *Parser

NewParser creates a new parser for the given token stream.

func (*Parser) Consume

func (p *Parser) Consume()

Consume consumes the current token, thereby removing it from the stream.

func (*Parser) Current

func (p *Parser) Current() *Token

Current returns the current token.

func (*Parser) End

func (p *Parser) End() bool

End returns the last token in the stream.

func (*Parser) Match

func (p *Parser) Match(types ...TokenType) *Token

Match returns and consumes the current token if it matches one of the given types.

func (*Parser) MatchName

func (p *Parser) MatchName(names ...string) *Token

MatchName returns and advances the current token if it matches one of the given names.

func (*Parser) Next

func (p *Parser) Next() *Token

Next returns and consumes the current token.

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

func (p *Parser) ParseExpressionNode() Node

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

func (p *Parser) ParseStatement() Statement

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

func (p *Parser) Peek(types ...TokenType) *Token

Peek returns the next token without consuming the current one if it matches one of the given types.

func (*Parser) PeekName

func (p *Parser) PeekName(names ...string) *Token

PeekName returns the next token without consuming the current one if it matches one of the given names.

func (*Parser) Pop

func (p *Parser) Pop() *Token

Pop returns the current token and advances to the next.

func (*Parser) SkipUntil

func (p *Parser) SkipUntil(names ...string)

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 Pos

type Pos interface {
	Pos() int
}

Pos is an interface that wraps the Pos method.

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

func (pos *Position) IsValid() bool

IsValid reports whether the position is valid.

func (*Position) Pos

func (pos *Position) Pos() int

Pos returns the current offset starting at 0.

func (Position) String

func (pos Position) String() 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 StatementParser func(parser, args *Parser) Statement

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.

func Lex

func Lex(input string, cfg *Config) *Stream

Lex lexes the input and returns a stream of tokens.

func NewStream

func NewStream(input any) *Stream

NewStream creates a new stream.

func (*Stream) Backup

func (s *Stream) Backup()

Backup backs up the stream to the previous token.

func (*Stream) Current

func (s *Stream) Current() *Token

Current returns the current token.

func (*Stream) EOF

func (s *Stream) EOF() bool

EOF returns true if the end of the stream has been reached.

func (*Stream) End

func (s *Stream) End() bool

End returns true if the end of the stream has been reached.

func (*Stream) IsError

func (s *Stream) IsError() bool

IsError returns true if the stream is in an error state.

func (*Stream) Next

func (s *Stream) Next() *Token

Next returns the next token and consumes it.

func (*Stream) Peek

func (s *Stream) Peek() *Token

Peek returns the next token without consuming it.

type StringNode

type StringNode struct {
	Location *Token
	Val      string
}

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
}

func (*TestCall) String

func (tc *TestCall) String() string

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

type Token struct {
	Type TokenType
	Val  string
	Pos  int
	Line int
	Col  int
}

Token represents a unit of lexing

func (Token) ErrorToken

func (t Token) ErrorToken() *errors.Token

ErrorToken converts the Token into an errors.Token.

func (Token) String

func (t Token) String() string

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

func (TokenType) String

func (t TokenType) String() string

type Trim

type Trim struct {
	Left  bool
	Right bool
}

type TupleNode

type TupleNode struct {
	Location *Token
	Val      []Expression
}

TupleNode represents a tuple literal node `{{ ('a', 'b') }}`.

func (*TupleNode) Position

func (t *TupleNode) Position() *Token

Position returns the start token of the Node.

func (*TupleNode) String

func (t *TupleNode) String() string

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 Visitor

type Visitor interface {
	Visit(node Node) (Visitor, error)
}

type WrapperNode

type WrapperNode struct {
	Location *Token
	Nodes    []Node
	EndTag   string
	Trim     *Trim
	LStrip   bool
}

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

Jump to

Keyboard shortcuts

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