KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sablecc > sablecc > launcher > SableCC


1 /* This file is part of SableCC ( http://sablecc.org ).
2  *
3  * Copyright 2007 Etienne M. Gagnon <egagnon@j-meg.com>
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package org.sablecc.sablecc.launcher;
19
20 import java.io.BufferedReader JavaDoc;
21 import java.io.File JavaDoc;
22 import java.io.FileReader JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.io.PushbackReader JavaDoc;
25 import java.util.LinkedList JavaDoc;
26 import java.util.List JavaDoc;
27
28 import org.sablecc.sablecc.GlobalInformation;
29 import org.sablecc.sablecc.SemanticVerifier;
30 import org.sablecc.sablecc.Verbosity;
31 import org.sablecc.sablecc.exception.InternalException;
32 import org.sablecc.sablecc.exception.InvalidArgumentException;
33 import org.sablecc.sablecc.exception.SemanticException;
34 import org.sablecc.sablecc.syntax3.lexer.Lexer;
35 import org.sablecc.sablecc.syntax3.lexer.LexerException;
36 import org.sablecc.sablecc.syntax3.node.Start;
37 import org.sablecc.sablecc.syntax3.parser.Parser;
38 import org.sablecc.sablecc.syntax3.parser.ParserException;
39
40 public class SableCC {
41
42     public static void main(
43             String JavaDoc[] args) {
44
45         try {
46             boolean check_only = false;
47             Verbosity verbosity = Verbosity.NORMAL;
48
49             // default destination directory is current working directory
50
File JavaDoc destinationDirectory = new File JavaDoc(System.getProperty("user.dir"))
51                     .getAbsoluteFile();
52
53             // parse command line arguments
54
Arguments arguments = new Arguments(args);
55
56             // handle option arguments
57
for (OptionArgument optionArgument : arguments.getOptionArguments()) {
58
59                 switch (optionArgument.getOption()) {
60
61                 case CHECK_ONLY:
62                     check_only = true;
63                     break;
64
65                 case DESTINATION:
66                     destinationDirectory = new File JavaDoc(optionArgument.getOperand())
67                             .getAbsoluteFile();
68
69                     if (!destinationDirectory.isDirectory()) {
70
71                         if (!destinationDirectory.exists()) {
72                             System.err.println("ERROR: " + destinationDirectory
73                                     + " does not exist");
74                             System.exit(1);
75                         }
76
77                         System.err.println("ERROR: " + destinationDirectory
78                                 + " is not a directory");
79                         System.exit(1);
80                     }
81
82                     break;
83
84                 case QUIET:
85                     verbosity = Verbosity.QUIET;
86                     break;
87
88                 case VERBOSE:
89                     verbosity = Verbosity.VERBOSE;
90                     break;
91
92                 case VERSION:
93                     System.out.println("SableCC version " + Version.VERSION);
94                     System.exit(0);
95
96                 case HELP:
97                     System.out.println("usage: sablecc "
98                             + Option.getShortHelpMessage()
99                             + " specification.sablecc ...");
100                     System.out.println("options:");
101                     System.out.println(Option.getLongHelpMessage());
102                     System.exit(0);
103
104                 default:
105                     throw new InternalException("unhandled option "
106                             + optionArgument.getOption());
107                 }
108             }
109
110             // handle text arguments
111
if (arguments.getTextArguments().size() == 0) {
112                 System.err.println("usage: sablecc "
113                         + Option.getShortHelpMessage()
114                         + " specification.sablecc ...");
115                 System.err.println("type 'sablecc -h' for more information.");
116                 System.exit(1);
117             }
118
119             switch (verbosity) {
120             case NORMAL:
121             case VERBOSE:
122                 System.out.println();
123                 System.out.println("SableCC version " + Version.VERSION);
124                 System.out
125                         .println("by Etienne M. Gagnon <egagnon@j-meg.com> and other contributors.");
126                 System.out.println();
127             }
128
129             List JavaDoc<File JavaDoc> specificationFiles = new LinkedList JavaDoc<File JavaDoc>();
130
131             // remember absolute paths, just in case the current working
132
// directory gets changed during specification compilation.
133
for (TextArgument textArgument : arguments.getTextArguments()) {
134
135                 if (!textArgument.getText().endsWith(".sablecc")) {
136                     System.err
137                             .println("ERROR: specification file name does not end with .sablecc: "
138                                     + textArgument.getText());
139                     System.exit(1);
140                 }
141
142                 File JavaDoc specificationFile = new File JavaDoc(textArgument.getText())
143                         .getAbsoluteFile();
144
145                 if (!specificationFile.isFile()) {
146
147                     if (!specificationFile.exists()) {
148                         System.err.println("ERROR: " + specificationFile
149                                 + " does not exist");
150                         System.exit(1);
151                     }
152
153                     System.err.println("ERROR: " + specificationFile
154                             + " is not a file");
155                     System.exit(1);
156                 }
157
158                 specificationFiles.add(specificationFile);
159             }
160
161             // compile specifications
162
for (File JavaDoc specificationFile : specificationFiles) {
163
164                 compile(specificationFile, destinationDirectory, check_only,
165                         verbosity);
166             }
167         }
168         catch (InvalidArgumentException e) {
169             System.err.println("ERROR: " + e.getMessage());
170             System.exit(1);
171         }
172         catch (SemanticException e) {
173             System.err.println("ERROR: semantic error on '"
174                     + e.getToken().getText() + "' at " + e.getMessage());
175         }
176         catch (ParserException e) {
177             System.err.println("ERROR: syntax error on '"
178                     + e.getToken().getText() + "' at " + e.getMessage());
179             System.exit(1);
180         }
181         catch (LexerException e) {
182             System.err.println("ERROR: lexical error at " + e.getMessage());
183             System.exit(1);
184         }
185         catch (RuntimeException JavaDoc e) {
186             e.printStackTrace(System.err);
187             System.err.println("INTERNAL ERROR: " + e.getMessage());
188             System.err
189                     .println("Please submit a defect ticket with the full error trace above on:");
190             System.err.println(" http://sablecc.org/");
191             System.exit(1);
192         }
193         catch (Error JavaDoc e) {
194             e.printStackTrace(System.err);
195             System.err.println("INTERNAL ERROR: " + e.getMessage());
196             System.err
197                     .println("Please submit a defect ticket with the full error trace above on:");
198             System.err.println(" http://sablecc.org/");
199             System.exit(1);
200         }
201
202         // finish gracefully
203
System.exit(0);
204     }
205
206     private static void compile(
207             File JavaDoc specificationFile,
208             File JavaDoc destinationDirectory,
209             boolean check_only,
210             Verbosity verbosity)
211             throws InvalidArgumentException, ParserException, LexerException,
212             SemanticException {
213
214         switch (verbosity) {
215         case NORMAL:
216         case VERBOSE:
217             System.out.println("Compiling " + specificationFile);
218         }
219
220         Start ast;
221
222         try {
223             FileReader JavaDoc fr = new FileReader JavaDoc(specificationFile);
224             BufferedReader JavaDoc br = new BufferedReader JavaDoc(fr);
225             PushbackReader JavaDoc pbr = new PushbackReader JavaDoc(br);
226
227             switch (verbosity) {
228             case VERBOSE:
229                 System.out.println(" Parsing");
230             }
231
232             ast = new Parser(new Lexer(pbr)).parse();
233
234             pbr.close();
235             br.close();
236             fr.close();
237         }
238         catch (IOException JavaDoc e) {
239             throw new InvalidArgumentException("cannot read "
240                     + specificationFile, e);
241         }
242
243         GlobalInformation globalInformation = new GlobalInformation(verbosity,
244                 ast);
245         new SemanticVerifier(globalInformation);
246
247         System.err.println("ERROR: unimplemented");
248         System.exit(1);
249     }
250 }
251
Popular Tags