generator

package
v0.0.0-...-65f6afe Latest Latest
Warning

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

Go to latest
Published: Nov 7, 2019 License: Apache-2.0 Imports: 18 Imported by: 0

Documentation

Overview

This package implements a static parser generator for parsing expression grammars (PEG) that is compatible with dynamic parser.

go:generate go run cmd/generator/generator-main.go --grammar=peg.peg --output=peg.peg.go --package=generator

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CaptureEndHandler

func CaptureEndHandler(r *Result, pos int) (int, error)

func CaptureHandler

func CaptureHandler(r *Result, pos int) (int, error)

func CaptureStartHandler

func CaptureStartHandler(r *Result, pos int) (int, error)

func Capture_1

func Capture_1(r *Result, pos int) (int, error)

func Capture_1_1

func Capture_1_1(r *Result, pos int) (int, error)

func Capture_1_2

func Capture_1_2(r *Result, pos int) (int, error)

func Capture_1_3

func Capture_1_3(r *Result, pos int) (int, error)

func Capture_1_4

func Capture_1_4(r *Result, pos int) (int, error)

func Capture_1_5

func Capture_1_5(r *Result, pos int) (int, error)

func CharClassHandler

func CharClassHandler(r *Result, pos int) (int, error)

func CharClass_1

func CharClass_1(r *Result, pos int) (int, error)

func CharClass_1_1

func CharClass_1_1(r *Result, pos int) (int, error)

func CharClass_1_2

func CharClass_1_2(r *Result, pos int) (int, error)

func CharClass_1_3

func CharClass_1_3(r *Result, pos int) (int, error)

func CharClass_1_3_capture

func CharClass_1_3_capture(r *Result, pos int) (int, error)

func CharClass_1_3_capture_1

func CharClass_1_3_capture_1(r *Result, pos int) (int, error)

func CharClass_1_3_capture_1_1

func CharClass_1_3_capture_1_1(r *Result, pos int) (int, error)

func CharClass_1_3_capture_1_1_paren_1

func CharClass_1_3_capture_1_1_paren_1(r *Result, pos int) (int, error)

func CharClass_1_3_capture_1_1_paren_1_1

func CharClass_1_3_capture_1_1_paren_1_1(r *Result, pos int) (int, error)

func CharClass_1_3_capture_1_1_paren_1_2

func CharClass_1_3_capture_1_1_paren_1_2(r *Result, pos int) (int, error)

func CharClass_1_3_capture_1_1_paren_1_2_plus

func CharClass_1_3_capture_1_1_paren_1_2_plus(r *Result, pos int) (int, error)

func CharClass_1_3_capture_1_1_paren_1_3

func CharClass_1_3_capture_1_1_paren_1_3(r *Result, pos int) (int, error)

func CharClass_1_3_capture_1_1_paren_2

func CharClass_1_3_capture_1_1_paren_2(r *Result, pos int) (int, error)

func CharClass_1_3_capture_1_1_paren_2_1

func CharClass_1_3_capture_1_1_paren_2_1(r *Result, pos int) (int, error)

func CharClass_1_3_capture_1_1_paren_2_1_star

func CharClass_1_3_capture_1_1_paren_2_1_star(r *Result, pos int) (int, error)

func CharClass_1_3_capture_1_1_paren_2_1_star_paren_1

func CharClass_1_3_capture_1_1_paren_2_1_star_paren_1(r *Result, pos int) (int, error)

func CharClass_1_3_capture_1_1_paren_2_1_star_paren_1_1

func CharClass_1_3_capture_1_1_paren_2_1_star_paren_1_1(r *Result, pos int) (int, error)

func CharClass_1_3_capture_1_1_paren_2_1_star_paren_1_1_neg

func CharClass_1_3_capture_1_1_paren_2_1_star_paren_1_1_neg(r *Result, pos int) (int, error)

func CharClass_1_3_capture_1_1_paren_2_1_star_paren_1_2

func CharClass_1_3_capture_1_1_paren_2_1_star_paren_1_2(r *Result, pos int) (int, error)

func CharClass_1_4

func CharClass_1_4(r *Result, pos int) (int, error)

func Construct

func Construct(n *parser.Node, callback func(string, Accessor) (interface{}, error), options *AccessorOptions) (interface{}, error)

