parser

package
v0.0.0-...-cacfa07 Latest Latest
Warning

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

Go to latest
Published: Oct 16, 2025 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package parser provides NFA construction and analysis utilities for regex patterns.

Package parser provides regex pattern parsing and AST manipulation.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrInvalidPattern indicates the pattern is syntactically invalid.
	ErrInvalidPattern = errors.New("invalid regex pattern")
)

Functions

func ComputeEpsilonClosure

func ComputeEpsilonClosure(state *State) map[*State]bool

ComputeEpsilonClosure computes the epsilon closure of a state. Returns all states reachable from the given state via epsilon transitions.

func CountAlternations

func CountAlternations(re *syntax.Regexp) int

CountAlternations returns the number of alternation operators.

func CountCaptures

func CountCaptures(re *syntax.Regexp) int

CountCaptures returns the number of capturing groups.

func CountQuantifiers

func CountQuantifiers(re *syntax.Regexp) int

CountQuantifiers returns the total number of quantifiers in the regex.

func FindAlternations

func FindAlternations(re *syntax.Regexp) []*syntax.Regexp

FindAlternations finds all alternation nodes in the regex.

func FindQuantifiers

func FindQuantifiers(re *syntax.Regexp) []*syntax.Regexp

FindQuantifiers finds all quantifier nodes in the regex.

func GetNestingDepth

func GetNestingDepth(re *syntax.Regexp) int

GetNestingDepth returns the maximum quantifier nesting depth.

func GetOp

func GetOp(re *syntax.Regexp) syntax.Op

GetOp returns the operation type of a regex node.

func HasQuantifier

func HasQuantifier(re *syntax.Regexp) bool

HasQuantifier returns true if the regex contains any quantifiers.

func IsAlternation

func IsAlternation(re *syntax.Regexp) bool

IsAlternation returns true if the node is an alternation.

func IsCapture

func IsCapture(re *syntax.Regexp) bool

IsCapture returns true if the node is a capturing group.

func IsQuantifier

func IsQuantifier(re *syntax.Regexp) bool

IsQuantifier returns true if the node is a quantifier.

func String

func String(re *syntax.Regexp) string

String returns a string representation of the regex.

func Walk

func Walk(re *syntax.Regexp, visitor func(*syntax.Regexp) bool)

Walk traverses the regex AST and calls the visitor function for each node.

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 BuildNFA

func BuildNFA(re *syntax.Regexp) (*NFA, error)

BuildNFA constructs an NFA from a parsed regex AST.

func NewNFA

func NewNFA() *NFA

NewNFA creates a new empty NFA.

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.

func (*NFA) NewState

func (nfa *NFA) NewState() *State

NewState creates a new state and adds it to the NFA.

func (*NFA) String

func (nfa *NFA) String() string

String returns a string representation of the NFA for debugging.

type Parser

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

Parser wraps Go's regexp/syntax parser and provides additional utilities.

func NewParser

func NewParser() *Parser

NewParser creates a new parser with default flags.

func NewParserWithFlags

func NewParserWithFlags(flags syntax.Flags) *Parser

NewParserWithFlags creates a new parser with custom syntax flags.

func (*Parser) MustParse

func (p *Parser) MustParse(pattern string) *syntax.Regexp

MustParse is like Parse but panics on error. Useful for testing.

func (*Parser) Parse

func (p *Parser) Parse(pattern string) (*syntax.Regexp, error)

Parse parses a regex pattern into an AST.

func (*Parser) Validate

func (p *Parser) Validate(pattern string) error

Validate checks if a pattern is syntactically valid.

type RuneRange

type RuneRange struct {
	Lo rune
	Hi rune
}

RuneRange represents an inclusive range of runes.

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 (^, $)
)

Jump to

Keyboard shortcuts

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