script

package
v0.0.0-...-20810c9 Latest Latest
Warning

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

Go to latest
Published: Mar 21, 2024 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	MatchYes = MatchResult{Matched: true}
	MatchNo  = MatchResult{Matched: false}
)
View Source
var ErrCharsNoEnd = errors.New("missing ] at end of chars")
View Source
var ErrEndOfInput = errors.New("end of input")
View Source
var ErrOneOfDelimiter = errors.New("one of delimiter")
View Source
var ErrOneOfEnd = errors.New("one of end")
View Source
var ErrOneOfNoEnd = errors.New("missing ) at end of one of")
View Source
var ErrRangeInvalid = errors.New("range has invalid numbers, min is optional but must be 0 or more and max is optional and must be more than or equal to min")
View Source
var ErrRangeNoDelimiter = errors.New("missing , in range")
View Source
var ErrRangeNoEnd = errors.New("missing } at end of range")
View Source
var ErrReadToEnd = errors.New("read to fail")
View Source
var ErrRuleNoEnd = errors.New("missing > at end of rule")
View Source
var ErrUnexpectedToken = errors.New("unexpected token")

Functions

func StdShortcuts

func StdShortcuts() map[rune]Matcher

Types

type Buffer

type Buffer[V any] struct {
	// contains filtered or unexported fields
}

func NewBuffer

func NewBuffer[V any](initialCapacity int) Buffer[V]

func (*Buffer[V]) Add

func (r *Buffer[V]) Add(data V)

func (*Buffer[V]) Clear

func (r *Buffer[V]) Clear(initialCapacity int)

func (Buffer[V]) Ended

func (r Buffer[V]) Ended() bool

func (Buffer[V]) Last

func (r Buffer[V]) Last() *V

func (Buffer[V]) Len

func (r Buffer[V]) Len() int

func (Buffer[V]) Peek

func (r Buffer[V]) Peek() *V

func (Buffer[V]) Pos

func (r Buffer[V]) Pos() int

func (*Buffer[V]) Read

func (r *Buffer[V]) Read() *V

func (*Buffer[V]) RemoveLast

func (r *Buffer[V]) RemoveLast() *V

func (*Buffer[V]) Reset

func (r *Buffer[V]) Reset(i int)

func (*Buffer[V]) Set

func (r *Buffer[V]) Set(data []V)

type Input

type Input struct {
	Matchers []MatcherMatch
	Runes    *RuneBuffer
	// contains filtered or unexported fields
}

The parsed output

type LogFn

type LogFn func(string, ...any)

type Logger

type Logger struct {
	OnGetGreedyMatches      func(input *RuneBuffer, out *RuleMatch, repeater, repeating Matcher, repeatMin, repeatMax, greedyMatches int, start Position)
	OnSetGreedyMatches      func(input *RuneBuffer, out *RuleMatch, repeater, repeating Matcher, repeatMin, repeatMax, greedyMatches int, start Position)
	OnDecreaseGreedyMatches func(input *RuneBuffer, out *RuleMatch, resetToMatcher Matcher, resetTo int, greediness int)
	OnMatchError            func(input *RuneBuffer, out *RuleMatch, m Matcher, err error)
	OnMatch                 func(input *RuneBuffer, out *RuleMatch, m Matcher, start Position)
	OnNotMatch              func(input *RuneBuffer, out *RuleMatch, m Matcher, start Position)
}

func NewFuncLogger

func NewFuncLogger(log LogFn) Logger

func NewStdLogger

func NewStdLogger() Logger

func (*Logger) Quiet

func (l *Logger) Quiet(greedy, matchError, notMatch, match bool)

type MatchAny

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

func (MatchAny) ID

func (m MatchAny) ID() int

func (MatchAny) Match

func (m MatchAny) Match(rs RuleSet, input *RuneBuffer, out *RuleMatch) (bool, error)

func (MatchAny) String

func (m MatchAny) String() string

type MatchChars

type MatchChars struct {
	Chars Set[rune]
	Not   bool
	// contains filtered or unexported fields
}

func (MatchChars) ID

func (m MatchChars) ID() int

func (MatchChars) Match

func (m MatchChars) Match(rs RuleSet, input *RuneBuffer, out *RuleMatch) (bool, error)

func (MatchChars) String

func (m MatchChars) String() string

type MatchLineEnd

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

