Search in sources :

Example 46 with Tree

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;
}
Also used : ParseTreeListener(org.antlr.v4.runtime.tree.ParseTreeListener) ErrorNode(org.antlr.v4.runtime.tree.ErrorNode) TerminalNode(org.antlr.v4.runtime.tree.TerminalNode)

Example 47 with Tree

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;
}
Also used : MultiMap(org.antlr.v4.runtime.misc.MultiMap) ParseTree(org.antlr.v4.runtime.tree.ParseTree)

Example 48 with Tree

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();
}
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 49 with Tree

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;
}
Also used : GrammarAST(org.antlr.v4.tool.ast.GrammarAST) Tree(org.antlr.runtime.tree.Tree)

Example 50 with Tree

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;
}
Also used : GrammarAST(org.antlr.v4.tool.ast.GrammarAST) Tree(org.antlr.runtime.tree.Tree)

Aggregations

ParseTree (org.antlr.v4.runtime.tree.ParseTree)30 CommonTokenStream (org.antlr.v4.runtime.CommonTokenStream)21 ANTLRInputStream (org.antlr.v4.runtime.ANTLRInputStream)15 GrammarAST (org.antlr.v4.tool.ast.GrammarAST)12 ParserRuleContext (org.antlr.v4.runtime.ParserRuleContext)9 ParseCancellationException (org.antlr.v4.runtime.misc.ParseCancellationException)8 ByteArrayInputStream (java.io.ByteArrayInputStream)6 InputStream (java.io.InputStream)6 ArrayList (java.util.ArrayList)6 BailErrorStrategy (org.antlr.v4.runtime.BailErrorStrategy)6 Tree (org.antlr.v4.runtime.tree.Tree)6 IOException (java.io.IOException)5 Tree (org.antlr.runtime.tree.Tree)4 ParserInterpreter (org.antlr.v4.runtime.ParserInterpreter)4 RecognitionException (org.antlr.v4.runtime.RecognitionException)4 Rectangle2D (java.awt.geom.Rectangle2D)3 FileInputStream (java.io.FileInputStream)3 Method (java.lang.reflect.Method)3 Map (java.util.Map)3 GrammarASTAdaptor (org.antlr.v4.parse.GrammarASTAdaptor)3