KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.Vector JavaDoc;
26 import java.io.*;
27 import org.javacc.Version;
28
29 /**
30  * This package contains data created as a result of parsing and semanticizing
31  * a JavaCC input file. This data is what is used by the back-ends of JavaCC as
32  * well as any other back-end of JavaCC related tools such as JJTree.
33  */

34 public class JavaCCGlobals {
35
36   /**
37    * String that identifies the JavaCC generated files.
38    */

39   protected static final String JavaDoc toolName = "JavaCC";
40
41   /**
42    * The name of the grammar file being processed.
43    */

44   static public String JavaDoc fileName;
45
46   /**
47    * The name of the original file (before processing by JJTree and JJCov).
48    * Currently this is the same as fileName.
49    */

50   static public String JavaDoc origFileName;
51
52   /**
53    * Set to true if this file has been processed by JJTree.
54    */

55   static public boolean jjtreeGenerated;
56
57   /**
58    * Set to true if this file has been processed by JJCov.
59    */

60   static public boolean jjcovGenerated;
61
62   /**
63    * The list of tools that have participated in generating the
64    * input grammar file.
65    */

66   static public Vector JavaDoc toolNames;
67
68   /**
69    * This prints the banner line when the various tools are invoked. This
70    * takes as argument the tool's full name and its version.
71    */

72   static public void bannerLine(String JavaDoc fullName, String JavaDoc ver) {
73     System.out.print("Java Compiler Compiler Version " + Version.version + " (" + fullName);
74     if (!ver.equals("")) {
75       System.out.print(" Version " + ver);
76     }
77     System.out.println(")");
78     //System.out.println("Copyright © 2002 Sun Microsystems, Inc. All rights reserved. Use is subject to license terms.");
79
}
80
81   /**
82    * The name of the parser class (what appears in PARSER_BEGIN and PARSER_END).
83    */

84   static public String JavaDoc cu_name;
85
86   /**
87    * This is a list of tokens that appear after "PARSER_BEGIN(name)" all the
88    * way until (but not including) the opening brace "{" of the class "name".
89    */

90   static public java.util.Vector JavaDoc cu_to_insertion_point_1 = new java.util.Vector JavaDoc();
91
92   /**
93    * This is the list of all tokens that appear after the tokens in
94    * "cu_to_insertion_point_1" and until (but not including) the closing brace "}"
95    * of the class "name".
96    */

97   static public java.util.Vector JavaDoc cu_to_insertion_point_2 = new java.util.Vector JavaDoc();
98
99   /**
100    * This is the list of all tokens that appear after the tokens in
101    * "cu_to_insertion_point_2" and until "PARSER_END(name)".
102    */

103   static public java.util.Vector JavaDoc cu_from_insertion_point_2 = new java.util.Vector JavaDoc();
104
105   /**
106    * A list of all grammar productions - normal and JAVACODE - in the order
107    * they appear in the input file. Each entry here will be a subclass of
108    * "NormalProduction".
109    */

110   static public java.util.Vector JavaDoc bnfproductions = new java.util.Vector JavaDoc();
111
112   /**
113    * A symbol table of all grammar productions - normal and JAVACODE. The
114    * symbol table is indexed by the name of the left hand side non-terminal.
115    * Its contents are of type "NormalProduction".
116    */

117   static public java.util.Hashtable JavaDoc production_table = new java.util.Hashtable JavaDoc();
118
119   /**
120    * A mapping of lexical state strings to their integer internal representation.
121    * Integers are stored as java.lang.Integer's.
122    */

123   static public java.util.Hashtable JavaDoc lexstate_S2I = new java.util.Hashtable JavaDoc();
124
125   /**
126    * A mapping of the internal integer representations of lexical states to
127    * their strings. Integers are stored as java.lang.Integer's.
128    */

129   static public java.util.Hashtable JavaDoc lexstate_I2S = new java.util.Hashtable JavaDoc();
130
131   /**
132    * The declarations to be inserted into the TokenManager class.
133    */

134   static public java.util.Vector JavaDoc token_mgr_decls;
135
136   /**
137    * The list of all TokenProductions from the input file. This list includes
138    * implicit TokenProductions that are created for uses of regular expressions
139    * within BNF productions.
140    */

141   static public java.util.Vector JavaDoc rexprlist = new java.util.Vector JavaDoc();
142
143   /**
144    * The total number of distinct tokens. This is therefore one more than the
145    * largest assigned token ordinal.
146    */

147   static public int tokenCount;
148
149   /**
150    * This is a symbol table that contains all named tokens (those that are
151    * defined with a label). The index to the table is the image of the label
152    * and the contents of the table are of type "RegularExpression".
153    */

154   static public java.util.Hashtable JavaDoc named_tokens_table = new java.util.Hashtable JavaDoc();
155
156   /**
157    * Contains the same entries as "named_tokens_table", but this is an ordered
158    * list which is ordered by the order of appearance in the input file.
159    */

160   static public java.util.Vector JavaDoc ordered_named_tokens = new java.util.Vector JavaDoc();
161
162   /**
163    * A mapping of ordinal values (represented as objects of type "Integer") to
164    * the corresponding labels (of type "String"). An entry exists for an ordinal
165    * value only if there is a labeled token corresponding to this entry.
166    * If there are multiple labels representing the same ordinal value, then
167    * only one label is stored.
168    */

169   static public java.util.Hashtable JavaDoc names_of_tokens = new java.util.Hashtable JavaDoc();
170
171   /**
172    * A mapping of ordinal values (represented as objects of type "Integer") to
173    * the corresponding RegularExpression's.
174    */

175   static public java.util.Hashtable JavaDoc rexps_of_tokens = new java.util.Hashtable JavaDoc();
176
177   /**
178    * This is a three-level symbol table that contains all simple tokens (those
179    * that are defined using a single string (with or without a label). The index
180    * to the first level table is a lexical state which maps to a second level
181    * hashtable. The index to the second level hashtable is the string of the
182    * simple token converted to upper case, and this maps to a third level hashtable.
183    * This third level hashtable contains the actual string of the simple token
184    * and maps it to its RegularExpression.
185    */

186   static public java.util.Hashtable JavaDoc simple_tokens_table = new java.util.Hashtable JavaDoc();
187
188   /**
189    * maskindex, jj2index, maskVals are variables that are shared between
190    * ParseEngine and ParseGen.
191    */

192   static protected int maskindex = 0;
193   static protected int jj2index = 0;
194   static protected Vector JavaDoc maskVals = new Vector JavaDoc();
195
196   static Action actForEof;
197   static String JavaDoc nextStateForEof;
198
199
200   // Some general purpose utilities follow.
201

202   /**
203    * Returns the identifying string for the file name, given a toolname
204    * used to generate it.
205    */

206   public static String JavaDoc getIdString(String JavaDoc toolName, String JavaDoc fileName) {
207      Vector JavaDoc toolNames = new Vector JavaDoc();
208      toolNames.addElement(toolName);
209      return getIdString(toolNames, fileName);
210   }
211
212   /**
213    * Returns the identifying string for the file name, given a set of tool
214    * names that are used to generate it.
215    */

216   public static String JavaDoc getIdString(Vector JavaDoc toolNames, String JavaDoc fileName) {
217      int i;
218      String JavaDoc toolNamePrefix = "Generated By:";
219
220      for (i = 0; i < toolNames.size() - 1; i++)
221         toolNamePrefix += (String JavaDoc)toolNames.elementAt(i) + "&";
222      toolNamePrefix += (String JavaDoc)toolNames.elementAt(i) + ":";
223
224      if (toolNamePrefix.length() > 200)
225      {
226         System.out.println("Tool names too long.");
227         throw new Error JavaDoc();
228      }
229
230      return toolNamePrefix + " Do not edit this line. " + addUnicodeEscapes(fileName);
231   }
232
233   /**
234    * Returns true if tool name passed is one of the tool names returned
235    * by getToolNames(fileName).
236    */

237   public static boolean isGeneratedBy(String JavaDoc toolName, String JavaDoc fileName) {
238      Vector JavaDoc v = getToolNames(fileName);
239
240      for (int i = 0; i < v.size(); i++)
241         if (toolName.equals(v.elementAt(i)))
242            return true;
243
244      return false;
245   }
246
247   private static Vector JavaDoc makeToolNameVector(String JavaDoc str) {
248      Vector JavaDoc retVal = new Vector JavaDoc();
249
250      int limit1 = str.indexOf('\n');
251      if (limit1 == -1) limit1 = 1000;
252      int limit2 = str.indexOf('\r');
253      if (limit2 == -1) limit2 = 1000;
254      int limit = (limit1 < limit2) ? limit1 : limit2;
255
256      String JavaDoc tmp;
257      if (limit == 1000) {
258        tmp = str;
259      } else {
260        tmp = str.substring(0, limit);
261      }
262
263      if (tmp.indexOf(':') == -1)
264         return retVal;
265
266      tmp = tmp.substring(tmp.indexOf(':') + 1);
267
268      if (tmp.indexOf(':') == -1)
269         return retVal;
270
271      tmp = tmp.substring(0, tmp.indexOf(':'));
272
273      int i = 0, j = 0;
274
275      while (j < tmp.length() && (i = tmp.indexOf('&', j)) != -1)
276      {
277         retVal.addElement(tmp.substring(j, i));
278         j = i + 1;
279      }
280
281      if (j < tmp.length())
282         retVal.addElement(tmp.substring(j));
283
284      return retVal;
285   }
286
287   /**
288    * Returns a Vector of names of the tools that have been used to generate
289    * the given file.
290    */

291   public static Vector JavaDoc getToolNames(String JavaDoc fileName) {
292      char[] buf = new char[256];
293      java.io.FileReader JavaDoc stream = null;
294      int read, total = 0;
295
296      try {
297        stream = new java.io.FileReader JavaDoc(fileName);
298
299        for (;;)
300           if ((read = stream.read(buf, total, buf.length - total)) != -1)
301           {
302              if ((total += read) == buf.length)
303                 break;
304           }
305           else
306              break;
307
308        return makeToolNameVector(new String JavaDoc(buf, 0, total));
309     } catch(java.io.FileNotFoundException JavaDoc e1) {
310     } catch(java.io.IOException JavaDoc e2) {
311        if (total > 0)
312           return makeToolNameVector(new String JavaDoc(buf, 0, total));
313     }
314     finally {
315        try { stream.close(); }
316        catch (Exception JavaDoc e3) { }
317     }
318
319     return new Vector JavaDoc();
320   }
321
322   public static void createOutputDir(File outputDir) {
323     if (!outputDir.exists()) {
324       JavaCCErrors.warning("Output directory \"" + outputDir + "\" does not exist. Creating the directory.");
325
326       if (!outputDir.mkdirs()) {
327         JavaCCErrors.semantic_error("Cannot create the output directory : " + outputDir);
328         return;
329       }
330     }
331
332     if (!outputDir.isDirectory()) {
333       JavaCCErrors.semantic_error("\"" + outputDir + " is not a valid output directory.");
334       return;
335     }
336
337     if (!outputDir.canWrite()) {
338       JavaCCErrors.semantic_error("Cannot write to the output output directory : \"" + outputDir + "\"");
339       return;
340     }
341   }
342
343   static public String JavaDoc staticOpt() {
344     if (Options.getStatic()) {
345       return "static ";
346     } else {
347       return "";
348     }
349   }
350
351   static public String JavaDoc add_escapes(String JavaDoc str) {
352     String JavaDoc retval = "";
353     char ch;
354     for (int i = 0; i < str.length(); i++) {
355       ch = str.charAt(i);
356       if (ch == '\b') {
357         retval += "\\b";
358       } else if (ch == '\t') {
359         retval += "\\t";
360       } else if (ch == '\n') {
361         retval += "\\n";
362       } else if (ch == '\f') {
363         retval += "\\f";
364       } else if (ch == '\r') {
365         retval += "\\r";
366       } else if (ch == '\"') {
367         retval += "\\\"";
368       } else if (ch == '\'') {
369         retval += "\\\'";
370       } else if (ch == '\\') {
371         retval += "\\\\";
372       } else if (ch < 0x20 || ch > 0x7e) {
373     String JavaDoc s = "0000" + Integer.toString(ch, 16);
374     retval += "\\u" + s.substring(s.length() - 4, s.length());
375       } else {
376         retval += ch;
377       }
378     }
379     return retval;
380   }
381
382   static public String JavaDoc addUnicodeEscapes(String JavaDoc str) {
383     String JavaDoc retval = "";
384     char ch;
385     for (int i = 0; i < str.length(); i++) {
386       ch = str.charAt(i);
387       if (ch < 0x20 || ch > 0x7e) {
388     String JavaDoc s = "0000" + Integer.toString(ch, 16);
389     retval += "\\u" + s.substring(s.length() - 4, s.length());
390       } else {
391         retval += ch;
392       }
393     }
394     return retval;
395   }
396
397   static protected int cline, ccol;
398
399   static protected void printTokenSetup(Token t) {
400     Token tt = t;
401     while (tt.specialToken != null) tt = tt.specialToken;
402     cline = tt.beginLine;
403     ccol = tt.beginColumn;
404   }
405
406   static protected void printTokenOnly(Token t, java.io.PrintWriter JavaDoc ostr) {
407     for (; cline < t.beginLine; cline++) {
408       ostr.println(""); ccol = 1;
409     }
410     for (; ccol < t.beginColumn; ccol++) {
411       ostr.print(" ");
412     }
413     if (t.kind == JavaCCParserConstants.STRING_LITERAL ||
414         t.kind == JavaCCParserConstants.CHARACTER_LITERAL)
415        ostr.print(addUnicodeEscapes(t.image));
416     else
417        ostr.print(t.image);
418     cline = t.endLine;
419     ccol = t.endColumn+1;
420     char last = t.image.charAt(t.image.length()-1);
421     if (last == '\n' || last == '\r') {
422       cline++; ccol = 1;
423     }
424   }
425
426   static protected void printToken(Token t, java.io.PrintWriter JavaDoc ostr) {
427     Token tt = t.specialToken;
428     if (tt != null) {
429       while (tt.specialToken != null) tt = tt.specialToken;
430       while (tt != null) {
431         printTokenOnly(tt, ostr);
432         tt = tt.next;
433       }
434     }
435     printTokenOnly(t, ostr);
436   }
437
438   static protected void printLeadingComments(Token t, java.io.PrintWriter JavaDoc ostr) {
439     if (t.specialToken == null) return;
440     Token tt = t.specialToken;
441     while (tt.specialToken != null) tt = tt.specialToken;
442     while (tt != null) {
443       printTokenOnly(tt, ostr);
444       tt = tt.next;
445     }
446     if (ccol != 1 && cline != t.beginLine) {
447       ostr.println("");
448       cline++; ccol = 1;
449     }
450   }
451
452   static protected void printTrailingComments(Token t, java.io.PrintWriter JavaDoc ostr) {
453     if (t.next == null) return;
454     printLeadingComments(t.next);
455   }
456
457   static protected String JavaDoc printTokenOnly(Token t) {
458     String JavaDoc retval = "";
459     for (; cline < t.beginLine; cline++) {
460       retval += "\n"; ccol = 1;
461     }
462     for (; ccol < t.beginColumn; ccol++) {
463       retval += " ";
464     }
465     if (t.kind == JavaCCParserConstants.STRING_LITERAL ||
466         t.kind == JavaCCParserConstants.CHARACTER_LITERAL)
467        retval += addUnicodeEscapes(t.image);
468     else
469        retval += t.image;
470     cline = t.endLine;
471     ccol = t.endColumn+1;
472     char last = t.image.charAt(t.image.length()-1);
473     if (last == '\n' || last == '\r') {
474       cline++; ccol = 1;
475     }
476     return retval;
477   }
478
479   static protected String JavaDoc printToken(Token t) {
480     String JavaDoc retval = "";
481     Token tt = t.specialToken;
482     if (tt != null) {
483       while (tt.specialToken != null) tt = tt.specialToken;
484       while (tt != null) {
485         retval += printTokenOnly(tt);
486         tt = tt.next;
487       }
488     }
489     retval += printTokenOnly(t);
490     return retval;
491   }
492
493   static protected String JavaDoc printLeadingComments(Token t) {
494     String JavaDoc retval = "";
495     if (t.specialToken == null) return retval;
496     Token tt = t.specialToken;
497     while (tt.specialToken != null) tt = tt.specialToken;
498     while (tt != null) {
499       retval += printTokenOnly(tt);
500       tt = tt.next;
501     }
502     if (ccol != 1 && cline != t.beginLine) {
503       retval += "\n";
504       cline++; ccol = 1;
505     }
506     return retval;
507   }
508
509   static protected String JavaDoc printTrailingComments(Token t) {
510     if (t.next == null) return "";
511     return printLeadingComments(t.next);
512   }
513
514    public static void reInit()
515    {
516       fileName = null;
517       origFileName = null;
518       jjtreeGenerated = false;
519       jjcovGenerated = false;
520       toolNames = null;
521       cu_name = null;
522       cu_to_insertion_point_1 = new java.util.Vector JavaDoc();
523       cu_to_insertion_point_2 = new java.util.Vector JavaDoc();
524       cu_from_insertion_point_2 = new java.util.Vector JavaDoc();
525       bnfproductions = new java.util.Vector JavaDoc();
526       production_table = new java.util.Hashtable JavaDoc();
527       lexstate_S2I = new java.util.Hashtable JavaDoc();
528       lexstate_I2S = new java.util.Hashtable JavaDoc();
529       token_mgr_decls = null;
530       rexprlist = new java.util.Vector JavaDoc();
531       tokenCount = 0;
532       named_tokens_table = new java.util.Hashtable JavaDoc();
533       ordered_named_tokens = new java.util.Vector JavaDoc();
534       names_of_tokens = new java.util.Hashtable JavaDoc();
535       rexps_of_tokens = new java.util.Hashtable JavaDoc();
536       simple_tokens_table = new java.util.Hashtable JavaDoc();
537       maskindex = 0;
538       jj2index = 0;
539       maskVals = new Vector JavaDoc();
540       cline = 0;
541       ccol = 0;
542       actForEof = null;
543       nextStateForEof = null;
544    }
545
546 }
547
Popular Tags