func (MatchLineEnd) ID

func (m MatchLineEnd) ID() int

func (MatchLineEnd) Match

func (m MatchLineEnd) Match(rs RuleSet, input *RuneBuffer, out *RuleMatch) (bool, error)

func (MatchLineEnd) String

func (m MatchLineEnd) String() string

type MatchLineStart

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

func (MatchLineStart) ID

func (m MatchLineStart) ID() int

func (MatchLineStart) Match

func (m MatchLineStart) Match(rs RuleSet, input *RuneBuffer, out *RuleMatch) (bool, error)

func (MatchLineStart) String

func (m MatchLineStart) String() string

type MatchLiteral

type MatchLiteral struct {
	Literal rune
	// contains filtered or unexported fields
}

func (MatchLiteral) ID

func (m MatchLiteral) ID() int

func (MatchLiteral) Match

func (m MatchLiteral) Match(rs RuleSet, input *RuneBuffer, out *RuleMatch) (bool, error)

func (MatchLiteral) String

func (m MatchLiteral) String() string

type MatchMaybe

type MatchMaybe struct {
	Maybe Matcher
	// contains filtered or unexported fields
}

func (MatchMaybe) ID

func (m MatchMaybe) ID() int

func (MatchMaybe) Match

func (m MatchMaybe) Match(rs RuleSet, input *RuneBuffer, out *RuleMatch) (bool, error)

func (MatchMaybe) String

func (m MatchMaybe) String() string

type MatchOneOf

type MatchOneOf struct {
	OneOf   []Buffer[Matcher]
	Capture int
	Reset   bool
	Not     bool
	// contains filtered or unexported fields
}

func (MatchOneOf) ID

func (m MatchOneOf) ID() int

func (MatchOneOf) Match

func (m MatchOneOf) Match(rs RuleSet, input *RuneBuffer, out *RuleMatch) (bool, error)

func (MatchOneOf) String

func (m MatchOneOf) String() string

type MatchReference

type MatchReference struct {
	Captured int
	// contains filtered or unexported fields
}

func (MatchReference) ID

func (m MatchReference) ID() int

func (MatchReference) Match

func (m MatchReference) Match(rs RuleSet, input *RuneBuffer, out *RuleMatch) (bool, error)

func (MatchReference) String

func (m MatchReference) String() string

type MatchRepeat

type MatchRepeat struct {
	Repeat Matcher
	// contains filtered or unexported fields
}

func (MatchRepeat) ID

func (m MatchRepeat) ID() int

func (MatchRepeat) Match

func (m MatchRepeat) Match(rs RuleSet, input *RuneBuffer, out *RuleMatch) (bool, error)

func (MatchRepeat) String

func (m MatchRepeat) String() string

type MatchRepeatMaybe

type MatchRepeatMaybe struct {
	RepeatMaybe Matcher
	// contains filtered or unexported fields
}

func (MatchRepeatMaybe) ID

func (m MatchRepeatMaybe) ID() int

func (MatchRepeatMaybe) Match

func (m MatchRepeatMaybe) Match(rs RuleSet, input *RuneBuffer, out *RuleMatch) (bool, error)

func (MatchRepeatMaybe) String

func (m MatchRepeatMaybe) String() string

type MatchRepeatRange

type MatchRepeatRange struct {
	RepeatRange Matcher
	Min         int
	Max         int
	// contains filtered or unexported fields
}

func (MatchRepeatRange) ID

func (m MatchRepeatRange) ID() int

func (MatchRepeatRange) Match

func (m MatchRepeatRange) Match(rs RuleSet, input *RuneBuffer, out *RuleMatch) (bool, error)

func (MatchRepeatRange) String

func (m MatchRepeatRange) String() string

type MatchResult

type MatchResult struct {
	Matched bool
	// A matcher that matched can have this number of alternative states.
	// For a repeating matcher this represents how many greedy matches over the
	// minimum it had. For a matcher with multiple options this represents the
	// number of options it never tried. This can be passed down to the Match
	// method with a decreased amount to force the matcher to try the alternatives
	// because the original match was not
	Alternatives *int
	// The children of this match result.
	Children []MatchResult
}

func (*MatchResult) ChildAt

func (mr *MatchResult) ChildAt(i int) *MatchResult

func (*MatchResult) SetChild

func (mr *MatchResult) SetChild(i int, child MatchResult)

