Search in sources :

Example 16 with TerminalNode

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

the class ParserRuleContext method getTokens.

public List<TerminalNode> getTokens(int ttype) {
    if (children == null) {
        return Collections.emptyList();
    }
    List<TerminalNode> tokens = null;
    for (ParseTree o : children) {
        if (o instanceof TerminalNode) {
            TerminalNode tnode = (TerminalNode) o;
            Token symbol = tnode.getSymbol();
            if (symbol.getType() == ttype) {
                if (tokens == null) {
                    tokens = new ArrayList<TerminalNode>();
                }
                tokens.add(tnode);
            }
        }
    }
    if (tokens == null) {
        return Collections.emptyList();
    }
    return tokens;
}
Also used : TerminalNode(org.antlr.v4.runtime.tree.TerminalNode) ParseTree(org.antlr.v4.runtime.tree.ParseTree)

Example 17 with TerminalNode

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

the class ParserRuleContext method getToken.

public TerminalNode getToken(int ttype, int i) {
    if (children == null || i < 0 || i >= children.size()) {
        return null;
    }
    // what token with ttype have we found?
    int j = -1;
    for (ParseTree o : children) {
        if (o instanceof TerminalNode) {
            TerminalNode tnode = (TerminalNode) o;
            Token symbol = tnode.getSymbol();
            if (symbol.getType() == ttype) {
                j++;
                if (j == i) {
                    return tnode;
                }
            }
        }
    }
    return null;
}
Also used : TerminalNode(org.antlr.v4.runtime.tree.TerminalNode) ParseTree(org.antlr.v4.runtime.tree.ParseTree)

Example 18 with TerminalNode

use of org.antlr.v4.runtime.tree.TerminalNode 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 19 with TerminalNode

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

the class TestVisitors method testVisitTerminalNode.

/**
	 * This test verifies the basic behavior of visitors, with an emphasis on
	 * {@link AbstractParseTreeVisitor#visitTerminal}.
	 */
@Test
public void testVisitTerminalNode() {
    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 String defaultResult() {
            return "";
        }

        @Override
        protected String aggregateResult(String aggregate, String nextResult) {
            return aggregate + nextResult;
        }
    };
    String result = listener.visit(context);
    String expected = "[@0,0:0='A',<1>,1:0]\n" + "[@1,1:0='<EOF>',<-1>,1:1]\n";
    Assert.assertEquals(expected, result);
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) TerminalNode(org.antlr.v4.runtime.tree.TerminalNode) ANTLRInputStream(org.antlr.v4.runtime.ANTLRInputStream) Test(org.junit.Test)

Example 20 with TerminalNode

use of org.antlr.v4.runtime.tree.TerminalNode 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)

Aggregations

TerminalNode (org.antlr.v4.runtime.tree.TerminalNode)19 SegmentCode (jetbrick.template.parser.code.SegmentCode)7 Token (org.antlr.v4.runtime.Token)6 ParseTree (org.antlr.v4.runtime.tree.ParseTree)5 TextCode (jetbrick.template.parser.code.TextCode)3 ANTLRInputStream (org.antlr.v4.runtime.ANTLRInputStream)3 CommonTokenStream (org.antlr.v4.runtime.CommonTokenStream)3 ParserRuleContext (org.antlr.v4.runtime.ParserRuleContext)3 Test (org.junit.Test)3 ArrayList (java.util.ArrayList)2 BlockCode (jetbrick.template.parser.code.BlockCode)2 Code (jetbrick.template.parser.code.Code)2 DefineExpressionCode (jetbrick.template.parser.code.DefineExpressionCode)2 ForExpressionCode (jetbrick.template.parser.code.ForExpressionCode)2 MacroCode (jetbrick.template.parser.code.MacroCode)2 ScopeCode (jetbrick.template.parser.code.ScopeCode)2 SegmentListCode (jetbrick.template.parser.code.SegmentListCode)2 TagCode (jetbrick.template.parser.code.TagCode)2 TemplateClassCode (jetbrick.template.parser.code.TemplateClassCode)2 RuleContext (org.antlr.v4.runtime.RuleContext)2