func EndOfLineHandler

func EndOfLineHandler(r *Result, pos int) (int, error)

func EndOfLine_1

func EndOfLine_1(r *Result, pos int) (int, error)

func EndOfLine_1_1

func EndOfLine_1_1(r *Result, pos int) (int, error)

func EndOfLine_1_1_star

func EndOfLine_1_1_star(r *Result, pos int) (int, error)

func EndOfLine_1_2

func EndOfLine_1_2(r *Result, pos int) (int, error)

func EndOfLine_1_2_paren_1

func EndOfLine_1_2_paren_1(r *Result, pos int) (int, error)

func EndOfLine_1_2_paren_1_1

func EndOfLine_1_2_paren_1_1(r *Result, pos int) (int, error)

func EndOfLine_1_2_paren_2

func EndOfLine_1_2_paren_2(r *Result, pos int) (int, error)

func EndOfLine_1_2_paren_2_1

func EndOfLine_1_2_paren_2_1(r *Result, pos int) (int, error)

func EndOfLine_1_2_paren_3

func EndOfLine_1_2_paren_3(r *Result, pos int) (int, error)

func EndOfLine_1_2_paren_3_1

func EndOfLine_1_2_paren_3_1(r *Result, pos int) (int, error)

func GrammarHandler

func GrammarHandler(r *Result, pos int) (int, error)

func Grammar_1

func Grammar_1(r *Result, pos int) (int, error)

func Grammar_1_1

func Grammar_1_1(r *Result, pos int) (int, error)

func Grammar_1_1_plus

func Grammar_1_1_plus(r *Result, pos int) (int, error)

func Grammar_1_2

func Grammar_1_2(r *Result, pos int) (int, error)

func IdentHandler

func IdentHandler(r *Result, pos int) (int, error)

func Ident_1

func Ident_1(r *Result, pos int) (int, error)

func Ident_1_1

func Ident_1_1(r *Result, pos int) (int, error)

func Ident_1_1_star

func Ident_1_1_star(r *Result, pos int) (int, error)

func Ident_1_2

func Ident_1_2(r *Result, pos int) (int, error)

func Ident_1_2_capture

func Ident_1_2_capture(r *Result, pos int) (int, error)

func Ident_1_2_capture_1

func Ident_1_2_capture_1(r *Result, pos int) (int, error)

func Ident_1_2_capture_1_1

func Ident_1_2_capture_1_1(r *Result, pos int) (int, error)

func Ident_1_2_capture_1_2

func Ident_1_2_capture_1_2(r *Result, pos int) (int, error)

func Ident_1_2_capture_1_2_star

func Ident_1_2_capture_1_2_star(r *Result, pos int) (int, error)

func LiteralHandler

func LiteralHandler(r *Result, pos int) (int, error)

func Literal_1

func Literal_1(r *Result, pos int) (int, error)

func Literal_1_1

func Literal_1_1(r *Result, pos int) (int, error)

func Literal_1_2

func Literal_1_2(r *Result, pos int) (int, error)

func Literal_1_2_capture

func Literal_1_2_capture(r *Result, pos int) (int, error)

func Literal_1_2_capture_1

func Literal_1_2_capture_1(r *Result, pos int) (int, error)

func Literal_1_2_capture_1_1

func Literal_1_2_capture_1_1(r *Result, pos int) (int, error)

func Literal_1_2_capture_1_2

func Literal_1_2_capture_1_2(r *Result, pos int) (int, error)

func Literal_1_2_capture_1_2_star

func Literal_1_2_capture_1_2_star(r *Result, pos int) (int, error)

func Literal_1_2_capture_1_2_star_paren_1

func Literal_1_2_capture_1_2_star_paren_1(r *Result, pos int) (int, error)

func Literal_1_2_capture_1_2_star_paren_1_1

func Literal_1_2_capture_1_2_star_paren_1_1(r *Result, pos int) (int, error)

func Literal_1_2_capture_1_2_star_paren_1_1_neg

func Literal_1_2_capture_1_2_star_paren_1_1_neg(r *Result, pos int) (int, error)

func Literal_1_2_capture_1_2_star_paren_1_2

func Literal_1_2_capture_1_2_star_paren_1_2(r *Result, pos int) (int, error)

func Literal_1_2_capture_1_3

