Search in sources :

Example 56 with Tree

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

the class BaseCppTest method execStartRule.

public ParseTree execStartRule(String startRuleName, Parser parser) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
    Method startRule = null;
    Object[] args = null;
    try {
        startRule = parser.getClass().getMethod(startRuleName);
    } catch (NoSuchMethodException nsme) {
        // try with int _p arg for recursive func
        startRule = parser.getClass().getMethod(startRuleName, int.class);
        args = new Integer[] { 0 };
    }
    ParseTree result = (ParseTree) startRule.invoke(parser, args);
    //		System.out.println("parse tree = "+result.toStringTree(parser));
    return result;
}
Also used : Method(java.lang.reflect.Method) ParseTree(org.antlr.v4.runtime.tree.ParseTree)

Example 57 with Tree

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

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

the class TestGrammarParserInterpreter method testInterp.

InterpreterRuleContext testInterp(LexerGrammar lg, Grammar g, String startRule, String input, String expectedParseTree) {
    LexerInterpreter lexEngine = lg.createLexerInterpreter(new ANTLRInputStream(input));
    CommonTokenStream tokens = new CommonTokenStream(lexEngine);
    GrammarParserInterpreter parser = g.createGrammarParserInterpreter(tokens);
    ParseTree t = parser.parse(g.rules.get(startRule).index);
    InterpreterTreeTextProvider nodeTextProvider = new InterpreterTreeTextProvider(g.getRuleNames());
    String treeStr = Trees.toStringTree(t, nodeTextProvider);
    //		System.out.println("parse tree: "+treeStr);
    assertEquals(expectedParseTree, treeStr);
    return (InterpreterRuleContext) t;
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) LexerInterpreter(org.antlr.v4.runtime.LexerInterpreter) GrammarParserInterpreter(org.antlr.v4.tool.GrammarParserInterpreter) InterpreterRuleContext(org.antlr.v4.runtime.InterpreterRuleContext) ANTLRInputStream(org.antlr.v4.runtime.ANTLRInputStream) ParseTree(org.antlr.v4.runtime.tree.ParseTree)

Example 59 with Tree

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

the class TestRig method process.

protected void process(Lexer lexer, Class<? extends Parser> parserClass, Parser parser, CharStream input) throws IOException, IllegalAccessException, InvocationTargetException, PrintException {
    lexer.setInputStream(input);
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    tokens.fill();
    if (showTokens) {
        for (Token tok : tokens.getTokens()) {
            if (tok instanceof CommonToken) {
                System.out.println(((CommonToken) tok).toString(lexer));
            } else {
                System.out.println(tok.toString());
            }
        }
    }
    if (startRuleName.equals(LEXER_START_RULE_NAME))
        return;
    if (diagnostics) {
        parser.addErrorListener(new DiagnosticErrorListener());
        parser.getInterpreter().setPredictionMode(PredictionMode.LL_EXACT_AMBIG_DETECTION);
    }
    if (printTree || gui || psFile != null) {
        parser.setBuildParseTree(true);
    }
    if (SLL) {
        // overrides diagnostics
        parser.getInterpreter().setPredictionMode(PredictionMode.SLL);
    }
    parser.setTokenStream(tokens);
    parser.setTrace(trace);
    try {
        Method startRule = parserClass.getMethod(startRuleName);
        ParserRuleContext tree = (ParserRuleContext) startRule.invoke(parser, (Object[]) null);
        if (printTree) {
            System.out.println(tree.toStringTree(parser));
        }
        if (gui) {
            Trees.inspect(tree, parser);
        }
        if (psFile != null) {
            // Generate postscript
            Trees.save(tree, parser, psFile);
        }
    } catch (NoSuchMethodException nsme) {
        System.err.println("No method for rule " + startRuleName + " or it has arguments");
    }
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) ParserRuleContext(org.antlr.v4.runtime.ParserRuleContext) DiagnosticErrorListener(org.antlr.v4.runtime.DiagnosticErrorListener) CommonToken(org.antlr.v4.runtime.CommonToken) Token(org.antlr.v4.runtime.Token) Method(java.lang.reflect.Method) CommonToken(org.antlr.v4.runtime.CommonToken)

Example 60 with Tree

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

the class TreePostScriptGenerator method getPS.

public String getPS() {
    // generate the edges and boxes (with text)
    generateEdges(getTree().getRoot());
    for (Tree node : treeLayout.getNodeBounds().keySet()) {
        generateNode(node);
    }
    Dimension size = treeLayout.getBounds().getBounds().getSize();
    doc.boundingBox(size.width, size.height);
    doc.close();
    return doc.getPS();
}
Also used : Tree(org.antlr.v4.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