ast

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Feb 24, 2020 License: BSD-3-Clause Imports: 4 Imported by: 20

Documentation

Overview

Package ast defines the abstract syntax tree for the PEG grammar.

The parser generator's PEG grammar generates a tree using this package that is then converted by the builder to the simplified AST used in the generated parser.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Inspect added in v1.1.0

func Inspect(expr Expression, f func(Expression) bool)

Inspect traverses an AST in depth-first order: It starts by calling f(expr); expr must not be nil. If f returns true, Inspect invokes f recursively for each of the non-nil children of expr, followed by a call of f(nil).

func Optimize added in v1.1.0

func Optimize(g *Grammar, alternateEntrypoints ...string)

Optimize walks a given grammar and optimizes the grammar in regards of parsing performance. This is done with several optimizations:

  • removal of unreferenced rules
  • replace rule references with a copy of the referenced Rule, if the referenced rule it self has no references.
  • resolve nested choice expressions
  • resolve choice expressions with only one alternative
  • resolve nested sequences expression
  • resolve sequence expressions with only one element
  • combine character class matcher and literal matcher, where possible

func Walk added in v1.1.0

func Walk(v Visitor, expr Expression)

Walk traverses an AST in depth-first order: It starts by calling v.Visit(expr); Expression must not be nil. If the visitor w returned by v.Visit(expr) is not nil, Walk is invoked recursively with visitor w for each of the non-nil children of Expression, followed by a call of w.Visit(nil).

Types

type ActionExpr

type ActionExpr struct {
	Expr   Expression
	Code   *CodeBlock
	FuncIx int
	// contains filtered or unexported fields
}

ActionExpr is an expression that has an associated block of code to execute when the expression matches.

func NewActionExpr

func NewActionExpr(p Pos) *ActionExpr

NewActionExpr creates a new action expression at the specified position.

func (*ActionExpr) Pos

func (a *ActionExpr) Pos() Pos

Pos returns the starting position of the node.

func (*ActionExpr) String

func (a *ActionExpr) String() string

String returns the textual representation of a node.

type AndCodeExpr

type AndCodeExpr struct {
	Code   *CodeBlock
	FuncIx int
	// contains filtered or unexported fields
}

AndCodeExpr is a zero-length matcher that is considered a match if the code block returns true.

func NewAndCodeExpr

func NewAndCodeExpr(p Pos) *AndCodeExpr

NewAndCodeExpr creates a new and (&) code expression at the specified position.

func (*AndCodeExpr) Pos

func (a *AndCodeExpr) Pos() Pos

Pos returns the starting position of the node.

func (*AndCodeExpr) String

func (a *AndCodeExpr) String() string

String returns the textual representation of a node.

type AndExpr

type AndExpr struct {
	Expr Expression
	// contains filtered or unexported fields
}

AndExpr is a zero-length matcher that is considered a match if the expression it contains is a match.

func NewAndExpr

func NewAndExpr(p Pos) *AndExpr

NewAndExpr creates a new and (&) expression at the specified position.

func (*AndExpr) Pos

func (a *AndExpr) Pos() Pos

Pos returns the starting position of the node.

func (*AndExpr) String

func (a *AndExpr) String() string

String returns the textual representation of a node.

type AnyMatcher

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

AnyMatcher is a matcher that matches any character except end-of-file.

func NewAnyMatcher

func NewAnyMatcher(p Pos, v string) *AnyMatcher

NewAnyMatcher creates a new any matcher at the specified position. The value is provided for completeness' sake, but it is always the dot.

func (*AnyMatcher) Pos

func (a *AnyMatcher) Pos() Pos

Pos returns the starting position of the node.

func (*AnyMatcher) String

func (a *AnyMatcher) String() string

String returns the textual representation of a node.

type CharClassMatcher

type CharClassMatcher struct {
	IgnoreCase     bool
	Inverted       bool
	Chars          []rune
	Ranges         []rune // pairs of low/high range
	UnicodeClasses []string
	// contains filtered or unexported fields
}

