parser

package
v0.37.0 Latest Latest
Warning

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

Go to latest
Published: May 17, 2023 License: Apache-2.0 Imports: 13 Imported by: 7

Documentation

Index

Constants

View Source
const (
	StackDelim = "@"
	At         = Rule(StackDelim)

	WrapRE = Rule(".wrapRE")
)

Variables

View Source
var (
	NoLimit      = -1
	DefaultLimit = 1
)

Functions

func AssertEqualNodes

func AssertEqualNodes(t *testing.T, v, u Node) bool

Types

type Associativity added in v0.10.0

type Associativity int
const (
	RightToLeft Associativity = iota - 1
	NonAssociative
	LeftToRight
)

func NewAssociativity added in v0.10.0

func NewAssociativity(s string) Associativity

func (Associativity) IsExtra added in v0.10.0

func (Associativity) IsExtra()

func (Associativity) String added in v0.10.0

func (a Associativity) String() string

type Choice added in v0.10.0

type Choice int

func (Choice) IsExtra added in v0.10.0

func (Choice) IsExtra()

type CutPoint added in v0.13.0

type CutPoint struct{ Term }

func (CutPoint) Parser added in v0.13.0

func (t CutPoint) Parser(rule Rule, c cache) Parser

func (CutPoint) Resolve added in v0.13.0

func (t CutPoint) Resolve(oldRule, newRule Rule) Term

func (CutPoint) String added in v0.13.0

func (t CutPoint) String() string

func (CutPoint) Unparse added in v0.13.0

func (t CutPoint) Unparse(g Grammar, e TreeElement, w io.Writer) (n int, err error)

type Cutpointdata added in v0.21.0

type Cutpointdata int32

type Delim added in v0.10.0

type Delim struct {
	Term            Term
	Sep             Term
	Assoc           Associativity
	CanStartWithSep bool
	CanEndWithSep   bool
}

func L2R added in v0.10.0

func L2R(term, sep Term) Delim

func NonAssoc added in v0.10.0

func NonAssoc(term, sep Term) Delim

func R2L added in v0.10.0

func R2L(term, sep Term) Delim

func (Delim) LRTerms added in v0.10.0

func (t Delim) LRTerms(node Node) LRTGen

func (Delim) Parser added in v0.10.0

func (t Delim) Parser(rule Rule, c cache) Parser

func (Delim) Resolve added in v0.10.0

func (t Delim) Resolve(oldRule, newRule Rule) Term

func (Delim) String added in v0.10.0

func (t Delim) String() string

func (Delim) Unparse added in v0.10.0

func (t Delim) Unparse(g Grammar, e TreeElement, w io.Writer) (n int, err error)

type Empty added in v0.10.0

type Empty struct{}

func (Empty) IsTreeElement added in v0.10.0

func (Empty) IsTreeElement()

type ExtRef added in v0.13.0

type ExtRef string

func (ExtRef) AsTerm added in v0.13.0

func (t ExtRef) AsTerm() Term

func (ExtRef) Parse added in v0.13.0

func (t ExtRef) Parse(scope Scope, input *Scanner, output *TreeElement, stk *call) (out error)

func (ExtRef) Parser added in v0.13.0

func (t ExtRef) Parser(name Rule, c cache) Parser

func (ExtRef) Resolve added in v0.13.0

func (t ExtRef) Resolve(oldRule, newRule Rule) Term

func (ExtRef) String added in v0.13.0

func (t ExtRef) String() string

func (ExtRef) Unparse added in v0.13.0

func (t ExtRef) Unparse(g Grammar, te TreeElement, w io.Writer) (n int, err error)

type ExternalRef added in v0.13.0

type ExternalRef func(scope Scope, input *Scanner) (TreeElement, error)

type ExternalRefs added in v0.13.0

type ExternalRefs map[string]ExternalRef

type Extra added in v0.6.0

type Extra interface {
	IsExtra()
}

type FatalError added in v0.13.0

type FatalError struct {
	ParseError
	Cutpointdata
}

type Grammar added in v0.10.0

type Grammar map[Rule]Term

func (Grammar) Compile added in v0.10.0

func (g Grammar) Compile(node any) Parsers

Compile prepares a grammar for parsing. The parser holds a copy of the grammar modified to support parser execution.

func (Grammar) String added in v0.10.0

func (g Grammar) String() string

func (Grammar) Unparse added in v0.10.0

func (g Grammar) Unparse(e TreeElement, w io.Writer) (n int, err error)

