KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > javacc > parser > Options


1 /*
2  * Copyright © 2002 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
3  * California 95054, U.S.A. All rights reserved. Sun Microsystems, Inc. has
4  * intellectual property rights relating to technology embodied in the product
5  * that is described in this document. In particular, and without limitation,
6  * these intellectual property rights may include one or more of the U.S.
7  * patents listed at http://www.sun.com/patents and one or more additional
8  * patents or pending patent applications in the U.S. and in other countries.
9  * U.S. Government Rights - Commercial software. Government users are subject
10  * to the Sun Microsystems, Inc. standard license agreement and applicable
11  * provisions of the FAR and its supplements. Use is subject to license terms.
12  * Sun, Sun Microsystems, the Sun logo and Java are trademarks or registered
13  * trademarks of Sun Microsystems, Inc. in the U.S. and other countries. This
14  * product is covered and controlled by U.S. Export Control laws and may be
15  * subject to the export or import laws in other countries. Nuclear, missile,
16  * chemical biological weapons or nuclear maritime end uses or end users,
17  * whether direct or indirect, are strictly prohibited. Export or reexport
18  * to countries subject to U.S. embargo or to entities identified on U.S.
19  * export exclusion lists, including, but not limited to, the denied persons
20  * and specially designated nationals lists is strictly prohibited.
21  */

22
23 package org.javacc.parser;
24
25 import java.io.File JavaDoc;
26 import java.util.HashMap JavaDoc;
27 import java.util.HashSet JavaDoc;
28 import java.util.Map JavaDoc;
29 import java.util.Set JavaDoc;
30
31 /**
32  * A class with static state that stores all option information.
33  */

