pigeon: github.com/mna/pigeon/ast Index | Files

package ast

import "github.com/mna/pigeon/ast"

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

Package Files

ast.go ast_optimize.go ast_walk.go

func Inspect Uses

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 Uses

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 Uses

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).

type ActionExpr Uses

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 Uses

func NewActionExpr(p Pos) *ActionExpr

NewActionExpr creates a new action expression at the specified position.

func (*ActionExpr) Pos Uses

func (a *ActionExpr) Pos() Pos

Pos returns the starting position of the node.

func (*ActionExpr) String Uses

func (a *ActionExpr) String() string

String returns the textual representation of a node.

type AndCodeExpr Uses

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 Uses

func NewAndCodeExpr(p Pos) *AndCodeExpr

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

func (*AndCodeExpr) Pos Uses

func (a *AndCodeExpr) Pos() Pos

Pos returns the starting position of the node.

func (*AndCodeExpr) String Uses

func (a *AndCodeExpr) String() string

String returns the textual representation of a node.

type AndExpr Uses

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 Uses

func NewAndExpr(p Pos) *AndExpr

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

func (*AndExpr) Pos Uses

func (a *AndExpr) Pos() Pos

Pos returns the starting position of the node.

func (*AndExpr) String Uses

func (a *AndExpr) String() string

String returns the textual representation of a node.

type AnyMatcher Uses

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

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

func NewAnyMatcher Uses

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 Uses

func (a *AnyMatcher) Pos() Pos

Pos returns the starting position of the node.

func (*AnyMatcher) String Uses

func (a *AnyMatcher) String() string

String returns the textual representation of a node.

type CharClassMatcher Uses

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 Uses

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 Uses

func (c *CharClassMatcher) Pos() Pos

Pos returns the starting position of the node.

func (*CharClassMatcher) String Uses

func (c *CharClassMatcher) String() string

String returns the textual representation of a node.

type ChoiceExpr Uses

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 Uses

func NewChoiceExpr(p Pos) *ChoiceExpr

NewChoiceExpr creates a choice expression at the specified position.

func (*ChoiceExpr) Pos Uses

func (c *ChoiceExpr) Pos() Pos

Pos returns the starting position of the node.

func (*ChoiceExpr) String Uses

func (c *ChoiceExpr) String() string

String returns the textual representation of a node.

type CodeBlock Uses

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

CodeBlock represents a code block.

func NewCodeBlock Uses

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 Uses

func (c *CodeBlock) Pos() Pos

Pos returns the starting position of the node.

func (*CodeBlock) String Uses

func (c *CodeBlock) String() string

String returns the textual representation of a node.

type Expression Uses

type Expression interface {
    Pos() Pos
}

Expression is the interface implemented by all expression types.

type FailureLabel Uses

type FailureLabel string

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

type Grammar Uses

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 Uses

func NewGrammar(p Pos) *Grammar

NewGrammar creates a new grammar at the specified position.

func (*Grammar) Pos Uses

func (g *Grammar) Pos() Pos

Pos returns the starting position of the node.

func (*Grammar) String Uses

func (g *Grammar) String() string

String returns the textual representation of a node.

type Identifier Uses

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

Identifier represents an identifier.

func NewIdentifier Uses

func NewIdentifier(p Pos, name string) *Identifier

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

func (*Identifier) Pos Uses

func (i *Identifier) Pos() Pos

Pos returns the starting position of the node.

func (*Identifier) String Uses

func (i *Identifier) String() string

String returns the textual representation of a node.

type LabeledExpr Uses

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 Uses

func NewLabeledExpr(p Pos) *LabeledExpr

NewLabeledExpr creates a new labeled expression at the specified position.

func (*LabeledExpr) Pos Uses

func (l *LabeledExpr) Pos() Pos

Pos returns the starting position of the node.

func (*LabeledExpr) String Uses

func (l *LabeledExpr) String() string

String returns the textual representation of a node.

type LitMatcher Uses

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 Uses

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 Uses

func (l *LitMatcher) Pos() Pos

Pos returns the starting position of the node.

func (*LitMatcher) String Uses

func (l *LitMatcher) String() string

String returns the textual representation of a node.

type NotCodeExpr Uses

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 Uses

func NewNotCodeExpr(p Pos) *NotCodeExpr

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

func (*NotCodeExpr) Pos Uses

func (n *NotCodeExpr) Pos() Pos

Pos returns the starting position of the node.

func (*NotCodeExpr) String Uses

func (n *NotCodeExpr) String() string

String returns the textual representation of a node.

