Search in sources :

Example 1 with RuleNode

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

the class IterativeParseTreeWalker method walk.

@Override
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 2 with RuleNode

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

the class ParseTreeWalker method exitRule.

protected void exitRule(ParseTreeListener listener, RuleNode r) {
    ParserRuleContext ctx = (ParserRuleContext) r.getRuleContext();
    ctx.exitRule(listener);
    listener.exitEveryRule(ctx);
}
Also used : ParserRuleContext(org.antlr.v4.runtime.ParserRuleContext)

Example 3 with RuleNode

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

the class Grammar method getStateToGrammarRegionMap.

public static Map<Integer, Interval> getStateToGrammarRegionMap(GrammarRootAST ast, IntervalSet grammarTokenTypes) {
    Map<Integer, Interval> stateToGrammarRegionMap = new HashMap<Integer, Interval>();
    if (ast == null)
        return stateToGrammarRegionMap;
    List<GrammarAST> nodes = ast.getNodesWithType(grammarTokenTypes);
    for (GrammarAST n : nodes) {
        if (n.atnState != null) {
            Interval tokenRegion = Interval.of(n.getTokenStartIndex(), n.getTokenStopIndex());
            org.antlr.runtime.tree.Tree ruleNode = null;
            // RULEs, BLOCKs of transformed recursive rules point to original token interval
            switch(n.getType()) {
                case ANTLRParser.RULE:
                    ruleNode = n;
                    break;
                case ANTLRParser.BLOCK:
                case ANTLRParser.CLOSURE:
                    ruleNode = n.getAncestor(ANTLRParser.RULE);
                    break;
            }
            if (ruleNode instanceof RuleAST) {
                String ruleName = ((RuleAST) ruleNode).getRuleName();
                Rule r = ast.g.getRule(ruleName);
                if (r instanceof LeftRecursiveRule) {
                    RuleAST originalAST = ((LeftRecursiveRule) r).getOriginalAST();
                    tokenRegion = Interval.of(originalAST.getTokenStartIndex(), originalAST.getTokenStopIndex());
                }
            }
            stateToGrammarRegionMap.put(n.atnState.stateNumber, tokenRegion);
        }
    }
    return stateToGrammarRegionMap;
}
Also used : RuleAST(org.antlr.v4.tool.ast.RuleAST) OrderedHashMap(org.antlr.v4.misc.OrderedHashMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) GrammarAST(org.antlr.v4.tool.ast.GrammarAST) Interval(org.antlr.v4.runtime.misc.Interval)

Example 4 with RuleNode

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

the class TestVisitors method testShouldNotVisitTerminal.

/**
	 * This test verifies that {@link AbstractParseTreeVisitor#shouldVisitNextChild} is called before visiting the first
	 * child. It also verifies that {@link AbstractParseTreeVisitor#defaultResult} provides the default return value for
	 * visiting a tree.
	 */
@Test
public void testShouldNotVisitTerminal() {
    String input = "A";
    VisitorBasicLexer lexer = new VisitorBasicLexer(new ANTLRInputStream(input));
    VisitorBasicParser parser = new VisitorBasicParser(new CommonTokenStream(lexer));
    VisitorBasicParser.SContext context = parser.s();
    Assert.assertEquals("(s A <EOF>)", context.toStringTree(parser));
    VisitorBasicVisitor<String> listener = new VisitorBasicBaseVisitor<String>() {

        @Override
        public String visitTerminal(TerminalNode node) {
            throw new RuntimeException("Should not be reachable");
        }

        @Override
        protected String defaultResult() {
            return "default result";
        }

        @Override
        protected boolean shouldVisitNextChild(RuleNode node, String currentResult) {
            return false;
        }
    };
    String result = listener.visit(context);
    String expected = "default result";
    Assert.assertEquals(expected, result);
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) RuleNode(org.antlr.v4.runtime.tree.RuleNode) TerminalNode(org.antlr.v4.runtime.tree.TerminalNode) ANTLRInputStream(org.antlr.v4.runtime.ANTLRInputStream) Test(org.junit.Test)

Example 5 with RuleNode

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

the class TestVisitors method testShouldNotVisitEOF.

/**
	 * This test verifies that {@link AbstractParseTreeVisitor#visitChildren} does not call
	 * {@link ParseTreeVisitor#visit} after {@link AbstractParseTreeVisitor#shouldVisitNextChild} returns
	 * {@code false}.
	 */
@Test
public void testShouldNotVisitEOF() {
    String input = "A";
    VisitorBasicLexer lexer = new VisitorBasicLexer(new ANTLRInputStream(input));
    VisitorBasicParser parser = new VisitorBasicParser(new CommonTokenStream(lexer));
    VisitorBasicParser.SContext context = parser.s();
    Assert.assertEquals("(s A <EOF>)", context.toStringTree(parser));
    VisitorBasicVisitor<String> listener = new VisitorBasicBaseVisitor<String>() {

        @Override
        public String visitTerminal(TerminalNode node) {
            return node.getSymbol().toString() + "\n";
        }

        @Override
        protected boolean shouldVisitNextChild(RuleNode node, String currentResult) {
            return currentResult == null || currentResult.isEmpty();
        }
    };
    String result = listener.visit(context);
    String expected = "[@0,0:0='A',<1>,1:0]\n";
    Assert.assertEquals(expected, result);
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) RuleNode(org.antlr.v4.runtime.tree.RuleNode) TerminalNode(org.antlr.v4.runtime.tree.TerminalNode) ANTLRInputStream(org.antlr.v4.runtime.ANTLRInputStream) Test(org.junit.Test)

Aggregations

ANTLRInputStream (org.antlr.v4.runtime.ANTLRInputStream)2 CommonTokenStream (org.antlr.v4.runtime.CommonTokenStream)2 ParserRuleContext (org.antlr.v4.runtime.ParserRuleContext)2 RuleNode (org.antlr.v4.runtime.tree.RuleNode)2 TerminalNode (org.antlr.v4.runtime.tree.TerminalNode)2 Test (org.junit.Test)2 ArrayDeque (java.util.ArrayDeque)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 OrderedHashMap (org.antlr.v4.misc.OrderedHashMap)1 IntegerStack (org.antlr.v4.runtime.misc.IntegerStack)1 Interval (org.antlr.v4.runtime.misc.Interval)1 GrammarAST (org.antlr.v4.tool.ast.GrammarAST)1 RuleAST (org.antlr.v4.tool.ast.RuleAST)1