1 package org.codehaus.groovy.antlr; 2 3 import java.awt.event.WindowAdapter ; 4 import java.awt.event.WindowEvent ; 5 import java.io.File ; 6 import java.io.FileReader ; 7 8 import org.codehaus.groovy.antlr.parser.GroovyLexer; 9 import org.codehaus.groovy.antlr.parser.GroovyRecognizer; 10 11 import antlr.ASTFactory; 12 import antlr.CommonAST; 13 import antlr.Token; 14 import antlr.collections.AST; 15 import antlr.debug.misc.ASTFrame; 16 17 class Main { 18 19 static boolean whitespaceIncluded = false; 20 21 static boolean showTree = false; 22 static boolean verbose = false; 24 public static void main(String [] args) { 25 try { 27 if (args.length > 0 ) { 29 System.err.println("Parsing..."); 30 31 for(int i=0; i< args.length;i++) { 33 if ( args[i].equals("-showtree") ) { 34 showTree = true; 35 } 36 else if ( args[i].equals("-verbose") ) { 40 verbose = true; 41 } 42 else if ( args[i].equals("-trace") ) { 43 GroovyRecognizer.tracing = true; 44 GroovyLexer.tracing = true; 45 } 46 else if ( args[i].equals("-traceParser") ) { 47 GroovyRecognizer.tracing = true; 48 } 49 else if ( args[i].equals("-traceLexer") ) { 50 GroovyLexer.tracing = true; 51 } 52 else if ( args[i].equals("-whitespaceIncluded") ) { 53 whitespaceIncluded = true; 54 } 55 else { 56 doFile(new File (args[i])); } 58 } } 59 else 60 System.err.println("Usage: java -jar groovyc.jar [-showtree] [-verbose] [-trace{,Lexer,Parser}]"+ 61 "<directory or file name>"); 62 } 63 catch(Exception e) { 64 System.err.println("exception: "+e); 65 e.printStackTrace(System.err); } 67 } 68 69 70 public static void doFile(File f) 73 throws Exception { 74 if (f.isDirectory()) { 76 String files[] = f.list(); 77 for(int i=0; i < files.length; i++) 78 doFile(new File (f, files[i])); 79 } 80 81 else if (f.getName().endsWith(".groovy")) { 83 System.err.println(" --- "+f.getAbsolutePath()); 84 SourceBuffer sourceBuffer = new SourceBuffer(); 86 UnicodeEscapingReader unicodeReader = new UnicodeEscapingReader(new FileReader (f),sourceBuffer); 87 GroovyLexer lexer = new GroovyLexer(unicodeReader); 88 unicodeReader.setLexer(lexer); 89 parseFile(f.getName(),lexer,sourceBuffer); 90 } 91 } 92 93 public static void parseFile(String f, GroovyLexer l, SourceBuffer sourceBuffer) 95 throws Exception { 96 try { 97 GroovyRecognizer parser = GroovyRecognizer.make(l); 99 parser.setSourceBuffer(sourceBuffer); 100 parser.setFilename(f); 101 102 if (whitespaceIncluded) { 103 GroovyLexer lexer = parser.getLexer(); 104 lexer.setWhitespaceIncluded(true); 105 while (true) { 106 Token t = lexer.nextToken(); 107 System.out.println(t); 108 if (t == null || t.getType() == Token.EOF_TYPE) break; 109 } 110 return; 111 } 112 113 parser.compilationUnit(); 115 116 System.out.println("parseFile "+f+" => "+parser.getAST()); 117 118 doTreeAction(f, parser.getAST(), parser.getTokenNames()); 120 } 121 catch (Exception e) { 122 System.err.println("parser exception: "+e); 123 e.printStackTrace(); } 125 } 126 127 public static void doTreeAction(String f, AST t, String [] tokenNames) { 128 if ( t==null ) return; 129 if ( showTree ) { 130 CommonAST.setVerboseStringConversion(true, tokenNames); 131 ASTFactory factory = new ASTFactory(); 132 AST r = factory.create(0,"AST ROOT"); 133 r.setFirstChild(t); 134 final ASTFrame frame = new ASTFrame("Groovy AST", r); 135 frame.setVisible(true); 136 frame.addWindowListener( 137 new WindowAdapter () { 138 public void windowClosing (WindowEvent e) { 139 frame.setVisible(false); frame.dispose(); 141 System.exit(0); 142 } 143 } 144 ); 145 if (verbose) System.out.println(t.toStringList()); 146 } 147 163 174 175 } 176 } 177 178 | Popular Tags |