Advertisement
Guest User

Untitled

a guest
Mar 19th, 2012
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.93 KB | None | 0 0
  1.     public static void parseSource(String source) {
  2.         try {
  3.             // First create a file stream using the povided file/path
  4.             // and tell the lexer that that is the character source.
  5.             // You can also use text that you have already read of course
  6.             // by using the string stream.
  7.             //
  8.             LEXER.setCharStream(new ANTLRFileStream(source, "UTF8"));
  9.  
  10.             // Using the lexer as the token source, we create a token
  11.             // stream to be consumed by the parser
  12.             //
  13.             CommonTokenStream tokens = new CommonTokenStream(LEXER);
  14.  
  15.             // Now we need an instance of our parser
  16.             //
  17.             JavaParser parser = new JavaParser(tokens);
  18.  
  19.             System.out.println("file: " + source);
  20.  
  21.             // Provide some user feedback
  22.             //
  23.             System.out.println("    Lexer Start");
  24.             long start = System.currentTimeMillis();
  25.  
  26.             // Force token load and lex (don't do this normally,
  27.             // it is just for timing the lexer)
  28.             //
  29.             tokens.LT(1);
  30.             long lexerStop = System.currentTimeMillis();
  31.             System.out.println("      lexed in " + (lexerStop - start) + "ms.");
  32.  
  33.             // And now we merely invoke the start rule for the parser
  34.             //
  35.             System.out.println("    Parser Start");
  36.             long pStart = System.currentTimeMillis();
  37.             JavaParser.javaSource_return psrReturn = parser.javaSource();
  38.             long stop = System.currentTimeMillis();
  39.             System.out.println("      Parsed in " + (stop - pStart) + "ms.");
  40.  
  41.             // Pick up the generic tree
  42.             //
  43.             Tree t = (Tree) psrReturn.getTree();
  44.  
  45.             // NOw walk it with the generic tree walker, which does nothing but
  46.             // verify the tree really.
  47.             //
  48.             try {
  49.                 JavaTreeParser.javaSource_return javaSource = null;
  50.                 if (parser.getNumberOfSyntaxErrors() == 0) {
  51.                     JavaTreeParser walker = new JavaTreeParser(new CommonTreeNodeStream(t));
  52.                     System.out.println("    AST Walk Start\n");
  53.                     pStart = System.currentTimeMillis();
  54.                     javaSource = walker.javaSource();
  55.                     stop = System.currentTimeMillis();
  56.                     System.out.println("\n      AST Walked in " + (stop - pStart) + "ms.");
  57.                 }
  58.                 if (MAKE_DOT && tokens.size() < 4096) {
  59.                     System.out.println(((Tree) javaSource.getTree()).toStringTree());
  60.                 }
  61.             } catch (Exception w) {
  62.                 System.out.println("AST walk caused exception.");
  63.                 w.printStackTrace();
  64.             }
  65.  
  66.         } catch (IOException ex) {
  67.             ex.printStackTrace();
  68.         } catch (RecognitionException e) {
  69.             e.printStackTrace();
  70.         }
  71.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement