frontend

package
v0.2.3 Latest Latest
Warning

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

Go to latest
Published: Jul 15, 2022 License: BSD-3-Clause Imports: 6 Imported by: 1

Documentation

Overview

Package frontend parses regular expressions and compiles them into NFA bytecode

Index

Constants

This section is empty.

Variables

View Source
var DEBUG = false

Turn on debug prints

Functions

func Generate

func Generate(ast AST) (inst.Slice, error)

Generate an NFA program from the AST for a regular expression.

func LineCol

func LineCol(text []byte, tc int) (line int, col int)

LineCol computes the line and column of a particular index inside of a byte slice.

Types

type AST

type AST interface {
	String() string
	Children() []AST
	Equals(AST) bool
}

AST is an abstract syntax tree for a regular expression.

func DesugarRanges

func DesugarRanges(ast AST) AST

DesugarRanges transform all Range nodes into Alternatives with individual characters

func NewAltMatch

func NewAltMatch(a, b AST) AST

NewAltMatch creates an AltMatch

func NewAlternation

func NewAlternation(choice, alternation AST) AST

NewAlternation creates an Alternation

func NewApplyOp

func NewApplyOp(op, atomic AST) AST

NewApplyOp applies a given op (Star, Plus, Maybe) to a tree

func NewConcat

func NewConcat(char, concat AST) AST

NewConcat concatenates two tree together

func NewEOS

func NewEOS() AST

NewEOS creates a EOS

func NewMatch

func NewMatch(ast AST) AST

NewMatch create a Match

func NewOp

func NewOp(op string) AST

NewOp constructs a Star, Plus, or Maybe from *, +, ? respectively

func Parse

func Parse(text []byte) (AST, error)

Parse a regular expression into an Abstract Syntax Tree (AST)

type AltMatch

type AltMatch struct {
	A AST
	B AST
}

AltMatch either match A or B and then finalize matching the string

func (*AltMatch) Children

func (a *AltMatch) Children() []AST

Children returns a list of the child nodes

func (*AltMatch) Equals

func (a *AltMatch) Equals(o AST) bool

Equals checks deep equality of the two trees

func (*AltMatch) String

func (a *AltMatch) String() string

String humanizes the subtree

type Alternation

type Alternation struct {
	A AST
	B AST
}

Alternation matches A or B

func (*Alternation) Children

func (a *Alternation) Children() []AST

Children returns a list of the child nodes

func (*Alternation) Equals

func (a *Alternation) Equals(o AST) bool

Equals checks deep equality of the two trees

func (*Alternation) String

func (a *Alternation) String() string

String humanizes the subtree

type Character

type Character struct {
	Char byte
}

Character matches a single byte

func NewCharacter

func NewCharacter(b byte) *Character

NewCharacter constructs a character

func (*Character) Children

func (c *Character) Children() []AST

Children returns a list of the child nodes

func (*Character) Equals

func (c *Character) Equals(o AST) bool

Equals checks deep equality of the two trees

func (*Character) String

func (c *Character) String() string

String humanizes the subtree

type Concat

type Concat struct {
	Items []AST
}

Concat matches each item in sequence

func (*Concat) Children

func (c *Concat) Children() []AST

Children returns a list of the child nodes

func (*Concat) Equals

func (c *Concat) Equals(o AST) bool

Equals checks deep equality of the two trees

func (*Concat) String

func (c *Concat) String() string

String humanizes the subtree

type EOS

type EOS struct{}

EOS end of string

func (*EOS) Children

func (e *EOS) Children() []AST

Children returns a list of the child nodes

func (*EOS) Equals

func (e *EOS) Equals(o AST) bool

Equals checks deep equality of the two trees

func (*EOS) String

func (e *EOS) String() string

String humanizes the subtree

type Match

type Match struct {
	AST
}

Match the tree AST finalizes the matching

func (*Match) Children

func (m *Match) Children() []AST

Children returns a list of the child nodes

func (*Match) Equals

func (m *Match) Equals(o AST) bool

Equals checks deep equality of the two trees

func (*Match) String

func (m *Match) String() string

String humanizes the subtree

type Maybe

type Maybe struct {
	AST
}

Maybe matches 0 or 1 times

func (*Maybe) Children

func (m *Maybe) Children() []AST

Children returns a list of the child nodes

func (*Maybe) Equals

func (m *Maybe) Equals(o AST) bool

Equals checks deep equality of the two trees

func (*Maybe) String

func (m *Maybe) String() string

String humanizes the subtree

type ParseError

type ParseError struct {
	Reason     string
	Production string
	TC         int
	// contains filtered or unexported fields
}

ParseError gives structured errors for parsing problems.

func Errorf

func Errorf(text []byte, tc int, format string, args ...interface{}) *ParseError

Errorf constructs a parse error with format for a particular location.

func (*ParseError) Chain

func (p *ParseError) Chain(e *ParseError) *ParseError

Chain joins multiple ParseErrors together

func (*ParseError) Error

func (p *ParseError) Error() string

Error implements the error interface

func (*ParseError) String

func (p *ParseError) String() string

String formats the error for humans

type Plus

type Plus struct {
	AST
}

Plus matches 1 or more times

func (*Plus) Children

func (p *Plus) Children() []AST

Children returns a list of the child nodes

func (*Plus) Equals

func (p *Plus) Equals(o AST) bool

Equals checks deep equality of the two trees

func (*Plus) String

func (p *Plus) String() string

String humanizes the subtree

type Range

type Range struct {
	From byte
	To   byte
}

Range matches byte ranges From-To inclusive

func NewAny

func NewAny() *Range

NewAny constructs the . operator

func NewRange

func NewRange(from, to byte) *Range

NewRange constructs a range operator

func (*Range) Children

func (r *Range) Children() []AST

Children returns a list of the child nodes

func (*Range) Equals

func (r *Range) Equals(o AST) bool

Equals checks deep equality of the two trees

func (*Range) String

func (r *Range) String() string

String humanizes the subtree

type Star

type Star struct {
	AST
}

Star is a kleene star (that is a repetition operator). Matches 0 or more times.

func (*Star) Children

func (s *Star) Children() []AST

Children returns a list of the child nodes

func (*Star) Equals

func (s *Star) Equals(o AST) bool

Equals checks deep equality of the two trees

func (*Star) String

func (s *Star) String() string

String humanizes the subtree

Jump to

Keyboard shortcuts

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