Unparse inverts the action of a parser, taking a generated AST and producing the source it came from. Currently, it doesn't quite do that, and is only being used for quick eyeballing to validate output.

type LRTGen added in v0.21.0

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

func (*LRTGen) Next added in v0.21.0

func (l *LRTGen) Next() Term

type LookAhead added in v0.32.0

type LookAhead struct {
	Term Term
}

func (LookAhead) Parser added in v0.32.0

func (t LookAhead) Parser(rule Rule, c cache) Parser

func (LookAhead) Resolve added in v0.32.0

func (t LookAhead) Resolve(oldRule, newRule Rule) Term

func (LookAhead) String added in v0.32.0

func (t LookAhead) String() string

func (LookAhead) Unparse added in v0.32.0

func (t LookAhead) Unparse(g Grammar, e TreeElement, w io.Writer) (n int, err error)

type Named added in v0.10.0

type Named struct {
	Name string
	Term Term
}

func Eq added in v0.10.0

func Eq(name string, term Term) Named

func (Named) AsTerm added in v0.13.0

func (t Named) AsTerm() Term

func (Named) Parser added in v0.10.0

func (t Named) Parser(rule Rule, c cache) Parser

func (Named) Resolve added in v0.10.0

func (t Named) Resolve(oldRule, newRule Rule) Term

func (Named) String added in v0.10.0

func (t Named) String() string

func (Named) Unparse added in v0.10.0

func (t Named) Unparse(g Grammar, e TreeElement, w io.Writer) (n int, err error)

type Node

type Node struct {
	Tag      string        `json:"tag"`
	Extra    Extra         `json:"extra"`
	Children []TreeElement `json:"nodes"`
}

func NewNode

func NewNode(tag string, extra Extra, children ...TreeElement) *Node

func (Node) Count

func (n Node) Count() int

func (Node) Format

func (n Node) Format(state fmt.State, c rune)

func (Node) Get

func (n Node) Get(path ...int) TreeElement

func (Node) GetNode

func (n Node) GetNode(path ...int) Node

func (Node) GetScanner

func (n Node) GetScanner(path ...int) Scanner

func (Node) GetString

func (n Node) GetString(path ...int) string

func (Node) IsTreeElement added in v0.6.0

func (Node) IsTreeElement()

func (Node) Normalize added in v0.6.0

func (n Node) Normalize() Node

func (Node) String

func (n Node) String() string

type Oneof added in v0.10.0

type Oneof []Term

func (Oneof) Parser added in v0.10.0

func (t Oneof) Parser(rule Rule, c cache) Parser

func (Oneof) Resolve added in v0.10.0

func (t Oneof) Resolve(oldRule, newRule Rule) Term

func (Oneof) String added in v0.10.0

func (t Oneof) String() string

func (Oneof) Unparse added in v0.10.0

func (t Oneof) Unparse(g Grammar, e TreeElement, w io.Writer) (n int, err error)

type ParseError added in v0.10.0

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

func (ParseError) Error added in v0.10.0

func (p ParseError) Error() string

type Parser

type Parser interface {
	Parse(scope Scope, input *Scanner, output *TreeElement, stk *call) error
	AsTerm() Term
}

type Parsers added in v0.10.0

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

Parsers holds Parsers generated by Grammar.Compile.

func (Parsers) Grammar added in v0.10.0

func (p Parsers) Grammar() Grammar

func (Parsers) HasRule added in v0.12.2

func (p Parsers) HasRule(rule Rule) bool

func (Parsers) MustParse added in v0.10.0

func (p Parsers) MustParse(rule Rule, input *Scanner) TreeElement

MustParse calls Parse and returns the result or panics if an error was returned.

func (Parsers) Node added in v0.10.0

func (p Parsers) Node() any

func (Parsers) Parse added in v0.10.0

func (p Parsers) Parse(rule Rule, input *Scanner) (TreeElement, error)

func (Parsers) ParseWithExternals added in v0.13.0

func (p Parsers) ParseWithExternals(rule Rule, input *Scanner, exts ExternalRefs) (TreeElement, error)

Parse parses some source per a given rule.

func (Parsers) Unparse added in v0.10.0

func (p Parsers) Unparse(e TreeElement, w io.Writer) (n int, err error)

type Quant added in v0.10.0

type Quant struct {
	Term Term
	Min  int
	Max  int // 0 = infinity
}

func Any added in v0.10.0

func Any(term Term) Quant

func Opt added in v0.10.0

func Opt(term Term) Quant

func Some added in v0.10.0

func Some(term Term) Quant