CharClassMatcher is a character class matcher. The value to match must be one of the specified characters, in a range of characters, or in the Unicode classes of characters.

func NewCharClassMatcher

func NewCharClassMatcher(p Pos, raw string) *CharClassMatcher

NewCharClassMatcher creates a new character class matcher at the specified position and with the specified raw value. It parses the raw value into the list of characters, ranges and Unicode classes.

func (*CharClassMatcher) Pos

func (c *CharClassMatcher) Pos() Pos

Pos returns the starting position of the node.

func (*CharClassMatcher) String

func (c *CharClassMatcher) String() string

String returns the textual representation of a node.

type ChoiceExpr

type ChoiceExpr struct {
	Alternatives []Expression
	// contains filtered or unexported fields
}

ChoiceExpr is an ordered sequence of expressions. The parser tries to match any of the alternatives in sequence and stops at the first one that matches.

func NewChoiceExpr

func NewChoiceExpr(p Pos) *ChoiceExpr

NewChoiceExpr creates a choice expression at the specified position.

func (*ChoiceExpr) Pos

func (c *ChoiceExpr) Pos() Pos

Pos returns the starting position of the node.

func (*ChoiceExpr) String

func (c *ChoiceExpr) String() string

String returns the textual representation of a node.

type CodeBlock

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

CodeBlock represents a code block.

func NewCodeBlock

func NewCodeBlock(p Pos, code string) *CodeBlock

NewCodeBlock creates a new code block at the specified position and with the specified value. The value includes the outer braces.

func (*CodeBlock) Pos

func (c *CodeBlock) Pos() Pos

Pos returns the starting position of the node.

func (*CodeBlock) String

func (c *CodeBlock) String() string

String returns the textual representation of a node.

type Expression

type Expression interface {
	Pos() Pos
}

Expression is the interface implemented by all expression types.

type FailureLabel added in v1.1.0

type FailureLabel string

FailureLabel is an identifier, which can by thrown and recovered in a grammar

type Grammar

type Grammar struct {
	Init  *CodeBlock
	Rules []*Rule
	// contains filtered or unexported fields
}

Grammar is the top-level node of the AST for the PEG grammar.

func NewGrammar

func NewGrammar(p Pos) *Grammar

NewGrammar creates a new grammar at the specified position.

func (*Grammar) Pos

func (g *Grammar) Pos() Pos

Pos returns the starting position of the node.

func (*Grammar) String

func (g *Grammar) String() string

String returns the textual representation of a node.

type Identifier

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

Identifier represents an identifier.

func NewIdentifier

func NewIdentifier(p Pos, name string) *Identifier

NewIdentifier creates a new identifier at the specified position and with the specified name.

func (*Identifier) Pos

func (i *Identifier) Pos() Pos

Pos returns the starting position of the node.

func (*Identifier) String

func (i *Identifier) String() string

String returns the textual representation of a node.

type LabeledExpr

type LabeledExpr struct {
	Label *Identifier
	Expr  Expression
	// contains filtered or unexported fields
}

LabeledExpr is an expression that has an associated label. Code blocks can access the value of the expression using that label, that becomes a local variable in the code.

func NewLabeledExpr

func NewLabeledExpr(p Pos) *LabeledExpr

NewLabeledExpr creates a new labeled expression at the specified position.

func (*LabeledExpr) Pos

func (l *LabeledExpr) Pos() Pos

Pos returns the starting position of the node.

func (*LabeledExpr) String

func (l *LabeledExpr) String() string

String returns the textual representation of a node.

type LitMatcher

type LitMatcher struct {
	IgnoreCase bool
	// contains filtered or unexported fields
}

LitMatcher is a string literal matcher. The value to match may be a double-quoted string, a single-quoted single character, or a back-tick quoted raw string.

func NewLitMatcher

func NewLitMatcher(p Pos, v string) *LitMatcher

NewLitMatcher creates a new literal matcher at the specified position and with the specified value.

func (*LitMatcher) Pos

func (l *LitMatcher) Pos() Pos

Pos returns the starting position of the node.

func (*LitMatcher) String

