Search in sources :

Example 1 with ErrorNode

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

the class JavaNodeCompletion method visitErrorNode.

@Override
public void visitErrorNode(ErrorNode arg0) {
    if (arg0.getText().equals("new")) {
        CompilationUnitContext cuc = (CompilationUnitContext) arg0.getParent();
        List<ParseTree> children = cuc.children;
        int tokenIndex = arg0.getSymbol().getTokenIndex();
        if (tokenIndex - 2 >= 0 && tokenIndex + 1 <= children.size()) {
            ParseTree variablePT = children.get(tokenIndex - 2);
            ParseTree typePT = children.get(tokenIndex + 1);
            String type = typePT.getText();
            String variable = variablePT.getText();
            AutocompleteCandidate c1 = new AutocompleteCandidate(JavaCompletionTypes.NAME, variable);
            registry.addCandidate(c1);
            if (type != null)
                classUtils.defineVariable(variable, type);
            return;
        }
    }
    if (arg0.getSymbol().getStartIndex() < cursor && arg0.getSymbol().getStopIndex() + 1 >= cursor) {
        // System.out.println("ERR: "+arg0.getSymbol().getStartIndex()+" "+arg0.getSymbol().getStopIndex()+" "+arg0.getSymbol().getText());
        if (arg0.getParent() instanceof CompilationUnitContext) {
            CompilationUnitContext cuc = (CompilationUnitContext) arg0.getParent();
            AutocompleteCandidate c = new AutocompleteCandidate(JavaCompletionTypes.INITIAL, arg0.getText());
            addQuery(c, cursor);
            AutocompleteCandidate c2 = new AutocompleteCandidate(JavaCompletionTypes.TOPLEVEL, arg0.getText());
            addQuery(c2, cursor);
            completeClassFromPath(cuc, arg0.getText());
            return;
        } else if (arg0.getParent() instanceof BlockStatementContext) {
            if (!arg0.getSymbol().getText().equals(".")) {
                AutocompleteCandidate c = new AutocompleteCandidate(JavaCompletionTypes.BLOCKLEVEL, arg0.getText());
                addQuery(c, cursor);
                c = new AutocompleteCandidate(JavaCompletionTypes.TYPE, arg0.getText());
                addQuery(c, cursor);
                c = new AutocompleteCandidate(JavaCompletionTypes.CUSTOM_TYPE, arg0.getText());
                addQuery(c, cursor);
                c = new AutocompleteCandidate(JavaCompletionTypes.NAME, arg0.getText());
                addQuery(c, cursor);
            } else {
                BlockStatementContext bs = (BlockStatementContext) arg0.getParent();
                if (bs.getChildCount() > 1) {
                    addQuery(classUtils.expandExpression(bs.getText(), registry, classUtils.DO_ALL), cursor);
                }
            }
        } else if (arg0.getParent() instanceof ExpressionContext) {
            // we are the rightmost child of the expression
            ParseTree chld = arg0.getParent().getChild(arg0.getParent().getChildCount() - 1);
            if (!chld.equals(arg0))
                return;
            addQuery(classUtils.expandExpression(arg0.getParent().getText(), registry, classUtils.DO_NON_STATIC), cursor);
        } else if (arg0.getParent() instanceof TypeDeclarationContext && arg0.getParent().getParent() != null && arg0.getParent().getParent() instanceof CompilationUnitContext) {
            AutocompleteCandidate c = new AutocompleteCandidate(JavaCompletionTypes.TOPLEVEL, arg0.getText());
            addQuery(c, cursor);
        } else if (arg0.getParent() instanceof MemberDeclarationContext && arg0.getParent().getParent() != null && arg0.getParent().getParent() instanceof ClassBodyDeclarationContext && arg0.getParent().getParent().getParent() != null && arg0.getParent().getParent().getParent() instanceof ClassBodyContext && arg0.getParent().getParent().getParent().getText().trim().startsWith("<missing '{'>")) {
            AutocompleteCandidate c = new AutocompleteCandidate(JavaCompletionTypes.CLASSLEVEL, arg0.getText());
            addQuery(c, cursor);
        } else if (arg0.getParent() instanceof MemberDeclarationContext && arg0.getParent().getParent() != null && arg0.getParent().getParent() instanceof ClassBodyDeclarationContext && arg0.getParent().getParent().getParent() != null && arg0.getParent().getParent().getParent() instanceof ClassBodyContext) {
            AutocompleteCandidate c = new AutocompleteCandidate(JavaCompletionTypes.TYPE, arg0.getText());
            addQuery(c, cursor);
            c = new AutocompleteCandidate(JavaCompletionTypes.CUSTOM_TYPE, arg0.getText());
            addQuery(c, cursor);
        }
    }
}
Also used : MemberDeclarationContext(com.twosigma.beakerx.javash.autocomplete.JavaParser.MemberDeclarationContext) CompilationUnitContext(com.twosigma.beakerx.javash.autocomplete.JavaParser.CompilationUnitContext) BlockStatementContext(com.twosigma.beakerx.javash.autocomplete.JavaParser.BlockStatementContext) ExpressionContext(com.twosigma.beakerx.javash.autocomplete.JavaParser.ExpressionContext) ClassBodyDeclarationContext(com.twosigma.beakerx.javash.autocomplete.JavaParser.ClassBodyDeclarationContext) AutocompleteCandidate(com.twosigma.beakerx.autocomplete.AutocompleteCandidate) ClassBodyContext(com.twosigma.beakerx.javash.autocomplete.JavaParser.ClassBodyContext) ParseTree(org.antlr.v4.runtime.tree.ParseTree) TypeDeclarationContext(com.twosigma.beakerx.javash.autocomplete.JavaParser.TypeDeclarationContext)

