use of org.antlr.v4.runtime.tree.Tree 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;
}
use of org.antlr.v4.runtime.tree.Tree in project antlr4 by antlr.
the class ParseTreePatternMatcher method matches.
/** Does {@code pattern} matched as rule patternRuleIndex match tree? Pass in a
* compiled pattern instead of a string representation of a tree pattern.
*/
public boolean matches(ParseTree tree, ParseTreePattern pattern) {
MultiMap<String, ParseTree> labels = new MultiMap<String, ParseTree>();
ParseTree mismatchedNode = matchImpl(tree, pattern.getPatternTree(), labels);
return mismatchedNode == null;
}
use of org.antlr.v4.runtime.tree.Tree 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();
}
use of org.antlr.v4.runtime.tree.Tree in project antlr4 by antlr.
the class LeftRecursiveRuleAnalyzer method stripLeftRecursion.
// TODO: this strips the tree properly, but since text()
// uses the start of stop token index and gets text from that
// ineffectively ignores this routine.
public GrammarAST stripLeftRecursion(GrammarAST altAST) {
GrammarAST lrlabel = null;
GrammarAST first = (GrammarAST) altAST.getChild(0);
int leftRecurRuleIndex = 0;
if (first.getType() == ELEMENT_OPTIONS) {
first = (GrammarAST) altAST.getChild(1);
leftRecurRuleIndex = 1;
}
// if label=rule
Tree rref = first.getChild(1);
if ((first.getType() == RULE_REF && first.getText().equals(ruleName)) || (rref != null && rref.getType() == RULE_REF && rref.getText().equals(ruleName))) {
if (first.getType() == ASSIGN || first.getType() == PLUS_ASSIGN)
lrlabel = (GrammarAST) first.getChild(0);
// remove rule ref (first child unless options present)
altAST.deleteChild(leftRecurRuleIndex);
// reset index so it prints properly (sets token range of
// ALT to start to right of left recur rule we deleted)
GrammarAST newFirstChild = (GrammarAST) altAST.getChild(leftRecurRuleIndex);
altAST.setTokenStartIndex(newFirstChild.getTokenStartIndex());
}
return lrlabel;
}
use of org.antlr.v4.runtime.tree.Tree in project antlr4 by antlr.
the class LeftRecursiveRuleAnalyzer method hasImmediateRecursiveRuleRefs.
/**
* Match (RULE RULE_REF (BLOCK (ALT .*) (ALT RULE_REF[self] .*) (ALT .*)))
* Match (RULE RULE_REF (BLOCK (ALT .*) (ALT (ASSIGN ID RULE_REF[self]) .*) (ALT .*)))
*/
public static boolean hasImmediateRecursiveRuleRefs(GrammarAST t, String ruleName) {
if (t == null)
return false;
GrammarAST blk = (GrammarAST) t.getFirstChildWithType(BLOCK);
if (blk == null)
return false;
int n = blk.getChildren().size();
for (int i = 0; i < n; i++) {
GrammarAST alt = (GrammarAST) blk.getChildren().get(i);
Tree first = alt.getChild(0);
if (first == null)
continue;
if (first.getType() == ELEMENT_OPTIONS) {
first = alt.getChild(1);
if (first == null) {
continue;
}
}
if (first.getType() == RULE_REF && first.getText().equals(ruleName))
return true;
Tree rref = first.getChild(1);
if (rref != null && rref.getType() == RULE_REF && rref.getText().equals(ruleName))
return true;
}
return false;
}
Aggregations