type NotExpr Uses

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 Uses

func NewNotExpr(p Pos) *NotExpr

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

func (*NotExpr) Pos Uses

func (n *NotExpr) Pos() Pos

Pos returns the starting position of the node.

func (*NotExpr) String Uses

func (n *NotExpr) String() string

String returns the textual representation of a node.

type OneOrMoreExpr Uses

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

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

func NewOneOrMoreExpr Uses

func NewOneOrMoreExpr(p Pos) *OneOrMoreExpr

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

func (*OneOrMoreExpr) Pos Uses

func (o *OneOrMoreExpr) Pos() Pos

Pos returns the starting position of the node.

func (*OneOrMoreExpr) String Uses

func (o *OneOrMoreExpr) String() string

String returns the textual representation of a node.

type Pos Uses

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

Pos represents a position in a source file.

func (Pos) String Uses

func (p Pos) String() string

String returns the textual representation of a position.

type RecoveryExpr Uses

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 Uses

func NewRecoveryExpr(p Pos) *RecoveryExpr

NewRecoveryExpr creates a choice expression at the specified position.

func (*RecoveryExpr) Pos Uses

func (r *RecoveryExpr) Pos() Pos

Pos returns the starting position of the node.

func (*RecoveryExpr) String Uses

func (r *RecoveryExpr) String() string

String returns the textual representation of a node.

type Rule Uses

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 Uses

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 Uses

func (r *Rule) Pos() Pos

Pos returns the starting position of the node.

func (*Rule) String Uses

func (r *Rule) String() string

String returns the textual representation of a node.

type RuleRefExpr Uses

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

RuleRefExpr is an expression that references a rule by name.

func NewRuleRefExpr Uses

func NewRuleRefExpr(p Pos) *RuleRefExpr

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

func (*RuleRefExpr) Pos Uses

func (r *RuleRefExpr) Pos() Pos

Pos returns the starting position of the node.

func (*RuleRefExpr) String Uses

func (r *RuleRefExpr) String() string

String returns the textual representation of a node.

type SeqExpr Uses

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 Uses

func NewSeqExpr(p Pos) *SeqExpr

NewSeqExpr creates a new sequence expression at the specified position.

func (*SeqExpr) Pos Uses

func (s *SeqExpr) Pos() Pos

Pos returns the starting position of the node.

func (*SeqExpr) String Uses

func (s *SeqExpr) String() string

String returns the textual representation of a node.

type StateCodeExpr Uses

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 Uses

func NewStateCodeExpr(p Pos) *StateCodeExpr

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

func (*StateCodeExpr) Pos Uses

func (s *StateCodeExpr) Pos() Pos

Pos returns the starting position of the node.

func (*StateCodeExpr) String Uses

func (s *StateCodeExpr) String() string

String returns the textual representation of a node.

type StringLit Uses

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

StringLit represents a string literal.

func NewStringLit Uses

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 Uses

func (s *StringLit) Pos() Pos

Pos returns the starting position of the node.

func (*StringLit) String Uses

func (s *StringLit) String() string

String returns the textual representation of a node.

type ThrowExpr Uses

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 Uses

func NewThrowExpr(p Pos) *ThrowExpr

NewThrowExpr creates a new throw expression at the specified position.

func (*ThrowExpr) Pos Uses

func (t *ThrowExpr) Pos() Pos

Pos returns the starting position of the node.

func (*ThrowExpr) String Uses

func (t *ThrowExpr) String() string

String returns the textual representation of a node.

type Visitor Uses

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 Uses

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

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

func NewZeroOrMoreExpr Uses

func NewZeroOrMoreExpr(p Pos) *ZeroOrMoreExpr

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

func (*ZeroOrMoreExpr) Pos Uses

func (z *ZeroOrMoreExpr) Pos() Pos

Pos returns the starting position of the node.

func (*ZeroOrMoreExpr) String Uses

func (z *ZeroOrMoreExpr) String() string

String returns the textual representation of a node.

type ZeroOrOneExpr Uses

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

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

func NewZeroOrOneExpr Uses

func NewZeroOrOneExpr(p Pos) *ZeroOrOneExpr

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

func (*ZeroOrOneExpr) Pos Uses

func (z *ZeroOrOneExpr) Pos() Pos

Pos returns the starting position of the node.

func (*ZeroOrOneExpr) String Uses

func (z *ZeroOrOneExpr) String() string

String returns the textual representation of a node.

Package ast imports 4 packages (graph) and is imported by 12 packages. Updated 2019-06-03. Refresh now. Tools for package owners.