parser

package
v0.0.0-...-c4f0eec Latest Latest
Warning

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

Go to latest
Published: Aug 21, 2014 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ItemError lex.ItemType = lex.ItemDefaultMax + 1 + iota
	ItemEOF
	ItemRawString
	ItemComment
	ItemNumber
	ItemComplex
	ItemChar
	ItemSpace
	ItemTagStart
	ItemTagEnd
	ItemSymbol
	ItemIdentifier
	ItemDoubleQuotedString
	ItemSingleQuotedString
	ItemBool
	ItemField
	ItemComma
	ItemOpenParen          // '('
	ItemCloseParen         // ')'
	ItemOpenSquareBracket  // '['
	ItemCloseSquareBracket // ']'
	ItemPeriod             // '.'
	ItemKeyword            // Delimiter
	ItemCall               // CALL
	ItemGet                // GET
	ItemSet                // SET
	ItemMacro              // MACRO
	ItemBlock              // BLOCK
	ItemForeach            // FOREACH
	ItemWhile              // WHILE
	ItemIn                 // IN
	ItemInclude            // INCLUDE
	ItemWith               // WITH
	ItemIf                 // IF
	ItemElse               // ELSE
	ItemElseIf             // ELSIF
	ItemUnless             // UNLESS
	ItemSwitch             // SWITCH
	ItemCase               // CASE
	ItemWrapper            // WRAPPER
	ItemDefault            // DEFAULT
	ItemEnd                // END
	ItemOperator           // Delimiter
	ItemRange              // ..
	ItemEquals             // ==
	ItemNotEquals          // !=
	ItemGT                 // >
	ItemLT                 // <
	ItemCmp                // <=>
	ItemLE                 // <=
	ItemGE                 // >=
	ItemShiftLeft          // <<
	ItemShiftRight         // >>
	ItemAssignAdd          // +=
	ItemAssignSub          // -=
	ItemAssignMul          // *=
	ItemAssignDiv          // /=
	ItemAssignMod          // %=
	ItemAnd                // &&
	ItemOr                 // ||
	ItemFatComma           // =>
	ItemIncr               // ++
	ItemDecr               // --
	ItemPlus
	ItemMinus
	ItemAsterisk
	ItemSlash
	ItemVerticalSlash
	ItemMod
	ItemAssign // =

	DefaultItemTypeMax
)

Variables

View Source
var DefaultSymbolSet = NewLexSymbolSet()

DefaultSymbolSet is the LexSymbolSet for symbols that are common to all syntax

Functions

This section is empty.

Types

type AST

type AST struct {
	Name      string    // name of the template
	ParseName string    // name of the top-level template during parsing
	Root      *ListNode // root of the tree
	Timestamp time.Time // last-modified date of this template
	// contains filtered or unexported fields
}

AST is represents the syntax tree for an Xslate template

func (*AST) String

func (ast *AST) String() string

String returns the textual representation of this AST

func (*AST) Visit

func (ast *AST) Visit() <-chan Node

Visit returns a channel which you can receive Node structs in order that that they would be processed

type AssignmentNode

type AssignmentNode struct {
	BaseNode
	Assignee   *LocalVarNode
	Expression Node
}

func NewAssignmentNode

func NewAssignmentNode(pos int, symbol string) *AssignmentNode

func (*AssignmentNode) Copy

func (n *AssignmentNode) Copy() Node

func (*AssignmentNode) String

func (n *AssignmentNode) String() string

func (*AssignmentNode) Visit

func (n *AssignmentNode) Visit(c chan Node)

type BaseNode

type BaseNode struct {
	NodeType // String() is delegated here
	// contains filtered or unexported fields
}

BaseNode is the most basic node with no extra data attached to it

func NewNoopNode

func NewNoopNode() *BaseNode

NewNoopNode returns a op that does nothing

func (*BaseNode) Copy

func (n *BaseNode) Copy() Node

func (*BaseNode) Pos

func (n *BaseNode) Pos() int

Pos returns the position of this node in the document

func (*BaseNode) Visit

func (n *BaseNode) Visit(c chan Node)

type BinaryNode