func (*MatchResult) TakeAlternative

func (mr *MatchResult) TakeAlternative() bool

func (MatchResult) WithAlternatives

func (mr MatchResult) WithAlternatives(alternatives int) MatchResult

type MatchRule

type MatchRule struct {
	Rule  string
	Alias string
	// contains filtered or unexported fields
}

func (MatchRule) ID

func (m MatchRule) ID() int

func (MatchRule) Match

func (m MatchRule) Match(rs RuleSet, input *RuneBuffer, out *RuleMatch) (bool, error)

func (MatchRule) String

func (m MatchRule) String() string

type MatchShortcut

type MatchShortcut struct {
	Shortcut Matcher
	// contains filtered or unexported fields
}

func (MatchShortcut) ID

func (m MatchShortcut) ID() int

func (MatchShortcut) Match

func (m MatchShortcut) Match(rs RuleSet, input *RuneBuffer, out *RuleMatch) (bool, error)

func (MatchShortcut) String

func (m MatchShortcut) String() string

type Matcher

type Matcher interface {
	String() string
	ID() int
	Match(rs RuleSet, input *RuneBuffer, out *RuleMatch) (bool, error)
}

type MatcherMatch

type MatcherMatch struct {
	// The matcher that matched
	Matcher Matcher
	// The index of this node in its matchers repitition logic. This is stored here
	// for when the matching logic needs to backtrack on its greediness to find a match.
	GreedyMatches int
	// The RuleMatch this matcher belongs to
	RuleMatch *RuleMatch
}

A matcher matched over

type Position

type Position struct {
	Index  int
	Column int
	Line   int
}

func (Position) String

func (p Position) String() string

type Range

type Range struct {
	Start Position
	End   Position
}

func (Range) Get

func (c Range) Get(in RuneBuffer) string

func (Range) GetRunes

func (c Range) GetRunes(in RuneBuffer) []rune

func (Range) String

func (c Range) String(in string) string

type Rule

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

func (Rule) Ignore

func (r Rule) Ignore() bool

func (*Rule) Match

func (r *Rule) Match(rs RuleSet, runes *RuneBuffer, input *Input, parent *RuleMatch) (*RuleMatch, error)

func (Rule) Name

func (r Rule) Name() string

func (*Rule) Parse

func (r *Rule) Parse(input string) error

func (Rule) String

func (r Rule) String() string

type RuleMatch

type RuleMatch struct {
	// The range over what this matched.
	Range
	// The alias/rule that created this node.
	Name string
	// The rule that was matched
	Rule *Rule
	// The matchers that matched in this rule
	Matchers []MatcherMatch
	// The captured groups in this rule
	Captures map[int][]Range
	// The parent of this node.
	Parent *RuleMatch
	// The children of this node as they occurred in the input.
	Children []*RuleMatch
	// The name-to-output nodes
	ChildIndex map[string][]int
	// Global input dat
	Input *Input
}

A rule matched over some range of input

func (*RuleMatch) Path

func (n *RuleMatch) Path() string

type RulePattern

type RulePattern string

func (RulePattern) Parse

func (r RulePattern) Parse() (parsed RulePatternParsed, err error)

type RulePatternParsed

type RulePatternParsed struct {
	Pattern    string
	IgnoreCase bool
	Greedy     bool
	Ignore     bool
}

type RuleSet

type RuleSet struct {
	Shortcuts map[rune]Matcher
	Logger    Logger
	// contains filtered or unexported fields
}

func NewRuleSet

func NewRuleSet(rules map[string]RulePattern, shortcuts map[rune]Matcher) (RuleSet, error)

func NewStdRuleSet

func NewStdRuleSet(rules map[string]RulePattern) (RuleSet, error)

func (RuleSet) Parse

func (rs RuleSet) Parse(input string) (*RuleMatch, error)

func (RuleSet) ParseRule

func (rs RuleSet) ParseRule(ruleName string, inputText string) (*RuleMatch, error)

func (RuleSet) Rule

func (rs RuleSet) Rule(name string) *Rule

type RuleToken

type RuleToken struct {
	Kind RuleTokenKind
	Rune
}

type RuleTokenKind

type RuleTokenKind rune

