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
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
17 static boolean verbose = false;
18 public static void main(String[] args) {
19
20 try {
21
22 if (args.length > 0 ) {
23 System.err.println("Parsing...");
24
25
26 for(int i=0; i< args.length;i++) {
27 if ( args[i].equals("-showtree") ) {
28 showTree = true;
29 }
30
31
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]));
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);
60 }
61 }
62
63
64
65
66 public static void doFile(File f)
67 throws Exception {
68
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
76 else if (f.getName().endsWith(".groovy")) {
77 System.err.println(" --- "+f.getAbsolutePath());
78
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
87 public static void parseFile(String f, GroovyLexer l)
88 throws Exception {
89 try {
90
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
106 parser.compilationUnit();
107
108 System.out.println("parseFile "+f+" => "+parser.getAST());
109
110
111 doTreeAction(f, parser.getAST(), parser.getTokenNames());
112 }
113 catch (Exception e) {
114 System.err.println("parser exception: "+e);
115 e.printStackTrace();
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);
132 frame.dispose();
133 System.exit(0);
134 }
135 }
136 );
137 if (verbose) System.out.println(t.toStringList());
138 }
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167 }
168 }
169