grammar

package
v0.4.8 Latest Latest
Warning

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

Go to latest
Published: Apr 17, 2024 License: MIT Imports: 22 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Evaluate added in v0.0.2

func Evaluate[T any](expression Expression, context *c.Context) (T, error)

func EvaluatePipeout added in v0.4.1

func EvaluatePipeout[T PipeOutInstance](a T, context *c.Context, writer OutputWriter) (any, error)

func EvaluateToString added in v0.1.1

func EvaluateToString(expression Expression, context *c.Context) (string, error)

func Execute

func Execute(filePath string, context *c.Context, code []byte) (any, error)

func MakeContext

func MakeContext(locals *c.Locals, builtIn c.BuiltIn, system c.System) *c.Context

func MakeDefaultBuiltIn

func MakeDefaultBuiltIn() c.BuiltIn

func Parse

func Parse(filename string, b []byte, opts ...Option) (any, error)

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

func ParseFile

func ParseFile(filename string, opts ...Option) (i any, err error)

ParseFile parses the file identified by filename.

func ParseReader

func ParseReader(filename string, r io.Reader, opts ...Option) (any, error)

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

Types

type Add added in v0.3.6

type Add struct {
	Left  Expression
	Right Expression
}

func (Add) Evaluate added in v0.3.6

func (a Add) Evaluate(context *c.Context) (any, error)

func (Add) String added in v0.3.6

func (v Add) String() string

type Assign

type Assign struct {
	Store Expression
	Value Expression
}

func (Assign) Evaluate

func (a Assign) Evaluate(context *c.Context) (any, error)

func (Assign) String

func (a Assign) String() string

type BindedLambda added in v0.3.11

type BindedLambda struct {
	Lambda  *Lambda
	Context *c.Context
}

func (BindedLambda) Evaluate added in v0.3.11

func (a BindedLambda) Evaluate(context *c.Context) (any, error)

func (BindedLambda) String added in v0.3.11

func (a BindedLambda) String() string

type Block

type Block []Expression

func (Block) Evaluate

func (a Block) Evaluate(context *c.Context) (any, error)

func (Block) String

func (a Block) String() string

type Call

type Call struct {
	Reference Reference
	Arguments NamedExpressionList
}

func (Call) Evaluate

func (a Call) Evaluate(context *c.Context) (any, error)

func (Call) String

func (a Call) String() string

type Cloner

type Cloner interface {
	Clone() any
}

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 Condition

type Condition struct {
	Condition Expression
	True      Expression
	False     Expression
}

func (Condition) Evaluate

func (a Condition) Evaluate(context *c.Context) (any, error)

func (Condition) String

func (a Condition) String() string

type Constant added in v0.0.2

type Constant struct {
	Value any
}

func (Constant) Evaluate added in v0.0.2

func (a Constant) Evaluate(context *c.Context) (any, error)

func (Constant) String added in v0.0.2

func (a Constant) String() string

type Each

type Each struct {
	List Expression
	Body Expression
}

func (Each) Evaluate

func (a Each) Evaluate(context *c.Context) (any, error)

func (Each) String

func (a Each) String() string

type Equal

type Equal struct {
	Left  Expression
	Right Expression
}

func (Equal) Evaluate

func (a Equal) Evaluate(context *c.Context) (any, error)

func (Equal) String

func (v Equal) String() string

type Expression

type Expression interface {
	String() string
	Evaluate(context *c.Context) (any, error)
}

func MakeAdd added in v0.3.6

func MakeAdd(l Expression, r Expression) Expression

func MakeCondition

func MakeCondition(c Expression, t Expression, f Expression) Expression

func MakeConstant added in v0.3.1

func MakeConstant(value any) Expression

func MakeEach

func MakeEach(l Expression, b Expression) Expression

func MakeEqual

func MakeEqual(l Expression, r Expression) Expression

func MakeNot

func MakeNot(s Expression) Expression

func MakePiped added in v0.4.1

func MakePiped[T PipedInstance, U PipedRef[T]](from Expression, to Expression) Expression

type ExpressionList

type ExpressionList []Expression

func (ExpressionList) Evaluate

func (a ExpressionList) Evaluate(context *c.Context) (any, error)

func (ExpressionList) String

func (a ExpressionList) String() string

type ExpressionStore added in v0.4.4

type ExpressionStore c.Store

func MakeExpressionStore added in v0.4.4

func MakeExpressionStore(a NamedExpressionList) (ExpressionStore, error)