34 public class Options {
35
36     /**
37      * Limit subclassing to derived classes.
38      */

39     protected Options() {
40     }
41
42     /**
43      * A mapping of option names (Strings) to values (Integer, Boolean, String).
44      * This table is initialized by the main program. Its contents defines the
45      * set of legal options. Its initial values define the default option
46      * values, and the option types can be determined from these values too.
47      */

48     protected static Map JavaDoc optionValues = null;
49
50     /**
51      * Convenience method to retrieve integer options.
52      */

53     protected static int intValue(final String JavaDoc option) {
54         return ((Integer JavaDoc) optionValues.get(option)).intValue();
55     }
56
57     /**
58      * Convenience method to retrieve boolean options.
59      */

60     protected static boolean booleanValue(final String JavaDoc option) {
61         return ((Boolean JavaDoc) optionValues.get(option)).booleanValue();
62     }
63
64     /**
65      * Convenience method to retrieve string options.
66      */

67     protected static String JavaDoc stringValue(final String JavaDoc option) {
68         return (String JavaDoc) optionValues.get(option);
69     }
70
71     /**
72      * Keep track of what options were set as a command line argument. We use
73      * this to see if the options set from the command line and the ones set in
74      * the input files clash in any way.
75      */

76     private static Set JavaDoc cmdLineSetting = null;
77
78     /**
79      * Keep track of what options were set from the grammar file. We use this to
80      * see if the options set from the command line and the ones set in the
81      * input files clash in any way.
82      */

83     private static Set JavaDoc inputFileSetting = null;
84
85     /**
86      * Initialize for JavaCC
87      */

88     public static void init() {
89         optionValues = new HashMap JavaDoc();
90         cmdLineSetting = new HashSet JavaDoc();
91         inputFileSetting = new HashSet JavaDoc();
92
93         optionValues.put("LOOKAHEAD", new Integer JavaDoc(1));
94         optionValues.put("CHOICE_AMBIGUITY_CHECK", new Integer JavaDoc(2));
95         optionValues.put("OTHER_AMBIGUITY_CHECK", new Integer JavaDoc(1));
96
97         optionValues.put("STATIC", Boolean.TRUE);
98         optionValues.put("DEBUG_PARSER", Boolean.FALSE);
99         optionValues.put("DEBUG_LOOKAHEAD", Boolean.FALSE);
100         optionValues.put("DEBUG_TOKEN_MANAGER", Boolean.FALSE);
101         optionValues.put("ERROR_REPORTING", Boolean.TRUE);
102         optionValues.put("JAVA_UNICODE_ESCAPE", Boolean.FALSE);
103         optionValues.put("UNICODE_INPUT", Boolean.FALSE);
104         optionValues.put("IGNORE_CASE", Boolean.FALSE);
105         optionValues.put("USER_TOKEN_MANAGER", Boolean.FALSE);
106         optionValues.put("USER_CHAR_STREAM", Boolean.FALSE);
107         optionValues.put("BUILD_PARSER", Boolean.TRUE);
108         optionValues.put("BUILD_TOKEN_MANAGER", Boolean.TRUE);
109         optionValues.put("TOKEN_MANAGER_USES_PARSER", Boolean.FALSE);
110         optionValues.put("SANITY_CHECK", Boolean.TRUE);
111         optionValues.put("FORCE_LA_CHECK", Boolean.FALSE);
112         optionValues.put("COMMON_TOKEN_ACTION", Boolean.FALSE);
113         optionValues.put("CACHE_TOKENS", Boolean.FALSE);
114         optionValues.put("KEEP_LINE_COLUMN", Boolean.TRUE);
115
116         optionValues.put("OUTPUT_DIRECTORY", ".");
117         optionValues.put("JDK_VERSION", "1.4");
118     }
119
120     /**
121      * Determine if a given command line argument might be an option flag.
122      * Command line options start with a dash (-).
123      *
124      * @param opt
125      * The command line argument to examine.
126      * @return True when the argument looks like an option flag.
127      */

128     public static boolean isOption(final String JavaDoc opt) {
129         return opt != null && opt.length() > 1 && opt.charAt(0) == '-';
130     }
131
132     public static void setInputFileOption(Object JavaDoc nameloc, Object JavaDoc valueloc,
133             String JavaDoc name, int value) {
134         String JavaDoc s = name.toUpperCase();
135         if (!optionValues.containsKey(s)) {
136             JavaCCErrors.warning(nameloc, "Bad option name \"" + name
137                     + "\". Option setting will be ignored.");
138             return;
139         }
140         Object JavaDoc Val = optionValues.get(s);
141         if (Val != null) {
142             if (!(Val instanceof Integer JavaDoc) || value <= 0) {
143                 JavaCCErrors.warning(valueloc, "Bad option value \"" + value
144                         + "\" for \"" + name
145                         + "\". Option setting will be ignored.");
146                 return;
147             }
148             if (inputFileSetting.contains(s)) {
149                 JavaCCErrors.warning(nameloc, "Duplicate option setting for \""
150                         + name + "\" will be ignored.");
151                 return;
152             }
153             if (cmdLineSetting.contains(s)) {
154                 if (((Integer JavaDoc) Val).intValue() != value) {
155                     JavaCCErrors.warning(nameloc, "Command line setting of \""
156                             + name + "\" modifies option value in file.");
157                 }
158                 return;
159             }
160         }
161
162         optionValues.put(s, new Integer JavaDoc(value));
163         inputFileSetting.add(s);
164     }
165
166     public static void setInputFileOption(Object JavaDoc nameloc, Object JavaDoc valueloc,
167             String JavaDoc name, boolean value) {
168         String JavaDoc s = name.toUpperCase();
169         if (!optionValues.containsKey(s)) {
170             JavaCCErrors.warning(nameloc, "Bad option name \"" + name
171                     + "\". Option setting will be ignored.");
172             return;
173         }
174         Object JavaDoc Val = optionValues.get(s);
175         if (Val != null) {
176             if (!(Val instanceof Boolean JavaDoc)) {
177                 JavaCCErrors.warning(valueloc, "Bad option value \"" + value
178                         + "\" for \"" + name
179                         + "\". Option setting will be ignored.");
180                 return;
181             }
182             if (inputFileSetting.contains(s)) {
183                 JavaCCErrors.warning(nameloc, "Duplicate option setting for \""
184                         + name + "\" will be ignored.");
185                 return;
186             }
187             if (cmdLineSetting.contains(s)) {
188                 if (((Boolean JavaDoc) Val).booleanValue() != value) {
189                     JavaCCErrors.warning(nameloc, "Command line setting of \""
190                             + name + "\" modifies option value in file.");
191                 }
192                 return;
193             }
194         }
195
196         optionValues.put(s, (value ? Boolean.TRUE : Boolean.FALSE));
197         inputFileSetting.add(s);
198     }
199
200     public static void setInputFileOption(Object JavaDoc nameloc, Object JavaDoc valueloc,
201             String JavaDoc name, String JavaDoc value) {
202         String JavaDoc s = name.toUpperCase();
203         if (!optionValues.containsKey(s)) {
204             JavaCCErrors.warning(nameloc, "Bad option name \"" + name
205                     + "\". Option setting will be ignored.");
206             return;
207         }
208         Object JavaDoc Val = optionValues.get(s);
209         if (Val != null) {
210             if (!(Val instanceof String JavaDoc)) {
211                 JavaCCErrors.warning(valueloc, "Bad option value \"" + value
212                         + "\" for \"" + name
213                         + "\". Option setting will be ignored.");
214                 return;
215             }
216             if (inputFileSetting.contains(s)) {
217                 JavaCCErrors.warning(nameloc, "Duplicate option setting for \""
218                         + name + "\" will be ignored.");
219                 return;
220             }
221             if (cmdLineSetting.contains(s)) {
222                 if (!Val.equals(value)) {
223                     JavaCCErrors.warning(nameloc, "Command line setting of \""
224                             + name + "\" modifies option value in file.");
225                 }
226                 return;
227             }
228         }
229
230         optionValues.put(s, value);
231         inputFileSetting.add(s);
232     }
233
234     /**
235      *
236      * @param arg
237      */

238     public static void setCmdLineOption(String JavaDoc arg) {
239         String JavaDoc s = arg.toUpperCase();
240         int index = 0;
241         String JavaDoc name;
242         Object JavaDoc Val;
243         while (index < s.length() && s.charAt(index) != '='
244                 && s.charAt(index) != ':') {
245             index++;
246         }
247         if (index < 2 || index >= s.length() - 1) {
248             if (index == s.length()) {
249                 if (s.length() > 3 && s.charAt(1) == 'N' && s.charAt(2) == 'O') {
250                     name = s.substring(3);
251                     Val = Boolean.FALSE;
252                 } else {
253                     name = s.substring(1);
254                     Val = Boolean.TRUE;
255                 }
256             } else {
257                 System.out.println("Warning: Bad option \"" + arg
258                         + "\" will be ignored.");
259                 return;
260             }
261         } else {
262             if (s.substring(index + 1).equals("TRUE")) {
263                 Val = Boolean.TRUE;
264             } else if (s.substring(index + 1).equals("FALSE")) {
265                 Val = Boolean.FALSE;
266             } else {
267                 try {
268                     int i = Integer.parseInt(s.substring(index + 1));
269                     if (i <= 0) {
270                         System.out.println("Warning: Bad option value in \""
271                                 + arg + "\" will be ignored.");
272                         return;
273                     }
274                     Val = new Integer JavaDoc(i);
275                 } catch (NumberFormatException JavaDoc e) {
276                     Val = arg.substring(index + 1);
277                     if (arg.length() > index + 2) {
278                         // i.e., there is space for two '"'s in value
279
if (arg.charAt(index + 1) == '"'
280                                 && arg.charAt(arg.length() - 1) == '"') {
281                             // remove the two '"'s.
282
Val = arg.substring(index + 2, arg.length() - 1);
283                         }
284                     }
285                 }
286             }
287             name = s.substring(1, index);
288         }
289         if (!optionValues.containsKey(name)) {
290             System.out.println("Warning: Bad option \"" + arg
291                     + "\" will be ignored.");
292             return;
293         }
294         Object JavaDoc valOrig = optionValues.get(name);
295         if (Val.getClass() != valOrig.getClass()) {
296             System.out.println("Warning: Bad option value in \"" + arg
297                     + "\" will be ignored.");
298             return;
299         }
300         if (cmdLineSetting.contains(name)) {
301             System.out.println("Warning: Duplicate option setting \"" + arg
302                     + "\" will be ignored.");
303             return;
304         }
305
306         optionValues.put(name, Val);
307         cmdLineSetting.add(name);
308     }
309
310     public static void normalize() {
311         if (getDebugLookahead() && !getDebugParser()) {
312             if (cmdLineSetting.contains("DEBUG_PARSER")
313                     || inputFileSetting.contains("DEBUG_PARSER")) {
314                 JavaCCErrors
315                         .warning("True setting of option DEBUG_LOOKAHEAD overrides false setting of option DEBUG_PARSER.");
316             }
317             optionValues.put("DEBUG_PARSER", Boolean.TRUE);
318         }
319     }
320
321     /**
322      * Find the lookahead setting.
323      *
324      * @return The requested lookahead value.
325      */

326     public static int getLookahead() {
327         return intValue("LOOKAHEAD");
328     }
329
330     /**
331      * Find the choice ambiguity check value.
332      *
333      * @return The requested choice ambiguity check value.
334      */

335     public static int getChoiceAmbiguityCheck() {
336         return intValue("CHOICE_AMBIGUITY_CHECK");
337     }
338
339     /**
340      * Find the other ambiguity check value.
341      *
342      * @return The requested other ambiguity check value.
343      */

344     public static int getOtherAmbiguityCheck() {
345         return intValue("OTHER_AMBIGUITY_CHECK");
346     }
347
348     /**
349      * Find the static value.
350      *
351      * @return The requested static value.
352      */

353     public static boolean getStatic() {
354         return booleanValue("STATIC");
355     }
356
357     /**
358      * Find the debug parser value.
359      *
360      * @return The requested debug parser value.
361      */

362     public static boolean getDebugParser() {
363         return booleanValue("DEBUG_PARSER");
364     }
365
366     /**
367      * Find the debug lookahead value.
368      *
369      * @return The requested debug lookahead value.
370      */

371     public static boolean getDebugLookahead() {
372         return booleanValue("DEBUG_LOOKAHEAD");
373     }
374
375     /**
376      * Find the debug tokenmanager value.
377      *
378      * @return The requested debug tokenmanager value.
379      */

380     public static boolean getDebugTokenManager() {
381         return booleanValue("DEBUG_TOKEN_MANAGER");
382     }
383
384     /**
385      * Find the error reporting value.
386      *
387      * @return The requested error reporting value.
388      */

389     public static boolean getErrorReporting() {
390         return booleanValue("ERROR_REPORTING");
391     }
392
393     /**
394      * Find the Java unicode escape value.
395      *
396      * @return The requested Java unicode escape value.
397      */

398     public static boolean getJavaUnicodeEscape() {
399         return booleanValue("JAVA_UNICODE_ESCAPE");
400     }
401
402     /**
403      * Find the unicode input value.
404      *
405      * @return The requested unicode input value.
406      */

407     public static boolean getUnicodeInput() {
408         return booleanValue("UNICODE_INPUT");
409     }
410
411     /**
412      * Find the ignore case value.
413      *
414      * @return The requested ignore case value.
415      */

416     public static boolean getIgnoreCase() {
417         return booleanValue("IGNORE_CASE");
418     }
419
420     /**
421      * Find the user tokenmanager value.
422      *
423      * @return The requested user tokenmanager value.
424      */

425     public static boolean getUserTokenManager() {
426         return booleanValue("USER_TOKEN_MANAGER");
427     }
428
429     /**
430      * Find the user charstream value.
431      *
432      * @return The requested user charstream value.
433      */

434     public static boolean getUserCharStream() {
435         return booleanValue("USER_CHAR_STREAM");
436     }
437
438     /**
439      * Find the build parser value.
440      *
441      * @return The requested build parser value.
442      */

443     public static boolean getBuildParser() {
444         return booleanValue("BUILD_PARSER");
445     }
446
447     /**
448      * Find the build token manager value.
449      *
450      * @return The requested build token manager value.
451      */

452     public static boolean getBuildTokenManager() {
453         return booleanValue("BUILD_TOKEN_MANAGER");
454     }
455
456     /**
457      * Find the token manager uses parser value.
458      *
459      * @return The requested token manager uses parser value;
460      */

461      public static boolean getTokenManagerUsesParser(){
462          return booleanValue("TOKEN_MANAGER_USES_PARSER");
463      }
464
465     /**
466      * Find the sanity check value.
467      *
468      * @return The requested sanity check value.
469      */

470     public static boolean getSanityCheck() {
471         return booleanValue("SANITY_CHECK");
472     }
473
474     /**
475      * Find the force lookahead check value.
476      *
477      * @return The requested force lookahead value.
478      */

479     public static boolean getForceLaCheck() {
480         return booleanValue("FORCE_LA_CHECK");
481     }
482
483     /**
484      * Find the common token action value.
485      *
486      * @return The requested common token action value.
487      */

488
489     public static boolean getCommonTokenAction() {
490         return booleanValue("COMMON_TOKEN_ACTION");
491     }
492
493     /**
494      * Find the cache tokens value.
495      *
496      * @return The requested cache tokens value.
497      */

498     public static boolean getCacheTokens() {
499         return booleanValue("CACHE_TOKENS");
500     }
501
502     /**
503      * Find the keep line column value.
504      *
505      * @return The requested keep line column value.
506      */

507     public static boolean getKeepLineColumn() {
508         return booleanValue("KEEP_LINE_COLUMN");
509     }
510
511     /**
512      * Find the JDK version
513      *
514      * @return The requested jdk version.
515      */

516     public static String JavaDoc getJdkVersion() {
517         return stringValue("JDK_VERSION");
518     }
519
520     /**
521      * Find the output directory.
522      *
523      * @return The requested output directory.
524      */

525     public static File JavaDoc getOutputDirectory() {
526         return new File JavaDoc(stringValue("OUTPUT_DIRECTORY"));
527     }
528 }
529
Popular Tags