parser

package
Version: v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jul 10, 2017 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package parser parses template source into an abstract syntax tree.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ASTBlock

type ASTBlock struct {
	Token

	Body     []ASTNode   // Body is the nodes before the first branch
	Branches []*ASTBlock // E.g. else and elseif w/in an if
	// contains filtered or unexported fields
}

ASTBlock represents a {% tag %}…{% endtag %}.

type ASTNode

type ASTNode interface {
	SourceLocation() SourceLoc
	SourceText() string
}

ASTNode is a node of an AST.

type ASTObject

type ASTObject struct {
	Token
	Expr expression.Expression
}

ASTObject is an {{ object }} object.

type ASTRaw

type ASTRaw struct {
	Slices []string
	// contains filtered or unexported fields
}

ASTRaw holds the text between the start and end of a raw tag.

func (*ASTRaw) SourceLocation added in v0.2.0

func (n *ASTRaw) SourceLocation() SourceLoc

func (*ASTRaw) SourceText added in v0.2.0

func (n *ASTRaw) SourceText() string

type ASTSeq

type ASTSeq struct {
	Children []ASTNode
	// contains filtered or unexported fields
}

ASTSeq is a sequence of nodes.

func (*ASTSeq) SourceLocation added in v0.2.0

func (n *ASTSeq) SourceLocation() SourceLoc

func (*ASTSeq) SourceText added in v0.2.0

func (n *ASTSeq) SourceText() string

type ASTTag

type ASTTag struct {
	Token
}

ASTTag is a tag {% tag %} that is not a block start or end.

type ASTText

type ASTText struct {
	Token
}

ASTText is a text span, that is rendered verbatim.

type BlockSyntax

type BlockSyntax interface {
	IsBlock() bool
	CanHaveParent(BlockSyntax) bool
	IsBlockEnd() bool
	IsBlockStart() bool
	IsBranch() bool
	ParentTags() []string
	RequiresParent() bool
	TagName() string
}

BlockSyntax supplies the parser with syntax information about blocks.

type Config

type Config struct {
	expression.Config
	Grammar  Grammar
	Filename string
	LineNo   int
}

A Config holds configuration information for parsing and rendering.

func NewConfig

func NewConfig(g Grammar) Config

NewConfig creates a parser Config.

func (Config) Parse

func (c Config) Parse(source string) (ASTNode, ParseError)

Parse parses a source template. It returns an AST root, that can be compiled and evaluated.

type Grammar

type Grammar interface {
	BlockSyntax(string) (BlockSyntax, bool)
}

Grammar supplies the parser with syntax information about blocks.

type ParseError

type ParseError interface {
	error
	Cause() error
	Filename() string
	LineNumber() int
}

A ParseError is a parse error during the template parsing.

type SourceLoc added in v0.2.0

type SourceLoc struct {
	Pathname string
	LineNo   int
}

SourceLoc contains a Token's source location.

func (SourceLoc) String added in v0.2.0

func (s SourceLoc) String() string

type Token

type Token struct {
	Type      TokenType
	SourceLoc SourceLoc
	Name      string // Name is the tag name of a tag Chunk. E.g. the tag name of "{% if 1 %}" is "if".
	Args      string // Parameters is the tag arguments of a tag Chunk. E.g. the tag arguments of "{% if 1 %}" is "1".
	Source    string // Source is the entirety of the token, including the "{{", "{%", etc. markers.
}

A Token is either an object {{a.b}}, a tag {%if a>b%}, or a text chunk (anything outside of {{}} and {%%}.)

func Scan

func Scan(data string, pathname string, firstLine int) []Token

Scan breaks a string into a sequence of Tokens.

func (Token) SourceLocation added in v0.2.0

func (c Token) SourceLocation() SourceLoc

SourceLocation returns the token's source location, for use in error reporting.

func (Token) SourceText added in v0.2.0

func (c Token) SourceText() string

SourceText returns the token's source text, for use in error reporting.

func (Token) String

func (c Token) String() string

type TokenType

type TokenType int

TokenType is the type of a Chunk

const (
	// TextTokenType is the type of a text Chunk
	TextTokenType TokenType = iota
	// TagTokenType is the type of a tag Chunk "{%…%}"
	TagTokenType
	// ObjTokenType is the type of an object Chunk "{{…}}"
	ObjTokenType
)

func (TokenType) String

func (i TokenType) String() string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto
y or Y : Canonical URL