consume method

Token consume()
inherited

Consume and return the {@linkplain #getCurrentToken current symbol}.

E.g., given the following input with [A] being the current lookahead symbol, this function moves the cursor to [B] and returns [A].

 A B
 ^

If the parser is not in error recovery mode, the consumed symbol is added to the parse tree using {@link ParserRuleContext#addChild}, and {@link ParseTreeListener#visitTerminal} is called on any parse listeners. If the parser is in error recovery mode, the consumed symbol is added to the parse tree using {@link #createErrorNode(ParserRuleContext, Token)} then {@link ParserRuleContext#addErrorNode(ErrorNode)} and {@link ParseTreeListener#visitErrorNode} is called on any parse listeners.

Implementation

Token consume() {
  final o = currentToken;
  if (o.type != IntStream.EOF) {
    inputStream.consume();
  }
  final hasListener = _parseListeners != null && _parseListeners!.isNotEmpty;
  if (buildParseTree || hasListener) {
    if (errorHandler.inErrorRecoveryMode(this)) {
      final node = context!.addErrorNode(createErrorNode(context!, o));
      if (_parseListeners != null) {
        for (var listener in _parseListeners!) {
          listener.visitErrorNode(node);
        }
      }
    } else {
      final node = context!.addChild(createTerminalNode(context!, o));
      if (_parseListeners != null) {
        for (var listener in _parseListeners!) {
          listener.visitTerminal(node);
        }
      }
    }
  }
  return o;
}