/** * Consume and return the {@linkplain #getCurrentToken current symbol}. * * <p>E.g., given the following input with {@code A} being the current lookahead symbol, this * function moves the cursor to {@code B} and returns {@code A}. * * <pre> * A B * ^ * </pre> * * If the parser is not in error recovery mode, the consumed symbol is added to the parse tree * using {@link ParserRuleContext#addChild(Token)}, and {@link ParseTreeListener#visitTerminal} is * called on any parse listeners. If the parser <em>is</em> in error recovery mode, the consumed * symbol is added to the parse tree using {@link ParserRuleContext#addErrorNode(Token)}, and * {@link ParseTreeListener#visitErrorNode} is called on any parse listeners. */ public Token consume() { Token o = getCurrentToken(); if (o.getType() != EOF) { getInputStream().consume(); } boolean hasListener = _parseListeners != null && !_parseListeners.isEmpty(); if (_buildParseTrees || hasListener) { if (_errHandler.inErrorRecoveryMode(this)) { ErrorNode node = _ctx.addErrorNode(o); if (_parseListeners != null) { for (ParseTreeListener listener : _parseListeners) { listener.visitErrorNode(node); } } } else { TerminalNode node = _ctx.addChild(o); if (_parseListeners != null) { for (ParseTreeListener listener : _parseListeners) { listener.visitTerminal(node); } } } } return o; }
public void walk(ParseTreeListener listener, ParseTree t) { if (t instanceof ErrorNode) { listener.visitErrorNode((ErrorNode) t); return; } else if (t instanceof TerminalNode) { listener.visitTerminal((TerminalNode) t); return; } GlobalInfo info = GlobalInfo.getInstance(); info.setNotVisitNodes(new ArrayList<Integer>()); RuleNode r = (RuleNode) t; enterRule(listener, r); int n = r.getChildCount(); for (int i = 0; i < n; i++) { Boolean found = false; for (int j = 0; j < info.getNotVisitNodes().size(); ++j) { if (info.getNotVisitNodes().get(j).equals(i)) { found = true; break; } } if (!found) { List<Integer> aux = info.getNotVisitNodes(); info.setNotVisitNodes(new ArrayList<Integer>()); walk(listener, r.getChild(i)); info.setNotVisitNodes(aux); } } exitRule(listener, r); info.setNotVisitNodes(new ArrayList<Integer>()); }
/** * Notify any parse listeners of an exit rule event. * * @see #addParseListener */ protected void triggerExitRuleEvent() { // reverse order walk of listeners for (int i = _parseListeners.size() - 1; i >= 0; i--) { ParseTreeListener listener = _parseListeners.get(i); _ctx.exitRule(listener); listener.exitEveryRule(_ctx); } }
protected void exitRule(ParseTreeListener listener, RuleNode r) { ParserRuleContext ctx = (ParserRuleContext) r.getRuleContext(); ctx.exitRule(listener); listener.exitEveryRule(ctx); GlobalInfo info = GlobalInfo.getInstance(); info.exitRuleSetup(); }
/** * Notify any parse listeners of an enter rule event. * * @see #addParseListener */ protected void triggerEnterRuleEvent() { for (ParseTreeListener listener : _parseListeners) { listener.enterEveryRule(_ctx); _ctx.enterRule(listener); } }