Documentation ¶
Overview ¶
Package pattern provides data types and functions for compiling patterns into GPeg VM programs.
Index ¶
- Variables
- func Compile(p Pattern) (isa.Program, error)
- func CountSubPatterns(p Pattern) int
- func MustCompile(p Pattern) isa.Program
- func Optimize(p isa.Program)
- func Prettify(p Pattern) string
- func WalkPattern(p Pattern, followInline bool, fn WalkFunc)
- type AltNode
- type AndNode
- type CapNode
- type CheckNode
- type ClassNode
- type DotNode
- type EmptyNode
- type EmptyOpNode
- type ErrorNode
- type GrammarNode
- type LiteralNode
- type MemoNode
- type NonTermNode
- type NotFoundError
- type NotNode
- type OptionalNode
- type Pattern
- func And(p Pattern) Pattern
- func Any(n uint8) Pattern
- func Cap(p Pattern, id int) Pattern
- func CapGrammar(start string, nonterms map[string]Pattern) Pattern
- func Check(p Pattern, c isa.Checker) Pattern
- func CheckFlags(p Pattern, c isa.Checker, id, flag int) Pattern
- func Concat(patts ...Pattern) Pattern
- func EmptyOp(op syntax.EmptyOp) Pattern
- func Error(msg string, recovery Pattern) Pattern
- func Get(p Pattern) Pattern
- func Grammar(start string, nonterms map[string]Pattern) Pattern
- func Literal(s string) Pattern
- func Memo(p Pattern) Pattern
- func MemoId(p Pattern, id int) Pattern
- func NonTerm(name string) Pattern
- func Not(p Pattern) Pattern
- func Optional(p Pattern) Pattern
- func Or(patts ...Pattern) Pattern
- func Plus(p Pattern) Pattern
- func Repeat(p Pattern, n int) Pattern
- func Search(p Pattern) Pattern
- func Set(chars charset.Set) Pattern
- func Star(p Pattern) Pattern
- type PlusNode
- type RepeatNode
- type SearchNode
- type SeqNode
- type StarNode
- type WalkFunc
Constants ¶
This section is empty.
Variables ¶
var InlineThreshold = 100
Nodes with trees larger than this size will not be inlined.
Functions ¶
func Compile ¶
Compile takes an input pattern and returns the result of compiling it into a parsing program, and optimizing the program.
func CountSubPatterns ¶
CountSubPatterns returns the number of subpatterns that exist in the given pattern.
func MustCompile ¶
MustCompile is the same as Compile but panics if there is an error during compilation.
func Optimize ¶
Optimize performs some optimization passes on the code in p. In particular it performs head-fail optimization and jump replacement.
func WalkPattern ¶
WalkPattern calls fn for every subpattern contained in p. If followInline is true, WalkPattern will walk over inlined patterns as well.
Types ¶
type AltNode ¶
type AltNode struct {
Left, Right Pattern
}
AltNode is the binary operator for alternation.
type EmptyOpNode ¶
EmptyOpNode is a node that performs a zero-width assertion.
type GrammarNode ¶
GrammarNode represents a grammar of non-terminals and their associated patterns. The Grammar must also have an entry non-terminal.
func (*GrammarNode) Compile ¶
func (p *GrammarNode) Compile() (isa.Program, error)
Compile this node.
func (*GrammarNode) Inline ¶
func (p *GrammarNode) Inline()
Inline performs inlining passes until the inliner reaches a steady-state.
type NonTermNode ¶
NonTermNode represents the use of a non-terminal. If this non-terminal is inlined during compilation, the `inlined` field will point to the pattern that is inlined.
type NotFoundError ¶
type NotFoundError struct {
Name string
}
A NotFoundError means a a non-terminal was not found during grammar compilation.
func (*NotFoundError) Error ¶
func (e *NotFoundError) Error() string
Error returns the error message.
type OptionalNode ¶
type OptionalNode struct {
Patt Pattern
}
OptionalNode is the operator for making a pattern optional.
type Pattern ¶
A Pattern is an object that can be compiled into a parsing program.
func And ¶
And returns the and predicate applied to a pattern: `&p`. The and predicate succeeds if matching `p` at the current position succeeds and does not consume any input. This is equivalent to `!!p`.
func Get ¶
Get returns a possibly optimized version of this pattern. Always use this function to read a pattern, especially if you will be using the types of the underlying nodes. This function performs optimizations like collapsing an alternation of two class nodes into one class node.
func Grammar ¶
Grammar builds a grammar from a map of non-terminal patterns. Any unresolved non-terminals are resolved with their definitions in the map.
func NonTerm ¶
NonTerm builds an unresolved non-terminal with a given name. NonTerms should be used together with `Grammar` to build a recursive grammar.
func Not ¶
Not returns the not predicate applied to a pattern: `!p`. The not predicate succeeds if matching `p` at the current position fails, and does not consume any input.
func Plus ¶
Plus returns the Kleene plus repetition of a pattern: `p+`. This matches one or more occurrences of p.
type RepeatNode ¶
RepeatNode represents the repetition of a pattern a constant number of times.
type SearchNode ¶
type SearchNode struct {
Patt Pattern
}
SearchNode represents a search for a certain pattern.
type SeqNode ¶
type SeqNode struct {
Left, Right Pattern
}
SeqNode is the binary operator for sequences.