PEGO

Package grammar

import "github.com/ornew/pego/grammar"

Overview

Package grammar defines the abstract syntax tree (AST) of PEGO grammars.

A grammar is a sequence of statements: type definitions (TypeDef) and rule definitions (RuleDef). A rule body is a parser expression (Expr), and actions and predicates are value expressions (Term).

Index

Constants

const (
	Prefix  = "prefix"
	Postfix = "postfix"
	Infix   = "infix"
)

Kinds of operators.

const (
	AssocLeft  = "left"
	AssocRight = "right"
	AssocNone  = "none"
)

Associativities.

Functions

func Format source

func Format(g *Grammar) string

Format converts g to PEGO source code.

Comments and line breaks recorded in the AST (see LineBreak) are kept, so formatting parsed source preserves its comments. Struct field types and trailing comments on consecutive lines are aligned. A grammar without such layout information (for example, one decoded from JSON) is printed with a blank line between definitions and one item per line in blocks.

func FormatExpr source

func FormatExpr(e Expr) string

FormatExpr returns the parser expression e in PEGO syntax.

func FormatTerm source

func FormatTerm(t Term) string

FormatTerm returns the value expression t in PEGO syntax.

func FormatType source

func FormatType(t TypeExpr) string

FormatType returns t in PEGO syntax.

func MarshalJSON source

func MarshalJSON(g *Grammar) ([]byte, error)

MarshalJSON converts g to JSON.

Types

type AliasSpec source

type AliasSpec struct {
	Type TypeExpr `json:"type"`
}

AliasSpec is a type alias, including an alias of a union type.

type And source

type And struct {
	Pos  Pos  `json:"-"`
	Expr Expr `json:"expr"`
}

And is a positive lookahead &a.

type Any source

type Any struct {
	Pos Pos `json:"-"`
}

Any matches any single character (.).

type Assign source

type Assign struct {
	Pos   Pos    `json:"-"`
	Name  string `json:"name"`
	Value Term   `json:"value"`
}

Assign defines a variable in a predicate: name = value.

type Atomic source

type Atomic struct {
	Pos  Pos  `json:"-"`
	Expr Expr `json:"expr"`
}

Atomic is an atomic expression @a.

type AttrArg source

type AttrArg struct {
	Name  string `json:"name"`
	Value Expr   `json:"value"`
}

AttrArg is an argument of an attribute. Its value is a parser expression; a string value is a Literal.

type Attribute source

type Attribute struct {
	Pos  Pos        `json:"-"`
	Name string     `json:"name"`
	Args []*AttrArg `json:"args,omitempty"`
}

Attribute is an attribute #name(arg=value, ...).

func (*Attribute) Arg source

func (a *Attribute) Arg(name string) (Expr, bool)

Arg returns the argument named name.

type Attributed source

type Attributed struct {
	Expr  Expr         `json:"expr"`
	Attrs []*Attribute `json:"attrs"`

	// Breaks[i], when not nil, puts Attrs[i] on a new line. Format honors
	// it only in the items of the top-level sequences of a rule body.
	Breaks []*LineBreak `json:"-"`
}

Attributed is an expression with attributes a #name(...).

type BeginInput source

type BeginInput struct {
	Pos Pos `json:"-"`
}

BeginInput matches the beginning of the input (^^).

type BeginLine source

type BeginLine struct {
	Pos Pos `json:"-"`
}

BeginLine matches the beginning of a line (^).

type Binary source

type Binary struct {
	Pos Pos    `json:"-"`
	Op  string `json:"op"`
	L   Term   `json:"l"`
	R   Term   `json:"r"`
}

Binary is a binary operation l op r.

type BoolLit source

type BoolLit struct {
	Value bool `json:"value"`
}

BoolLit is a boolean literal, true or false.

type Bottom source

type Bottom struct {
	Pos Pos `json:"-"`
}

Bottom is the bottom expression _|_.

