compiler

package
v0.0.0-...-46dcc07 Latest Latest
Warning

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

Go to latest
Published: Jul 5, 2023 License: MIT Imports: 19 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Parse

func Parse(filename string, b []byte, opts ...Option) (interface{}, error)

Parse parses the data from b using filename as information in the error messages.

func ParseFile

func ParseFile(filename string, opts ...Option) (i interface{}, err error)

ParseFile parses the file identified by filename.

func ParseReader

func ParseReader(filename string, r io.Reader, opts ...Option) (interface{}, error)

ParseReader parses the data from r using filename as information in the error messages.

Types

type ArrayExpression

type ArrayExpression struct {
	*GraphNode
	Expressions []Expression
}

func (*ArrayExpression) Compile

func (n *ArrayExpression) Compile(w Context, parent Node) (err error)

func (ArrayExpression) RawValue

func (s ArrayExpression) RawValue(w Context, parent Node) *string

type Assignment

type Assignment struct {
	*GraphNode
	Variable   *Variable
	Expression Expression
}

func (*Assignment) Compile

func (n *Assignment) Compile(w Context, parent Node) (err error)

type Attribute

type Attribute struct {
	*GraphNode
	Name      string
	Value     Expression
	Unescaped bool
}

func (*Attribute) Compile

func (n *Attribute) Compile(w Context, parent Node) (err error)

type BinaryExpression

type BinaryExpression struct {
	*GraphNode
	Op string
	X  Expression
	Y  Expression
}

func (*BinaryExpression) Compile

func (n *BinaryExpression) Compile(w Context, parent Node) (err error)

func (BinaryExpression) RawValue

func (s BinaryExpression) RawValue(w Context, parent Node) *string

type Block

type Block struct {
	*GraphNode
	ParentBlock *Block
	Name        string
	Modifier    string
	GlobalName  string
	Block       Node
}

func (*Block) Compile

func (n *Block) Compile(w Context, parent Node) (err error)

type BooleanExpression

type BooleanExpression struct {
	*GraphNode
	Value bool
}

func (*BooleanExpression) Compile

func (n *BooleanExpression) Compile(w Context, parent Node) (err error)

func (BooleanExpression) RawValue

func (s BooleanExpression) RawValue(w Context, parent Node) *string

type Cloner

type Cloner interface {
	Clone() interface{}
}

Cloner is implemented by any value that has a Clone method, which returns a copy of the value. This is mainly used for types which are not passed by value (e.g map, slice, chan) or structs that contain such types.

This is used in conjunction with the global state feature to create proper copies of the state to allow the parser to properly restore the state in the case of backtracking.

type Comment

type Comment struct {
	*GraphNode
	Value  string
	Silent bool
}

func (*Comment) Compile

func (n *Comment) Compile(w Context, parent Node) (err error)

type Context

type Context interface {
	ParseFile(name string) (*Root, error)
	ParseReader(name string, reader io.Reader) (*Root, error)
	ReadFile(name string) (data []byte, templateName string, err error)
	CompileFile(name string) (string, error)
	Compile(root *Root) (_ string, err error)

	String() string
	WriteTo(io.Writer) (int64, error)
	FileExtension() string
	// contains filtered or unexported methods
}

func NewContext

func NewContext(finder FileFinder, indentString string) Context

type Define

type Define struct {
	*GraphNode
	Name   string
	Args   []string
	Tpl    string
	Hidden bool
}

func (*Define) Compile

func (n *Define) Compile(w Context, parent Node) (err error)

type Dir

type Dir interface {
	Open(string) (io.Reader, error)
}

A Dir implements FileSystem using the native file system restricted to a specific directory tree. While the FileSystem.Open method takes '/'-separated paths, a Dir's string value is a filename on the native file system, not a URL, so it is separated by filepath.Separator, which isn't necessarily '/'.

type DocType

type DocType struct {
	*GraphNode
	Value string
}

func (*DocType) Compile

func (n *DocType) Compile(w Context, parent Node) (err error)

type Each

type Each struct {
	*GraphNode
	IndexVariable   *Variable
	ElementVariable *Variable
	Container       Expression
	Block           Node
}

func (*Each) Compile

func (n *Each) Compile(w Context, parent Node) (err error)

type Expression

type Expression interface {
	Node
	RawValue(w Context, parent Node) *string
}

type Extend

type Extend struct {
	*GraphNode
	File    string
	Handled bool
}

func (*Extend) Compile

func (n *Extend) Compile(w Context, parent Node) (err error)

type FieldExpression

type FieldExpression struct {
	*GraphNode
	Variable *Variable
}

func (*FieldExpression) Compile

func (n *FieldExpression) Compile(w Context, parent Node) (err error)

func (FieldExpression) RawValue

func (s FieldExpression) RawValue(w Context, parent Node) *string

type FileFindWalker

type FileFindWalker interface {
	Walk(accept func(path string) bool, do func(f iofs.File, templateName string) error) error
}

type FileFinder