func (l *LitMatcher) String() string

String returns the textual representation of a node.

type NotCodeExpr

type NotCodeExpr struct {
	Code   *CodeBlock
	FuncIx int
	// contains filtered or unexported fields
}

NotCodeExpr is a zero-length matcher that is considered a match if the code block returns false.

func NewNotCodeExpr

func NewNotCodeExpr(p Pos) *NotCodeExpr

NewNotCodeExpr creates a new not (!) code expression at the specified position.

func (*NotCodeExpr) Pos

func (n *NotCodeExpr) Pos() Pos

Pos returns the starting position of the node.

func (*NotCodeExpr) String

func (n *NotCodeExpr) String() string

String returns the textual representation of a node.

type NotExpr

type NotExpr struct {
	Expr Expression
	// contains filtered or unexported fields
}

NotExpr is a zero-length matcher that is considered a match if the expression it contains is not a match.

func NewNotExpr

func NewNotExpr(p Pos) *NotExpr

NewNotExpr creates a new not (!) expression at the specified position.

func (*NotExpr) Pos

func (n *NotExpr) Pos() Pos

Pos returns the starting position of the node.

func (*NotExpr) String

func (n *NotExpr) String() string

String returns the textual representation of a node.

type OneOrMoreExpr

type OneOrMoreExpr struct {
	Expr Expression
	// contains filtered or unexported fields
}

OneOrMoreExpr is an expression that can be matched one or more times.

func NewOneOrMoreExpr

func NewOneOrMoreExpr(p Pos) *OneOrMoreExpr

NewOneOrMoreExpr creates a new one or more expression at the specified position.

func (*OneOrMoreExpr) Pos

func (o *OneOrMoreExpr) Pos() Pos

Pos returns the starting position of the node.

func (*OneOrMoreExpr) String

func (o *OneOrMoreExpr) String() string

String returns the textual representation of a node.

type Pos

type Pos struct {
	Filename string
	Line     int
	Col      int
	Off      int
}

Pos represents a position in a source file.

func (Pos) String

func (p Pos) String() string

String returns the textual representation of a position.

type RecoveryExpr added in v1.1.0

type RecoveryExpr struct {
	Expr        Expression
	RecoverExpr Expression
	Labels      []FailureLabel
	// contains filtered or unexported fields
}

RecoveryExpr is an ordered sequence of expressions. The parser tries to match any of the alternatives in sequence and stops at the first one that matches.

func NewRecoveryExpr added in v1.1.0

func NewRecoveryExpr(p Pos) *RecoveryExpr

NewRecoveryExpr creates a choice expression at the specified position.

func (*RecoveryExpr) Pos added in v1.1.0

func (r *RecoveryExpr) Pos() Pos

Pos returns the starting position of the node.

func (*RecoveryExpr) String added in v1.1.0

func (r *RecoveryExpr) String() string

String returns the textual representation of a node.

type Rule

type Rule struct {
	Name        *Identifier
	DisplayName *StringLit
	Expr        Expression
	// contains filtered or unexported fields
}

Rule represents a rule in the PEG grammar. It has a name, an optional display name to be used in error messages, and an expression.

func NewRule

func NewRule(p Pos, name *Identifier) *Rule

NewRule creates a rule with at the specified position and with the specified name as identifier.

func (*Rule) Pos

func (r *Rule) Pos() Pos

Pos returns the starting position of the node.

func (*Rule) String

func (r *Rule) String() string

String returns the textual representation of a node.

type RuleRefExpr

type RuleRefExpr struct {
	Name *Identifier
	// contains filtered or unexported fields
}

RuleRefExpr is an expression that references a rule by name.

func NewRuleRefExpr

func NewRuleRefExpr(p Pos) *RuleRefExpr

NewRuleRefExpr creates a new rule reference expression at the specified position.

func (*RuleRefExpr) Pos

func (r *RuleRefExpr) Pos() Pos

Pos returns the starting position of the node.

func (*RuleRefExpr) String

func (r *RuleRefExpr) String() string

String returns the textual representation of a node.