type BinaryNode struct {
	BaseNode
	Left  Node
	Right Node
}

func NewDivNode

func NewDivNode(pos int) *BinaryNode

func NewEqualsNode

func NewEqualsNode(pos int) *BinaryNode

func NewFetchArrayElementNode

func NewFetchArrayElementNode(pos int) *BinaryNode

func NewGTNode

func NewGTNode(pos int) *BinaryNode

func NewLTNode

func NewLTNode(pos int) *BinaryNode

func NewMinusNode

func NewMinusNode(pos int) *BinaryNode

func NewMulNode

func NewMulNode(pos int) *BinaryNode

func NewNotEqualsNode

func NewNotEqualsNode(pos int) *BinaryNode

func NewPlusNode

func NewPlusNode(pos int) *BinaryNode

func NewRangeNode

func NewRangeNode(pos int, start, end Node) *BinaryNode

func (*BinaryNode) Copy

func (n *BinaryNode) Copy() Node

func (*BinaryNode) Visit

func (n *BinaryNode) Visit(c chan Node)

type Builder

type Builder struct {
}

func NewBuilder

func NewBuilder() *Builder

func (*Builder) Backup

func (b *Builder) Backup(ctx *builderCtx)

func (*Builder) Backup2

func (b *Builder) Backup2(ctx *builderCtx, t1 lex.LexItem)

func (*Builder) DeclareLocalVarIfNew

func (b *Builder) DeclareLocalVarIfNew(ctx *builderCtx, symbol lex.LexItem)

func (*Builder) LocalVarOrFetchSymbol

func (b *Builder) LocalVarOrFetchSymbol(ctx *builderCtx, token lex.LexItem) Node

func (*Builder) Next

func (b *Builder) Next(ctx *builderCtx) lex.LexItem

func (*Builder) NextNonSpace

func (b *Builder) NextNonSpace(ctx *builderCtx) lex.LexItem

func (*Builder) Parse

func (b *Builder) Parse(name string, l lex.Lexer) (ast *AST, err error)

func (*Builder) ParseArrayElementFetch

func (b *Builder) ParseArrayElementFetch(ctx *builderCtx, invocant Node) Node

func (*Builder) ParseAssignment

func (b *Builder) ParseAssignment(ctx *builderCtx) Node

func (*Builder) ParseElse

func (b *Builder) ParseElse(ctx *builderCtx) Node

func (*Builder) ParseExpression

func (b *Builder) ParseExpression(ctx *builderCtx, canPrint bool) (n Node)

func (*Builder) ParseExpressionOrAssignment

func (b *Builder) ParseExpressionOrAssignment(ctx *builderCtx, canPrint bool) Node

func (*Builder) ParseFilter

func (b *Builder) ParseFilter(ctx *builderCtx, n Node) Node

func (*Builder) ParseForeach

func (b *Builder) ParseForeach(ctx *builderCtx) Node

func (*Builder) ParseFunCall

func (b *Builder) ParseFunCall(ctx *builderCtx, invocant Node) Node

func (*Builder) ParseGroup

func (b *Builder) ParseGroup(ctx *builderCtx) Node

func (*Builder) ParseIf

func (b *Builder) ParseIf(ctx *builderCtx) Node

func (*Builder) ParseInclude

func (b *Builder) ParseInclude(ctx *builderCtx) Node

func (*Builder) ParseList

func (b *Builder) ParseList(ctx *builderCtx) Node

func (*Builder) ParseListVariableOrMakeArray

func (b *Builder) ParseListVariableOrMakeArray(ctx *builderCtx) Node

func (*Builder) ParseLiteral

func (b *Builder) ParseLiteral(ctx *builderCtx) Node

func (*Builder) ParseMacro

func (b *Builder) ParseMacro(ctx *builderCtx) Node

func (*Builder) ParseMakeArray

func (b *Builder) ParseMakeArray(ctx *builderCtx) Node

func (*Builder) ParseMethodCallOrMapLookup

func (b *Builder) ParseMethodCallOrMapLookup(ctx *builderCtx, invocant Node) Node

func (*Builder) ParseRange

func (b *Builder) ParseRange(ctx *builderCtx) Node