type FileFinder interface {
	Find(name string) (f iofs.File, templateName string, err error)
}

type FinderFS

type FinderFS struct {
	FS iofs.FS
}

func NewFinderFS

func NewFinderFS(FS iofs.FS) *FinderFS

func (FinderFS) Find

func (ff FinderFS) Find(name string) (f iofs.File, templateName string, err error)

func (FinderFS) Walk

func (ff FinderFS) Walk(accept func(path string) bool, do func(f iofs.File, templateName string) error) error

type FloatExpression

type FloatExpression struct {
	*GraphNode
	Value float64
}

func (*FloatExpression) Compile

func (n *FloatExpression) Compile(w Context, parent Node) (err error)

func (FloatExpression) RawValue

func (s FloatExpression) RawValue(w Context, parent Node) *string

type FsDir

type FsDir string

Exposes operating system filesystem

func (FsDir) Open

func (dir FsDir) Open(fp string) (io.Reader, error)

type FunctionCallExpression

type FunctionCallExpression struct {
	*GraphNode
	X         Expression
	Arguments []Expression
}

func (*FunctionCallExpression) Compile

func (n *FunctionCallExpression) Compile(w Context, parent Node) (err error)

func (FunctionCallExpression) RawValue

func (s FunctionCallExpression) RawValue(w Context, parent Node) *string

type GraphNode

type GraphNode struct {
	Position
	Parent Node
	Scope  *Scope
}

func NewNode

func NewNode(pos Position) *GraphNode

func (*GraphNode) Compile

func (n *GraphNode) Compile(w Context, parent Node) (err error)

type If

type If struct {
	*GraphNode
	PositiveBlock Node
	NegativeBlock Node
	Condition     Expression
}

func (*If) Compile

func (n *If) Compile(w Context, parent Node) (err error)

type Import

type Import struct {
	*GraphNode
	File string
}

func (*Import) Compile

func (n *Import) Compile(w Context, parent Node) (err error)

type IndexExpression

type IndexExpression struct {
	*GraphNode
	X     Expression
	Index Expression
}

func (*IndexExpression) Compile

func (n *IndexExpression) Compile(w Context, parent Node) (err error)

func (IndexExpression) RawValue

func (s IndexExpression) RawValue(w Context, parent Node) *string

type IntegerExpression

type IntegerExpression struct {
	*GraphNode
	Value int64
}

func (*IntegerExpression) Compile

func (n *IntegerExpression) Compile(w Context, parent Node) (err error)

func (IntegerExpression) RawValue

func (s IntegerExpression) RawValue(w Context, parent Node) *string

type Interpolation

type Interpolation struct {
	*GraphNode
	Expr      Expression
	Unescaped bool
}

func (Interpolation) Compile

func (n Interpolation) Compile(w Context, parent Node) (err error)

type List

type List struct {
	*GraphNode
	Nodes   []Node
	Append  *List
	Prepend *List
}

func (*List) Compile

func (n *List) Compile(w Context, parent Node) (err error)

type MemberExpression

type MemberExpression struct {
	*GraphNode
	X    Expression
	Name string
}

func (*MemberExpression) Compile

func (n *MemberExpression) Compile(w Context, parent Node) (err error)

func (MemberExpression) RawValue

func (s MemberExpression) RawValue(w Context, parent Node) *string

type Mixin

type Mixin struct {
	*GraphNode
	Name      string
	Arguments []MixinArgument
	Block     Node
}

func (*Mixin) Compile

func (n *Mixin) Compile(w Context, parent Node) (err error)

type MixinArgument

type MixinArgument struct {
	*GraphNode
	Name    *Variable
	Default Expression
}

type MixinCall

type MixinCall struct {
	*GraphNode
	Name      string
	Arguments []Expression
}

func (*MixinCall) Compile

func (n *MixinCall) Compile(w Context, parent Node) (err error)

type NilExpression

type NilExpression struct {
	*GraphNode
}

func (*NilExpression) Compile

func (n *NilExpression) Compile(w Context, parent Node) (err error)

func (NilExpression) RawValue

func (s NilExpression) RawValue(w Context, parent Node) *string

type Node

type Node interface {
	Compile(w Context, parent Node) error
	// contains filtered or unexported methods
}

type ObjectExpression

type ObjectExpression struct {
	*GraphNode
	Expressions map[string]Expression
}

func (*ObjectExpression) Compile

func (n *ObjectExpression) Compile(w Context, parent Node) (err error)

func (ObjectExpression) RawValue

func (s ObjectExpression) RawValue(w Context, parent Node) *string

type Option

type Option func(*parser) Option

Option is a function that can set an option on the parser. It returns the previous setting as an Option.

func AllowInvalidUTF8

func AllowInvalidUTF8(b bool) Option

AllowInvalidUTF8 creates an Option to allow invalid UTF-8 bytes. Every invalid UTF-8 byte is treated as a utf8.RuneError (U+FFFD) by character class matchers and is matched by the any matcher. The returned matched value, c.text and c.offset are NOT affected.