type Call source

type Call struct {
	Pos  Pos    `json:"-"`
	Func string `json:"func"`
	Args []Term `json:"args"`
}

Call is a call of a built-in function f(args...).

type Capture source

type Capture struct {
	Pos  Pos    `json:"-"`
	Name string `json:"name"`
	Expr Expr   `json:"expr"`
}

Capture is a capture name:a.

type CaptureRef source

type CaptureRef struct {
	Pos  Pos    `json:"-"`
	Name string `json:"name"`
}

CaptureRef is a reference $name to a capture or a lambda parameter.

type CharClass source

type CharClass struct {
	Pos     Pos         `json:"-"`
	Ranges  []CharRange `json:"ranges"`
	Negated bool        `json:"negated,omitempty"`
}

CharClass is a character class (?...), or (?^...) if Negated is set.

type CharRange source

type CharRange struct {
	Lo rune `json:"lo"`
	Hi rune `json:"hi"`
}

CharRange is a character range [Lo, Hi].

type Choice source

type Choice struct {
	Alts []Expr `json:"alts"`

	// Breaks[i], when not nil, puts Alts[i] (with its "/") on a new line.
	// Format honors it only in the top-level choice of a rule body.
	Breaks []*LineBreak `json:"-"`
}

Choice is an ordered choice a / b.

type Comment source

type Comment struct {
	Pos Pos
	// Text is the comment including the leading "//", without the line
	// break and trailing white space.
	Text string
	// Blank reports whether a blank line precedes the comment.
	Blank bool
}

Comment is a line comment ("// ...") in PEGO source.

type Cut source

type Cut struct {
	Pos Pos `json:"-"`
}

Cut is a cut --.

type Discard source

type Discard struct {
	Pos  Pos  `json:"-"`
	Expr Expr `json:"expr"`
}

Discard is a discarded expression -a.

type EndInput source

type EndInput struct {
	Pos Pos `json:"-"`
}

EndInput matches the end of the input ($$).

type EndLine source

type EndLine struct {
	Pos Pos `json:"-"`
}

EndLine matches the end of a line ($).

type Expr source

type Expr interface {
	// contains filtered or unexported methods
}

Expr is a parser expression.

type Field source

type Field struct {
	Pos  Pos      `json:"-"`
	Name string   `json:"name"`
	Type TypeExpr `json:"type"`

	Break *LineBreak `json:"-"`
}

Field is a field of a struct type.

type FieldInit source

type FieldInit struct {
	Pos   Pos    `json:"-"`
	Name  string `json:"name"`
	Value Term   `json:"value"`
}

FieldInit initializes a field of a struct value.

type Grammar source

type Grammar struct {
	Package    string      `json:"package,omitempty"`
	Statements []Statement `json:"statements"`

	// PackageBreak holds the comments before the package clause, and
	// EndBreak those at the end of the file (see LineBreak).
	PackageBreak *LineBreak `json:"-"`
	EndBreak     *LineBreak `json:"-"`
}

Grammar is a whole grammar.

func UnmarshalJSON source

func UnmarshalJSON(data []byte) (*Grammar, error)

UnmarshalJSON decodes a grammar from JSON.

func (*Grammar) AllComments source

func (g *Grammar) AllComments() []*Comment

AllComments returns every comment kept in g, in no particular order.

func (*Grammar) Rules source

func (g *Grammar) Rules() []*RuleDef

Rules returns the rule definitions of the grammar in declaration order.

func (*Grammar) Types source

func (g *Grammar) Types() []*TypeDef

Types returns the type definitions of the grammar in declaration order.

type IndexRef source

type IndexRef struct {
	Pos   Pos `json:"-"`
	Index int `json:"index"`
}

IndexRef is a positional reference $n. $0 is the list of all elements.

type IntLit source

type IntLit struct {
	Value int `json:"value"`
}

IntLit is an integer literal.

type Lambda source

