View Javadoc

1   package org.codehaus.groovy.antlr;
2   import java.io.*;
3   import antlr.collections.AST;
4   import antlr.collections.impl.*;
5   import antlr.debug.misc.*;
6   import antlr.*;
7   //import com.thoughtworks.xstream.XStream;
8   import org.codehaus.groovy.antlr.parser.*;
9   import java.awt.event.*;
10  
11  class Main {
12  
13      static boolean whitespaceIncluded = false;
14  
15  	static boolean showTree = false;
16      //static boolean xml = false;
17  	static boolean verbose = false;
18      public static void main(String[] args) {
19  		// Use a try/catch block for parser exceptions
20  		try {
21  			// if we have at least one command-line argument
22  			if (args.length > 0 ) {
23  				System.err.println("Parsing...");
24  
25  				// for each directory/file specified on the command line
26  				for(int i=0; i< args.length;i++) {
27  					if ( args[i].equals("-showtree") ) {
28  						showTree = true;
29  					}
30                      //else if ( args[i].equals("-xml") ) {
31                      //    xml = true;
32                      //}
33  					else if ( args[i].equals("-verbose") ) {
34  						verbose = true;
35  					}
36  					else if ( args[i].equals("-trace") ) {
37  						GroovyRecognizer.tracing = true;
38  						GroovyLexer.tracing = true;
39  					}
40  					else if ( args[i].equals("-traceParser") ) {
41  						GroovyRecognizer.tracing = true;
42  					}
43  					else if ( args[i].equals("-traceLexer") ) {
44  						GroovyLexer.tracing = true;
45  					}
46                                          else if ( args[i].equals("-whitespaceIncluded") ) {
47                                              whitespaceIncluded = true;
48                                          }
49                                          else {
50  						doFile(new File(args[i])); // parse it
51  					}
52  				} }
53  			else
54  				System.err.println("Usage: java -jar groovyc.jar [-showtree] [-verbose] [-trace{,Lexer,Parser}]"+
55                                     "<directory or file name>");
56  		}
57  		catch(Exception e) {
58  			System.err.println("exception: "+e);
59  			e.printStackTrace(System.err);   // so we can get stack trace
60  		}
61  	}
62  
63  
64  	// This method decides what action to take based on the type of
65  	//   file we are looking at
66  	public static void doFile(File f)
67  							  throws Exception {
68  		// If this is a directory, walk each file/dir in that directory
69  		if (f.isDirectory()) {
70  			String files[] = f.list();
71  			for(int i=0; i < files.length; i++)
72  				doFile(new File(f, files[i]));
73  		}
74  
75  		// otherwise, if this is a groovy file, parse it!
76  		else if (f.getName().endsWith(".groovy")) {
77  			System.err.println(" --- "+f.getAbsolutePath());
78  			// parseFile(f.getName(), new FileInputStream(f));
79              UnicodeEscapingReader unicodeReader = new UnicodeEscapingReader(new FileReader(f));
80              GroovyLexer lexer = new GroovyLexer(unicodeReader);
81              unicodeReader.setLexer(lexer);
82  			parseFile(f.getName(),lexer);
83  		}
84  	}
85  
86  	// Here's where we do the real work...
87  	public static void parseFile(String f, GroovyLexer l)
88  								 throws Exception {
89  		try {
90  			// Create a parser that reads from the scanner
91  			GroovyRecognizer parser = GroovyRecognizer.make(l);
92  			parser.setFilename(f);
93                          
94                          if (whitespaceIncluded) {
95                              GroovyLexer lexer = parser.getLexer();
96                              lexer.setWhitespaceIncluded(true);
97                              while (true) {
98                                  Token t = lexer.nextToken();
99                                  System.out.println(t);
100                                 if (t == null || t.getType() == Token.EOF_TYPE)  break;
101                             }
102                             return;
103                         }
104 
105 			// start parsing at the compilationUnit rule
106 			parser.compilationUnit();
107 			
108 			System.out.println("parseFile "+f+" => "+parser.getAST());
109 
110 			// do something with the tree
111 			doTreeAction(f, parser.getAST(), parser.getTokenNames());
112 		}
113 		catch (Exception e) {
114 			System.err.println("parser exception: "+e);
115 			e.printStackTrace();   // so we can get stack trace		
116 		}
117 	}
118 	
119 	public static void doTreeAction(String f, AST t, String[] tokenNames) {
120 		if ( t==null ) return;
121 		if ( showTree ) {
122 			((CommonAST)t).setVerboseStringConversion(true, tokenNames);
123 			ASTFactory factory = new ASTFactory();
124 			AST r = factory.create(0,"AST ROOT");
125 			r.setFirstChild(t);
126 			final ASTFrame frame = new ASTFrame("Groovy AST", r);
127 			frame.setVisible(true);
128 			frame.addWindowListener(
129 				new WindowAdapter() {
130                    public void windowClosing (WindowEvent e) {
131                        frame.setVisible(false); // hide the Frame
132                        frame.dispose();
133                        System.exit(0);
134                    }
135 		        }
136 			);
137 			if (verbose)  System.out.println(t.toStringList());
138 		}
139         /*if ( xml ) {
140 			((CommonAST)t).setVerboseStringConversion(true, tokenNames);
141 			ASTFactory factory = new ASTFactory();
142 			AST r = factory.create(0,"AST ROOT");
143 			r.setFirstChild(t);
144             XStream xstream = new XStream();
145             xstream.alias("ast", CommonAST.class);
146 			try {
147                 xstream.toXML(r,new FileWriter(f + ".xml"));
148                 System.out.println("Written AST to " + f + ".xml");
149             } catch (Exception e) {
150                 System.out.println("couldn't write to " + f + ".xml");
151                 e.printStackTrace();
152             }
153 			//if (verbose)  System.out.println(t.toStringList());
154 		}*/
155 	/*@todo
156 		GroovyTreeParser tparse = new GroovyTreeParser();
157 		try {
158 			tparse.compilationUnit(t);
159 			if (verbose)  System.out.println("successful walk of result AST for "+f);
160 		}
161 		catch (RecognitionException e) {
162 			System.err.println(e.getMessage());
163 			e.printStackTrace();
164 		}
165 	@todo*/
166 
167 	}
168 }
169