Search in sources :

Example 21 with ErrorNode

use of org.antlr.v4.runtime.tree.ErrorNode in project antlr4 by antlr.

the class Parser method consume.

/**
 * 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}.</p>
 *
 * <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(TerminalNode)}, 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 #createErrorNode(ParserRuleContext, Token)} then
 * {@link ParserRuleContext#addErrorNode(ErrorNode)} 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(createErrorNode(_ctx, o));
            if (_parseListeners != null) {
                for (ParseTreeListener listener : _parseListeners) {
                    listener.visitErrorNode(node);
                }
            }
        } else {
            TerminalNode node = _ctx.addChild(createTerminalNode(_ctx, o));
            if (_parseListeners != null) {
                for (ParseTreeListener listener : _parseListeners) {
                    listener.visitTerminal(node);
                }
            }
        }
    }
    return o;
}
Also used : ParseTreeListener(org.antlr.v4.runtime.tree.ParseTreeListener) ErrorNode(org.antlr.v4.runtime.tree.ErrorNode) TerminalNode(org.antlr.v4.runtime.tree.TerminalNode)

Example 22 with ErrorNode

use of org.antlr.v4.runtime.tree.ErrorNode in project antlr4 by antlr.

the class ParserRuleContext method addErrorNode.

/**
 * Add a child to this node based upon badToken.  It
 *  creates a ErrorNodeImpl rather than using
 *  {@link Parser#createErrorNode(ParserRuleContext, Token)}. I'm leaving this
 *  in for compatibility but the parser doesn't use this anymore.
 */
@Deprecated
public ErrorNode addErrorNode(Token badToken) {
    ErrorNodeImpl t = new ErrorNodeImpl(badToken);
    addAnyChild(t);
    t.setParent(this);
    return t;
}
Also used : ErrorNodeImpl(org.antlr.v4.runtime.tree.ErrorNodeImpl)

Example 23 with ErrorNode

use of org.antlr.v4.runtime.tree.ErrorNode in project antlr4 by antlr.

the class ParserRuleContext method copyFrom.

/**
 * COPY a ctx (I'm deliberately not using copy constructor) to avoid
 *  confusion with creating node with parent. Does not copy children
 *  (except error leaves).
 *
 *  This is used in the generated parser code to flip a generic XContext
 *  node for rule X to a YContext for alt label Y. In that sense, it is
 *  not really a generic copy function.
 *
 *  If we do an error sync() at start of a rule, we might add error nodes
 *  to the generic XContext so this function must copy those nodes to
 *  the YContext as well else they are lost!
 */
public void copyFrom(ParserRuleContext ctx) {
    this.parent = ctx.parent;
    this.invokingState = ctx.invokingState;
    this.start = ctx.start;
    this.stop = ctx.stop;
    // copy any error nodes to alt label node
    if (ctx.children != null) {
        this.children = new ArrayList<>();
        // reset parent pointer for any error nodes
        for (ParseTree child : ctx.children) {
            if (child instanceof ErrorNode) {
                addChild((ErrorNode) child);
            }
        }
    }
}
Also used : ErrorNode(org.antlr.v4.runtime.tree.ErrorNode) ParseTree(org.antlr.v4.runtime.tree.ParseTree)

Example 24 with ErrorNode

use of org.antlr.v4.runtime.tree.ErrorNode in project antlr4 by antlr.

the class Trees method getNodeText.

public static String getNodeText(Tree t, List<String> ruleNames) {
    if (ruleNames != null) {
        if (t instanceof RuleContext) {
            int ruleIndex = ((RuleContext) t).getRuleContext().getRuleIndex();
            String ruleName = ruleNames.get(ruleIndex);
            int altNumber = ((RuleContext) t).getAltNumber();
            if (altNumber != ATN.INVALID_ALT_NUMBER) {
                return ruleName + ":" + altNumber;
            }
            return ruleName;
        } else if (t instanceof ErrorNode) {
            return t.toString();
        } else if (t instanceof TerminalNode) {
            Token symbol = ((TerminalNode) t).getSymbol();
            if (symbol != null) {
                String s = symbol.getText();
                return s;
            }
        }
    }
    // no recog for rule names
    Object payload = t.getPayload();
    if (payload instanceof Token) {
        return ((Token) payload).getText();
    }
    return t.getPayload().toString();
}
Also used : RuleContext(org.antlr.v4.runtime.RuleContext) ParserRuleContext(org.antlr.v4.runtime.ParserRuleContext) CommonToken(org.antlr.v4.runtime.CommonToken) Token(org.antlr.v4.runtime.Token)

Aggregations

Token (org.antlr.v4.runtime.Token)9 ErrorNode (org.antlr.v4.runtime.tree.ErrorNode)8 ParserRuleContext (org.antlr.v4.runtime.ParserRuleContext)6 IntervalSet (org.antlr.v4.runtime.misc.IntervalSet)3 ErrorNodeImpl (org.antlr.v4.runtime.tree.ErrorNodeImpl)3 ParseTree (org.antlr.v4.runtime.tree.ParseTree)3 Rectangle2D (java.awt.geom.Rectangle2D)2 ArrayDeque (java.util.ArrayDeque)2 BaseErrorListener (org.antlr.v4.runtime.BaseErrorListener)2 CommonToken (org.antlr.v4.runtime.CommonToken)2 CommonTokenStream (org.antlr.v4.runtime.CommonTokenStream)2 RecognitionException (org.antlr.v4.runtime.RecognitionException)2 RuleContext (org.antlr.v4.runtime.RuleContext)2 IntegerStack (org.antlr.v4.runtime.misc.IntegerStack)2 ParseTreeListener (org.antlr.v4.runtime.tree.ParseTreeListener)2 TerminalNode (org.antlr.v4.runtime.tree.TerminalNode)2 BaseRuntimeTest.antlrOnString (org.antlr.v4.test.runtime.BaseRuntimeTest.antlrOnString)2 ST (org.stringtemplate.v4.ST)2 AutocompleteCandidate (com.twosigma.beakerx.autocomplete.AutocompleteCandidate)1 BlockStatementContext (com.twosigma.beakerx.javash.autocomplete.JavaParser.BlockStatementContext)1