func Literal_1_2_capture_1_3(r *Result, pos int) (int, error)

func Literal_2

func Literal_2(r *Result, pos int) (int, error)

func Literal_2_1

func Literal_2_1(r *Result, pos int) (int, error)

func Literal_2_2

func Literal_2_2(r *Result, pos int) (int, error)

func Literal_2_2_capture

func Literal_2_2_capture(r *Result, pos int) (int, error)

func Literal_2_2_capture_1

func Literal_2_2_capture_1(r *Result, pos int) (int, error)

func Literal_2_2_capture_1_1

func Literal_2_2_capture_1_1(r *Result, pos int) (int, error)

func Literal_2_2_capture_1_2

func Literal_2_2_capture_1_2(r *Result, pos int) (int, error)

func Literal_2_2_capture_1_2_star

func Literal_2_2_capture_1_2_star(r *Result, pos int) (int, error)

func Literal_2_2_capture_1_2_star_paren_1

func Literal_2_2_capture_1_2_star_paren_1(r *Result, pos int) (int, error)

func Literal_2_2_capture_1_2_star_paren_1_1

func Literal_2_2_capture_1_2_star_paren_1_1(r *Result, pos int) (int, error)

func Literal_2_2_capture_1_2_star_paren_1_1_neg

func Literal_2_2_capture_1_2_star_paren_1_1_neg(r *Result, pos int) (int, error)

func Literal_2_2_capture_1_2_star_paren_1_2

func Literal_2_2_capture_1_2_star_paren_1_2(r *Result, pos int) (int, error)

func Literal_2_2_capture_1_3

func Literal_2_2_capture_1_3(r *Result, pos int) (int, error)

func MakeCharClassHandler

func MakeCharClassHandler(cc *charclass.CharClass, handlerName string) []ast.Decl

func MakeChoiceHandler

func MakeChoiceHandler(group []string, handlerName string) []ast.Decl

func MakeDotHandler

func MakeDotHandler(handlerName string) []ast.Decl

func MakeGroupHandler

func MakeGroupHandler(group []string, handlerName string) []ast.Decl

func MakeLiteralHandler

func MakeLiteralHandler(val, handlerName string) []ast.Decl

func MakePlusHandler

func MakePlusHandler(handlerName, subHandler string) []ast.Decl

func MakePredicateHandler

func MakePredicateHandler(handlerName, subHandler string, positive bool) []ast.Decl

func MakeQuestionHandler

func MakeQuestionHandler(handlerName, subHandler string) []ast.Decl

func MakeRHSHandler

func MakeRHSHandler(handlerName, ruleName string, rhs *RHS) []ast.Decl

func MakeStarHandler

func MakeStarHandler(handlerName, subHandler string) []ast.Decl

func MakeTermHandler

func MakeTermHandler(term *Term, handlerName string) []ast.Decl

func MakeTermsHandler

func MakeTermsHandler(terms []*Term, handlerName string) []ast.Decl

func NegPredHandler

func NegPredHandler(r *Result, pos int) (int, error)

func NegPred_1

func NegPred_1(r *Result, pos int) (int, error)

func NegPred_1_1

func NegPred_1_1(r *Result, pos int) (int, error)

func NegPred_1_2

func NegPred_1_2(r *Result, pos int) (int, error)

func NegPred_1_3

func NegPred_1_3(r *Result, pos int) (int, error)

func New

func New(source string) (*generator, error)

func ParensHandler

func ParensHandler(r *Result, pos int) (int, error)

func Parens_1

func Parens_1(r *Result, pos int) (int, error)

func Parens_1_1

func Parens_1_1(r *Result, pos int) (int, error)

func Parens_1_2

func Parens_1_2(r *Result, pos int) (int, error)

func Parens_1_3

func Parens_1_3(r *Result, pos int) (int, error)

func Parens_1_4

func Parens_1_4(r *Result, pos int) (int, error)

func Parens_1_5

func Parens_1_5(r *Result, pos int) (int, error)

func PredHandler

func PredHandler(r *Result, pos int) (int, error)

func Pred_1

func Pred_1(r *Result, pos int) (int, error)

func Pred_1_1

func Pred_1_1(r *Result, pos int) (int, error)

func Pred_1_2

func Pred_1_2(r *Result, pos int) (int, error)