type Lambda struct {
	Params []string `json:"params"`
	Body   Term     `json:"body"`
}

Lambda is a function (a, b) => body.

type LineBreak source

type LineBreak struct {
	// Comments are the comments on their own lines before the node, in
	// source order. Comments that Format cannot keep in place, because it
	// joins the lines they end, are added to the nearest preceding
	// LineBreak.
	Comments []*Comment
	// Blank reports whether a blank line directly precedes the node.
	Blank bool
	// Trailing is the comment at the end of the line that starts with the
	// node, if any.
	Trailing *Comment
}

LineBreak describes the start of a line in PEGO source: the comments before it and the comment at its end.

type ListType source

type ListType struct {
	Elem TypeExpr `json:"elem"`
}

ListType is a list type []T.

type Literal source

type Literal struct {
	Pos   Pos    `json:"-"`
	Value string `json:"value"`
}

Literal is a string literal "...".

type Member source

type Member struct {
	Pos  Pos    `json:"-"`
	X    Term   `json:"x"`
	Name string `json:"name"`
}

Member is a field reference x.Name.

type New source

type New struct {
	Pos    Pos          `json:"-"`
	Type   string       `json:"type"`
	Fields []*FieldInit `json:"fields,omitempty"`
}

New creates a struct value: new Type{Field: value, ...}.

type NilLit source

type NilLit struct{}

NilLit is the empty value nil.

type Not source

type Not struct {
	Pos  Pos  `json:"-"`
	Expr Expr `json:"expr"`
}

Not is a negative lookahead !a.

type Optional source

type Optional struct {
	Pos  Pos  `json:"-"`
	Expr Expr `json:"expr"`
}

Optional is an optional expression a?. Its value is the value of a, or nil. Pos is the position where a begins, as in Repeat.

type OptionalType source

type OptionalType struct {
	Elem TypeExpr `json:"elem"`
}

OptionalType is an optional type *T.

type Pos source

type Pos struct {
	Line int
	Col  int
}

Pos is a 1-based position in the source. The zero value means that the position is unknown.

func (Pos) IsValid source

func (p Pos) IsValid() bool

IsValid reports whether the position is known.

type Pratt source

type Pratt struct {
	Pos      Pos             `json:"-"`
	Skip     Expr            `json:"skip,omitempty"`
	Operands []*PrattOperand `json:"operands"`
	Levels   []*PrattLevel   `json:"levels"`

	// SkipBreak holds the comments before the skip line, and CloseBreak
	// those before the closing brace.
	SkipBreak  *LineBreak `json:"-"`
	CloseBreak *LineBreak `json:"-"`
}

Pratt is a Pratt expression pratt { ... }. It may appear only at the top level of a rule body.

type PrattLevel source

type PrattLevel struct {
	Name      string           `json:"name,omitempty"`
	Operators []*PrattOperator `json:"operators"`

	Break *LineBreak `json:"-"`
	// OneLine writes the level on one line ("level { infix left "+" }").
	OneLine bool `json:"-"`
	// CloseBreak holds the comments before the closing brace.
	CloseBreak *LineBreak `json:"-"`
}

PrattLevel is a precedence level. Levels declared earlier bind more loosely.

type PrattOperand source

type PrattOperand struct {
	Expr   Expr `json:"expr"`
	Action Term `json:"action,omitempty"`

	Break *LineBreak `json:"-"`
}

PrattOperand is an operand line.

type PrattOperator source

type PrattOperator struct {
	Pos    Pos    `json:"-"`
	Kind   string `json:"kind"`
	Assoc  string `json:"assoc,omitempty"`
	Expr   Expr   `json:"expr"`
	Action Term   `json:"action,omitempty"`

	Break *LineBreak `json:"-"`
}

PrattOperator is a declaration of an operator.

type Predicate source

type Predicate struct {
	Pos  Pos  `json:"-"`
	Term Term `json:"term"`
}