func (*Builder) ParseRawString

func (b *Builder) ParseRawString(ctx *builderCtx) Node

func (*Builder) ParseStatements

func (b *Builder) ParseStatements(ctx *builderCtx) Node

func (*Builder) ParseTemplate

func (b *Builder) ParseTemplate(ctx *builderCtx) Node

func (*Builder) ParseTemplateOrText

func (b *Builder) ParseTemplateOrText(ctx *builderCtx) Node

func (*Builder) ParseTerm

func (b *Builder) ParseTerm(ctx *builderCtx) Node

func (*Builder) ParseWhile

func (b *Builder) ParseWhile(ctx *builderCtx) Node

func (*Builder) ParseWrapper

func (b *Builder) ParseWrapper(ctx *builderCtx) Node

func (*Builder) Peek

func (b *Builder) Peek(ctx *builderCtx) lex.LexItem

func (*Builder) PeekNonSpace

func (b *Builder) PeekNonSpace(ctx *builderCtx) lex.LexItem

func (*Builder) Start

func (b *Builder) Start(ctx *builderCtx)

func (*Builder) Unexpected

func (b *Builder) Unexpected(ctx *builderCtx, format string, args ...interface{})

type ElseNode

type ElseNode struct {
	*ListNode
	IfNode Node
}

func NewElseNode

func NewElseNode(pos int) *ElseNode

type FetchFieldNode

type FetchFieldNode struct {
	BaseNode
	Container Node
	FieldName string
}

func NewFetchFieldNode

func NewFetchFieldNode(pos int, container Node, field string) *FetchFieldNode

func (*FetchFieldNode) Copy

func (n *FetchFieldNode) Copy() Node

func (*FetchFieldNode) Visit

func (n *FetchFieldNode) Visit(c chan Node)

type FilterNode

type FilterNode struct {
	*UnaryNode
	Name string
}

func NewFilterNode

func NewFilterNode(pos int, name string, child Node) *FilterNode

func (*FilterNode) Copy

func (n *FilterNode) Copy() Node

func (*FilterNode) Visit

func (n *FilterNode) Visit(c chan Node)

type ForeachNode

type ForeachNode struct {
	*ListNode
	IndexVarName string
	IndexVarIdx  int
	List         Node
}

func NewForeachNode

func NewForeachNode(pos int, symbol string) *ForeachNode

func (*ForeachNode) Copy

func (n *ForeachNode) Copy() Node

func (*ForeachNode) String

func (n *ForeachNode) String() string

func (*ForeachNode) Visit

func (n *ForeachNode) Visit(c chan Node)

type Frame

type Frame struct {
	*util.Frame
	Node NodeAppender

	// This contains names of local variables, mapped to their
	// respective location in the framestack
	LvarNames map[string]int
}

Frame is the frame struct used during parsing, which has a bit of extension over the common Frame struct.

func NewFrame

func NewFrame(s *util.Stack) *Frame

type FunCallNode

type FunCallNode struct {
	BaseNode
	Invocant Node
	Args     *ListNode
}

func NewFunCallNode

func NewFunCallNode(pos int, invocant Node, args *ListNode) *FunCallNode

func (*FunCallNode) Copy

func (n *FunCallNode) Copy() Node

func (*FunCallNode) Visit

func (n *FunCallNode) Visit(c chan Node)

type IfNode

type IfNode struct {
	*ListNode
	BooleanExpression Node
}

func NewIfNode

func NewIfNode(pos int, exp Node) *IfNode

func (*IfNode) Copy

func (n *IfNode) Copy() Node

func (*IfNode) Visit

func (n *IfNode) Visit(c chan Node)

type IncludeNode

type IncludeNode struct {
	BaseNode
	IncludeTarget   Node
	AssignmentNodes []Node
}

func NewIncludeNode

func NewIncludeNode(pos int, include Node) *IncludeNode

func (*IncludeNode) AppendAssignment

func (n *IncludeNode) AppendAssignment(a Node)

func (*IncludeNode) Copy

func (n *IncludeNode) Copy() Node

func (*IncludeNode) Visit

func (n *IncludeNode) Visit(c chan Node)

