exp: golang.org/x/exp/ebnf Index | Files

package ebnf

import "golang.org/x/exp/ebnf"

Package ebnf is a library for EBNF grammars. The input is text ([]byte) satisfying the following grammar (represented itself in EBNF):

Production  = name "=" [ Expression ] "." .
Expression  = Alternative { "|" Alternative } .
Alternative = Term { Term } .
Term        = name | token [ "…" token ] | Group | Option | Repetition .
Group       = "(" Expression ")" .
Option      = "[" Expression "]" .
Repetition  = "{" Expression "}" .

A name is a Go identifier, a token is a Go string, and comments and white space follow the same rules as for the Go language. Production names starting with an uppercase Unicode letter denote non-terminal productions (i.e., productions which allow white-space and comments between tokens); all other production names denote lexical productions.

Index

Package Files

ebnf.go parser.go

func Verify Uses

func Verify(grammar Grammar, start string) error

Verify checks that:

- all productions used are defined
- all productions defined are used when beginning at start
- lexical productions refer only to other lexical productions

Position information is interpreted relative to the file set fset.

type Alternative Uses

type Alternative []Expression // x | y | z

An Alternative node represents a non-empty list of alternative expressions.

func (Alternative) Pos Uses

func (x Alternative) Pos() scanner.Position

type Bad Uses

type Bad struct {
    TokPos scanner.Position
    Error  string // parser error message
}

A Bad node stands for pieces of source code that lead to a parse error.

func (*Bad) Pos Uses

func (x *Bad) Pos() scanner.Position

type Expression Uses

type Expression interface {
    // Pos is the position of the first character of the syntactic construct
    Pos() scanner.Position
}

An Expression node represents a production expression.

type Grammar Uses

type Grammar map[string]*Production

A Grammar is a set of EBNF productions. The map is indexed by production name.

func Parse Uses

func Parse(filename string, src io.Reader) (Grammar, error)

Parse parses a set of EBNF productions from source src. It returns a set of productions. Errors are reported for incorrect syntax and if a production is declared more than once; the filename is used only for error positions.

type Group Uses

type Group struct {
    Lparen scanner.Position
    Body   Expression // (body)
}

A Group node represents a grouped expression.

func (*Group) Pos Uses

func (x *Group) Pos() scanner.Position

type Name Uses

type Name struct {
    StringPos scanner.Position
    String    string
}

A Name node represents a production name.

func (*Name) Pos Uses

func (x *Name) Pos() scanner.Position

type Option Uses

type Option struct {
    Lbrack scanner.Position
    Body   Expression // [body]
}

An Option node represents an optional expression.

func (*Option) Pos Uses

func (x *Option) Pos() scanner.Position

type Production Uses

type Production struct {
    Name *Name
    Expr Expression
}

A Production node represents an EBNF production.

func (*Production) Pos Uses

func (x *Production) Pos() scanner.Position

type Range Uses

type Range struct {
    Begin, End *Token // begin ... end
}

A List node represents a range of characters.

func (*Range) Pos Uses

func (x *Range) Pos() scanner.Position

type Repetition Uses

type Repetition struct {
    Lbrace scanner.Position
    Body   Expression // {body}
}

A Repetition node represents a repeated expression.

func (*Repetition) Pos Uses

func (x *Repetition) Pos() scanner.Position

type Sequence Uses

type Sequence []Expression // x y z

A Sequence node represents a non-empty list of sequential expressions.

func (Sequence) Pos Uses

func (x Sequence) Pos() scanner.Position

type Token Uses

type Token struct {
    StringPos scanner.Position
    String    string
}

A Token node represents a literal.

func (*Token) Pos Uses

func (x *Token) Pos() scanner.Position

Package ebnf imports 7 packages (graph) and is imported by 9 packages. Updated 2017-08-12. Refresh now. Tools for package owners.