Search in sources :

Example 21 with TerminalNode

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

Example 22 with TerminalNode

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

the class BaseCppTest method writeParserTestFile.

protected void writeParserTestFile(String parserName, String lexerName, String listenerName, String visitorName, String parserStartRuleName, boolean debug, boolean trace) {
    if (!parserStartRuleName.endsWith(")"))
        parserStartRuleName += "()";
    ST outputFileST = new ST("#include \\<iostream>\n" + "\n" + "#include \"antlr4-runtime.h\"\n" + "#include \"<lexerName>.h\"\n" + "#include \"<parserName>.h\"\n" + "\n" + "using namespace antlr4;\n" + "\n" + "class TreeShapeListener : public tree::ParseTreeListener {\n" + "public:\n" + "  void visitTerminal(tree::TerminalNode *) override {}\n" + "  void visitErrorNode(tree::ErrorNode *) override {}\n" + "  void exitEveryRule(ParserRuleContext *) override {}\n" + "  void enterEveryRule(ParserRuleContext *ctx) override {\n" + "    for (auto child : ctx->children) {\n" + "      tree::ParseTree *parent = child->parent;\n" + "      ParserRuleContext *rule = dynamic_cast\\<ParserRuleContext *>(parent);\n" + "      if (rule != ctx) {\n" + "        throw \"Invalid parse tree shape detected.\";\n" + "      }\n" + "    }\n" + "  }\n" + "};\n" + "\n" + "\n" + "int main(int argc, const char* argv[]) {\n" + "  ANTLRFileStream input(argv[1]);\n" + "  <lexerName> lexer(&input);\n" + "  CommonTokenStream tokens(&lexer);\n" + "<createParser>" + "\n" + "  tree::ParseTree *tree = parser.<parserStartRuleName>;\n" + "  TreeShapeListener listener;\n" + "  tree::ParseTreeWalker::DEFAULT.walk(&listener, tree);\n" + "\n" + "  return 0;\n" + "}\n");
    String stSource = "  <parserName> parser(&tokens);\n";
    if (debug) {
        stSource += "  DiagnosticErrorListener errorListener;\n";
        stSource += "  parser.addErrorListener(&errorListener);\n";
    }
    if (trace)
        stSource += "  parser.setTrace(true);\n";
    ST createParserST = new ST(stSource);
    outputFileST.add("createParser", createParserST);
    outputFileST.add("parserName", parserName);
    outputFileST.add("lexerName", lexerName);
    outputFileST.add("listenerName", listenerName);
    outputFileST.add("visitorName", visitorName);
    outputFileST.add("parserStartRuleName", parserStartRuleName);
    writeFile(tmpdir, "Test.cpp", outputFileST.render());
}
Also used : ST(org.stringtemplate.v4.ST) STGroupString(org.stringtemplate.v4.STGroupString) BaseRuntimeTest.antlrOnString(org.antlr.v4.test.runtime.BaseRuntimeTest.antlrOnString)

Example 23 with TerminalNode

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

the class TestXPath method getNodeStrings.

public List<String> getNodeStrings(String input, String xpath, String startRuleName, String parserName, String lexerName) throws Exception {
    Pair<Parser, Lexer> pl = getParserAndLexer(input, parserName, lexerName);
    Parser parser = pl.a;
    ParseTree tree = execStartRule(startRuleName, parser);
    List<String> nodes = new ArrayList<String>();
    for (ParseTree t : XPath.findAll(tree, xpath, parser)) {
        if (t instanceof RuleContext) {
            RuleContext r = (RuleContext) t;
            nodes.add(parser.getRuleNames()[r.getRuleIndex()]);
        } else {
            TerminalNode token = (TerminalNode) t;
            nodes.add(token.getText());
        }
    }
    return nodes;
}
Also used : Lexer(org.antlr.v4.runtime.Lexer) RuleContext(org.antlr.v4.runtime.RuleContext) ArrayList(java.util.ArrayList) TerminalNode(org.antlr.v4.runtime.tree.TerminalNode) ParseTree(org.antlr.v4.runtime.tree.ParseTree) Parser(org.antlr.v4.runtime.Parser)

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