## Language rules rules - `[]` = characters within are valid for matching, if a hyphen is between two characters in a sequence its a range - `.` = any character - `\` = escapes [](){}*+?.^$! - `\shortcut` = is a matcher in a shortcut map in the rule set - `^` = start of line - `$` = end of line - `*` = 0 or more of previous matcher - `+` = 1 or more of previous matcher - `?` = 0 or 1 of previous matcher - `{n}` = n of previous matcher - `{min,max}` = min to max of previous matcher. min defaults to 0, max defaults to inf. - `(a|b|c)` = one of the options - `(?=a)` = matches if a matches and doesn't read it - `(?!a)` = matches if a does not match and doesn't read it - `(?:a)` = matches if a matches but doesn't capture it - `\n` = matches group "n" captured in rule - `<refName:ruleName>` = a rule has a subset rule that's named. if the name is reused in a rule it is a list of values - `<ruleName>` = a rule has a subset rule that's named the ruleName - any other characters are considered a literal

var (
	RuleTokenEOF                  RuleTokenKind = 0
	RuleTokenEscape               RuleTokenKind = '\\'
	RuleTokenAny                  RuleTokenKind = '.'
	RuleTokenRepeatMaybe          RuleTokenKind = '*'
	RuleTokenRepeat               RuleTokenKind = '+'
	RuleTokenRepeatRangeStart     RuleTokenKind = '{'
	RuleTokenRepeatRangeEnd       RuleTokenKind = '}'
	RuleTokenRepeatRangeDelimiter RuleTokenKind = ','
	RuleTokenMaybe                RuleTokenKind = '?'
	RuleTokenLiteral              RuleTokenKind = 'l'
	RuleTokenShortcut             RuleTokenKind = 's'
	RuleTokenOneOfStart           RuleTokenKind = '('
	RuleTokenOneOfEnd             RuleTokenKind = ')'
	RuleTokenOneOfDelimiter       RuleTokenKind = '|'
	RuleTokenCharsStart           RuleTokenKind = '['
	RuleTokenCharsEnd             RuleTokenKind = ']'
	RuleTokenCharsRange           RuleTokenKind = '-'
	RuleTokenCharsNot             RuleTokenKind = '^'
	RuleTokenRuleStart            RuleTokenKind = '<'
	RuleTokenRuleEnd              RuleTokenKind = '>'
	RuleTokenRuleDelimiter        RuleTokenKind = ':'
	RuleTokenLineStart            RuleTokenKind = '^'
	RuleTokenLineEnd              RuleTokenKind = '$'
)

func RuneToRuleTokenKind

func RuneToRuleTokenKind(k rune) RuleTokenKind

type Rune

type Rune struct {
	Value rune
	Position
}

func (*Rune) Rune

func (r *Rune) Rune() rune

type RuneBuffer

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

func NewRuneBuffer

func NewRuneBuffer(data string) *RuneBuffer

func (*RuneBuffer) Ended

func (b *RuneBuffer) Ended() bool

func (RuneBuffer) Get

func (b RuneBuffer) Get(start, end int) string

func (RuneBuffer) GetRunes

func (b RuneBuffer) GetRunes(start, end int) []rune

func (RuneBuffer) Len

func (b RuneBuffer) Len() int

func (RuneBuffer) Max

func (b RuneBuffer) Max() Position

func (RuneBuffer) Peek

func (b RuneBuffer) Peek() (r *Rune)

func (RuneBuffer) Pos

func (b RuneBuffer) Pos() Position

func (*RuneBuffer) Read

func (b *RuneBuffer) Read() (r *Rune)

func (*RuneBuffer) Reset

func (b *RuneBuffer) Reset(pos Position)

func (*RuneBuffer) Set

func (b *RuneBuffer) Set(data string)

type Set

type Set[V comparable] map[V]struct{}

func NewSet

func NewSet[V comparable](values ...V) Set[V]

func (Set[V]) Add

func (s Set[V]) Add(value V)

func (Set[V]) Has

func (s Set[V]) Has(value V) bool

func (Set[V]) Remove

func (s Set[V]) Remove(value V)

func (Set[V]) Values

func (s Set[V]) Values() []V

type Stack

type Stack[V any] struct {
	// contains filtered or unexported fields
}

func (Stack[V]) Peek

func (s Stack[V]) Peek() *V

func (*Stack[V]) Pop

func (s *Stack[V]) Pop() *V

func (*Stack[V]) Push

func (s *Stack[V]) Push(value V)

Jump to

Keyboard shortcuts

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