Package org.jline.builtins
Class NfaMatcher<T>
java.lang.Object
org.jline.builtins.NfaMatcher<T>
- Type Parameters:
T- the type of objects to match against the pattern
Non-deterministic Finite Automaton (NFA) implementation for pattern matching.
This class implements a Thompson NFA for regular expression matching. It converts a regular expression to postfix notation, builds an NFA, and uses it to match sequences of objects against the pattern.
The implementation is based on the algorithm described in Russ Cox's article: https://swtch.com/~rsc/regexp/regexp1.html
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static classRepresents a fragment of the NFA during construction.(package private) static classRepresents a state in the NFA. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final BiFunction<T, String, Boolean> Function to match an input against a patternprivate final StringThe regular expression patternprivate NfaMatcher.StateThe start state of the NFA -
Constructor Summary
ConstructorsConstructorDescriptionNfaMatcher(String regexp, BiFunction<T, String, Boolean> matcher) Creates a new NfaMatcher with the specified regular expression and matcher function. -
Method Summary
Modifier and TypeMethodDescription(package private) voidAdds a state to the set of states, handling split states recursively.voidcompile()Compiles the regular expression into an NFA.booleanMatches a list of arguments against the pattern.matchPartial(List<T> args) Returns the list of possible matcher names for the next object(package private) static NfaMatcher.StateConverts a postfix expression to an NFA.Converts a regular expression to postfix notation.
-
Field Details
-
regexp
The regular expression pattern -
matcher
Function to match an input against a pattern -
start
The start state of the NFA
-
-
Constructor Details
-
NfaMatcher
Creates a new NfaMatcher with the specified regular expression and matcher function.- Parameters:
regexp- the regular expression patternmatcher- the function to match an input against a pattern
-
-
Method Details
-
compile
public void compile()Compiles the regular expression into an NFA.This method is called automatically when needed, but can be called explicitly to precompile the pattern.
-
match
Matches a list of arguments against the pattern.This method uses the NFA to determine if the sequence of arguments matches the regular expression pattern.
- Parameters:
args- the list of arguments to match- Returns:
- true if the arguments match the pattern, false otherwise
-
matchPartial
Returns the list of possible matcher names for the next object- Parameters:
args- input list- Returns:
- the list of possible matcher names for the next object
-
addState
Adds a state to the set of states, handling split states recursively.- Parameters:
l- the set of states to add tos- the state to add
-
toNfa
Converts a postfix expression to an NFA.- Parameters:
postfix- the postfix expression- Returns:
- the start state of the NFA
-
toPostFix
Converts a regular expression to postfix notation.- Parameters:
regexp- the regular expression- Returns:
- the postfix expression as a list of tokens
-