Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static void parseSource(String source) {
- try {
- // First create a file stream using the povided file/path
- // and tell the lexer that that is the character source.
- // You can also use text that you have already read of course
- // by using the string stream.
- //
- LEXER.setCharStream(new ANTLRFileStream(source, "UTF8"));
- // Using the lexer as the token source, we create a token
- // stream to be consumed by the parser
- //
- CommonTokenStream tokens = new CommonTokenStream(LEXER);
- // Now we need an instance of our parser
- //
- JavaParser parser = new JavaParser(tokens);
- System.out.println("file: " + source);
- // Provide some user feedback
- //
- System.out.println(" Lexer Start");
- long start = System.currentTimeMillis();
- // Force token load and lex (don't do this normally,
- // it is just for timing the lexer)
- //
- tokens.LT(1);
- long lexerStop = System.currentTimeMillis();
- System.out.println(" lexed in " + (lexerStop - start) + "ms.");
- // And now we merely invoke the start rule for the parser
- //
- System.out.println(" Parser Start");
- long pStart = System.currentTimeMillis();
- JavaParser.javaSource_return psrReturn = parser.javaSource();
- long stop = System.currentTimeMillis();
- System.out.println(" Parsed in " + (stop - pStart) + "ms.");
- // Pick up the generic tree
- //
- Tree t = (Tree) psrReturn.getTree();
- // NOw walk it with the generic tree walker, which does nothing but
- // verify the tree really.
- //
- try {
- JavaTreeParser.javaSource_return javaSource = null;
- if (parser.getNumberOfSyntaxErrors() == 0) {
- JavaTreeParser walker = new JavaTreeParser(new CommonTreeNodeStream(t));
- System.out.println(" AST Walk Start\n");
- pStart = System.currentTimeMillis();
- javaSource = walker.javaSource();
- stop = System.currentTimeMillis();
- System.out.println("\n AST Walked in " + (stop - pStart) + "ms.");
- }
- if (MAKE_DOT && tokens.size() < 4096) {
- System.out.println(((Tree) javaSource.getTree()).toStringTree());
- }
- } catch (Exception w) {
- System.out.println("AST walk caused exception.");
- w.printStackTrace();
- }
- } catch (IOException ex) {
- ex.printStackTrace();
- } catch (RecognitionException e) {
- e.printStackTrace();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement