Package monq.jfa

Class RecordingMatcher<T>

java.lang.Object
monq.jfa.RecordingMatcher<T>
Type Parameters:
T - type of the value stored in the Dfa

public class RecordingMatcher<T> extends Object
A matcher looping on Dfa.transition(monq.jfa.CharSource), recording character positions when traversing a state which tests true for a given predicate. See match(Dfa, CharSource) for details.
  • Constructor Details

    • RecordingMatcher

      public RecordingMatcher(StringBuilder out, IntArray stops, Predicate<T> isIntermediateStop)
      Creates the instance.
      Parameters:
      out - where to append matching characters
      stops - where to record character positions
      isIntermediateStop - must return true iff the value tested denotes an intermediate stop state. Will never be called with null.
    • RecordingMatcher

      public RecordingMatcher(Predicate<T> isStop)
      Calls the other constructor with default empty StringBuilder and IntArray.
  • Method Details

    • out

      public StringBuilder out()
      The string builder provided to the constructor or created internally recording matching output.
    • stops

      public IntArray stops()
      The array provided to the constructor or created internally recording traversal of states tested true by the constructor provided predicate.
    • getPart

      public String getPart(int i)
      Returns parts of out() depending on value recorded in stops(). In particular it returns substring(stops()[i-1], stops()[i] with the two border values defaulting to 0 and out().length() respectively
      Parameters:
      i - the part to retrieve.
      Returns:
      the extracted string
    • match

      public T match(Dfa<T> dfa, CharSource in) throws IOException
      Matches a Dfa against the input. Matching input is recorded in out(). While traversing states of the Dfa, a state's value, if any, is tested with the predicate defined in the constructor. For a sequence of the same value ('==') testing true, the first character position which is different is recorded in stops().

      Example: Consider an automaton state self looping on character 'a', like for a "a*" having a value such that the predicate is true. Further assume that a 'b' leads to a different state, not having a value. Then, after matching "aaab", 3 is recorded in stops(). This allows to retrieve the sub-match for "a*". The easiest way is to used getPart(int).

      Note: The regular expression parser does not currently have means to mark positions in regular expression to work with the RecordingMatcher. The states' values must be added by using an NfaBuilder. A respective predicate must be defined and must fit to the predicate for stop provided by Dfa.withIsStopValue(java.util.function.Predicate<T>).

      Parameters:
      dfa - to use for matching
      in - input to match into
      Returns:
      the value of the last stop state visited or null. If non-null is returned, the output text may still be empty if the Dfa matches the empty string.
      Throws:
      IOException
      See Also:
    • match

      public T match(Dfa<T> dfa, String in) throws IOException
      Throws:
      IOException