func Pred_1_3

func Pred_1_3(r *Result, pos int) (int, error)

func RHSHandler

func RHSHandler(r *Result, pos int) (int, error)

func RHS_1

func RHS_1(r *Result, pos int) (int, error)

func RHS_1_1

func RHS_1_1(r *Result, pos int) (int, error)

func RHS_1_2

func RHS_1_2(r *Result, pos int) (int, error)

func RHS_1_2_star

func RHS_1_2_star(r *Result, pos int) (int, error)

func RHS_1_2_star_paren_1

func RHS_1_2_star_paren_1(r *Result, pos int) (int, error)

func RHS_1_2_star_paren_1_1

func RHS_1_2_star_paren_1_1(r *Result, pos int) (int, error)

func RHS_1_2_star_paren_1_2

func RHS_1_2_star_paren_1_2(r *Result, pos int) (int, error)

func RHS_1_2_star_paren_1_3

func RHS_1_2_star_paren_1_3(r *Result, pos int) (int, error)

func RuleHandler

func RuleHandler(r *Result, pos int) (int, error)

func Rule_1

func Rule_1(r *Result, pos int) (int, error)

func Rule_1_1

func Rule_1_1(r *Result, pos int) (int, error)

func Rule_1_2

func Rule_1_2(r *Result, pos int) (int, error)

func Rule_1_3

func Rule_1_3(r *Result, pos int) (int, error)

func Rule_1_4

func Rule_1_4(r *Result, pos int) (int, error)

func Rule_1_5

func Rule_1_5(r *Result, pos int) (int, error)

func Rule_1_6

func Rule_1_6(r *Result, pos int) (int, error)

func Rule_1_7

func Rule_1_7(r *Result, pos int) (int, error)

func Rule_1_7_question

func Rule_1_7_question(r *Result, pos int) (int, error)

func SpecialHandler

func SpecialHandler(r *Result, pos int) (int, error)

func Special_1

func Special_1(r *Result, pos int) (int, error)

func Special_1_1

func Special_1_1(r *Result, pos int) (int, error)

func Special_1_2

func Special_1_2(r *Result, pos int) (int, error)

func Special_1_2_capture

func Special_1_2_capture(r *Result, pos int) (int, error)

func Special_1_2_capture_1

func Special_1_2_capture_1(r *Result, pos int) (int, error)

func Special_1_2_capture_1_1

func Special_1_2_capture_1_1(r *Result, pos int) (int, error)

func TermHandler

func TermHandler(r *Result, pos int) (int, error)

func Term_1

func Term_1(r *Result, pos int) (int, error)

func Term_1_1

func Term_1_1(r *Result, pos int) (int, error)

func Term_2

func Term_2(r *Result, pos int) (int, error)

func Term_2_1

func Term_2_1(r *Result, pos int) (int, error)

func Term_3

func Term_3(r *Result, pos int) (int, error)

func Term_3_1

func Term_3_1(r *Result, pos int) (int, error)

func Term_4

func Term_4(r *Result, pos int) (int, error)

func Term_4_1

func Term_4_1(r *Result, pos int) (int, error)

func Term_5

func Term_5(r *Result, pos int) (int, error)

func Term_5_1

func Term_5_1(r *Result, pos int) (int, error)

func Term_6

func Term_6(r *Result, pos int) (int, error)

func Term_6_1

func Term_6_1(r *Result, pos int) (int, error)

func Term_7

func Term_7(r *Result, pos int) (int, error)

func Term_7_1

func Term_7_1(r *Result, pos int) (int, error)

func Term_8

func Term_8(r *Result, pos int) (int, error)

func Term_8_1

func Term_8_1(r *Result, pos int) (int, error)

func TermsHandler

func TermsHandler(r *Result, pos int) (int, error)

func Terms_1

func Terms_1(r *Result, pos int) (int, error)

func Terms_1_1

func Terms_1_1(r *Result, pos int) (int, error)

func Terms_1_1_plus

func Terms_1_1_plus(r *Result, pos int) (int, error)

Types

type Accessor