func (ExpressionStore) Evaluate added in v0.4.4

func (a ExpressionStore) Evaluate(context *c.Context) (any, error)

func (ExpressionStore) String added in v0.4.4

func (a ExpressionStore) String() string

type Expressions

type Expressions []Expression

func (Expressions) Evaluate

func (a Expressions) Evaluate(context *c.Context) (any, error)

func (Expressions) String

func (a Expressions) String() string

type FormatData added in v0.0.2

type FormatData struct {
	Format Expression
	Data   Expression
}

func (FormatData) Evaluate added in v0.0.2

func (a FormatData) Evaluate(context *c.Context) (any, error)

func (FormatData) String added in v0.0.2

func (v FormatData) String() string

type IdentifierList

type IdentifierList struct {
	Identifiers []string
}

func (IdentifierList) Evaluate

func (a IdentifierList) Evaluate(context *c.Context) (any, error)

func (IdentifierList) String

func (a IdentifierList) String() string

type Include

type Include struct {
	FileName Expression
}

func (Include) Evaluate

func (a Include) Evaluate(context *c.Context) (any, error)

func (Include) String

func (a Include) String() string

type Invoke

type Invoke struct {
	Expressions Expressions
}

func (Invoke) Evaluate

func (a Invoke) Evaluate(context *c.Context) (any, error)

func (Invoke) String

func (a Invoke) String() string

type Lambda

type Lambda struct {
	Arguments IdentifierList
	Body      Expression
}

func (Lambda) Evaluate

func (a Lambda) Evaluate(context *c.Context) (any, error)

func (Lambda) String

func (a Lambda) String() string

type Literal

type Literal struct {
	Value any
}

func MakeLiteral

func MakeLiteral(s string) Literal

func (Literal) Evaluate

func (v Literal) Evaluate(context *c.Context) (any, error)

func (Literal) String

func (v Literal) String() string

type LiteralList

type LiteralList []Literal

func MakeLiteralList

func MakeLiteralList(s []string) LiteralList

func (LiteralList) Evaluate

func (v LiteralList) Evaluate(context *c.Context) (any, error)

func (LiteralList) String

func (v LiteralList) String() string

type Member

type Member struct {
	Identifier string
}

func (Member) Evaluate

func (a Member) Evaluate(context *c.Context) (any, error)

func (Member) String

func (a Member) String() string

type NamedExpression added in v0.4.2

type NamedExpression c.KeyValue[string, Expression]

func (NamedExpression) Evaluate added in v0.4.2

func (a NamedExpression) Evaluate(context *c.Context) (any, error)

func (NamedExpression) String added in v0.4.2

func (a NamedExpression) String() string

type NamedExpressionList added in v0.4.2

type NamedExpressionList []NamedExpression

func (NamedExpressionList) Evaluate added in v0.4.2

func (a NamedExpressionList) Evaluate(context *c.Context) (any, error)

func (NamedExpressionList) String added in v0.4.2

func (a NamedExpressionList) String() string

type Not

type Not struct {
	Expression Expression
}

func (Not) Evaluate

func (a Not) Evaluate(context *c.Context) (any, error)

func (Not) String

func (v Not) String() 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 any) Option

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

func InitState

func InitState(key string, value any) 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 OutputCapture added in v0.4.3

type OutputCapture Expressions

func (OutputCapture) Evaluate added in v0.4.3

func (a OutputCapture) Evaluate(context *c.Context) (any, error)

func (OutputCapture) String added in v0.4.3

func (a OutputCapture) String() string

type OutputWriter added in v0.4.1

type OutputWriter func(fname, content string) error

func Writer added in v0.4.1

func Writer(mode int) OutputWriter

type ParseData added in v0.0.2

type ParseData struct {
	Format Expression
	Data   Expression
}

func (ParseData) Evaluate added in v0.0.2

func (a ParseData) Evaluate(context *c.Context) (any, error)

func (ParseData) String added in v0.0.2

func (v ParseData) String() string

type ParsedString

type ParsedString struct {
	Quote   string
	RawBody string
}

func MakeParsedString

func MakeParsedString(quote string, body string) ParsedString

func (ParsedString) Evaluate

func (a ParsedString) Evaluate(context *c.Context) (any, error)

func (ParsedString) String

func (a ParsedString) String() string

func (ParsedString) Unquote

func (a ParsedString) Unquote() string

type Pipe added in v0.3.5

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

func (Pipe) Evaluate added in v0.3.5

