ast

package
v2.0.2+incompatible Latest Latest
Warning

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

Go to latest
Published: Mar 22, 2018 License: MIT Imports: 3 Imported by: 98

Documentation

Overview

Package ast provides structures to represent a handlebars Abstract Syntax Tree, and a Visitor interface to visit that tree.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func HelperNameStr

func HelperNameStr(node Node) (string, bool)

HelperNameStr returns the string representation of a helper name, with a boolean set to false if this is not a valid helper name.

helperName : path | dataName | STRING | NUMBER | BOOLEAN | UNDEFINED | NULL

func LiteralStr

func LiteralStr(node Node) (string, bool)

LiteralStr returns the string representation of literal value, with a boolean set to false if this is not a literal.

func PathExpressionStr

func PathExpressionStr(node Node) (string, bool)

PathExpressionStr returns the string representation of path expression value, with a boolean set to false if this is not a path expression.

func Print

func Print(node Node) string

Print returns a string representation of given AST, that can be used for debugging purpose.

Types

type BlockStatement

type BlockStatement struct {
	NodeType
	Loc

	Expression *Expression

	Program *Program
	Inverse *Program

	// whitespace management
	OpenStrip    *Strip
	InverseStrip *Strip
	CloseStrip   *Strip
}

BlockStatement represents a block node.

func NewBlockStatement

func NewBlockStatement(pos int, line int) *BlockStatement

NewBlockStatement instanciates a new block node.

func (*BlockStatement) Accept

func (node *BlockStatement) Accept(visitor Visitor) interface{}

Accept is the receiver entry point for visitors.

func (*BlockStatement) String

func (node *BlockStatement) String() string

String returns a string representation of receiver that can be used for debugging.

type BooleanLiteral

type BooleanLiteral struct {
	NodeType
	Loc

	Value    bool
	Original string
}

BooleanLiteral represents a boolean node.

func NewBooleanLiteral

func NewBooleanLiteral(pos int, line int, val bool, original string) *BooleanLiteral

NewBooleanLiteral instanciates a new boolean node.

func (*BooleanLiteral) Accept

func (node *BooleanLiteral) Accept(visitor Visitor) interface{}

Accept is the receiver entry point for visitors.

func (*BooleanLiteral) Canonical

func (node *BooleanLiteral) Canonical() string

Canonical returns the canonical form of boolean node as a string (ie. "true" | "false").

func (*BooleanLiteral) String

func (node *BooleanLiteral) String() string

String returns a string representation of receiver that can be used for debugging.

type CommentStatement

type CommentStatement struct {
	NodeType
	Loc

	Value string

	// whitespace management
	Strip *Strip
}

CommentStatement represents a comment node.

func NewCommentStatement

func NewCommentStatement(pos int, line int, val string) *CommentStatement

NewCommentStatement instanciates a new comment node.

func (*CommentStatement) Accept

func (node *CommentStatement) Accept(visitor Visitor) interface{}

Accept is the receiver entry point for visitors.

func (*CommentStatement) String

func (node *CommentStatement) String() string

String returns a string representation of receiver that can be used for debugging.

type ContentStatement

type ContentStatement struct {
	NodeType
	Loc

	Value    string
	Original string

	// whitespace management
	RightStripped bool
	LeftStripped  bool
}

ContentStatement represents a content node.

func NewContentStatement

func NewContentStatement(pos int, line int, val string) *ContentStatement

NewContentStatement instanciates a new content node.

func (*ContentStatement) Accept

func (node *ContentStatement) Accept(visitor Visitor) interface{}

Accept is the receiver entry point for visitors.

func (*ContentStatement) String

func (node *ContentStatement) String() string

String returns a string representation of receiver that can be used for debugging.

type Expression

type Expression struct {
	NodeType
	Loc

	Path   Node   // PathExpression | StringLiteral | BooleanLiteral | NumberLiteral
	Params []Node // [ Expression ... ]
	Hash   *Hash
}

Expression represents an expression node.

func NewExpression

func NewExpression(pos int, line int) *Expression

NewExpression instanciates a new expression node.

func (*Expression) Accept

func (node *Expression) Accept(visitor Visitor) interface{}

Accept is the receiver entry point for visitors.

func (*Expression) Canonical

func (node *Expression) Canonical() string

Canonical returns the canonical form of expression node as a string.

func (*Expression) FieldPath

func (node *Expression) FieldPath() *PathExpression

FieldPath returns path expression representing a field path, or nil if this is not a field path.

func (*Expression) HelperName

func (node *Expression) HelperName() string

HelperName returns helper name, or an empty string if this expression can't be a helper.

