matcher

package
v1.4.0 Latest Latest
Warning

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

Go to latest
Published: Apr 21, 2025 License: Apache-2.0 Imports: 5 Imported by: 2

Documentation

Index

Constants

View Source
const (
	DbgFlagMatchVar dbgFlags = 1 << iota
	DbgFlagAll               = DbgFlagMatchVar
)

Variables

View Source
var (
	// ErrVarAssigned error
	ErrVarAssigned = errors.New("variable is already assigned")
)

Functions

func SetDebug added in v1.3.8

func SetDebug(flags dbgFlags)

Types

type Choices added in v1.3.8

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

Choices represents a choice matcher.

func Choice

func Choice(options ...Matcher) *Choices

Choice: R1 | R2 | ... | Rn Should be used with CheckConflicts.

func (*Choices) CheckConflicts added in v1.3.8

func (p *Choices) CheckConflicts(conflict func(firsts [][]any, i, at int))

func (*Choices) First added in v1.3.8

func (p *Choices) First(in []any) (first []any, mayEmpty bool)

func (*Choices) Match added in v1.3.8

func (p *Choices) Match(src []*types.Token, ctx *Context) (n int, result any, err error)

type Context

type Context struct {
	Fset    *token.FileSet
	FileEnd token.Pos

	Left    int
	LastErr error
	// contains filtered or unexported fields
}

Context represents the context of a matching process.

func NewContext added in v1.3.7

func NewContext(fset *token.FileSet, fileEnd token.Pos, toks []*types.Token) *Context

NewContext creates a new matching context.

func (*Context) NewError

func (p *Context) NewError(pos token.Pos, msg string) *Error

NewError creates a new error.

func (*Context) NewErrorf

func (p *Context) NewErrorf(pos token.Pos, format string, args ...any) error

NewErrorf creates a new error with a format string.

func (*Context) SetLastError added in v1.3.7

func (p *Context) SetLastError(left int, err error)

SetLastError sets the last error.

type Error

type Error struct {
	Fset *token.FileSet
	Pos  token.Pos
	Msg  string

	// is a runtime error
	Dyn bool
}

Error represents a matching error.

func (*Error) Error

func (p *Error) Error() string

type ListRetProc added in v1.3.7

type ListRetProc = func([]any) any

type MatchToken added in v1.3.8

type MatchToken struct {
	Tok token.Token
	Lit string
}

MatchToken represents a matching literal.

func (*MatchToken) String added in v1.3.8

func (p *MatchToken) String() string

type Matcher

type Matcher interface {
	Match(src []*types.Token, ctx *Context) (n int, result any, err error)
	First(in []any) (first []any, mayEmpty bool) // can be token.Token or *MatchToken
}

Matcher represents a matcher.

func Adjoin added in v1.3.8

func Adjoin(a, b Matcher) Matcher

Adjoin: R1 ++ R2

func List

func List(a, b Matcher) Matcher

List: R1 % R2 is equivalent to R1 *(R2 R1)

func Literal

func Literal(tok token.Token, lit string) Matcher

Literal: "abc", 'a', 123, 1.23, etc.

func Repeat0

func Repeat0(r Matcher) Matcher

Repeat0: *R

func Repeat01

func Repeat01(r Matcher) Matcher

Repeat01: ?R

func Repeat1

func Repeat1(r Matcher) Matcher

Repeat1: +R

func Sequence

func Sequence(items ...Matcher) Matcher

Sequence: R1 R2 ... Rn Should length of items > 0

func String added in v1.3.8

func String(quoteCh byte) Matcher

String returns a matcher that matches a string literal.

func Token

func Token(tok token.Token) Matcher

Token: ADD, SUB, IDENT, INT, FLOAT, CHAR, STRING, etc.

func True added in v1.3.8

func True() Matcher

True returns a matcher that always succeeds.

func WhiteSpace added in v1.3.8

func WhiteSpace() Matcher

WhiteSpace returns a matcher that matches whitespace.

type RecursiveError added in v1.3.8

type RecursiveError struct {
	*Var
}

RecursiveError represents a recursive error.

func (RecursiveError) Error added in v1.3.8

func (e RecursiveError) Error() string

type RetProc added in v1.3.7

type RetProc = func(any) any

type Var

type Var struct {
	Elem Matcher
	Name string
	Pos  token.Pos

	RetProc any
}

func NewVar

func NewVar(pos token.Pos, name string) *Var

NewVar creates a new Var instance.

func (*Var) Assign

func (p *Var) Assign(elem Matcher) error

Assign assigns a value to this variable.

func (*Var) First added in v1.3.8

func (p *Var) First(in []any) (first []any, mayEmpty bool)

func (*Var) Match

func (p *Var) Match(src []*types.Token, ctx *Context) (n int, result any, err error)

Jump to

Keyboard shortcuts

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