evaluatePredicate method

bool evaluatePredicate(
  1. CharStream input,
  2. int ruleIndex,
  3. int predIndex,
  4. bool speculative,
)

Evaluate a predicate specified in the lexer.

If [speculative] is [true], this method was called before {@link #consume} for the matched character. This method should call {@link #consume} before evaluating the predicate to ensure position sensitive values, including {@link Lexer#getText}, {@link Lexer#getLine}, and {@link Lexer#getCharPositionInLine}, properly reflect the current lexer state. This method should restore [input] and the simulator to the original state before returning (i.e. undo the actions made by the call to {@link #consume}.

@param input The input stream. @param ruleIndex The rule containing the predicate. @param predIndex The index of the predicate within the rule. @param speculative true if the current index in input is one character before the predicate's location.

@return true if the specified predicate evaluates to true.

Implementation

bool evaluatePredicate(
  CharStream input,
  int ruleIndex,
  int predIndex,
  bool speculative,
) {
  if (!speculative) {
    return recog.sempred(null, ruleIndex, predIndex);
  }

  final savedCharPositionInLine = charPositionInLine;
  final savedLine = line;
  final index = input.index;
  final marker = input.mark();
  try {
    consume(input);
    return recog.sempred(null, ruleIndex, predIndex);
  } finally {
    charPositionInLine = savedCharPositionInLine;
    line = savedLine;
    input.seek(index);
    input.release(marker);
  }
}