pattern

package
v0.0.0-...-7534ea8 Latest Latest
Warning

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

Go to latest
Published: Jul 2, 2022 License: MIT Imports: 5 Imported by: 4

Documentation

Overview

Package pattern provides data types and functions for compiling patterns into GPeg VM programs.

Index

Constants

This section is empty.

Variables

View Source
var InlineThreshold = 100

Nodes with trees larger than this size will not be inlined.

Functions

func Compile

func Compile(p Pattern) (isa.Program, error)

Compile takes an input pattern and returns the result of compiling it into a parsing program, and optimizing the program.

func CountSubPatterns

func CountSubPatterns(p Pattern) int

CountSubPatterns returns the number of subpatterns that exist in the given pattern.

func MustCompile

func MustCompile(p Pattern) isa.Program

MustCompile is the same as Compile but panics if there is an error during compilation.

func Optimize

func Optimize(p isa.Program)

Optimize performs some optimization passes on the code in p. In particular it performs head-fail optimization and jump replacement.

func Prettify

func Prettify(p Pattern) string

func WalkPattern

func WalkPattern(p Pattern, followInline bool, fn WalkFunc)

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.

func (*AltNode) Compile

func (p *AltNode) Compile() (isa.Program, error)

Compile this node.

type AndNode

type AndNode struct {
	Patt Pattern
}

AndNode is the and predicate.

func (*AndNode) Compile

func (p *AndNode) Compile() (isa.Program, error)

Compile this node.

type CapNode

type CapNode struct {
	Patt Pattern
	Id   int
}

CapNode marks a pattern to be captured with a certain ID.

func (*CapNode) Compile

func (p *CapNode) Compile() (isa.Program, error)

Compile this node.

type CheckNode

type CheckNode struct {
	Patt     Pattern
	Checker  isa.Checker
	Id, Flag int
}

CheckNode marks a pattern to be checker by a certain checker.

func (*CheckNode) Compile

func (p *CheckNode) Compile() (isa.Program, error)

Compile this node.

type ClassNode

type ClassNode struct {
	Chars charset.Set
}

ClassNode represents a character set.

func (*ClassNode) Compile

func (p *ClassNode) Compile() (isa.Program, error)

Compile this node.

type DotNode

type DotNode struct {
	N uint8
}

DotNode represents the pattern to match any byte.

func (*DotNode) Compile

func (p *DotNode) Compile() (isa.Program, error)

Compile this node.

type EmptyNode

type EmptyNode struct {
}

EmtpyNode represents the empty pattern.

func (*EmptyNode) Compile

func (p *EmptyNode) Compile() (isa.Program, error)

Compile this node.

type EmptyOpNode

type EmptyOpNode struct {
	Op syntax.EmptyOp
}

EmptyOpNode is a node that performs a zero-width assertion.

func (*EmptyOpNode) Compile

func (p *EmptyOpNode) Compile() (isa.Program, error)

Compile this node.

type ErrorNode

type ErrorNode struct {
	Message string
	Recover Pattern
}

ErrorNode represents a pattern that fails with a certain error message.

func (*ErrorNode) Compile

func (p *ErrorNode) Compile() (isa.Program, error)

Compile this node.

type GrammarNode

type GrammarNode struct {
	Defs  map[string]Pattern
	Start string
}

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 LiteralNode

type LiteralNode struct {
	Str string
}

LiteralNode represents a literal string.

func (*LiteralNode) Compile

func (p *LiteralNode) Compile() (isa.Program, error)

Compile this node.

type MemoNode

type MemoNode struct {
	Patt Pattern
	Id   int
}

MemoNode marks a pattern to be memoized with a certain ID.

func (*MemoNode) Compile

func (p *MemoNode) Compile() (isa.Program, error)

Compile this node.

type NonTermNode

type NonTermNode struct {
	Name    string
	Inlined Pattern
}

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.

func (*NonTermNode) Compile

func (p *NonTermNode) Compile() (isa.Program, error)

Compile this node.

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 NotNode

type NotNode struct {
	Patt Pattern
}

NotNode is the not predicate.