func (a Pipe) Evaluate(context *c.Context) (any, error)

func (*Pipe) From added in v0.3.5

func (a *Pipe) From() Ref[Expression]

func (Pipe) String added in v0.3.5

func (a Pipe) String() string

func (*Pipe) To added in v0.3.5

func (a *Pipe) To() Ref[Expression]

type PipeAppend added in v0.4.1

type PipeAppend Pipe

func (PipeAppend) Evaluate added in v0.4.1

func (a PipeAppend) Evaluate(context *c.Context) (any, error)

func (*PipeAppend) From added in v0.4.1

func (a *PipeAppend) From() Ref[Expression]

func (PipeAppend) String added in v0.4.1

func (a PipeAppend) String() string

func (*PipeAppend) To added in v0.4.1

func (a *PipeAppend) To() Ref[Expression]

type PipeIn added in v0.4.1

type PipeIn Pipe

func (PipeIn) Evaluate added in v0.4.1

func (a PipeIn) Evaluate(context *c.Context) (any, error)

func (*PipeIn) From added in v0.4.1

func (a *PipeIn) From() Ref[Expression]

func (PipeIn) String added in v0.4.1

func (a PipeIn) String() string

func (*PipeIn) To added in v0.4.1

func (a *PipeIn) To() Ref[Expression]

type PipeOut added in v0.4.1

type PipeOut Pipe

func (PipeOut) Evaluate added in v0.4.1

func (a PipeOut) Evaluate(context *c.Context) (any, error)

func (*PipeOut) From added in v0.4.1

func (a *PipeOut) From() Ref[Expression]

func (PipeOut) String added in v0.4.1

func (a PipeOut) String() string

func (*PipeOut) To added in v0.4.1

func (a *PipeOut) To() Ref[Expression]

type PipeOutInstance added in v0.4.1

type PipeOutInstance interface {
	*PipeOut | *PipeAppend
	Piped
}

type Piped added in v0.4.1

type Piped interface {
	From() Ref[Expression]
	To() Ref[Expression]
}

type PipedInstance added in v0.4.1

type PipedInstance interface {
	Pipe | PipeIn | PipeOut | PipeAppend
	Expression
}

type PipedRef added in v0.4.1

type PipedRef[T any] interface {
	*T
	Piped
}

type PointerValue added in v0.4.1

type PointerValue[T any] struct {
	// contains filtered or unexported fields
}

func MakeRef added in v0.4.1

func MakeRef[T any](v *T) *PointerValue[T]

func (*PointerValue[T]) Get added in v0.4.1

func (a *PointerValue[T]) Get() *T

func (*PointerValue[T]) Set added in v0.4.1

func (a *PointerValue[T]) Set(v *T) *T

type Ref added in v0.4.1

type Ref[T any] interface {
	Get() *T
	Set(v *T) *T
}

type Reference

type Reference struct {
	Expression Expression
}

func (Reference) Evaluate

func (a Reference) Evaluate(context *c.Context) (any, error)

func (Reference) String

func (a Reference) String() string

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 StoreAccess

type StoreAccess struct {
	Reference Expression
	Index     Expression
}

func (StoreAccess) Evaluate

func (a StoreAccess) Evaluate(context *c.Context) (any, error)

func (StoreAccess) String

func (a StoreAccess) String() string

type StoreReference

type StoreReference struct {
	Store     c.Store
	Reference string
}

func (StoreReference) Get

func (a StoreReference) Get() any

func (StoreReference) Set

func (a StoreReference) Set(v any) any

type Template added in v0.1.1

type Template struct {
	Store     StoreAccess
	Arguments IdentifierList
	Value     Expression
	Text      string
}

func MakeTemplate added in v0.1.1

func MakeTemplate(store StoreAccess, args IdentifierList, body string) Template

func (Template) Evaluate added in v0.1.1

func (a Template) Evaluate(context *c.Context) (any, error)

func (Template) String added in v0.1.1

func (a Template) String() string

type Value

type Value interface {
	Get() any
	Set(v any) any
}

type Write

type Write struct {
	Expression Expressions
}

func (Write) Evaluate

func (a Write) Evaluate(context *c.Context) (any, error)

func (Write) String

func (a Write) String() string

type WriteLn

type WriteLn Write

func (WriteLn) Evaluate

func (a WriteLn) Evaluate(context *c.Context) (any, error)

func (WriteLn) String

func (a WriteLn) String() string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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