public ParseTree parse(String startRule, List visitedStates) throws RecognitionException { ParseTreeBuilder actions = new ParseTreeBuilder(grammar.name); try { parse(startRule, actions, visitedStates); } catch (RecognitionException re) { // Errors are tracked via the ANTLRDebugInterface // Exceptions are used just to blast out of the parse engine // The error will be in the parse tree. } return actions.getTree(); }
public static void main(String[] args) throws IOException { XQueryTree tree = new XQueryTree(); ParseTreeBuilder builder = new XQueryParseTreeBuilder("MainModule"); try { String code = code( "xquery version '1.0-ml';", "declare variable $null as element(func:null) := ''", "$null" ); ANTLRStringStream source; if (StringUtils.isNotBlank(PATH)) { source = new ANTLRStringStream(FileUtils.fileRead(PATH)); } else { source = new ANTLRStringStream(code); source.name = code.toString(); } XQueryLexer lexer = new XQueryLexer(source); TokenStream tokenStream = new LazyTokenStream(lexer); // XQueryParser parser = new XQueryParser(tokenStream, builder); XQueryParser parser = new XQueryParser(tokenStream); parser.setCharSource(source); parser.setTreeAdaptor(new XQueryTreeAdaptor()); tree = (XQueryTree) parser.p_Module().getTree(); } catch (Exception e) { e.printStackTrace(); } finally { if (INCLUDE_PARSE_TREE) { System.out.print(builder.getTree().toStringTree()); } if (INCLUDE_AST) { System.out.print(tree.toStringTree()); } } }
public static void main(String[] args) throws IOException { XQueryTree tree = new XQueryTree(); ParseTreeBuilder builder = new XQueryParseTreeBuilder("MainModule"); File directory = new File(CODE_ROOT); System.out.println("Parsing files in " + CODE_ROOT); List<File> files = FileUtils.getFiles(directory, CODE_FILTER, ""); String treeDirectory = System.getProperty("java.io.tmpdir") + "/parse-trees"; FileUtils.mkdir(treeDirectory); FileUtils.cleanDirectory(treeDirectory); // files = Arrays.asList(new File[]{ new // File("C:\\Users\\cieslinskice\\Documents\\Code\\xqyShared\\common\\src\\main\\xquery\\http\\http.xqy")}); for (File file : files) { System.out.println("Analyzing " + file.getPath() + ":"); try { ANTLRStringStream source = new ANTLRFileStream(file.getAbsolutePath()); source.name = file.getPath(); XQueryLexer lexer = new XQueryLexer(source); TokenStream tokenStream = new LazyTokenStream(lexer); // XQueryParser parser = new XQueryParser(tokenStream, builder); XQueryParser parser = new XQueryParser(tokenStream); parser.setCharSource(source); parser.setTreeAdaptor(new XQueryTreeAdaptor()); tree = (XQueryTree) parser.p_Module().getTree(); } catch (Exception e) { e.printStackTrace(); } finally { String outputDirectory = StringUtils.substringAfter(file.getPath(), CODE_ROOT); FileUtils.mkdir(treeDirectory + outputDirectory); String parseName = treeDirectory + outputDirectory + "/" + StringUtils.substringBefore(file.getName(), ".") + "-parsetree.txt"; String treeName = treeDirectory + outputDirectory + "/" + StringUtils.substringBefore(file.getName(), ".") + "-AST.txt"; // System.out.println("Writing parse tree to " + parseName); // FileUtils.fileWrite(parseName, builder.getTree().toStringTree()); System.out.println("Writing AST to " + parseName); FileUtils.fileWrite(treeName, tree.toStringTree()); } } System.out.println("File parsing complete"); }