type SeqExpr

type SeqExpr struct {
	Exprs []Expression
	// contains filtered or unexported fields
}

SeqExpr is an ordered sequence of expressions, all of which must match if the SeqExpr is to be a match itself.

func NewSeqExpr

func NewSeqExpr(p Pos) *SeqExpr

NewSeqExpr creates a new sequence expression at the specified position.

func (*SeqExpr) Pos

func (s *SeqExpr) Pos() Pos

Pos returns the starting position of the node.

func (*SeqExpr) String

func (s *SeqExpr) String() string

String returns the textual representation of a node.

type StateCodeExpr added in v1.1.0

type StateCodeExpr struct {
	Code   *CodeBlock
	FuncIx int
	// contains filtered or unexported fields
}

StateCodeExpr is an expression which can modify the internal state of the parser.

func NewStateCodeExpr added in v1.1.0

func NewStateCodeExpr(p Pos) *StateCodeExpr

NewStateCodeExpr creates a new state (#) code expression at the specified position.

func (*StateCodeExpr) Pos added in v1.1.0

func (s *StateCodeExpr) Pos() Pos

Pos returns the starting position of the node.

func (*StateCodeExpr) String added in v1.1.0

func (s *StateCodeExpr) String() string

String returns the textual representation of a node.

type StringLit

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

StringLit represents a string literal.

func NewStringLit

func NewStringLit(p Pos, val string) *StringLit

NewStringLit creates a new string literal at the specified position and with the specified value.

func (*StringLit) Pos

func (s *StringLit) Pos() Pos

Pos returns the starting position of the node.

func (*StringLit) String

func (s *StringLit) String() string

String returns the textual representation of a node.

type ThrowExpr added in v1.1.0

type ThrowExpr struct {
	Label string
	// contains filtered or unexported fields
}

ThrowExpr is an expression that throws an FailureLabel to be catched by a RecoveryChoiceExpr.

func NewThrowExpr added in v1.1.0

func NewThrowExpr(p Pos) *ThrowExpr

NewThrowExpr creates a new throw expression at the specified position.

func (*ThrowExpr) Pos added in v1.1.0

func (t *ThrowExpr) Pos() Pos

Pos returns the starting position of the node.

func (*ThrowExpr) String added in v1.1.0

func (t *ThrowExpr) String() string

String returns the textual representation of a node.

type Visitor added in v1.1.0

type Visitor interface {
	Visit(expr Expression) (w Visitor)
}

A Visitor implements a Visit method, which is invoked for each Expression encountered by Walk. If the result visitor w is not nil, Walk visits each of the children of Expression with the visitor w, followed by a call of w.Visit(nil).

type ZeroOrMoreExpr

type ZeroOrMoreExpr struct {
	Expr Expression
	// contains filtered or unexported fields
}

ZeroOrMoreExpr is an expression that can be matched zero or more times.

func NewZeroOrMoreExpr

func NewZeroOrMoreExpr(p Pos) *ZeroOrMoreExpr

NewZeroOrMoreExpr creates a new zero or more expression at the specified position.

func (*ZeroOrMoreExpr) Pos

func (z *ZeroOrMoreExpr) Pos() Pos

Pos returns the starting position of the node.

func (*ZeroOrMoreExpr) String

func (z *ZeroOrMoreExpr) String() string

String returns the textual representation of a node.

type ZeroOrOneExpr

type ZeroOrOneExpr struct {
	Expr Expression
	// contains filtered or unexported fields
}

ZeroOrOneExpr is an expression that can be matched zero or one time.

func NewZeroOrOneExpr

func NewZeroOrOneExpr(p Pos) *ZeroOrOneExpr

NewZeroOrOneExpr creates a new zero or one expression at the specified position.

func (*ZeroOrOneExpr) Pos

func (z *ZeroOrOneExpr) Pos() Pos

Pos returns the starting position of the node.

func (*ZeroOrOneExpr) String

func (z *ZeroOrOneExpr) String() string

String returns the textual representation of a node.

Jump to

Keyboard shortcuts

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