Example 2 with ErrorNode

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

the class Trees method getNodeText.

public static String getNodeText(@NotNull Tree t, @Nullable List<String> ruleNames) {
    if (ruleNames != null) {
        if (t instanceof RuleNode) {
            RuleContext ruleContext = ((RuleNode) t).getRuleContext();
            int ruleIndex = ruleContext.getRuleIndex();
            String ruleName = ruleNames.get(ruleIndex);
            int altNumber = ruleContext.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)

Example 3 with ErrorNode

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

the class ParseTreeWalker method walk.

public void walk(ParseTreeListener listener, ParseTree t) {
    final Deque<ParseTree> nodeStack = new ArrayDeque<ParseTree>();
    final IntegerStack indexStack = new IntegerStack();
    ParseTree currentNode = t;
    int currentIndex = 0;
    while (currentNode != null) {
        // pre-order visit
        if (currentNode instanceof ErrorNode) {
            listener.visitErrorNode((ErrorNode) currentNode);
        } else if (currentNode instanceof TerminalNode) {
            listener.visitTerminal((TerminalNode) currentNode);
        } else {
            final RuleNode r = (RuleNode) currentNode;
            enterRule(listener, r);
        }
        // Move down to first child, if exists
        if (currentNode.getChildCount() > 0) {
            nodeStack.push(currentNode);
            indexStack.push(currentIndex);
            currentIndex = 0;
            currentNode = currentNode.getChild(0);
            continue;
        }
        // No child nodes, so walk tree
        do {
            // post-order visit
            if (currentNode instanceof RuleNode) {
                exitRule(listener, (RuleNode) currentNode);
            }
            // No parent, so no siblings
            if (nodeStack.isEmpty()) {
                currentNode = null;
                currentIndex = 0;
                break;
            }
            // Move to next sibling if possible
            currentNode = nodeStack.peek().getChild(++currentIndex);
            if (currentNode != null) {
                break;
            }
            // No next sibling, so move up
            currentNode = nodeStack.pop();
            currentIndex = indexStack.pop();
        } while (currentNode != null);
    }
}
Also used : IntegerStack(org.antlr.v4.runtime.misc.IntegerStack) ArrayDeque(java.util.ArrayDeque)

Example 4 with ErrorNode

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

the class TreeViewer method paintBox.

protected void paintBox(Graphics g, Tree tree) {
    Rectangle2D.Double box = getBoundsOfNode(tree);
    // draw the box in the background
    boolean ruleFailedAndMatchedNothing = false;
    if (tree instanceof ParserRuleContext) {
        ParserRuleContext ctx = (ParserRuleContext) tree;
        ruleFailedAndMatchedNothing = ctx.exception != null && ctx.stop != null && ctx.stop.getTokenIndex() < ctx.start.getTokenIndex();
    }
    if (isHighlighted(tree) || boxColor != null || tree instanceof ErrorNode || ruleFailedAndMatchedNothing) {
        if (isHighlighted(tree))
            g.setColor(highlightedBoxColor);
        else if (tree instanceof ErrorNode || ruleFailedAndMatchedNothing)
            g.setColor(LIGHT_RED);
        else
            g.setColor(boxColor);
        g.fillRoundRect((int) box.x, (int) box.y, (int) box.width - 1, (int) box.height - 1, arcSize, arcSize);
    }
    if (borderColor != null) {
        g.setColor(borderColor);
        g.drawRoundRect((int) box.x, (int) box.y, (int) box.width - 1, (int) box.height - 1, arcSize, arcSize);
    }
    // draw the text on top of the box (possibly multiple lines)
    g.setColor(textColor);
    String s = getText(tree);
    String[] lines = s.split("\n");
    FontMetrics m = getFontMetrics(font);
    int x = (int) box.x + arcSize / 2 + nodeWidthPadding;
    int y = (int) box.y + m.getAscent() + m.getLeading() + 1 + nodeHeightPadding;
    for (int i = 0; i < lines.length; i++) {
        text(g, lines[i], x, y);
        y += m.getHeight();
    }
}
Also used : ParserRuleContext(org.antlr.v4.runtime.ParserRuleContext) FontMetrics(java.awt.FontMetrics) Rectangle2D(java.awt.geom.Rectangle2D) ErrorNode(org.antlr.v4.runtime.tree.ErrorNode)

Example 5 with ErrorNode

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

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 = createTerminalNode(_ctx, o);
            _ctx.addChild(node);
            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)

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