Documentation
¶
Overview ¶
Package parser provides NFA construction and analysis utilities for regex patterns.
Package parser provides regex pattern parsing and AST manipulation.
Index ¶
- Variables
- func ComputeEpsilonClosure(state *State) map[*State]bool
- func CountAlternations(re *syntax.Regexp) int
- func CountCaptures(re *syntax.Regexp) int
- func CountQuantifiers(re *syntax.Regexp) int
- func FindAlternations(re *syntax.Regexp) []*syntax.Regexp
- func FindQuantifiers(re *syntax.Regexp) []*syntax.Regexp
- func GetNestingDepth(re *syntax.Regexp) int
- func GetOp(re *syntax.Regexp) syntax.Op
- func HasQuantifier(re *syntax.Regexp) bool
- func IsAlternation(re *syntax.Regexp) bool
- func IsCapture(re *syntax.Regexp) bool
- func IsQuantifier(re *syntax.Regexp) bool
- func String(re *syntax.Regexp) string
- func Walk(re *syntax.Regexp, visitor func(*syntax.Regexp) bool)
- type CharClass
- type NFA
- type Parser
- type RuneRange
- type State
- type Transition
- type TransitionLabel
- type TransitionType
Constants ¶
This section is empty.
Variables ¶
var ( // ErrInvalidPattern indicates the pattern is syntactically invalid. ErrInvalidPattern = errors.New("invalid regex pattern") )
Functions ¶
func ComputeEpsilonClosure ¶
ComputeEpsilonClosure computes the epsilon closure of a state. Returns all states reachable from the given state via epsilon transitions.
func CountAlternations ¶
CountAlternations returns the number of alternation operators.
func CountCaptures ¶
CountCaptures returns the number of capturing groups.
func CountQuantifiers ¶
CountQuantifiers returns the total number of quantifiers in the regex.
func FindAlternations ¶
FindAlternations finds all alternation nodes in the regex.
func FindQuantifiers ¶
FindQuantifiers finds all quantifier nodes in the regex.
func GetNestingDepth ¶
GetNestingDepth returns the maximum quantifier nesting depth.
func HasQuantifier ¶
HasQuantifier returns true if the regex contains any quantifiers.
func IsAlternation ¶
IsAlternation returns true if the node is an alternation.
func IsQuantifier ¶
IsQuantifier returns true if the node is a quantifier.
Types ¶
type CharClass ¶
type CharClass struct {
Ranges []RuneRange // Inclusive ranges
Negate bool // True if this is a negated class
}
CharClass represents a set of characters that can be matched.
type NFA ¶
type NFA struct {
Start *State
Accept *State
States []*State
StateCount int
Transitions map[*State][]*Transition
}
NFA represents a Non-deterministic Finite Automaton constructed from a regex.
func (*NFA) AddEpsilonTransition ¶
func (nfa *NFA) AddEpsilonTransition(from, to *State) *Transition
AddEpsilonTransition adds an epsilon transition (no input consumed).
func (*NFA) AddTransition ¶
func (nfa *NFA) AddTransition(from, to *State, label TransitionLabel) *Transition
AddTransition adds a transition between two states.
type Parser ¶
type Parser struct {
// contains filtered or unexported fields
}
Parser wraps Go's regexp/syntax parser and provides additional utilities.
func NewParserWithFlags ¶
NewParserWithFlags creates a new parser with custom syntax flags.
type State ¶
type State struct {
ID int
IsAccept bool
Transitions []*Transition
EpsilonTo []*State // States reachable via epsilon transitions
}
State represents a state in the NFA.
type Transition ¶
type Transition struct {
From *State
To *State
Label TransitionLabel
IsEpsilon bool
}
Transition represents a transition between states.
type TransitionLabel ¶
type TransitionLabel struct {
Type TransitionType
Runes []rune // For literal characters
Class *CharClass // For character classes
Op syntax.Op // For special operations
}
TransitionLabel represents what causes a transition.
type TransitionType ¶
type TransitionType int
TransitionType indicates the type of transition.
const ( TransitionLiteral TransitionType = iota // Match specific character(s) TransitionClass // Match character class TransitionAny // Match any character (.) TransitionEpsilon // Epsilon (no input consumed) TransitionAnchor // Anchor (^, $) )