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;
}
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());
}
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;
}
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");
}
}
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();
}
Aggregations