func (*NotNode) Compile

func (p *NotNode) Compile() (isa.Program, error)

Compile this node.

type OptionalNode

type OptionalNode struct {
	Patt Pattern
}

OptionalNode is the operator for making a pattern optional.

func (*OptionalNode) Compile

func (p *OptionalNode) Compile() (isa.Program, error)

Compile this node.

type Pattern

type Pattern interface {
	Compile() (isa.Program, error)
}

A Pattern is an object that can be compiled into a parsing program.

func And

func And(p Pattern) Pattern

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 Any

func Any(n uint8) Pattern

Any consumes n characters, and only fails if there aren't enough input characters left.

func Cap

func Cap(p Pattern, id int) Pattern

Cap marks a pattern to be captured.

func CapGrammar

func CapGrammar(start string, nonterms map[string]Pattern) Pattern

func Check

func Check(p Pattern, c isa.Checker) Pattern

Check marks a pattern to be checked with the given checker.

func CheckFlags

func CheckFlags(p Pattern, c isa.Checker, id, flag int) Pattern

func Concat

func Concat(patts ...Pattern) Pattern

Concat concatenates n patterns: `p1 p2 p3...`.

func EmptyOp

func EmptyOp(op syntax.EmptyOp) Pattern

func Error

func Error(msg string, recovery Pattern) Pattern

Error is a pattern that throws an error with the given message.

func Get

func Get(p Pattern) Pattern

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

func Grammar(start string, nonterms map[string]Pattern) Pattern

Grammar builds a grammar from a map of non-terminal patterns. Any unresolved non-terminals are resolved with their definitions in the map.

func Literal

func Literal(s string) Pattern

Literal matches a given string literal.

func Memo

func Memo(p Pattern) Pattern

Memo marks a pattern as memoizable.

func MemoId

func MemoId(p Pattern, id int) Pattern

MemoId marks a pattern as memoizable with a particular ID.

func NonTerm

func NonTerm(name string) Pattern

NonTerm builds an unresolved non-terminal with a given name. NonTerms should be used together with `Grammar` to build a recursive grammar.

func Not

func Not(p Pattern) Pattern

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 Optional

func Optional(p Pattern) Pattern

Optional matches at most 1 occurrence of p: `p?`.

func Or

func Or(patts ...Pattern) Pattern

Or returns the ordered choice between n patterns: `p1 / p2 / p3...`.

func Plus

func Plus(p Pattern) Pattern

Plus returns the Kleene plus repetition of a pattern: `p+`. This matches one or more occurrences of p.

func Repeat

func Repeat(p Pattern, n int) Pattern

Repeat matches p exactly n times

func Search(p Pattern) Pattern

Search is a dedicated operator for creating searches. It will match the first occurrence of the given pattern. Use Star(Search(p)) to match the last occurrence (for a non-overlapping pattern).

func Set

func Set(chars charset.Set) Pattern

Set matches any character in the given set.

func Star

func Star(p Pattern) Pattern

Star returns the Kleene star repetition of a pattern: `p*`. This matches zero or more occurrences of p.

type PlusNode

type PlusNode struct {
	Patt Pattern
}

PlusNode is the operator for the Kleene plus.

func (*PlusNode) Compile

func (p *PlusNode) Compile() (isa.Program, error)

Compile this node.

type RepeatNode

type RepeatNode struct {
	Patt Pattern
	N    int
}

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.

func (*SearchNode) Compile

func (p *SearchNode) Compile() (isa.Program, error)

Compile this node.

type SeqNode

type SeqNode struct {
	Left, Right Pattern
}

SeqNode is the binary operator for sequences.

func (*SeqNode) Compile

func (p *SeqNode) Compile() (isa.Program, error)

Compile this node.

type StarNode

type StarNode struct {
	Patt Pattern
}

StarNode is the operator for the Kleene star.

func (*StarNode) Compile

func (p *StarNode) Compile() (isa.Program, error)

Compile this node.

type WalkFunc

type WalkFunc func(sub Pattern)

WalkFunc is a function that takes a pattern.

Jump to

Keyboard shortcuts

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