type LexSymbol

type LexSymbol struct {
	Name     string
	Type     lex.ItemType
	Priority float32
}

LexSymbol holds the pre-defined symbols to be lexed

type LexSymbolList

type LexSymbolList []LexSymbol

LexSymbolList a list of LexSymbols. Normally you do not need to use it. This is mainly only useful for sorting LexSymbols

func (LexSymbolList) Sort

func (list LexSymbolList) Sort() LexSymbolList

Sort returns a sorted list of LexSymbols, sorted by Priority

type LexSymbolSet

type LexSymbolSet struct {
	Map        map[string]LexSymbol
	SortedList LexSymbolList
}

LexSymbolSet is the container for symbols.

func NewLexSymbolSet

func NewLexSymbolSet() *LexSymbolSet

NewLexSymbolSet creates a new LexSymbolSet

func (*LexSymbolSet) Copy

func (l *LexSymbolSet) Copy() *LexSymbolSet

Copy creates a new copy of the given LexSymbolSet

func (*LexSymbolSet) Count

func (l *LexSymbolSet) Count() int

Count returns the number of symbols registered

func (*LexSymbolSet) Get

func (l *LexSymbolSet) Get(name string) LexSymbol

Get returns the LexSymbol associated with `name`

func (*LexSymbolSet) GetSortedList

func (l *LexSymbolSet) GetSortedList() LexSymbolList

GetSortedList returns the lsit of LexSymbols in order that they should be searched for in the tempalte

func (*LexSymbolSet) Set

func (l *LexSymbolSet) Set(name string, typ lex.ItemType, prio ...float32)

Set creates and sets a new LexItem to `name`

type LexSymbolSorter

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

LexSymbolSorter sorts a list of LexSymbols by priority

func (LexSymbolSorter) Len

func (s LexSymbolSorter) Len() int

Len returns the length of the list

func (LexSymbolSorter) Less

func (s LexSymbolSorter) Less(i, j int) bool

Less returns true if the i-th element's Priority is less than the j-th element

func (LexSymbolSorter) Swap

func (s LexSymbolSorter) Swap(i, j int)

Swap swaps the elements at i and j

type Lexer

type Lexer struct {
	lex.Lexer
	// contains filtered or unexported fields
}

func NewReaderLexer

func NewReaderLexer(rdr io.Reader, ss *LexSymbolSet) *Lexer

func NewStringLexer

func NewStringLexer(template string, ss *LexSymbolSet) *Lexer

func (*Lexer) SetTagEnd

func (l *Lexer) SetTagEnd(s string)

func (*Lexer) SetTagStart

func (l *Lexer) SetTagStart(s string)

type ListNode

type ListNode struct {
	BaseNode
	Nodes []Node
}

func NewListNode

func NewListNode(pos int) *ListNode

func NewPrintNode

func NewPrintNode(pos int, arg Node) *ListNode

func NewPrintRawNode

func NewPrintRawNode(pos int) *ListNode

func NewRootNode

func NewRootNode() *ListNode

func (*ListNode) Append

func (l *ListNode) Append(n Node)

func (*ListNode) Copy

func (l *ListNode) Copy() Node

func (*ListNode) Visit

func (l *ListNode) Visit(c chan Node)

type LocalVarNode

type LocalVarNode struct {
	BaseNode
	Name   string
	Offset int
}

func NewLocalVarNode

func NewLocalVarNode(pos int, symbol string, idx int) *LocalVarNode

func (*LocalVarNode) Copy

func (n *LocalVarNode) Copy() Node

func (*LocalVarNode) String

func (n *LocalVarNode) String() string

func (*LocalVarNode) Visit

func (n *LocalVarNode) Visit(c chan Node)

type MacroNode

type MacroNode struct {
	*ListNode
	Name      string
	LocalVar  *LocalVarNode
	Arguments []*LocalVarNode
}

func NewMacroNode

func NewMacroNode(pos int, name string) *MacroNode

func (*MacroNode) AppendArg

func (n *MacroNode) AppendArg(arg *LocalVarNode)

type MethodCallNode

type MethodCallNode struct {
	BaseNode
	Invocant   Node
	MethodName string
	Args       *ListNode
}

