Class DfaFilter<T>
- Type Parameters:
T- the type of value stored in theDfaused for matching.
Dfa to match the start of some input and calls a handler on the match.-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumStrategy for handling the next character of the input stream if no match is possible. -
Constructor Summary
ConstructorsConstructorDescriptionDfaFilter(Dfa<T> dfa, MatchHandler<T> handler, DfaFilter.OnFailedMatch onFailedMatch) Creates the filter. -
Method Summary
Modifier and TypeMethodDescriptionvoidfilter(StringBuilder out, CharSource in) A shortcut for callingnext(java.lang.StringBuilder, monq.jfa.CharSource)in a loop likebooleannext(StringBuilder out, CharSource in) Performs one match of the Dfa and calls thehandlerdefined in the constructor.voidSets the DFA to be used for matching.voidsetOnFailedMatch(DfaFilter.OnFailedMatch onFailedMatch) Sets the strategy for handling unmatched characters.
-
Constructor Details
-
DfaFilter
Creates the filter.- Parameters:
dfa- to match prefixes of the inputhandler- to call if a match is foundonFailedMatch- how to proceed if the start of the input can not be matched by the dfa
-
-
Method Details
-
filter
A shortcut for callingnext(java.lang.StringBuilder, monq.jfa.CharSource)in a loop likewhile (next(out, in)) { // just run }- Throws:
IOException
-
next
Performs one match of the Dfa and calls thehandlerdefined in the constructor. The handler is allowed to freely change the whole of theoutparameter, so in principle its length may even shrink if this method is called with a non-emptyout.If no match is found, or if an empty match was found, one character is read. If this is EOF, the method returns
false. Otherwise the character is handled according to the constructor parameterDfaFilter.OnFailedMatchandtrueis returned.NOTE: If the Dfa matches the empty string, the handler is guaranteed to be called. For a sequence of characters which don't match, this means that each call to this method will
- find an empty match
- call the handler
- read one character and, if not EOF, handle it as drop, copy or throw
- return
falseif we had EOF or otherwisetrue
Calling the
handlerfor each character just because the empty string matches can be inefficient. If this is not really intended, make sure the Dfa does not match the empty string. Consider, for example, to use"a+"instead of"a*"and don't make your whole regular expression optional like"(big regexp)?". If in doubt, useDfa.matchesEmpty()to check if it is a stop state at runtime.- Parameters:
out- where to place matching characters and on what to call the handler- Returns:
falseif no more character was available in the input (EOF). Note: if a match ends at EOF, say "aaa" matches an "a+",trueis returned and false will be returned only on the next call.- Throws:
IOException- as thrown byDfa.match(monq.jfa.CharSource, java.lang.StringBuilder)
-
setDfa
Sets the DFA to be used for matching. This allows for a handler called by thefilter(java.lang.StringBuilder, monq.jfa.CharSource)method to change the DFA on the fly.- Parameters:
dfa- to use
-
setOnFailedMatch
Sets the strategy for handling unmatched characters.- Parameters:
onFailedMatch- to use
-