func (Quant) Contains added in v0.10.0

func (t Quant) Contains(i int) bool

func (Quant) MaxLessThan added in v0.10.0

func (t Quant) MaxLessThan(i int) bool

func (Quant) Parser added in v0.10.0

func (t Quant) Parser(rule Rule, c cache) Parser

func (Quant) Resolve added in v0.10.0

func (t Quant) Resolve(oldRule, newRule Rule) Term

func (Quant) String added in v0.10.0

func (t Quant) String() string

func (Quant) Unparse added in v0.10.0

func (t Quant) Unparse(g Grammar, e TreeElement, w io.Writer) (n int, err error)

type RE added in v0.10.0

type RE string

func (RE) Parser added in v0.10.0

func (t RE) Parser(rule Rule, c cache) Parser

func (RE) Resolve added in v0.10.0

func (t RE) Resolve(oldRule, newRule Rule) Term

func (RE) String added in v0.10.0

func (t RE) String() string

func (RE) Unparse added in v0.10.0

func (t RE) Unparse(g Grammar, e TreeElement, w io.Writer) (n int, err error)

type REF added in v0.10.0

type REF struct {
	Ident   string
	Default Term
}

func (REF) AsTerm added in v0.13.0

func (t REF) AsTerm() Term

func (*REF) Parse added in v0.10.0

func (t *REF) Parse(scope Scope, input *Scanner, output *TreeElement, stk *call) (out error)

func (REF) Parser added in v0.10.0

func (t REF) Parser(rule Rule, c cache) Parser

func (REF) Resolve added in v0.10.0

func (t REF) Resolve(oldRule, newRule Rule) Term

func (REF) String added in v0.10.0

func (t REF) String() string

func (REF) Unparse added in v0.10.0

func (t REF) Unparse(g Grammar, e TreeElement, w io.Writer) (n int, err error)

type Rule added in v0.10.0

type Rule string

func NodeRule added in v0.10.0

func NodeRule(e Node) Rule

func (Rule) Parser added in v0.10.0

func (t Rule) Parser(rule Rule, c cache) Parser

func (Rule) Resolve added in v0.10.0

func (t Rule) Resolve(oldRule, newRule Rule) Term

func (Rule) String added in v0.10.0

func (t Rule) String() string

func (Rule) Unparse added in v0.10.0

func (t Rule) Unparse(g Grammar, e TreeElement, w io.Writer) (n int, err error)

type S added in v0.10.0

type S string

func (S) Parser added in v0.10.0

func (t S) Parser(rule Rule, c cache) Parser

func (S) Resolve added in v0.10.0

func (t S) Resolve(oldRule, newRule Rule) Term

func (S) String added in v0.10.0

func (t S) String() string

func (S) Unparse added in v0.10.0

func (t S) Unparse(g Grammar, e TreeElement, w io.Writer) (n int, err error)

type Scanner

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

func MergeScanners added in v0.16.0

func MergeScanners(items ...Scanner) (Scanner, error)

func NewScanner

func NewScanner(str string) *Scanner

func NewScannerAt

func NewScannerAt(str string, offset, size int) *Scanner

func NewScannerWithFilename added in v0.14.0

func NewScannerWithFilename(str, filename string) *Scanner

func (Scanner) Contains added in v0.17.0

func (s Scanner) Contains(sn Scanner) bool

func (Scanner) Context

func (s Scanner) Context(limitLines int) string

func (*Scanner) Eat

func (s *Scanner) Eat(i int, eaten *Scanner) *Scanner

Eat returns a scanner containing the next i bytes and advances s past them.

func (*Scanner) EatRegexp

func (s *Scanner) EatRegexp(re *regexp.Regexp, match *Scanner, captures []Scanner) (n int, ok bool)

EatRegexp eats the text matching a regexp, populating match (if != nil) with the whole match and captures (if != nil) with any captured groups. Returns n as the number of captures set and ok iff a match was found.

func (*Scanner) EatString

func (s *Scanner) EatString(str string, eaten *Scanner) bool

func (Scanner) Filename added in v0.14.0

func (s Scanner) Filename() string

The name of the file from which the source is derived (or empty if none).

func (Scanner) Format

func (s Scanner) Format(state fmt.State, c rune)

func (Scanner) IsNil added in v0.16.0

func (s Scanner) IsNil() bool

func (Scanner) IsTreeElement added in v0.6.0

func (Scanner) IsTreeElement()

func (Scanner) Offset

func (s Scanner) Offset() int

The position of the start of the scanner within the original source.

