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);
}
}
}
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();
}
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);
}
}
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();
}
}
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;
}
Aggregations