Predicate is a predicate Term.

type Ref source

type Ref struct {
	Pos   Pos    `json:"-"`
	Name  string `json:"name"`
	Level string `json:"level,omitempty"`
}

Ref is a call of a rule. If Level is set, the call parses only with that level of the Pratt expression and the levels above it.

type Repeat source

type Repeat struct {
	Pos  Pos  `json:"-"`
	Expr Expr `json:"expr"`
	Min  int  `json:"min"`
	Max  int  `json:"max"`
}

Repeat is a repetition a{Min,Max}. A negative Max means no upper bound. a* is {0,} and a+ is {1,}.

Pos is the position where the repeated expression begins (its opening parenthesis, if it is parenthesized).

type RuleDef source

type RuleDef struct {
	Pos    Pos      `json:"-"`
	Name   string   `json:"name"`
	Type   TypeExpr `json:"type,omitempty"`
	Expr   Expr     `json:"expr"`
	Action Term     `json:"action,omitempty"`

	// Break holds the comments before the definition. When it is nil,
	// Format separates the definition from the previous one with a blank
	// line.
	Break *LineBreak `json:"-"`
	// BodyBreak, when not nil, puts the body on a new line after "=", and
	// ActionBreak puts "->" on a new line.
	BodyBreak   *LineBreak `json:"-"`
	ActionBreak *LineBreak `json:"-"`
}

RuleDef is a rule definition (def Name: Type = Expr -> Action).

type Seq source

type Seq struct {
	Items []Expr `json:"items"`

	// Breaks[i], when not nil, puts Items[i] on a new line. Format honors
	// it only in the top-level sequences of a rule body.
	Breaks []*LineBreak `json:"-"`
}

Seq is a sequence a b.

type Statement source

type Statement interface {
	// contains filtered or unexported methods
}

Statement is a top-level definition in a grammar.

type StringLit source

type StringLit struct {
	Value string `json:"value"`
}

StringLit is a string literal.

type StructSpec source

type StructSpec struct {
	Fields []*Field `json:"fields"`

	// OneLine writes the struct on one line ("struct { A T, B U }").
	OneLine bool `json:"-"`
	// CloseBreak holds the comments before the closing brace.
	CloseBreak *LineBreak `json:"-"`
}

StructSpec is a struct type.

type Term source

type Term interface {
	// contains filtered or unexported methods
}

Term is a value expression used in actions and predicates.

type TerminalSpec source

type TerminalSpec struct{}

TerminalSpec is a terminal type.

type Top source

type Top struct {
	Pos Pos `json:"-"`
}

Top is the top expression _.

type TypeDef source

type TypeDef struct {
	Pos  Pos      `json:"-"`
	Name string   `json:"name"`
	Spec TypeSpec `json:"spec"`

	// Break holds the comments before the definition. When it is nil,
	// Format separates the definition from the previous one with a blank
	// line.
	Break *LineBreak `json:"-"`
}

TypeDef is a type definition (type Name Spec).

type TypeExpr source

type TypeExpr interface {
	// contains filtered or unexported methods
}

TypeExpr is a reference to a type.

type TypeRef source

type TypeRef struct {
	Pos  Pos    `json:"-"`
	Name string `json:"name"`
}

TypeRef is a reference to a type by name.

type TypeSpec source

type TypeSpec interface {
	// contains filtered or unexported methods
}

TypeSpec is the right-hand side of a type definition.

type Unary source

type Unary struct {
	Pos Pos    `json:"-"`
	Op  string `json:"op"`
	X   Term   `json:"x"`
}

Unary is a unary operation op x.

type UnionType source

type UnionType struct {
	Types []TypeExpr `json:"types"`
}

UnionType is a union type T | U.

type VarRef source

type VarRef struct {
	Pos  Pos    `json:"-"`
	Name string `json:"name"`
}

VarRef is a reference name to a variable in a predicate.