func (*Expression) LiteralStr

func (node *Expression) LiteralStr() (string, bool)

LiteralStr returns the string representation of literal value, with a boolean set to false if this is not a literal.

func (*Expression) String

func (node *Expression) String() string

String returns a string representation of receiver that can be used for debugging.

type Hash

type Hash struct {
	NodeType
	Loc

	Pairs []*HashPair
}

Hash represents a hash node.

func NewHash

func NewHash(pos int, line int) *Hash

NewHash instanciates a new hash node.

func (*Hash) Accept

func (node *Hash) Accept(visitor Visitor) interface{}

Accept is the receiver entry point for visitors.

func (*Hash) String

func (node *Hash) String() string

String returns a string representation of receiver that can be used for debugging.

type HashPair

type HashPair struct {
	NodeType
	Loc

	Key string
	Val Node // Expression
}

HashPair represents a hash pair node.

func NewHashPair

func NewHashPair(pos int, line int) *HashPair

NewHashPair instanciates a new hash pair node.

func (*HashPair) Accept

func (node *HashPair) Accept(visitor Visitor) interface{}

Accept is the receiver entry point for visitors.

func (*HashPair) String

func (node *HashPair) String() string

String returns a string representation of receiver that can be used for debugging.

type Loc

type Loc struct {
	Pos  int // Byte position
	Line int // Line number
}

Loc represents the position of a parsed node in source file.

func (Loc) Location

func (l Loc) Location() Loc

Location returns itself, and permits struct includers to satisfy that part of Node interface.

type MustacheStatement

type MustacheStatement struct {
	NodeType
	Loc

	Unescaped  bool
	Expression *Expression

	// whitespace management
	Strip *Strip
}

MustacheStatement represents a mustache node.

func NewMustacheStatement

func NewMustacheStatement(pos int, line int, unescaped bool) *MustacheStatement

NewMustacheStatement instanciates a new mustache node.

func (*MustacheStatement) Accept

func (node *MustacheStatement) Accept(visitor Visitor) interface{}

Accept is the receiver entry point for visitors.

func (*MustacheStatement) String

func (node *MustacheStatement) String() string

String returns a string representation of receiver that can be used for debugging.

type Node

type Node interface {
	// node type
	Type() NodeType

	// location of node in original input string
	Location() Loc

	// string representation, used for debugging
	String() string

	// accepts visitor
	Accept(Visitor) interface{}
}

Node is an element in the AST.

type NodeType

type NodeType int

NodeType represents an AST Node type.

const (
	// NodeProgram is the program node
	NodeProgram NodeType = iota

	// NodeMustache is the mustache statement node
	NodeMustache

	// NodeBlock is the block statement node
	NodeBlock

	// NodePartial is the partial statement node
	NodePartial

	// NodeContent is the content statement node
	NodeContent

	// NodeComment is the comment statement node
	NodeComment

	// NodeExpression is the expression node
	NodeExpression

	// NodeSubExpression is the subexpression node
	NodeSubExpression

	// NodePath is the expression path node
	NodePath

	// NodeBoolean is the literal boolean node
	NodeBoolean

	// NodeNumber is the literal number node
	NodeNumber

	// NodeString is the literal string node
	NodeString

	// NodeHash is the hash node
	NodeHash

	// NodeHashPair is the hash pair node
	NodeHashPair
)

func (NodeType) Type

func (t NodeType) Type() NodeType

Type returns itself, and permits struct includers to satisfy that part of Node interface.

type NumberLiteral

type NumberLiteral struct {
	NodeType
	Loc

	Value    float64
	IsInt    bool
	Original string
}

NumberLiteral represents a number node.

func NewNumberLiteral

func NewNumberLiteral(pos int, line int, val float64, isInt bool, original string) *NumberLiteral

NewNumberLiteral instanciates a new number node.

func (*NumberLiteral) Accept

func (node *NumberLiteral) Accept(visitor Visitor) interface{}

Accept is the receiver entry point for visitors.

func (*NumberLiteral) Canonical

func (node *NumberLiteral) Canonical() string

Canonical returns the canonical form of number node as a string (eg: "12", "-1.51").

func (*NumberLiteral) Number

func (node *NumberLiteral) Number() interface{}

Number returns an integer or a float.

func (*NumberLiteral) String

func (node *NumberLiteral) String() string

String returns a string representation of receiver that can be used for debugging.

type PartialStatement

type PartialStatement struct {
	NodeType
	Loc

	Name   Node   // PathExpression | SubExpression
	Params []Node // [ Expression ... ]
	Hash   *Hash

	// whitespace management
	Strip  *Strip
	Indent string
}

