bnf

package
v0.0.0 Latest Latest
Warning

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

Go to latest
Published: Mar 25, 2019 License: BSD-2-Clause Imports: 6 Imported by: 0

Documentation

Overview

Package bnf contains parser for BNF metalanguage.

Index

Constants

This section is empty.

Variables

View Source
var ErrNotImplemented = errors.New("bnf: not implemented")
View Source
var ErrUnexpectedChar = errors.New("bnf: unexpected character")

Functions

func Visit

func Visit(root Node, f VisitorFunc) error

Visit implements in-order graph traversal procedure.

Types

type BNF

type BNF struct {
	Rules []*ProductionRule
}

BNF types corresponds parsed BNF grammar.

func Parse

func Parse(source []byte) (*BNF, error)

Parse parses BNF grammar.

func (*BNF) String

func (bnf *BNF) String() string

type BNFParser

type BNFParser struct {
	Reader io.Reader
	// contains filtered or unexported fields
}

func (*BNFParser) Parse

func (p *BNFParser) Parse() (*BNF, error)

type Error

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

Error represent parsing error and contains some context information.

func (*Error) Error

func (e *Error) Error() string

type Expression

type Expression []*Term

Expression is a list of terminals and non-terminals.

func (Expression) Left

func (e Expression) Left() Node

func (Expression) Right

func (e Expression) Right() Node

func (*Expression) String

func (e *Expression) String() string

type List

type List = Expression

type Node

type Node interface {
	// Get the left child of a node.
	Left() Node
	// Get the right child of a node.
	Right() Node
}

Node is a node of binary tree. In each node of a tree there is a token.

type ProductionRule

type ProductionRule struct {
	Token       // Points to lexeme that contains `::=`.
	Name        *Term
	Stmt        Node
	Expressions []Expression
}

ProductionRule is a production rule itself. Actually, it contains several rules for a non-terminal.

func (*ProductionRule) Left

func (r *ProductionRule) Left() Node

func (*ProductionRule) Right

func (r *ProductionRule) Right() Node

func (*ProductionRule) String

func (r *ProductionRule) String() string

type Stmt

type Stmt struct {
	Token // Points to lexeme that contains '|`.
	Head  List
	Tail  Node
}

func (*Stmt) Left

func (s *Stmt) Left() Node

func (*Stmt) Right

func (s *Stmt) Right() Node

type Term

type Term struct {
	Name     []byte
	Terminal bool
	// Begin encodes position where token begins. The possition is relative to
	// begin position of parent token.
	Begin int
	// End encodes position where token ends. The position is relateive as well
	// as in case of begin.
	End int
}

Term represents terminal or non-terminal.

func (*Term) Left

func (t *Term) Left() Node

func (*Term) Right

func (t *Term) Right() Node

func (*Term) String

func (t *Term) String() string

type Token

type Token = Term

type VisitorFunc

type VisitorFunc func(Node) error

VisitorFunc is a callback type for graph traversing. Its argument is the current node of traversing.

Jump to

Keyboard shortcuts

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