001: package org.codehaus.groovy.antlr;
002:
003: import java.awt.event.WindowAdapter;
004: import java.awt.event.WindowEvent;
005: import java.io.File;
006: import java.io.FileReader;
007:
008: import org.codehaus.groovy.antlr.parser.GroovyLexer;
009: import org.codehaus.groovy.antlr.parser.GroovyRecognizer;
010:
011: import antlr.ASTFactory;
012: import antlr.CommonAST;
013: import antlr.Token;
014: import antlr.collections.AST;
015: import antlr.debug.misc.ASTFrame;
016:
017: class Main {
018:
019: static boolean whitespaceIncluded = false;
020:
021: static boolean showTree = false;
022: //static boolean xml = false;
023: static boolean verbose = false;
024:
025: public static void main(String[] args) {
026: // Use a try/catch block for parser exceptions
027: try {
028: // if we have at least one command-line argument
029: if (args.length > 0) {
030: System.err.println("Parsing...");
031:
032: // for each directory/file specified on the command line
033: for (int i = 0; i < args.length; i++) {
034: if (args[i].equals("-showtree")) {
035: showTree = true;
036: }
037: //else if ( args[i].equals("-xml") ) {
038: // xml = true;
039: //}
040: else if (args[i].equals("-verbose")) {
041: verbose = true;
042: } else if (args[i].equals("-trace")) {
043: GroovyRecognizer.tracing = true;
044: GroovyLexer.tracing = true;
045: } else if (args[i].equals("-traceParser")) {
046: GroovyRecognizer.tracing = true;
047: } else if (args[i].equals("-traceLexer")) {
048: GroovyLexer.tracing = true;
049: } else if (args[i].equals("-whitespaceIncluded")) {
050: whitespaceIncluded = true;
051: } else {
052: doFile(new File(args[i])); // parse it
053: }
054: }
055: } else
056: System.err
057: .println("Usage: java -jar groovyc.jar [-showtree] [-verbose] [-trace{,Lexer,Parser}]"
058: + "<directory or file name>");
059: } catch (Exception e) {
060: System.err.println("exception: " + e);
061: e.printStackTrace(System.err); // so we can get stack trace
062: }
063: }
064:
065: // This method decides what action to take based on the type of
066: // file we are looking at
067: public static void doFile(File f) throws Exception {
068: // If this is a directory, walk each file/dir in that directory
069: if (f.isDirectory()) {
070: String files[] = f.list();
071: for (int i = 0; i < files.length; i++)
072: doFile(new File(f, files[i]));
073: }
074:
075: // otherwise, if this is a groovy file, parse it!
076: else if (f.getName().endsWith(".groovy")) {
077: System.err.println(" --- " + f.getAbsolutePath());
078: // parseFile(f.getName(), new FileInputStream(f));
079: SourceBuffer sourceBuffer = new SourceBuffer();
080: UnicodeEscapingReader unicodeReader = new UnicodeEscapingReader(
081: new FileReader(f), sourceBuffer);
082: GroovyLexer lexer = new GroovyLexer(unicodeReader);
083: unicodeReader.setLexer(lexer);
084: parseFile(f.getName(), lexer, sourceBuffer);
085: }
086: }
087:
088: // Here's where we do the real work...
089: public static void parseFile(String f, GroovyLexer l,
090: SourceBuffer sourceBuffer) throws Exception {
091: try {
092: // Create a parser that reads from the scanner
093: GroovyRecognizer parser = GroovyRecognizer.make(l);
094: parser.setSourceBuffer(sourceBuffer);
095: parser.setFilename(f);
096:
097: if (whitespaceIncluded) {
098: GroovyLexer lexer = parser.getLexer();
099: lexer.setWhitespaceIncluded(true);
100: while (true) {
101: Token t = lexer.nextToken();
102: System.out.println(t);
103: if (t == null || t.getType() == Token.EOF_TYPE)
104: break;
105: }
106: return;
107: }
108:
109: // start parsing at the compilationUnit rule
110: parser.compilationUnit();
111:
112: System.out.println("parseFile " + f + " => "
113: + parser.getAST());
114:
115: // do something with the tree
116: doTreeAction(f, parser.getAST(), parser.getTokenNames());
117: } catch (Exception e) {
118: System.err.println("parser exception: " + e);
119: e.printStackTrace(); // so we can get stack trace
120: }
121: }
122:
123: public static void doTreeAction(String f, AST t, String[] tokenNames) {
124: if (t == null)
125: return;
126: if (showTree) {
127: CommonAST.setVerboseStringConversion(true, tokenNames);
128: ASTFactory factory = new ASTFactory();
129: AST r = factory.create(0, "AST ROOT");
130: r.setFirstChild(t);
131: final ASTFrame frame = new ASTFrame("Groovy AST", r);
132: frame.setVisible(true);
133: frame.addWindowListener(new WindowAdapter() {
134: public void windowClosing(WindowEvent e) {
135: frame.setVisible(false); // hide the Frame
136: frame.dispose();
137: System.exit(0);
138: }
139: });
140: if (verbose)
141: System.out.println(t.toStringList());
142: }
143: /*if ( xml ) {
144: ((CommonAST)t).setVerboseStringConversion(true, tokenNames);
145: ASTFactory factory = new ASTFactory();
146: AST r = factory.create(0,"AST ROOT");
147: r.setFirstChild(t);
148: XStream xstream = new XStream();
149: xstream.alias("ast", CommonAST.class);
150: try {
151: xstream.toXML(r,new FileWriter(f + ".xml"));
152: System.out.println("Written AST to " + f + ".xml");
153: } catch (Exception e) {
154: System.out.println("couldn't write to " + f + ".xml");
155: e.printStackTrace();
156: }
157: //if (verbose) System.out.println(t.toStringList());
158: }*/
159: /*@todo
160: GroovyTreeParser tparse = new GroovyTreeParser();
161: try {
162: tparse.compilationUnit(t);
163: if (verbose) System.out.println("successful walk of result AST for "+f);
164: }
165: catch (RecognitionException e) {
166: System.err.println(e.getMessage());
167: e.printStackTrace();
168: }
169: @todo*/
170:
171: }
172: }
|