Search in sources :

Example 6 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