The default is false.

func Debug

func Debug(b bool) Option

Debug creates an Option to set the debug flag to b. When set to true, debugging information is printed to stdout while parsing.

The default is false.

func Entrypoint

func Entrypoint(ruleName string) Option

Entrypoint creates an Option to set the rule name to use as entrypoint. The rule name must have been specified in the -alternate-entrypoints if generating the parser with the -optimize-grammar flag, otherwise it may have been optimized out. Passing an empty string sets the entrypoint to the first rule in the grammar.

The default is to start parsing at the first rule in the grammar.

func GlobalStore

func GlobalStore(key string, value interface{}) Option

GlobalStore creates an Option to set a key to a certain value in the globalStore.

func InitState

func InitState(key string, value interface{}) Option

InitState creates an Option to set a key to a certain value in the global "state" store.

func MaxExpressions

func MaxExpressions(maxExprCnt uint64) Option

MaxExpressions creates an Option to stop parsing after the provided number of expressions have been parsed, if the value is 0 then the parser will parse for as many steps as needed (possibly an infinite number).

The default for maxExprCnt is 0.

func Memoize

func Memoize(b bool) Option

Memoize creates an Option to set the memoize flag to b. When set to true, the parser will cache all results so each expression is evaluated only once. This guarantees linear parsing time even for pathological cases, at the expense of more memory and slower times for typical cases.

The default is false.

func Recover

func Recover(b bool) Option

Recover creates an Option to set the recover flag to b. When set to true, this causes the parser to recover from panics and convert it to an error. Setting it to false can be useful while debugging to access the full stack trace.

The default is true.

func Statistics

func Statistics(stats *Stats, choiceNoMatch string) Option

Statistics adds a user provided Stats struct to the parser to allow the user to process the results after the parsing has finished. Also the key for the "no match" counter is set.

Example usage:

input := "input"
stats := Stats{}
_, err := Parse("input-file", []byte(input), Statistics(&stats, "no match"))
if err != nil {
    log.Panicln(err)
}
b, err := json.MarshalIndent(stats.ChoiceAltCnt, "", "  ")
if err != nil {
    log.Panicln(err)
}
fmt.Println(string(b))

type Position

type Position struct {
	Line   int
	Col    int
	Offset int
}

type Root

type Root struct {
	*GraphNode
	Extends  *Root
	List     *List
	Filename string
}

func (*Root) Compile

func (n *Root) Compile(w Context, parent Node) (err error)

type Scope

type Scope struct {
	Owner     *List
	Variables map[string]*Variable
	Node      Node
}

type Stats

type Stats struct {
	// ExprCnt counts the number of expressions processed during parsing
	// This value is compared to the maximum number of expressions allowed
	// (set by the MaxExpressions option).
	ExprCnt uint64

	// ChoiceAltCnt is used to count for each ordered choice expression,
	// which alternative is used how may times.
	// These numbers allow to optimize the order of the ordered choice expression
	// to increase the performance of the parser
	//
	// The outer key of ChoiceAltCnt is composed of the name of the rule as well
	// as the line and the column of the ordered choice.
	// The inner key of ChoiceAltCnt is the number (one-based) of the matching alternative.
	// For each alternative the number of matches are counted. If an ordered choice does not
	// match, a special counter is incremented. The name of this counter is set with
	// the parser option Statistics.
	// For an alternative to be included in ChoiceAltCnt, it has to match at least once.
	ChoiceAltCnt map[string]map[string]int
}

Stats stores some statistics, gathered during parsing

type StringExpression

type StringExpression struct {
	*GraphNode
	Value string
}

func (*StringExpression) Compile

func (n *StringExpression) Compile(w Context, parent Node) (err error)

func (StringExpression) RawValue

func (s StringExpression) RawValue(w Context, parent Node) *string

type StringInputDir

type StringInputDir string

Exposes a string as an unnamed file

func (StringInputDir) Open

func (dir StringInputDir) Open(path string) (io.Reader, error)

type Tag

type Tag struct {
	*GraphNode
	Block      Node
	Text       *TextList
	Name       string
	Raw        bool
	SelfClose  bool
	Attributes []*Attribute
}

func (*Tag) Compile

func (n *Tag) Compile(w Context, parent Node) (err error)

type Text

type Text struct {
	*GraphNode
	Value string
}

func (*Text) Compile

func (n *Text) Compile(w Context, parent Node) (err error)

type TextList

type TextList struct {
	*GraphNode
	Nodes []Node
}

func (*TextList) Compile

func (n *TextList) Compile(w Context, parent Node) (err error)

type UnaryExpression

type UnaryExpression struct {
	*GraphNode
	Op string
	X  Expression
}

func (*UnaryExpression) Compile

func (n *UnaryExpression) Compile(w Context, parent Node) (err error)

func (UnaryExpression) RawValue

func (s UnaryExpression) RawValue(w Context, parent Node) *string

type Variable

type Variable struct {
	*GraphNode
	Name string
}

Jump to

Keyboard shortcuts

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