PartialStatement represents a partial node.

func NewPartialStatement

func NewPartialStatement(pos int, line int) *PartialStatement

NewPartialStatement instanciates a new partial node.

func (*PartialStatement) Accept

func (node *PartialStatement) Accept(visitor Visitor) interface{}

Accept is the receiver entry point for visitors.

func (*PartialStatement) String

func (node *PartialStatement) String() string

String returns a string representation of receiver that can be used for debugging.

type PathExpression

type PathExpression struct {
	NodeType
	Loc

	Original string
	Depth    int
	Parts    []string
	Data     bool
	Scoped   bool
}

PathExpression represents a path expression node.

func NewPathExpression

func NewPathExpression(pos int, line int, data bool) *PathExpression

NewPathExpression instanciates a new path expression node.

func (*PathExpression) Accept

func (node *PathExpression) Accept(visitor Visitor) interface{}

Accept is the receiver entry point for visitors.

func (*PathExpression) IsDataRoot

func (node *PathExpression) IsDataRoot() bool

IsDataRoot returns true if path expression is @root.

func (*PathExpression) Part

func (node *PathExpression) Part(part string)

Part adds path part.

func (*PathExpression) Sep

func (node *PathExpression) Sep(separator string)

Sep adds path separator.

func (*PathExpression) String

func (node *PathExpression) String() string

String returns a string representation of receiver that can be used for debugging.

type Program

type Program struct {
	NodeType
	Loc

	Body        []Node // [ Statement ... ]
	BlockParams []string
	Chained     bool

	// whitespace management
	Strip *Strip
}

Program represents a program node.

func NewProgram

func NewProgram(pos int, line int) *Program

NewProgram instanciates a new program node.

func (*Program) Accept

func (node *Program) Accept(visitor Visitor) interface{}

Accept is the receiver entry point for visitors.

func (*Program) AddStatement

func (node *Program) AddStatement(statement Node)

AddStatement adds given statement to program.

func (*Program) String

func (node *Program) String() string

String returns a string representation of receiver that can be used for debugging.

type StringLiteral

type StringLiteral struct {
	NodeType
	Loc

	Value string
}

StringLiteral represents a string node.

func NewStringLiteral

func NewStringLiteral(pos int, line int, val string) *StringLiteral

NewStringLiteral instanciates a new string node.

func (*StringLiteral) Accept

func (node *StringLiteral) Accept(visitor Visitor) interface{}

Accept is the receiver entry point for visitors.

func (*StringLiteral) String

func (node *StringLiteral) String() string

String returns a string representation of receiver that can be used for debugging.

type Strip

type Strip struct {
	Open  bool
	Close bool

	OpenStandalone   bool
	CloseStandalone  bool
	InlineStandalone bool
}

Strip describes node whitespace management.

func NewStrip

func NewStrip(openStr, closeStr string) *Strip

NewStrip instanciates a Strip for given open and close mustaches.

func NewStripForStr

func NewStripForStr(str string) *Strip

NewStripForStr instanciates a Strip for given tag.

func (*Strip) String

func (s *Strip) String() string

String returns a string representation of receiver that can be used for debugging.

type SubExpression

type SubExpression struct {
	NodeType
	Loc

	Expression *Expression
}

SubExpression represents a subexpression node.

func NewSubExpression

func NewSubExpression(pos int, line int) *SubExpression

NewSubExpression instanciates a new subexpression node.

func (*SubExpression) Accept

func (node *SubExpression) Accept(visitor Visitor) interface{}

Accept is the receiver entry point for visitors.

func (*SubExpression) String

func (node *SubExpression) String() string

String returns a string representation of receiver that can be used for debugging.

type Visitor

type Visitor interface {
	VisitProgram(*Program) interface{}

	// statements
	VisitMustache(*MustacheStatement) interface{}
	VisitBlock(*BlockStatement) interface{}
	VisitPartial(*PartialStatement) interface{}
	VisitContent(*ContentStatement) interface{}
	VisitComment(*CommentStatement) interface{}

	// expressions
	VisitExpression(*Expression) interface{}
	VisitSubExpression(*SubExpression) interface{}
	VisitPath(*PathExpression) interface{}

	// literals
	VisitString(*StringLiteral) interface{}
	VisitBoolean(*BooleanLiteral) interface{}
	VisitNumber(*NumberLiteral) interface{}

	// miscellaneous
	VisitHash(*Hash) interface{}
	VisitHashPair(*HashPair) interface{}
}

Visitor is the interface to visit an AST.

Jump to

Keyboard shortcuts

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