type Accessor interface {
	// Node returns the parse tree node that is currently being converted
	// (so technically this is not about child access).
	Node() *parser.Node
	// String returns the string value associated with a child node.
	// In case of error it records the error and returns empty string.
	String(name string) string
	// GetString returns the string value associated with a child node.
	// It returns an error if the requested child node does not exist
	// or has different type.
	GetString(name string) (string, error)
	// Get returns the interface type matching the given type instance.
	// In case of error it records the error and returns nil.
	Get(name string, ty interface{}) interface{}
	// GetTyped returns the interface type matching the given type instance.
	// If the requested child does not exist or has different type, it returns
	// an error. If the requested type is a slice, and only one child instance
	// exists, it is returned as an 1-element slice.
	GetTyped(name string, ty interface{}) (interface{}, error)
	// Returns the node label of the ith child in the parse tree. If the index
	// is out of bounds, it records an error and
	Child(i int) string
}

Accessor is the interface given to the callback during the semantic tree construction.

type AccessorOptions

type AccessorOptions struct {
	// If true, the Check() method will return error if it detects that some
	// of children node were converted to non-trivial semantic subtrees,
	// but were not used during semantic tree construction of this node.
	ErrorOnUnusedChild bool
}

AccessorOptions configures the behavior of error checking of Accessor.

type Grammar

type Grammar struct {
	// Rule is a dictionary of rules.
	Rules map[string]*Rule
	// RuleNames keeps the list of rule name in the original definition order.
	RuleNames []string
	Source    string
}

Grammar represents the complete PEG grammar.

func ConvertGrammar2

func ConvertGrammar2(n *parser.Node) (*Grammar, error)

ConvertGrammar2 is a reimplementation of the semantic tree reconstruction using callbacks.

func Parse2

func Parse2(input string) (*Grammar, error)

Parse parses the PEG grammar source into a grammar.

func (*Grammar) String

func (g *Grammar) String() string

type NodeStack

type NodeStack []*parser.Node

func (*NodeStack) Pop

func (s *NodeStack) Pop() *parser.Node

func (*NodeStack) Push

func (s *NodeStack) Push(n *parser.Node)

type RHS

type RHS struct {
	// Terms is the RHS, top iteration over choices, and inside
	// iteration over concatenation of terms.
	Terms [][]*Term
}

RHS is the right-hand side of one rule or the contents of parenthesized expression.

func (*RHS) String

func (rhs *RHS) String() string

type Result

type Result struct {
	Source string
	Memo   map[int]map[int]*parser.Node
	Level  int
	Tree   *parser.Node
	NodeStack
}

func Parse

func Parse(source string) (*Result, error)

func (*Result) Attach

func (r *Result) Attach(n *parser.Node)

func (*Result) TopNode

func (r *Result) TopNode() *parser.Node

type Rule

type Rule struct {
	// Ident is the name of the rule, defined in LHS.
	Ident string
	// RHS is the rule's right-hand side.
	*RHS
}

Rule represent one PEG rule (Rule <- RHS).

func (*Rule) String

func (rule *Rule) String() string

type Special

type Special struct {
	*Term
	// Rune is one of '*' '?' '+'.
	Rune rune
}

Special is a term with a option or repeat special modifer (*?+).

func (*Special) String

func (s *Special) String() string

type Term

type Term struct {
	Parens  *RHS
	NegPred *Term
	Pred    *Term
	*Special
	Capture *RHS
	*charclass.CharClass
	Literal string
	Ident   string
}

Term is one term. Note: Special characters in the grammar are handled specially and are not mapped to Term one-to-one. Instead, *?+ combine with the previous Term, and . is converted to a special CharClass.

func (*Term) String

func (t *Term) String() string

Directories

Path Synopsis
cmd
parse-grammar
Binary parse-grammar-main is a utility binary to read parse and print PEG grammars.
Binary parse-grammar-main is a utility binary to read parse and print PEG grammars.
Binary main shows an example of using the parser that was generated from parser expression grammar (PEG).
Binary main shows an example of using the parser that was generated from parser expression grammar (PEG).
Package gogen contains a few functions for ad-hoc generation of Go source snippets.
Package gogen contains a few functions for ad-hoc generation of Go source snippets.
Package template provides the code templates that are emitted to generate the parser.
Package template provides the code templates that are emitted to generate the parser.
Binary gentests is used to generated the test sources from the tests package.
Binary gentests is used to generated the test sources from the tests package.

Jump to

Keyboard shortcuts

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