func NewMethodCallNode

func NewMethodCallNode(pos int, invocant Node, method string, args *ListNode) *MethodCallNode

func (*MethodCallNode) Copy

func (n *MethodCallNode) Copy() Node

func (*MethodCallNode) Visit

func (n *MethodCallNode) Visit(c chan Node)

type Node

type Node interface {
	Type() NodeType
	String() string
	Copy() Node
	Pos() int
	Visit(chan Node)
}

Node defines the interface for an AST node

type NodeAppender

type NodeAppender interface {
	Node
	Append(Node)
}

type NodeType

type NodeType int

NodeType is used to distinguish each AST node

const (
	NodeNoop NodeType = iota
	NodeRoot
	NodeText
	NodeNumber
	NodeInt
	NodeFloat
	NodeIf
	NodeElse
	NodeList
	NodeForeach
	NodeWhile
	NodeWrapper
	NodeInclude
	NodeAssignment
	NodeLocalVar
	NodeFetchField
	NodeFetchArrayElement
	NodeMethodCall
	NodeFunCall
	NodePrint
	NodePrintRaw
	NodeFetchSymbol
	NodeRange
	NodePlus
	NodeMinus
	NodeMul
	NodeDiv
	NodeEquals
	NodeNotEquals
	NodeLT
	NodeGT
	NodeMakeArray
	NodeGroup
	NodeFilter
	NodeMacro
	NodeMax
)

func (NodeType) String

func (n NodeType) String() string

func (NodeType) Type

func (n NodeType) Type() NodeType

Type returns the current node type

type NumberNode

type NumberNode struct {
	BaseNode
	Value reflect.Value
}

func NewFloatNode

func NewFloatNode(pos int, v float64) *NumberNode

func NewIntNode

func NewIntNode(pos int, v int64) *NumberNode

func NewNumberNode

func NewNumberNode(pos int, num reflect.Value) *NumberNode

func (*NumberNode) Copy

func (n *NumberNode) Copy() Node

func (*NumberNode) Visit

func (n *NumberNode) Visit(c chan Node)

type Parser

type Parser interface {
	Parse(string, []byte) (*AST, error)
	ParseString(string, string) (*AST, error)
	ParseReader(string, io.Reader) (*AST, error)
}

Parser defines the interface for Xslate parsers

type TextNode

type TextNode struct {
	BaseNode
	Text []byte
}

func NewFetchSymbolNode

func NewFetchSymbolNode(pos int, symbol string) *TextNode

func NewTextNode

func NewTextNode(pos int, arg string) *TextNode

func (*TextNode) Copy

func (n *TextNode) Copy() Node

func (*TextNode) String

func (n *TextNode) String() string

func (*TextNode) Visit

func (n *TextNode) Visit(c chan Node)

type UnaryNode

type UnaryNode struct {
	BaseNode
	Child Node
}

func NewGroupNode

func NewGroupNode(pos int) *UnaryNode

func NewMakeArrayNode

func NewMakeArrayNode(pos int, child Node) *UnaryNode

func (*UnaryNode) Copy

func (n *UnaryNode) Copy() Node

func (*UnaryNode) Visit

func (n *UnaryNode) Visit(c chan Node)

type WhileNode

type WhileNode struct {
	*ListNode
	Condition Node
}

func NewWhileNode

func NewWhileNode(pos int, n Node) *WhileNode

func (*WhileNode) Copy

func (n *WhileNode) Copy() Node

func (*WhileNode) Visit

func (n *WhileNode) Visit(c chan Node)

type WrapperNode

type WrapperNode struct {
	*ListNode
	WrapperName string
	// XXX need to make this configurable. currently it's only "content"
	// WrapInto string
	AssignmentNodes []Node
}

func NewWrapperNode

func NewWrapperNode(pos int, template string) *WrapperNode

func (*WrapperNode) AppendAssignment

func (n *WrapperNode) AppendAssignment(a Node)

func (*WrapperNode) Copy

func (n *WrapperNode) Copy() Node

func (*WrapperNode) Visit

func (n *WrapperNode) Visit(c chan Node)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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