func (Scanner) Position added in v0.14.0

func (s Scanner) Position() (int, int)

The 1-indexed line and column number of the start of the scanner within the original source.

func (Scanner) Skip

func (s Scanner) Skip(i int) *Scanner

func (Scanner) Slice

func (s Scanner) Slice(a, b int) *Scanner

func (Scanner) String

func (s Scanner) String() string

func (Scanner) StripSource added in v0.4.0

func (s Scanner) StripSource() Scanner

type Scope added in v0.13.0

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

func (Scope) GetCutPoint added in v0.13.0

func (s Scope) GetCutPoint() Cutpointdata

func (Scope) GetExternal added in v0.13.0

func (s Scope) GetExternal(ident string) ExternalRef

func (Scope) GetVal added in v0.13.0

func (s Scope) GetVal(ident string) (Parser, TreeElement, bool)

func (Scope) Has added in v0.13.0

func (s Scope) Has(ident string) bool

func (Scope) Keys added in v0.13.0

func (s Scope) Keys() frozen.Set[string]

func (Scope) Merge added in v0.28.0

func (s Scope) Merge(t Scope) Scope

func (Scope) ReplaceCutPoint added in v0.13.0

func (s Scope) ReplaceCutPoint(force bool) (newScope Scope, prev, replacement Cutpointdata)

func (Scope) String added in v0.13.0

func (s Scope) String() string

func (Scope) With added in v0.13.0

func (s Scope) With(ident string, v any) Scope

func (Scope) WithExternals added in v0.13.0

func (s Scope) WithExternals(extRefs ExternalRefs) Scope

func (Scope) WithVal added in v0.13.0

func (s Scope) WithVal(ident string, p Parser, val TreeElement) Scope

type ScopedGrammar added in v0.13.0

type ScopedGrammar struct {
	Term    Term
	Grammar Grammar
}

func (ScopedGrammar) Parser added in v0.13.0

func (t ScopedGrammar) Parser(name Rule, c cache) Parser

func (ScopedGrammar) Resolve added in v0.13.0

func (t ScopedGrammar) Resolve(oldRule, newRule Rule) Term

func (ScopedGrammar) String added in v0.13.0

func (t ScopedGrammar) String() string

func (ScopedGrammar) Unparse added in v0.13.0

func (t ScopedGrammar) Unparse(g Grammar, e TreeElement, w io.Writer) (n int, err error)

type Seq added in v0.10.0

type Seq []Term

func (Seq) Parser added in v0.10.0

func (t Seq) Parser(rule Rule, c cache) Parser

func (Seq) Resolve added in v0.10.0

func (t Seq) Resolve(oldRule, newRule Rule) Term

func (Seq) String added in v0.10.0

func (t Seq) String() string

func (Seq) Unparse added in v0.10.0

func (t Seq) Unparse(g Grammar, e TreeElement, w io.Writer) (n int, err error)

type Stack added in v0.10.0

type Stack []Term

func (Stack) Parser added in v0.10.0

func (t Stack) Parser(_ Rule, _ cache) Parser

func (Stack) Resolve added in v0.10.0

func (t Stack) Resolve(oldRule, newRule Rule) Term

func (Stack) String added in v0.10.0

func (t Stack) String() string

func (Stack) Term added in v0.13.0

func (t Stack) Term() Term

func (Stack) Unparse added in v0.10.0

func (t Stack) Unparse(_ Grammar, _ TreeElement, _ io.Writer) (int, error)

type StopError added in v0.30.0

type StopError interface {
	IsStopError()
}

type Term added in v0.10.0

type Term interface {
	fmt.Stringer
	Parser(name Rule, c cache) Parser
	Unparse(g Grammar, e TreeElement, w io.Writer) (n int, err error)
	Resolve(oldRule, newRule Rule) Term
}

Term represents the terms of a grammar specification.

type TreeElement added in v0.6.0

type TreeElement interface {
	IsTreeElement()
}

type UnconsumedInputError added in v0.13.0

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

func UnconsumedInput added in v0.13.0

func UnconsumedInput(residue Scanner, result TreeElement) UnconsumedInputError

UnconsumedInput is returned by a successful parse that didn't fully consume the input.

func (UnconsumedInputError) Error added in v0.13.0

func (e UnconsumedInputError) Error() string

func (UnconsumedInputError) Residue added in v0.13.0

func (e UnconsumedInputError) Residue() *Scanner

func (UnconsumedInputError) Result added in v0.13.0

func (e UnconsumedInputError) Result() TreeElement

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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