Class DfaFilter<T>

java.lang.Object
monq.jfa.filter.DfaFilter<T>
Type Parameters:
T - the type of value stored in the Dfa used for matching.

public class DfaFilter<T> extends Object
Uses a Dfa to match the start of some input and calls a handler on the match.
  • Constructor Details

    • DfaFilter

      public DfaFilter(Dfa<T> dfa, MatchHandler<T> handler, DfaFilter.OnFailedMatch onFailedMatch)
      Creates the filter.
      Parameters:
      dfa - to match prefixes of the input
      handler - to call if a match is found
      onFailedMatch - how to proceed if the start of the input can not be matched by the dfa
  • Method Details

    • filter

      public void filter(StringBuilder out, CharSource in) throws IOException
      A shortcut for calling next(java.lang.StringBuilder, monq.jfa.CharSource) in a loop like
       while (next(out, in)) {
         // just run
       }
      Throws:
      IOException
    • next

      public boolean next(StringBuilder out, CharSource in) throws IOException
      Performs one match of the Dfa and calls the handler defined in the constructor. The handler is allowed to freely change the whole of the out parameter, so in principle its length may even shrink if this method is called with a non-empty out.

      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 parameter DfaFilter.OnFailedMatch and true is 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

      1. find an empty match
      2. call the handler
      3. read one character and, if not EOF, handle it as drop, copy or throw
      4. return false if we had EOF or otherwise true

      Calling the handler for 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, use Dfa.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:
      false if no more character was available in the input (EOF). Note: if a match ends at EOF, say "aaa" matches an "a+", true is returned and false will be returned only on the next call.
      Throws:
      IOException - as thrown by Dfa.match(monq.jfa.CharSource, java.lang.StringBuilder)
    • setDfa

      public void setDfa(Dfa<T> dfa)
      Sets the DFA to be used for matching. This allows for a handler called by the filter(java.lang.StringBuilder, monq.jfa.CharSource) method to change the DFA on the fly.
      Parameters:
      dfa - to use
    • setOnFailedMatch

      public void setOnFailedMatch(DfaFilter.OnFailedMatch onFailedMatch)
      Sets the strategy for handling unmatched characters.
      Parameters:
      onFailedMatch - to use