KickJava   Java API By Example, From Geeks To Geeks.

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


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.*;
26
27 public class JavaFiles
28     extends JavaCCGlobals
29     implements JavaCCParserConstants
30 {
31
32   static PrintWriter ostr;
33
34   /**
35    * ID of the latest version (of JavaCC) in which one of the CharStream classes
36    * or the CharStream interface is modified.
37    */

38   static final String JavaDoc charStreamVersion = "4.0";
39
40   /**
41    * ID of the latest version (of JavaCC) in which the TokenManager interface is modified.
42    */

43   static final String JavaDoc tokenManagerVersion = "3.0";
44
45   /**
46    * ID of the latest version (of JavaCC) in which the Token class is modified.
47    */

48   static final String JavaDoc tokenVersion = "3.0";
49
50   /**
51    * ID of the latest version (of JavaCC) in which the ParseException class is
52    * modified.
53    */

54   static final String JavaDoc parseExceptionVersion = "3.0";
55
56   /**
57    * ID of the latest version (of JavaCC) in which the TokenMgrError class is
58    * modified.
59    */

60   static final String JavaDoc tokenMgrErrorVersion = "3.0";
61
62   /**
63    * Replaces all backslahes with double backslashes.
64    */

65   static String JavaDoc replaceBackslash(String JavaDoc str)
66   {
67      StringBuffer JavaDoc b;
68      int i = 0, len = str.length();
69
70      while (i < len && str.charAt(i++) != '\\') ;
71
72      if (i == len) // No backslash found.
73
return str;
74
75      char c;
76      b = new StringBuffer JavaDoc();
77      for (i = 0; i < len; i++)
78        if ((c = str.charAt(i)) == '\\')
79           b.append("\\\\");
80        else
81           b.append(c);
82
83      return b.toString();
84   }
85
86   static void CheckVersion(String JavaDoc fileName, String JavaDoc versionId)
87   {
88      fileName = replaceBackslash(fileName);
89      String JavaDoc firstLine = "/* " + getIdString(toolName, fileName) +
90                                          " Version " + versionId + " */";
91      char[] buf = new char[firstLine.length()];
92
93      try {
94        File fp = new File(Options.getOutputDirectory(), fileName);
95        Reader stream = new FileReader(fp);
96        int read, total = 0;
97
98        for (;;)
99           if ((read = stream.read(buf, 0, buf.length)) > 0)
100           {
101              if ((total += read) == buf.length)
102                 if (new String JavaDoc(buf).equals(firstLine))
103                    return;
104                 else
105                    break;
106           }
107           else
108              break;
109     } catch(FileNotFoundException e1) {
110       // This should never happen
111
JavaCCErrors.semantic_error("Could not open file " + fileName + " for writing.");
112       throw new Error JavaDoc();
113     } catch(IOException e2) {
114     }
115
116     JavaCCErrors.warning(fileName + ": File is obsolete. Please rename or delete this file so" +
117                        " that a new one can be generated for you.");
118   }
119
120   public static void gen_JavaCharStream() {
121     File tmp;
122     if ((tmp = new File(Options.getOutputDirectory(), "JavaCharStream.java")).exists()) {
123       CheckVersion("JavaCharStream.java", charStreamVersion);
124       return;
125     }
126     System.out.println("File \"JavaCharStream.java\" does not exist. Will create one.");
127     try {
128       ostr = new PrintWriter(
129                 new BufferedWriter(
130                    new FileWriter(tmp),
131                    8192
132                 )
133              );
134     } catch (IOException e) {
135       JavaCCErrors.semantic_error("Could not open file JavaCharStream.java for writing.");
136       throw new Error JavaDoc();
137     }
138
139     ostr.println("/* " + getIdString(toolName, "JavaCharStream.java") + " Version " + charStreamVersion + " */");
140
141     if (cu_to_insertion_point_1.size() != 0 &&
142         ((Token)cu_to_insertion_point_1.elementAt(0)).kind == PACKAGE
143        ) {
144       for (int i = 1; i < cu_to_insertion_point_1.size(); i++) {
145         if (((Token)cu_to_insertion_point_1.elementAt(i)).kind == SEMICOLON) {
146           cline = ((Token)(cu_to_insertion_point_1.elementAt(0))).beginLine;
147           ccol = ((Token)(cu_to_insertion_point_1.elementAt(0))).beginColumn;
148           for (int j = 0; j <= i; j++) {
149             printToken((Token)(cu_to_insertion_point_1.elementAt(j)), ostr);
150           }
151           ostr.println("");
152           ostr.println("");
153           break;
154         }
155       }
156     }
157     String JavaDoc prefix = (Options.getStatic() ? " static " : " ");
158     ostr.println("/**");
159     ostr.println(" * An implementation of interface CharStream, where the stream is assumed to");
160     ostr.println(" * contain only ASCII characters (with java-like unicode escape processing).");
161     ostr.println(" */");
162     ostr.println("");
163     ostr.println("public class JavaCharStream");
164     ostr.println("{");
165     ostr.println(" public static final boolean staticFlag = " +
166                                          Options.getStatic() + ";");
167     ostr.println(" static final int hexval(char c) throws java.io.IOException {");
168     ostr.println(" switch(c)");
169     ostr.println(" {");
170     ostr.println(" case '0' :");
171     ostr.println(" return 0;");
172     ostr.println(" case '1' :");
173     ostr.println(" return 1;");
174     ostr.println(" case '2' :");
175     ostr.println(" return 2;");
176     ostr.println(" case '3' :");
177     ostr.println(" return 3;");
178     ostr.println(" case '4' :");
179     ostr.println(" return 4;");
180     ostr.println(" case '5' :");
181     ostr.println(" return 5;");
182     ostr.println(" case '6' :");
183     ostr.println(" return 6;");
184     ostr.println(" case '7' :");
185     ostr.println(" return 7;");
186     ostr.println(" case '8' :");
187     ostr.println(" return 8;");
188     ostr.println(" case '9' :");
189     ostr.println(" return 9;");
190     ostr.println("");
191     ostr.println(" case 'a' :");
192     ostr.println(" case 'A' :");
193     ostr.println(" return 10;");
194     ostr.println(" case 'b' :");
195     ostr.println(" case 'B' :");
196     ostr.println(" return 11;");
197     ostr.println(" case 'c' :");
198     ostr.println(" case 'C' :");
199     ostr.println(" return 12;");
200     ostr.println(" case 'd' :");
201     ostr.println(" case 'D' :");
202     ostr.println(" return 13;");
203     ostr.println(" case 'e' :");
204     ostr.println(" case 'E' :");
205     ostr.println(" return 14;");
206     ostr.println(" case 'f' :");
207     ostr.println(" case 'F' :");
208     ostr.println(" return 15;");
209     ostr.println(" }");
210     ostr.println("");
211     ostr.println(" throw new java.io.IOException(); // Should never come here");
212     ostr.println(" }");
213     ostr.println("");
214     ostr.println(prefix + "public int bufpos = -1;");
215     ostr.println(prefix + "int bufsize;");
216     ostr.println(prefix + "int available;");
217     ostr.println(prefix + "int tokenBegin;");
218
219     if (OtherFilesGen.keepLineCol)
220     {
221        ostr.println(prefix + "protected int bufline[];");
222        ostr.println(prefix + "protected int bufcolumn[];");
223        ostr.println("");
224        ostr.println(prefix + "protected int column = 0;");
225        ostr.println(prefix + "protected int line = 1;");
226        ostr.println("");
227        ostr.println(prefix + "protected boolean prevCharIsCR = false;");
228        ostr.println(prefix + "protected boolean prevCharIsLF = false;");
229     }
230
231     ostr.println("");
232     ostr.println(prefix + "protected java.io.Reader inputStream;");
233     ostr.println("");
234     ostr.println(prefix + "protected char[] nextCharBuf;");
235     ostr.println(prefix + "protected char[] buffer;");
236     ostr.println(prefix + "protected int maxNextCharInd = 0;");
237     ostr.println(prefix + "protected int nextCharInd = -1;");
238     ostr.println(prefix + "protected int inBuf = 0;");
239     ostr.println(prefix + "protected int tabSize = 8;");
240     ostr.println("");
241     ostr.println(prefix + "protected void setTabSize(int i) { tabSize = i; }");
242     ostr.println(prefix + "protected int getTabSize(int i) { return tabSize; }");
243     ostr.println("");
244     ostr.println(prefix + "protected void ExpandBuff(boolean wrapAround)");
245     ostr.println(" {");
246     ostr.println(" char[] newbuffer = new char[bufsize + 2048];");
247
248     if (OtherFilesGen.keepLineCol)
249     {
250        ostr.println(" int newbufline[] = new int[bufsize + 2048];");
251        ostr.println(" int newbufcolumn[] = new int[bufsize + 2048];");
252     }
253
254     ostr.println("");
255     ostr.println(" try");
256     ostr.println(" {");
257     ostr.println(" if (wrapAround)");
258     ostr.println(" {");
259     ostr.println(" System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin);");
260     ostr.println(" System.arraycopy(buffer, 0, newbuffer,");
261     ostr.println(" bufsize - tokenBegin, bufpos);");
262     ostr.println(" buffer = newbuffer;");
263
264     if (OtherFilesGen.keepLineCol)
265     {
266        ostr.println("");
267        ostr.println(" System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin);");
268        ostr.println(" System.arraycopy(bufline, 0, newbufline, bufsize - tokenBegin, bufpos);");
269        ostr.println(" bufline = newbufline;");
270        ostr.println("");
271        ostr.println(" System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin);");
272        ostr.println(" System.arraycopy(bufcolumn, 0, newbufcolumn, bufsize - tokenBegin, bufpos);");
273        ostr.println(" bufcolumn = newbufcolumn;");
274     }
275
276     ostr.println("");
277     ostr.println(" bufpos += (bufsize - tokenBegin);");
278     ostr.println(" }");
279     ostr.println(" else");
280     ostr.println(" {");
281     ostr.println(" System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin);");
282     ostr.println(" buffer = newbuffer;");
283
284     if (OtherFilesGen.keepLineCol)
285     {
286        ostr.println("");
287        ostr.println(" System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin);");
288        ostr.println(" bufline = newbufline;");
289        ostr.println("");
290        ostr.println(" System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin);");
291        ostr.println(" bufcolumn = newbufcolumn;");
292     }
293
294     ostr.println("");
295     ostr.println(" bufpos -= tokenBegin;");
296     ostr.println(" }");
297     ostr.println(" }");
298     ostr.println(" catch (Throwable t)");
299     ostr.println(" {");
300     ostr.println(" throw new Error(t.getMessage());");
301     ostr.println(" }");
302     ostr.println("");
303     ostr.println(" available = (bufsize += 2048);");
304     ostr.println(" tokenBegin = 0;");
305     ostr.println(" }");
306     ostr.println("");
307     ostr.println(prefix + "protected void FillBuff() throws java.io.IOException");
308     ostr.println(" {");
309     ostr.println(" int i;");
310     ostr.println(" if (maxNextCharInd == 4096)");
311     ostr.println(" maxNextCharInd = nextCharInd = 0;");
312     ostr.println("");
313     ostr.println(" try {");
314     ostr.println(" if ((i = inputStream.read(nextCharBuf, maxNextCharInd,");
315     ostr.println(" 4096 - maxNextCharInd)) == -1)");
316     ostr.println(" {");
317     ostr.println(" inputStream.close();");
318     ostr.println(" throw new java.io.IOException();");
319     ostr.println(" }");
320     ostr.println(" else");
321     ostr.println(" maxNextCharInd += i;");
322     ostr.println(" return;");
323     ostr.println(" }");
324     ostr.println(" catch(java.io.IOException e) {");
325     ostr.println(" if (bufpos != 0)");
326     ostr.println(" {");
327     ostr.println(" --bufpos;");
328     ostr.println(" backup(0);");
329     ostr.println(" }");
330
331     if (OtherFilesGen.keepLineCol)
332     {
333        ostr.println(" else");
334        ostr.println(" {");
335        ostr.println(" bufline[bufpos] = line;");
336        ostr.println(" bufcolumn[bufpos] = column;");
337        ostr.println(" }");
338     }
339
340     ostr.println(" throw e;");
341     ostr.println(" }");
342     ostr.println(" }");
343     ostr.println("");
344     ostr.println(prefix + "protected char ReadByte() throws java.io.IOException");
345     ostr.println(" {");
346     ostr.println(" if (++nextCharInd >= maxNextCharInd)");
347     ostr.println(" FillBuff();");
348     ostr.println("");
349     ostr.println(" return nextCharBuf[nextCharInd];");
350     ostr.println(" }");
351     ostr.println("");
352     ostr.println(prefix + "public char BeginToken() throws java.io.IOException");
353     ostr.println(" { ");
354     ostr.println(" if (inBuf > 0)");
355     ostr.println(" {");
356     ostr.println(" --inBuf;");
357     ostr.println("");
358     ostr.println(" if (++bufpos == bufsize)");
359     ostr.println(" bufpos = 0;");
360     ostr.println("");
361     ostr.println(" tokenBegin = bufpos;");
362     ostr.println(" return buffer[bufpos];");
363     ostr.println(" }");
364     ostr.println("");
365     ostr.println(" tokenBegin = 0;");
366     ostr.println(" bufpos = -1;");
367     ostr.println("");
368     ostr.println(" return readChar();");
369     ostr.println(" } ");
370     ostr.println("");
371     ostr.println(prefix + "protected void AdjustBuffSize()");
372     ostr.println(" {");
373     ostr.println(" if (available == bufsize)");
374     ostr.println(" {");
375     ostr.println(" if (tokenBegin > 2048)");
376     ostr.println(" {");
377     ostr.println(" bufpos = 0;");
378     ostr.println(" available = tokenBegin;");
379     ostr.println(" }");
380     //ostr.println(" else if (tokenBegin < 0)");
381
//ostr.println(" bufpos = 0;");
382
ostr.println(" else");
383     ostr.println(" ExpandBuff(false);");
384     ostr.println(" }");
385     ostr.println(" else if (available > tokenBegin)");
386     ostr.println(" available = bufsize;");
387     ostr.println(" else if ((tokenBegin - available) < 2048)");
388     ostr.println(" ExpandBuff(true);");
389     ostr.println(" else");
390     ostr.println(" available = tokenBegin;");
391     ostr.println(" }");
392
393     if (OtherFilesGen.keepLineCol)
394     {
395        ostr.println("");
396        ostr.println(prefix + "protected void UpdateLineColumn(char c)");
397        ostr.println(" {");
398        ostr.println(" column++;");
399        ostr.println("");
400        ostr.println(" if (prevCharIsLF)");
401        ostr.println(" {");
402        ostr.println(" prevCharIsLF = false;");
403        ostr.println(" line += (column = 1);");
404        ostr.println(" }");
405        ostr.println(" else if (prevCharIsCR)");
406        ostr.println(" {");
407        ostr.println(" prevCharIsCR = false;");
408        ostr.println(" if (c == '\\n')");
409        ostr.println(" {");
410        ostr.println(" prevCharIsLF = true;");
411        ostr.println(" }");
412        ostr.println(" else");
413        ostr.println(" line += (column = 1);");
414        ostr.println(" }");
415        ostr.println("");
416        ostr.println(" switch (c)");
417        ostr.println(" {");
418        ostr.println(" case '\\r' :");
419        ostr.println(" prevCharIsCR = true;");
420        ostr.println(" break;");
421        ostr.println(" case '\\n' :");
422        ostr.println(" prevCharIsLF = true;");
423        ostr.println(" break;");
424        ostr.println(" case '\\t' :");
425        ostr.println(" column--;");
426        ostr.println(" column += (tabSize - (column % tabSize));");
427        ostr.println(" break;");
428        ostr.println(" default :");
429        ostr.println(" break;");
430        ostr.println(" }");
431        ostr.println("");
432        ostr.println(" bufline[bufpos] = line;");
433        ostr.println(" bufcolumn[bufpos] = column;");
434        ostr.println(" }");
435     }
436
437     ostr.println("");
438     ostr.println(prefix + "public char readChar() throws java.io.IOException");
439     ostr.println(" {");
440     ostr.println(" if (inBuf > 0)");
441     ostr.println(" {");
442     ostr.println(" --inBuf;");
443     ostr.println("");
444     ostr.println(" if (++bufpos == bufsize)");
445     ostr.println(" bufpos = 0;");
446     ostr.println("");
447     ostr.println(" return buffer[bufpos];");
448     ostr.println(" }");
449     ostr.println("");
450     ostr.println(" char c;");
451     ostr.println("");
452     ostr.println(" if (++bufpos == available)");
453     ostr.println(" AdjustBuffSize();");
454     ostr.println("");
455     ostr.println(" if ((buffer[bufpos] = c = ReadByte()) == '\\\\')");
456     ostr.println(" {");
457
458     if (OtherFilesGen.keepLineCol)
459     {
460     ostr.println(" UpdateLineColumn(c);");
461     }
462
463     ostr.println("");
464     ostr.println(" int backSlashCnt = 1;");
465     ostr.println("");
466     ostr.println(" for (;;) // Read all the backslashes");
467     ostr.println(" {");
468     ostr.println(" if (++bufpos == available)");
469     ostr.println(" AdjustBuffSize();");
470     ostr.println("");
471     ostr.println(" try");
472     ostr.println(" {");
473     ostr.println(" if ((buffer[bufpos] = c = ReadByte()) != '\\\\')");
474     ostr.println(" {");
475
476     if (OtherFilesGen.keepLineCol)
477     {
478     ostr.println(" UpdateLineColumn(c);");
479     }
480
481     ostr.println(" // found a non-backslash char.");
482     ostr.println(" if ((c == 'u') && ((backSlashCnt & 1) == 1))");
483     ostr.println(" {");
484     ostr.println(" if (--bufpos < 0)");
485     ostr.println(" bufpos = bufsize - 1;");
486     ostr.println("");
487     ostr.println(" break;");
488     ostr.println(" }");
489     ostr.println("");
490     ostr.println(" backup(backSlashCnt);");
491     ostr.println(" return '\\\\';");
492     ostr.println(" }");
493     ostr.println(" }");
494     ostr.println(" catch(java.io.IOException e)");
495     ostr.println(" {");
496     ostr.println(" if (backSlashCnt > 1)");
497     ostr.println(" backup(backSlashCnt);");
498     ostr.println("");
499     ostr.println(" return '\\\\';");
500     ostr.println(" }");
501     ostr.println("");
502
503     if (OtherFilesGen.keepLineCol)
504     {
505     ostr.println(" UpdateLineColumn(c);");
506     }
507
508     ostr.println(" backSlashCnt++;");
509     ostr.println(" }");
510     ostr.println("");
511     ostr.println(" // Here, we have seen an odd number of backslash's followed by a 'u'");
512     ostr.println(" try");
513     ostr.println(" {");
514     ostr.println(" while ((c = ReadByte()) == 'u')");
515
516     if (OtherFilesGen.keepLineCol)
517     {
518     ostr.println(" ++column;");
519     }
520     else
521     {
522        ostr.println(" ;");
523     }
524
525     ostr.println("");
526     ostr.println(" buffer[bufpos] = c = (char)(hexval(c) << 12 |");
527     ostr.println(" hexval(ReadByte()) << 8 |");
528     ostr.println(" hexval(ReadByte()) << 4 |");
529     ostr.println(" hexval(ReadByte()));");
530     ostr.println("");
531
532     if (OtherFilesGen.keepLineCol)
533     {
534     ostr.println(" column += 4;");
535     }
536
537     ostr.println(" }");
538     ostr.println(" catch(java.io.IOException e)");
539     ostr.println(" {");
540
541     if (OtherFilesGen.keepLineCol)
542     {
543        ostr.println(" throw new Error(\"Invalid escape character at line \" + line +");
544        ostr.println(" \" column \" + column + \".\");");
545     }
546     else
547     {
548        ostr.println(" throw new Error(\"Invalid escape character in input\");");
549     }
550
551     ostr.println(" }");
552     ostr.println("");
553     ostr.println(" if (backSlashCnt == 1)");
554     ostr.println(" return c;");
555     ostr.println(" else");
556     ostr.println(" {");
557     ostr.println(" backup(backSlashCnt - 1);");
558     ostr.println(" return '\\\\';");
559     ostr.println(" }");
560     ostr.println(" }");
561     ostr.println(" else");
562     ostr.println(" {");
563
564     if (OtherFilesGen.keepLineCol)
565     {
566        ostr.println(" UpdateLineColumn(c);");
567     }
568
569     ostr.println(" return (c);");
570     ostr.println(" }");
571     ostr.println(" }");
572     ostr.println("");
573     ostr.println(" /**");
574     ostr.println(" * @deprecated ");
575     ostr.println(" * @see #getEndColumn");
576     ostr.println(" */");
577     ostr.println("");
578     ostr.println(prefix + "public int getColumn() {");
579
580     if (OtherFilesGen.keepLineCol)
581     {
582     ostr.println(" return bufcolumn[bufpos];");
583     }
584     else
585     {
586        ostr.println(" return -1;");
587     }
588
589     ostr.println(" }");
590     ostr.println("");
591     ostr.println(" /**");
592     ostr.println(" * @deprecated ");
593     ostr.println(" * @see #getEndLine");
594     ostr.println(" */");
595     ostr.println("");
596     ostr.println(prefix + "public int getLine() {");
597
598     if (OtherFilesGen.keepLineCol)
599     {
600        ostr.println(" return bufline[bufpos];");
601     }
602     else
603     {
604        ostr.println(" return -1;");
605     }
606
607     ostr.println(" }");
608     ostr.println("");
609     ostr.println(prefix + "public int getEndColumn() {");
610
611     if (OtherFilesGen.keepLineCol)
612     {
613     ostr.println(" return bufcolumn[bufpos];");
614     }
615     else
616     {
617        ostr.println(" return -1;");
618     }
619
620     ostr.println(" }");
621     ostr.println("");
622     ostr.println(prefix + "public int getEndLine() {");
623
624     if (OtherFilesGen.keepLineCol)
625     {
626     ostr.println(" return bufline[bufpos];");
627     }
628     else
629     {
630        ostr.println(" return -1;");
631     }
632
633     ostr.println(" }");
634     ostr.println("");
635     ostr.println(prefix + "public int getBeginColumn() {");
636
637     if (OtherFilesGen.keepLineCol)
638     {
639     ostr.println(" return bufcolumn[tokenBegin];");
640     }
641     else
642     {
643        ostr.println(" return -1;");
644     }
645
646     ostr.println(" }");
647     ostr.println("");
648     ostr.println(prefix + "public int getBeginLine() {");
649
650     if (OtherFilesGen.keepLineCol)
651     {
652     ostr.println(" return bufline[tokenBegin];");
653     }
654     else
655     {
656        ostr.println(" return -1;");
657     }
658
659     ostr.println(" }");
660     ostr.println("");
661     ostr.println(prefix + "public void backup(int amount) {");
662     ostr.println("");
663     ostr.println(" inBuf += amount;");
664     ostr.println(" if ((bufpos -= amount) < 0)");
665     ostr.println(" bufpos += bufsize;");
666     ostr.println(" }");
667     ostr.println("");
668     ostr.println(" public JavaCharStream(java.io.Reader dstream,");
669     ostr.println(" int startline, int startcolumn, int buffersize)");
670     ostr.println(" {");
671
672     if (Options.getStatic())
673     {
674        ostr.println(" if (inputStream != null)");
675        ostr.println(" throw new Error(\"\\n ERROR: Second call to the constructor of a static JavaCharStream. You must\\n\" +");
676        ostr.println(" \" either use ReInit() or set the JavaCC option STATIC to false\\n\" +");
677        ostr.println(" \" during the generation of this class.\");");
678     }
679
680     ostr.println(" inputStream = dstream;");
681
682     if (OtherFilesGen.keepLineCol)
683     {
684     ostr.println(" line = startline;");
685     ostr.println(" column = startcolumn - 1;");
686     }
687
688     ostr.println("");
689     ostr.println(" available = bufsize = buffersize;");
690     ostr.println(" buffer = new char[buffersize];");
691
692     if (OtherFilesGen.keepLineCol)
693     {
694     ostr.println(" bufline = new int[buffersize];");
695     ostr.println(" bufcolumn = new int[buffersize];");
696     }
697
698     ostr.println(" nextCharBuf = new char[4096];");
699     ostr.println(" }");
700     ostr.println("");
701     ostr.println(" public JavaCharStream(java.io.Reader dstream,");
702     ostr.println(" int startline, int startcolumn)");
703     ostr.println(" {");
704     ostr.println(" this(dstream, startline, startcolumn, 4096);");
705     ostr.println(" }");
706     ostr.println("");
707     ostr.println(" public JavaCharStream(java.io.Reader dstream)");
708     ostr.println(" {");
709     ostr.println(" this(dstream, 1, 1, 4096);");
710     ostr.println(" }");
711     ostr.println(" public void ReInit(java.io.Reader dstream,");
712     ostr.println(" int startline, int startcolumn, int buffersize)");
713     ostr.println(" {");
714     ostr.println(" inputStream = dstream;");
715
716     if (OtherFilesGen.keepLineCol)
717     {
718     ostr.println(" line = startline;");
719     ostr.println(" column = startcolumn - 1;");
720     }
721
722     ostr.println("");
723     ostr.println(" if (buffer == null || buffersize != buffer.length)");
724     ostr.println(" {");
725     ostr.println(" available = bufsize = buffersize;");
726     ostr.println(" buffer = new char[buffersize];");
727
728     if (OtherFilesGen.keepLineCol)
729     {
730     ostr.println(" bufline = new int[buffersize];");
731     ostr.println(" bufcolumn = new int[buffersize];");
732     }
733
734     ostr.println(" nextCharBuf = new char[4096];");
735     ostr.println(" }");
736
737     if (OtherFilesGen.keepLineCol)
738     {
739     ostr.println(" prevCharIsLF = prevCharIsCR = false;");
740     }
741
742     ostr.println(" tokenBegin = inBuf = maxNextCharInd = 0;");
743     ostr.println(" nextCharInd = bufpos = -1;");
744     ostr.println(" }");
745     ostr.println("");
746     ostr.println(" public void ReInit(java.io.Reader dstream,");
747     ostr.println(" int startline, int startcolumn)");
748     ostr.println(" {");
749     ostr.println(" ReInit(dstream, startline, startcolumn, 4096);");
750     ostr.println(" }");
751     ostr.println("");
752     ostr.println(" public void ReInit(java.io.Reader dstream)");
753     ostr.println(" {");
754     ostr.println(" ReInit(dstream, 1, 1, 4096);");
755     ostr.println(" }");
756     ostr.println(" public JavaCharStream(java.io.InputStream dstream, String encoding, int startline,");
757     ostr.println(" int startcolumn, int buffersize) throws java.io.UnsupportedEncodingException");
758     ostr.println(" {");
759     ostr.println(" this(encoding == null ? new java.io.InputStreamReader(dstream) : new java.io.InputStreamReader(dstream, encoding), startline, startcolumn, buffersize);");
760     ostr.println(" }");
761     ostr.println("");
762     ostr.println(" public JavaCharStream(java.io.InputStream dstream, int startline,");
763     ostr.println(" int startcolumn, int buffersize)");
764     ostr.println(" {");
765     ostr.println(" this(new java.io.InputStreamReader(dstream), startline, startcolumn, 4096);");
766     ostr.println(" }");
767     ostr.println("");
768     ostr.println(" public JavaCharStream(java.io.InputStream dstream, String encoding, int startline,");
769     ostr.println(" int startcolumn) throws java.io.UnsupportedEncodingException");
770     ostr.println(" {");
771     ostr.println(" this(dstream, encoding, startline, startcolumn, 4096);");
772     ostr.println(" }");
773     ostr.println("");
774     ostr.println(" public JavaCharStream(java.io.InputStream dstream, int startline,");
775     ostr.println(" int startcolumn)");
776     ostr.println(" {");
777     ostr.println(" this(dstream, startline, startcolumn, 4096);");
778     ostr.println(" }");
779     ostr.println("");
780     ostr.println(" public JavaCharStream(java.io.InputStream dstream, String encoding) throws java.io.UnsupportedEncodingException");
781     ostr.println(" {");
782     ostr.println(" this(dstream, encoding, 1, 1, 4096);");
783     ostr.println(" }");
784     ostr.println("");
785     ostr.println(" public JavaCharStream(java.io.InputStream dstream)");
786     ostr.println(" {");
787     ostr.println(" this(dstream, 1, 1, 4096);");
788     ostr.println(" }");
789     ostr.println("");
790     ostr.println(" public void ReInit(java.io.InputStream dstream, String encoding, int startline,");
791     ostr.println(" int startcolumn, int buffersize) throws java.io.UnsupportedEncodingException");
792     ostr.println(" {");
793     ostr.println(" ReInit(encoding == null ? new java.io.InputStreamReader(dstream) : new java.io.InputStreamReader(dstream, encoding), startline, startcolumn, buffersize);");
794     ostr.println(" }");
795     ostr.println("");
796     ostr.println(" public void ReInit(java.io.InputStream dstream, int startline,");
797     ostr.println(" int startcolumn, int buffersize)");
798     ostr.println(" {");
799     ostr.println(" ReInit(new java.io.InputStreamReader(dstream), startline, startcolumn, buffersize);");
800     ostr.println(" }");
801     ostr.println(" public void ReInit(java.io.InputStream dstream, String encoding, int startline,");
802     ostr.println(" int startcolumn) throws java.io.UnsupportedEncodingException");
803     ostr.println(" {");
804     ostr.println(" ReInit(dstream, encoding, startline, startcolumn, 4096);");
805     ostr.println(" }");
806     ostr.println(" public void ReInit(java.io.InputStream dstream, int startline,");
807     ostr.println(" int startcolumn)");
808     ostr.println(" {");
809     ostr.println(" ReInit(dstream, startline, startcolumn, 4096);");
810     ostr.println(" }");
811     ostr.println(" public void ReInit(java.io.InputStream dstream, String encoding) throws java.io.UnsupportedEncodingException");
812     ostr.println(" {");
813     ostr.println(" ReInit(dstream, encoding, 1, 1, 4096);");
814     ostr.println(" }");
815     ostr.println("");
816     ostr.println(" public void ReInit(java.io.InputStream dstream)");
817     ostr.println(" {");
818     ostr.println(" ReInit(dstream, 1, 1, 4096);");
819     ostr.println(" }");
820     ostr.println("");
821     ostr.println(prefix + "public String GetImage()");
822     ostr.println(" {");
823     ostr.println(" if (bufpos >= tokenBegin)");
824     ostr.println(" return new String(buffer, tokenBegin, bufpos - tokenBegin + 1);");
825     ostr.println(" else");
826     ostr.println(" return new String(buffer, tokenBegin, bufsize - tokenBegin) +");
827     ostr.println(" new String(buffer, 0, bufpos + 1);");
828     ostr.println(" }");
829     ostr.println("");
830     ostr.println(prefix + "public char[] GetSuffix(int len)");
831     ostr.println(" {");
832     ostr.println(" char[] ret = new char[len];");
833     ostr.println("");
834     ostr.println(" if ((bufpos + 1) >= len)");
835     ostr.println(" System.arraycopy(buffer, bufpos - len + 1, ret, 0, len);");
836     ostr.println(" else");
837     ostr.println(" {");
838     ostr.println(" System.arraycopy(buffer, bufsize - (len - bufpos - 1), ret, 0,");
839     ostr.println(" len - bufpos - 1);");
840     ostr.println(" System.arraycopy(buffer, 0, ret, len - bufpos - 1, bufpos + 1);");
841     ostr.println(" }");
842     ostr.println("");
843     ostr.println(" return ret;");
844     ostr.println(" }");
845     ostr.println("");
846     ostr.println(prefix + "public void Done()");
847     ostr.println(" {");
848     ostr.println(" nextCharBuf = null;");
849     ostr.println(" buffer = null;");
850
851     if (OtherFilesGen.keepLineCol)
852     {
853     ostr.println(" bufline = null;");
854     ostr.println(" bufcolumn = null;");
855     }
856
857     ostr.println(" }");
858
859     if (OtherFilesGen.keepLineCol)
860     {
861     ostr.println("");
862     ostr.println(" /**");
863     ostr.println(" * Method to adjust line and column numbers for the start of a token.");
864     ostr.println(" */");
865     ostr.println(prefix + "public void adjustBeginLineColumn(int newLine, int newCol)");
866     ostr.println(" {");
867     ostr.println(" int start = tokenBegin;");
868     ostr.println(" int len;");
869     ostr.println("");
870     ostr.println(" if (bufpos >= tokenBegin)");
871     ostr.println(" {");
872     ostr.println(" len = bufpos - tokenBegin + inBuf + 1;");
873     ostr.println(" }");
874     ostr.println(" else");
875     ostr.println(" {");
876     ostr.println(" len = bufsize - tokenBegin + bufpos + 1 + inBuf;");
877     ostr.println(" }");
878     ostr.println("");
879     ostr.println(" int i = 0, j = 0, k = 0;");
880     ostr.println(" int nextColDiff = 0, columnDiff = 0;");
881     ostr.println("");
882     ostr.println(" while (i < len &&");
883     ostr.println(" bufline[j = start % bufsize] == bufline[k = ++start % bufsize])");
884     ostr.println(" {");
885     ostr.println(" bufline[j] = newLine;");
886     ostr.println(" nextColDiff = columnDiff + bufcolumn[k] - bufcolumn[j];");
887     ostr.println(" bufcolumn[j] = newCol + columnDiff;");
888     ostr.println(" columnDiff = nextColDiff;");
889     ostr.println(" i++;");
890     ostr.println(" } ");
891     ostr.println("");
892     ostr.println(" if (i < len)");
893     ostr.println(" {");
894     ostr.println(" bufline[j] = newLine++;");
895     ostr.println(" bufcolumn[j] = newCol + columnDiff;");
896     ostr.println("");
897     ostr.println(" while (i++ < len)");
898     ostr.println(" {");
899     ostr.println(" if (bufline[j = start % bufsize] != bufline[++start % bufsize])");
900     ostr.println(" bufline[j] = newLine++;");
901     ostr.println(" else");
902     ostr.println(" bufline[j] = newLine;");
903     ostr.println(" }");
904     ostr.println(" }");
905     ostr.println("");
906     ostr.println(" line = bufline[j];");
907     ostr.println(" column = bufcolumn[j];");
908     ostr.println(" }");
909     ostr.println("");
910     }
911
912     ostr.println("}");
913     ostr.close();
914   }
915
916   public static void gen_SimpleCharStream() {
917     File tmp;
918     if ((tmp = new File(Options.getOutputDirectory(), "SimpleCharStream.java")).exists()) {
919       CheckVersion("SimpleCharStream.java", charStreamVersion);
920       return;
921     }
922
923     System.out.println("File \"SimpleCharStream.java\" does not exist. Will create one.");
924     try {
925       ostr = new PrintWriter(
926                 new BufferedWriter(
927                    new FileWriter(tmp),
928                    8192
929                 )
930              );
931     } catch (IOException e) {
932       JavaCCErrors.semantic_error("Could not open file SimpleCharStream.java for writing.");
933       throw new Error JavaDoc();
934     }
935
936     ostr.println("/* " + getIdString(toolName, "SimpleCharStream.java") + " Version " + charStreamVersion + " */");
937
938     if (cu_to_insertion_point_1.size() != 0 &&
939         ((Token)cu_to_insertion_point_1.elementAt(0)).kind == PACKAGE
940        ) {
941       for (int i = 1; i < cu_to_insertion_point_1.size(); i++) {
942         if (((Token)cu_to_insertion_point_1.elementAt(i)).kind == SEMICOLON) {
943           cline = ((Token)(cu_to_insertion_point_1.elementAt(0))).beginLine;
944           ccol = ((Token)(cu_to_insertion_point_1.elementAt(0))).beginColumn;
945           for (int j = 0; j <= i; j++) {
946             printToken((Token)(cu_to_insertion_point_1.elementAt(j)), ostr);
947           }
948           ostr.println("");
949           ostr.println("");
950           break;
951         }
952       }
953     }
954     String JavaDoc prefix = (Options.getStatic() ? " static " : " ");
955     ostr.println("/**");
956     ostr.println(" * An implementation of interface CharStream, where the stream is assumed to");
957     ostr.println(" * contain only ASCII characters (without unicode processing).");
958     ostr.println(" */");
959     ostr.println("");
960     ostr.println("public class SimpleCharStream");
961     ostr.println("{");
962     ostr.println(" public static final boolean staticFlag = " +
963                                          Options.getStatic() + ";");
964     ostr.println(prefix + "int bufsize;");
965     ostr.println(prefix + "int available;");
966     ostr.println(prefix + "int tokenBegin;");
967     ostr.println(prefix + "public int bufpos = -1;");
968
969     if (OtherFilesGen.keepLineCol)
970     {
971        ostr.println(prefix + "protected int bufline[];");
972        ostr.println(prefix + "protected int bufcolumn[];");
973        ostr.println("");
974        ostr.println(prefix + "protected int column = 0;");
975        ostr.println(prefix + "protected int line = 1;");
976        ostr.println("");
977        ostr.println(prefix + "protected boolean prevCharIsCR = false;");
978        ostr.println(prefix + "protected boolean prevCharIsLF = false;");
979     }
980
981     ostr.println("");
982     ostr.println(prefix + "protected java.io.Reader inputStream;");
983     ostr.println("");
984     ostr.println(prefix + "protected char[] buffer;");
985     ostr.println(prefix + "protected int maxNextCharInd = 0;");
986     ostr.println(prefix + "protected int inBuf = 0;");
987     ostr.println(prefix + "protected int tabSize = 8;");
988     ostr.println("");
989     ostr.println(prefix + "protected void setTabSize(int i) { tabSize = i; }");
990     ostr.println(prefix + "protected int getTabSize(int i) { return tabSize; }");
991     ostr.println("");
992     ostr.println("");
993     ostr.println(prefix + "protected void ExpandBuff(boolean wrapAround)");
994     ostr.println(" {");
995     ostr.println(" char[] newbuffer = new char[bufsize + 2048];");
996
997     if (OtherFilesGen.keepLineCol)
998     {
999        ostr.println(" int newbufline[] = new int[bufsize + 2048];");
1000       ostr.println(" int newbufcolumn[] = new int[bufsize + 2048];");
1001    }
1002
1003    ostr.println("");
1004    ostr.println(" try");
1005    ostr.println(" {");
1006    ostr.println(" if (wrapAround)");
1007    ostr.println(" {");
1008    ostr.println(" System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin);");
1009    ostr.println(" System.arraycopy(buffer, 0, newbuffer,");
1010    ostr.println(" bufsize - tokenBegin, bufpos);");
1011    ostr.println(" buffer = newbuffer;");
1012
1013    if (OtherFilesGen.keepLineCol)
1014    {
1015    ostr.println("");
1016    ostr.println(" System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin);");
1017    ostr.println(" System.arraycopy(bufline, 0, newbufline, bufsize - tokenBegin, bufpos);");
1018    ostr.println(" bufline = newbufline;");
1019    ostr.println("");
1020    ostr.println(" System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin);");
1021    ostr.println(" System.arraycopy(bufcolumn, 0, newbufcolumn, bufsize - tokenBegin, bufpos);");
1022    ostr.println(" bufcolumn = newbufcolumn;");
1023    }
1024
1025    ostr.println("");
1026    ostr.println(" maxNextCharInd = (bufpos += (bufsize - tokenBegin));");
1027    ostr.println(" }");
1028    ostr.println(" else");
1029    ostr.println(" {");
1030    ostr.println(" System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin);");
1031    ostr.println(" buffer = newbuffer;");
1032
1033    if (OtherFilesGen.keepLineCol)
1034    {
1035    ostr.println("");
1036    ostr.println(" System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin);");
1037    ostr.println(" bufline = newbufline;");
1038    ostr.println("");
1039    ostr.println(" System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin);");
1040    ostr.println(" bufcolumn = newbufcolumn;");
1041    }
1042
1043    ostr.println("");
1044    ostr.println(" maxNextCharInd = (bufpos -= tokenBegin);");
1045    ostr.println(" }");
1046    ostr.println(" }");
1047    ostr.println(" catch (Throwable t)");
1048    ostr.println(" {");
1049    ostr.println(" throw new Error(t.getMessage());");
1050    ostr.println(" }");
1051    ostr.println("");
1052    ostr.println("");
1053    ostr.println(" bufsize += 2048;");
1054    ostr.println(" available = bufsize;");
1055    ostr.println(" tokenBegin = 0;");
1056    ostr.println(" }");
1057    ostr.println("");
1058    ostr.println(prefix + "protected void FillBuff() throws java.io.IOException");
1059    ostr.println(" {");
1060    ostr.println(" if (maxNextCharInd == available)");
1061    ostr.println(" {");
1062    ostr.println(" if (available == bufsize)");
1063    ostr.println(" {");
1064    ostr.println(" if (tokenBegin > 2048)");
1065    ostr.println(" {");
1066    ostr.println(" bufpos = maxNextCharInd = 0;");
1067    ostr.println(" available = tokenBegin;");
1068    ostr.println(" }");
1069    ostr.println(" else if (tokenBegin < 0)");
1070    ostr.println(" bufpos = maxNextCharInd = 0;");
1071    ostr.println(" else");
1072    ostr.println(" ExpandBuff(false);");
1073    ostr.println(" }");
1074    ostr.println(" else if (available > tokenBegin)");
1075    ostr.println(" available = bufsize;");
1076    ostr.println(" else if ((tokenBegin - available) < 2048)");
1077    ostr.println(" ExpandBuff(true);");
1078    ostr.println(" else");
1079    ostr.println(" available = tokenBegin;");
1080    ostr.println(" }");
1081    ostr.println("");
1082    ostr.println(" int i;");
1083    ostr.println(" try {");
1084    ostr.println(" if ((i = inputStream.read(buffer, maxNextCharInd,");
1085    ostr.println(" available - maxNextCharInd)) == -1)");
1086    ostr.println(" {");
1087    ostr.println(" inputStream.close();");
1088    ostr.println(" throw new java.io.IOException();");
1089    ostr.println(" }");
1090    ostr.println(" else");
1091    ostr.println(" maxNextCharInd += i;");
1092    ostr.println(" return;");
1093    ostr.println(" }");
1094    ostr.println(" catch(java.io.IOException e) {");
1095    ostr.println(" --bufpos;");
1096    ostr.println(" backup(0);");
1097    ostr.println(" if (tokenBegin == -1)");
1098    ostr.println(" tokenBegin = bufpos;");
1099    ostr.println(" throw e;");
1100    ostr.println(" }");
1101    ostr.println(" }");
1102    ostr.println("");
1103    ostr.println(prefix + "public char BeginToken() throws java.io.IOException");
1104    ostr.println(" {");
1105    ostr.println(" tokenBegin = -1;");
1106    ostr.println(" char c = readChar();");
1107    ostr.println(" tokenBegin = bufpos;");
1108    ostr.println("");
1109    ostr.println(" return c;");
1110    ostr.println(" }");
1111
1112    if (OtherFilesGen.keepLineCol)
1113    {
1114    ostr.println("");
1115    ostr.println(prefix + "protected void UpdateLineColumn(char c)");
1116    ostr.println(" {");
1117    ostr.println(" column++;");
1118    ostr.println("");
1119    ostr.println(" if (prevCharIsLF)");
1120    ostr.println(" {");
1121    ostr.println(" prevCharIsLF = false;");
1122    ostr.println(" line += (column = 1);");
1123    ostr.println(" }");
1124    ostr.println(" else if (prevCharIsCR)");
1125    ostr.println(" {");
1126    ostr.println(" prevCharIsCR = false;");
1127    ostr.println(" if (c == '\\n')");
1128    ostr.println(" {");
1129    ostr.println(" prevCharIsLF = true;");
1130    ostr.println(" }");
1131    ostr.println(" else");
1132    ostr.println(" line += (column = 1);");
1133    ostr.println(" }");
1134    ostr.println("");
1135    ostr.println(" switch (c)");
1136    ostr.println(" {");
1137    ostr.println(" case '\\r' :");
1138    ostr.println(" prevCharIsCR = true;");
1139    ostr.println(" break;");
1140    ostr.println(" case '\\n' :");
1141    ostr.println(" prevCharIsLF = true;");
1142    ostr.println(" break;");
1143    ostr.println(" case '\\t' :");
1144    ostr.println(" column--;");
1145    ostr.println(" column += (tabSize - (column % tabSize));");
1146    ostr.println(" break;");
1147    ostr.println(" default :");
1148    ostr.println(" break;");
1149    ostr.println(" }");
1150    ostr.println("");
1151    ostr.println(" bufline[bufpos] = line;");
1152    ostr.println(" bufcolumn[bufpos] = column;");
1153    ostr.println(" }");
1154    }
1155
1156    ostr.println("");
1157    ostr.println(prefix + "public char readChar() throws java.io.IOException");
1158    ostr.println(" {");
1159    ostr.println(" if (inBuf > 0)");
1160    ostr.println(" {");
1161    ostr.println(" --inBuf;");
1162    ostr.println("");
1163    ostr.println(" if (++bufpos == bufsize)");
1164    ostr.println(" bufpos = 0;");
1165    ostr.println("");
1166    ostr.println(" return buffer[bufpos];");
1167    ostr.println(" }");
1168    ostr.println("");
1169    ostr.println(" if (++bufpos >= maxNextCharInd)");
1170    ostr.println(" FillBuff();");
1171    ostr.println("");
1172    ostr.println(" char c = buffer[bufpos];");
1173    ostr.println("");
1174
1175    if (OtherFilesGen.keepLineCol)
1176    {
1177    ostr.println(" UpdateLineColumn(c);");
1178    }
1179
1180    ostr.println(" return (c);");
1181    ostr.println(" }");
1182    ostr.println("");
1183    ostr.println(" /**");
1184    ostr.println(" * @deprecated ");
1185    ostr.println(" * @see #getEndColumn");
1186    ostr.println(" */");
1187    ostr.println("");
1188    ostr.println(prefix + "public int getColumn() {");
1189
1190    if (OtherFilesGen.keepLineCol)
1191    {
1192    ostr.println(" return bufcolumn[bufpos];");
1193    }
1194    else
1195    {
1196       ostr.println(" return -1;");
1197    }
1198
1199    ostr.println(" }");
1200    ostr.println("");
1201    ostr.println(" /**");
1202    ostr.println(" * @deprecated ");
1203    ostr.println(" * @see #getEndLine");
1204    ostr.println(" */");
1205    ostr.println("");
1206    ostr.println(prefix + "public int getLine() {");
1207
1208    if (OtherFilesGen.keepLineCol)
1209    {
1210    ostr.println(" return bufline[bufpos];");
1211    }
1212    else
1213    {
1214       ostr.println(" return -1;");
1215    }
1216
1217    ostr.println(" }");
1218    ostr.println("");
1219    ostr.println(prefix + "public int getEndColumn() {");
1220
1221    if (OtherFilesGen.keepLineCol)
1222    {
1223    ostr.println(" return bufcolumn[bufpos];");
1224    }
1225    else
1226    {
1227       ostr.println(" return -1;");
1228    }
1229
1230    ostr.println(" }");
1231    ostr.println("");
1232    ostr.println(prefix + "public int getEndLine() {");
1233
1234    if (OtherFilesGen.keepLineCol)
1235    {
1236    ostr.println(" return bufline[bufpos];");
1237    }
1238    else
1239    {
1240       ostr.println(" return -1;");
1241    }
1242
1243    ostr.println(" }");
1244    ostr.println("");
1245    ostr.println(prefix + "public int getBeginColumn() {");
1246
1247    if (OtherFilesGen.keepLineCol)
1248    {
1249    ostr.println(" return bufcolumn[tokenBegin];");
1250    }
1251    else
1252    {
1253       ostr.println(" return -1;");
1254    }
1255
1256    ostr.println(" }");
1257    ostr.println("");
1258    ostr.println(prefix + "public int getBeginLine() {");
1259
1260    if (OtherFilesGen.keepLineCol)
1261    {
1262    ostr.println(" return bufline[tokenBegin];");
1263    }
1264    else
1265    {
1266       ostr.println(" return -1;");
1267    }
1268
1269    ostr.println(" }");
1270    ostr.println("");
1271    ostr.println(prefix + "public void backup(int amount) {");
1272    ostr.println("");
1273    ostr.println(" inBuf += amount;");
1274    ostr.println(" if ((bufpos -= amount) < 0)");
1275    ostr.println(" bufpos += bufsize;");
1276    ostr.println(" }");
1277    ostr.println("");
1278    ostr.println(" public SimpleCharStream(java.io.Reader dstream, int startline,");
1279    ostr.println(" int startcolumn, int buffersize)");
1280    ostr.println(" {");
1281
1282    if (Options.getStatic())
1283    {
1284       ostr.println(" if (inputStream != null)");
1285       ostr.println(" throw new Error(\"\\n ERROR: Second call to the constructor of a static SimpleCharStream. You must\\n\" +");
1286       ostr.println(" \" either use ReInit() or set the JavaCC option STATIC to false\\n\" +");
1287       ostr.println(" \" during the generation of this class.\");");
1288    }
1289
1290    ostr.println(" inputStream = dstream;");
1291
1292    if (OtherFilesGen.keepLineCol)
1293    {
1294    ostr.println(" line = startline;");
1295    ostr.println(" column = startcolumn - 1;");
1296    }
1297
1298    ostr.println("");
1299    ostr.println(" available = bufsize = buffersize;");
1300    ostr.println(" buffer = new char[buffersize];");
1301
1302    if (OtherFilesGen.keepLineCol)
1303    {
1304    ostr.println(" bufline = new int[buffersize];");
1305    ostr.println(" bufcolumn = new int[buffersize];");
1306    }
1307
1308    ostr.println(" }");
1309    ostr.println("");
1310    ostr.println(" public SimpleCharStream(java.io.Reader dstream, int startline,");
1311    ostr.println(" int startcolumn)");
1312    ostr.println(" {");
1313    ostr.println(" this(dstream, startline, startcolumn, 4096);");
1314    ostr.println(" }");
1315    ostr.println("");
1316    ostr.println(" public SimpleCharStream(java.io.Reader dstream)");
1317    ostr.println(" {");
1318    ostr.println(" this(dstream, 1, 1, 4096);");
1319    ostr.println(" }");
1320    ostr.println(" public void ReInit(java.io.Reader dstream, int startline,");
1321    ostr.println(" int startcolumn, int buffersize)");
1322    ostr.println(" {");
1323    ostr.println(" inputStream = dstream;");
1324
1325    if (OtherFilesGen.keepLineCol)
1326    {
1327    ostr.println(" line = startline;");
1328    ostr.println(" column = startcolumn - 1;");
1329    }
1330
1331    ostr.println("");
1332    ostr.println(" if (buffer == null || buffersize != buffer.length)");
1333    ostr.println(" {");
1334    ostr.println(" available = bufsize = buffersize;");
1335    ostr.println(" buffer = new char[buffersize];");
1336
1337    if (OtherFilesGen.keepLineCol)
1338    {
1339    ostr.println(" bufline = new int[buffersize];");
1340    ostr.println(" bufcolumn = new int[buffersize];");
1341    }
1342
1343    ostr.println(" }");
1344
1345    if (OtherFilesGen.keepLineCol)
1346    {
1347    ostr.println(" prevCharIsLF = prevCharIsCR = false;");
1348    }
1349
1350    ostr.println(" tokenBegin = inBuf = maxNextCharInd = 0;");
1351    ostr.println(" bufpos = -1;");
1352    ostr.println(" }");
1353    ostr.println("");
1354    ostr.println(" public void ReInit(java.io.Reader dstream, int startline,");
1355    ostr.println(" int startcolumn)");
1356    ostr.println(" {");
1357    ostr.println(" ReInit(dstream, startline, startcolumn, 4096);");
1358    ostr.println(" }");
1359    ostr.println("");
1360    ostr.println(" public void ReInit(java.io.Reader dstream)");
1361    ostr.println(" {");
1362    ostr.println(" ReInit(dstream, 1, 1, 4096);");
1363    ostr.println(" }");
1364    ostr.println(" public SimpleCharStream(java.io.InputStream dstream, String encoding, int startline,");
1365    ostr.println(" int startcolumn, int buffersize) throws java.io.UnsupportedEncodingException");
1366    ostr.println(" {");
1367    ostr.println(" this(encoding == null ? new java.io.InputStreamReader(dstream) : new java.io.InputStreamReader(dstream, encoding), startline, startcolumn, buffersize);");
1368    ostr.println(" }");
1369    ostr.println("");
1370    ostr.println(" public SimpleCharStream(java.io.InputStream dstream, int startline,");
1371    ostr.println(" int startcolumn, int buffersize)");
1372    ostr.println(" {");
1373    ostr.println(" this(new java.io.InputStreamReader(dstream), startline, startcolumn, buffersize);");
1374    ostr.println(" }");
1375    ostr.println("");
1376    ostr.println(" public SimpleCharStream(java.io.InputStream dstream, String encoding, int startline,");
1377    ostr.println(" int startcolumn) throws java.io.UnsupportedEncodingException");
1378    ostr.println(" {");
1379    ostr.println(" this(dstream, encoding, startline, startcolumn, 4096);");
1380    ostr.println(" }");
1381    ostr.println("");
1382    ostr.println(" public SimpleCharStream(java.io.InputStream dstream, int startline,");
1383    ostr.println(" int startcolumn)");
1384    ostr.println(" {");
1385    ostr.println(" this(dstream, startline, startcolumn, 4096);");
1386    ostr.println(" }");
1387    ostr.println("");
1388    ostr.println(" public SimpleCharStream(java.io.InputStream dstream, String encoding) throws java.io.UnsupportedEncodingException");
1389    ostr.println(" {");
1390    ostr.println(" this(dstream, encoding, 1, 1, 4096);");
1391    ostr.println(" }");
1392    ostr.println("");
1393    ostr.println(" public SimpleCharStream(java.io.InputStream dstream)");
1394    ostr.println(" {");
1395    ostr.println(" this(dstream, 1, 1, 4096);");
1396    ostr.println(" }");
1397    ostr.println("");
1398    ostr.println(" public void ReInit(java.io.InputStream dstream, String encoding, int startline,");
1399    ostr.println(" int startcolumn, int buffersize) throws java.io.UnsupportedEncodingException");
1400    ostr.println(" {");
1401    ostr.println(" ReInit(encoding == null ? new java.io.InputStreamReader(dstream) : new java.io.InputStreamReader(dstream, encoding), startline, startcolumn, buffersize);");
1402    ostr.println(" }");
1403    ostr.println("");
1404    ostr.println(" public void ReInit(java.io.InputStream dstream, int startline,");
1405    ostr.println(" int startcolumn, int buffersize)");
1406    ostr.println(" {");
1407    ostr.println(" ReInit(new java.io.InputStreamReader(dstream), startline, startcolumn, buffersize);");
1408    ostr.println(" }");
1409    ostr.println("");
1410    ostr.println(" public void ReInit(java.io.InputStream dstream, String encoding) throws java.io.UnsupportedEncodingException");
1411    ostr.println(" {");
1412    ostr.println(" ReInit(dstream, encoding, 1, 1, 4096);");
1413    ostr.println(" }");
1414    ostr.println("");
1415    ostr.println(" public void ReInit(java.io.InputStream dstream)");
1416    ostr.println(" {");
1417    ostr.println(" ReInit(dstream, 1, 1, 4096);");
1418    ostr.println(" }");
1419    ostr.println(" public void ReInit(java.io.InputStream dstream, String encoding, int startline,");
1420    ostr.println(" int startcolumn) throws java.io.UnsupportedEncodingException");
1421    ostr.println(" {");
1422    ostr.println(" ReInit(dstream, encoding, startline, startcolumn, 4096);");
1423    ostr.println(" }");
1424    ostr.println(" public void ReInit(java.io.InputStream dstream, int startline,");
1425    ostr.println(" int startcolumn)");
1426    ostr.println(" {");
1427    ostr.println(" ReInit(dstream, startline, startcolumn, 4096);");
1428    ostr.println(" }");
1429    ostr.println(prefix + "public String GetImage()");
1430    ostr.println(" {");
1431    ostr.println(" if (bufpos >= tokenBegin)");
1432    ostr.println(" return new String(buffer, tokenBegin, bufpos - tokenBegin + 1);");
1433    ostr.println(" else");
1434    ostr.println(" return new String(buffer, tokenBegin, bufsize - tokenBegin) +");
1435    ostr.println(" new String(buffer, 0, bufpos + 1);");
1436    ostr.println(" }");
1437    ostr.println("");
1438    ostr.println(prefix + "public char[] GetSuffix(int len)");
1439    ostr.println(" {");
1440    ostr.println(" char[] ret = new char[len];");
1441    ostr.println("");
1442    ostr.println(" if ((bufpos + 1) >= len)");
1443    ostr.println(" System.arraycopy(buffer, bufpos - len + 1, ret, 0, len);");
1444    ostr.println(" else");
1445    ostr.println(" {");
1446    ostr.println(" System.arraycopy(buffer, bufsize - (len - bufpos - 1), ret, 0,");
1447    ostr.println(" len - bufpos - 1);");
1448    ostr.println(" System.arraycopy(buffer, 0, ret, len - bufpos - 1, bufpos + 1);");
1449    ostr.println(" }");
1450    ostr.println("");
1451    ostr.println(" return ret;");
1452    ostr.println(" }");
1453    ostr.println("");
1454    ostr.println(prefix + "public void Done()");
1455    ostr.println(" {");
1456    ostr.println(" buffer = null;");
1457
1458    if (OtherFilesGen.keepLineCol)
1459    {
1460    ostr.println(" bufline = null;");
1461    ostr.println(" bufcolumn = null;");
1462    }
1463
1464    ostr.println(" }");
1465
1466    if (OtherFilesGen.keepLineCol)
1467    {
1468    ostr.println("");
1469    ostr.println(" /**");
1470    ostr.println(" * Method to adjust line and column numbers for the start of a token.");
1471    ostr.println(" */");
1472    ostr.println(prefix + "public void adjustBeginLineColumn(int newLine, int newCol)");
1473    ostr.println(" {");
1474    ostr.println(" int start = tokenBegin;");
1475    ostr.println(" int len;");
1476    ostr.println("");
1477    ostr.println(" if (bufpos >= tokenBegin)");
1478    ostr.println(" {");
1479    ostr.println(" len = bufpos - tokenBegin + inBuf + 1;");
1480    ostr.println(" }");
1481    ostr.println(" else");
1482    ostr.println(" {");
1483    ostr.println(" len = bufsize - tokenBegin + bufpos + 1 + inBuf;");
1484    ostr.println(" }");
1485    ostr.println("");
1486    ostr.println(" int i = 0, j = 0, k = 0;");
1487    ostr.println(" int nextColDiff = 0, columnDiff = 0;");
1488    ostr.println("");
1489    ostr.println(" while (i < len &&");
1490    ostr.println(" bufline[j = start % bufsize] == bufline[k = ++start % bufsize])");
1491    ostr.println(" {");
1492    ostr.println(" bufline[j] = newLine;");
1493    ostr.println(" nextColDiff = columnDiff + bufcolumn[k] - bufcolumn[j];");
1494    ostr.println(" bufcolumn[j] = newCol + columnDiff;");
1495    ostr.println(" columnDiff = nextColDiff;");
1496    ostr.println(" i++;");
1497    ostr.println(" } ");
1498    ostr.println("");
1499    ostr.println(" if (i < len)");
1500    ostr.println(" {");
1501    ostr.println(" bufline[j] = newLine++;");
1502    ostr.println(" bufcolumn[j] = newCol + columnDiff;");
1503    ostr.println("");
1504    ostr.println(" while (i++ < len)");
1505    ostr.println(" {");
1506    ostr.println(" if (bufline[j = start % bufsize] != bufline[++start % bufsize])");
1507    ostr.println(" bufline[j] = newLine++;");
1508    ostr.println(" else");
1509    ostr.println(" bufline[j] = newLine;");
1510    ostr.println(" }");
1511    ostr.println(" }");
1512    ostr.println("");
1513    ostr.println(" line = bufline[j];");
1514    ostr.println(" column = bufcolumn[j];");
1515    ostr.println(" }");
1516    ostr.println("");
1517    }
1518
1519    ostr.println("}");
1520    ostr.close();
1521  }
1522
1523  public static void gen_CharStream() {
1524    File tmp;
1525    if ((tmp = new File(Options.getOutputDirectory(), "CharStream.java")).exists()) {
1526      CheckVersion("CharStream.java", charStreamVersion);
1527      return;
1528    }
1529    System.out.println("File \"CharStream.java\" does not exist. Will create one.");
1530    try {
1531      ostr = new PrintWriter(
1532                new BufferedWriter(
1533                   new FileWriter(tmp),
1534                   8192
1535                )
1536             );
1537    } catch (IOException e) {
1538      JavaCCErrors.semantic_error("Could not open file CharStream.java for writing.");
1539      throw new Error JavaDoc();
1540    }
1541
1542    ostr.println("/* " + getIdString(toolName, "CharStream.java") + " Version " + charStreamVersion + " */");
1543
1544    if (cu_to_insertion_point_1.size() != 0 &&
1545        ((Token)cu_to_insertion_point_1.elementAt(0)).kind == PACKAGE
1546       ) {
1547      for (int i = 1; i < cu_to_insertion_point_1.size(); i++) {
1548        if (((Token)cu_to_insertion_point_1.elementAt(i)).kind == SEMICOLON) {
1549          cline = ((Token)(cu_to_insertion_point_1.elementAt(0))).beginLine;
1550          ccol = ((Token)(cu_to_insertion_point_1.elementAt(0))).beginColumn;
1551          for (int j = 0; j <= i; j++) {
1552            printToken((Token)(cu_to_insertion_point_1.elementAt(j)), ostr);
1553          }
1554          ostr.println("");
1555          ostr.println("");
1556          break;
1557        }
1558      }
1559    }
1560    ostr.println("/**");
1561    ostr.println(" * This interface describes a character stream that maintains line and");
1562    ostr.println(" * column number positions of the characters. It also has the capability");
1563    ostr.println(" * to backup the stream to some extent. An implementation of this");
1564    ostr.println(" * interface is used in the TokenManager implementation generated by");
1565    ostr.println(" * JavaCCParser.");
1566    ostr.println(" *");
1567    ostr.println(" * All the methods except backup can be implemented in any fashion. backup");
1568    ostr.println(" * needs to be implemented correctly for the correct operation of the lexer.");
1569    ostr.println(" * Rest of the methods are all used to get information like line number,");
1570    ostr.println(" * column number and the String that constitutes a token and are not used");
1571    ostr.println(" * by the lexer. Hence their implementation won't affect the generated lexer's");
1572    ostr.println(" * operation.");
1573    ostr.println(" */");
1574    ostr.println("");
1575    ostr.println("public interface CharStream {");
1576    ostr.println("");
1577    ostr.println(" /**");
1578    ostr.println(" * Returns the next character from the selected input. The method");
1579    ostr.println(" * of selecting the input is the responsibility of the class");
1580    ostr.println(" * implementing this interface. Can throw any java.io.IOException.");
1581    ostr.println(" */");
1582    ostr.println(" char readChar() throws java.io.IOException;");
1583    ostr.println("");
1584    ostr.println(" /**");
1585    ostr.println(" * Returns the column position of the character last read.");
1586    ostr.println(" * @deprecated ");
1587    ostr.println(" * @see #getEndColumn");
1588    ostr.println(" */");
1589    ostr.println(" int getColumn();");
1590    ostr.println("");
1591    ostr.println(" /**");
1592    ostr.println(" * Returns the line number of the character last read.");
1593    ostr.println(" * @deprecated ");
1594    ostr.println(" * @see #getEndLine");
1595    ostr.println(" */");
1596    ostr.println(" int getLine();");
1597    ostr.println("");
1598    ostr.println(" /**");
1599    ostr.println(" * Returns the column number of the last character for current token (being");
1600    ostr.println(" * matched after the last call to BeginTOken).");
1601    ostr.println(" */");
1602    ostr.println(" int getEndColumn();");
1603    ostr.println("");
1604    ostr.println(" /**");
1605    ostr.println(" * Returns the line number of the last character for current token (being");
1606    ostr.println(" * matched after the last call to BeginTOken).");
1607    ostr.println(" */");
1608    ostr.println(" int getEndLine();");
1609    ostr.println("");
1610    ostr.println(" /**");
1611    ostr.println(" * Returns the column number of the first character for current token (being");
1612    ostr.println(" * matched after the last call to BeginTOken).");
1613    ostr.println(" */");
1614    ostr.println(" int getBeginColumn();");
1615    ostr.println("");
1616    ostr.println(" /**");
1617    ostr.println(" * Returns the line number of the first character for current token (being");
1618    ostr.println(" * matched after the last call to BeginTOken).");
1619    ostr.println(" */");
1620    ostr.println(" int getBeginLine();");
1621    ostr.println("");
1622    ostr.println(" /**");
1623    ostr.println(" * Backs up the input stream by amount steps. Lexer calls this method if it");
1624    ostr.println(" * had already read some characters, but could not use them to match a");
1625    ostr.println(" * (longer) token. So, they will be used again as the prefix of the next");
1626    ostr.println(" * token and it is the implemetation's responsibility to do this right.");
1627    ostr.println(" */");
1628    ostr.println(" void backup(int amount);");
1629    ostr.println("");
1630    ostr.println(" /**");
1631    ostr.println(" * Returns the next character that marks the beginning of the next token.");
1632    ostr.println(" * All characters must remain in the buffer between two successive calls");
1633    ostr.println(" * to this method to implement backup correctly.");
1634    ostr.println(" */");
1635    ostr.println(" char BeginToken() throws java.io.IOException;");
1636    ostr.println("");
1637    ostr.println(" /**");
1638    ostr.println(" * Returns a string made up of characters from the marked token beginning ");
1639    ostr.println(" * to the current buffer position. Implementations have the choice of returning");
1640    ostr.println(" * anything that they want to. For example, for efficiency, one might decide");
1641    ostr.println(" * to just return null, which is a valid implementation.");
1642    ostr.println(" */");
1643    ostr.println(" String GetImage();");
1644    ostr.println("");
1645    ostr.println(" /**");
1646    ostr.println(" * Returns an array of characters that make up the suffix of length 'len' for");
1647    ostr.println(" * the currently matched token. This is used to build up the matched string");
1648    ostr.println(" * for use in actions in the case of MORE. A simple and inefficient");
1649    ostr.println(" * implementation of this is as follows :");
1650    ostr.println(" *");
1651    ostr.println(" * {");
1652    ostr.println(" * String t = GetImage();");
1653    ostr.println(" * return t.substring(t.length() - len, t.length()).toCharArray();");
1654    ostr.println(" * }");
1655    ostr.println(" */");
1656    ostr.println(" char[] GetSuffix(int len);");
1657    ostr.println("");
1658    ostr.println(" /**");
1659    ostr.println(" * The lexer calls this function to indicate that it is done with the stream");
1660    ostr.println(" * and hence implementations can free any resources held by this class.");
1661    ostr.println(" * Again, the body of this function can be just empty and it will not");
1662    ostr.println(" * affect the lexer's operation.");
1663    ostr.println(" */");
1664    ostr.println(" void Done();");
1665    ostr.println("");
1666    ostr.println("}");
1667    ostr.close();
1668  }
1669
1670  public static void gen_ParseException() {
1671    File tmp;
1672    if ((tmp = new File(Options.getOutputDirectory(), "ParseException.java")).exists()) {
1673      CheckVersion("ParseException.java", parseExceptionVersion);
1674      return;
1675    }
1676    System.out.println("File \"ParseException.java\" does not exist. Will create one.");
1677    try {
1678      ostr = new PrintWriter(
1679                new BufferedWriter(
1680                   new FileWriter(tmp),
1681                   8192
1682                )
1683             );
1684    } catch (IOException e) {
1685      JavaCCErrors.semantic_error("Could not open file ParseException.java for writing.");
1686      throw new Error JavaDoc();
1687    }
1688
1689    ostr.println("/* " + getIdString(toolName, "ParseException.java") + " Version " + parseExceptionVersion + " */");
1690
1691    if (cu_to_insertion_point_1.size() != 0 &&
1692        ((Token)cu_to_insertion_point_1.elementAt(0)).kind == PACKAGE
1693       ) {
1694      for (int i = 1; i < cu_to_insertion_point_1.size(); i++) {
1695        if (((Token)cu_to_insertion_point_1.elementAt(i)).kind == SEMICOLON) {
1696          cline = ((Token)(cu_to_insertion_point_1.elementAt(0))).beginLine;
1697          ccol = ((Token)(cu_to_insertion_point_1.elementAt(0))).beginColumn;
1698          for (int j = 0; j <= i; j++) {
1699            printToken((Token)(cu_to_insertion_point_1.elementAt(j)), ostr);
1700          }
1701          ostr.println("");
1702          ostr.println("");
1703          break;
1704        }
1705      }
1706    }
1707    ostr.println("/**");
1708    ostr.println(" * This exception is thrown when parse errors are encountered.");
1709    ostr.println(" * You can explicitly create objects of this exception type by");
1710    ostr.println(" * calling the method generateParseException in the generated");
1711    ostr.println(" * parser.");
1712    ostr.println(" *");
1713    ostr.println(" * You can modify this class to customize your error reporting");
1714    ostr.println(" * mechanisms so long as you retain the public fields.");
1715    ostr.println(" */");
1716    ostr.println("public class ParseException extends Exception {");
1717    ostr.println("");
1718    ostr.println(" /**");
1719    ostr.println(" * This constructor is used by the method \"generateParseException\"");
1720    ostr.println(" * in the generated parser. Calling this constructor generates");
1721    ostr.println(" * a new object of this type with the fields \"currentToken\",");
1722    ostr.println(" * \"expectedTokenSequences\", and \"tokenImage\" set. The boolean");
1723    ostr.println(" * flag \"specialConstructor\" is also set to true to indicate that");
1724    ostr.println(" * this constructor was used to create this object.");
1725    ostr.println(" * This constructor calls its super class with the empty string");
1726    ostr.println(" * to force the \"toString\" method of parent class \"Throwable\" to");
1727    ostr.println(" * print the error message in the form:");
1728    ostr.println(" * ParseException: <result of getMessage>");
1729    ostr.println(" */");
1730    ostr.println(" public ParseException(Token currentTokenVal,");
1731    ostr.println(" int[][] expectedTokenSequencesVal,");
1732    ostr.println(" String[] tokenImageVal");
1733    ostr.println(" )");
1734    ostr.println(" {");
1735    ostr.println(" super(\"\");");
1736    ostr.println(" specialConstructor = true;");
1737    ostr.println(" currentToken = currentTokenVal;");
1738    ostr.println(" expectedTokenSequences = expectedTokenSequencesVal;");
1739    ostr.println(" tokenImage = tokenImageVal;");
1740    ostr.println(" }");
1741    ostr.println("");
1742    ostr.println(" /**");
1743    ostr.println(" * The following constructors are for use by you for whatever");
1744    ostr.println(" * purpose you can think of. Constructing the exception in this");
1745    ostr.println(" * manner makes the exception behave in the normal way - i.e., as");
1746    ostr.println(" * documented in the class \"Throwable\". The fields \"errorToken\",");
1747    ostr.println(" * \"expectedTokenSequences\", and \"tokenImage\" do not contain");
1748    ostr.println(" * relevant information. The JavaCC generated code does not use");
1749    ostr.println(" * these constructors.");
1750    ostr.println(" */");
1751    ostr.println("");
1752    ostr.println(" public ParseException() {");
1753    ostr.println(" super();");
1754    ostr.println(" specialConstructor = false;");
1755    ostr.println(" }");
1756    ostr.println("");
1757    ostr.println(" public ParseException(String message) {");
1758    ostr.println(" super(message);");
1759    ostr.println(" specialConstructor = false;");
1760    ostr.println(" }");
1761    ostr.println("");
1762    ostr.println(" /**");
1763    ostr.println(" * This variable determines which constructor was used to create");
1764    ostr.println(" * this object and thereby affects the semantics of the");
1765    ostr.println(" * \"getMessage\" method (see below).");
1766    ostr.println(" */");
1767    ostr.println(" protected boolean specialConstructor;");
1768    ostr.println("");
1769    ostr.println(" /**");
1770    ostr.println(" * This is the last token that has been consumed successfully. If");
1771    ostr.println(" * this object has been created due to a parse error, the token");
1772    ostr.println(" * followng this token will (therefore) be the first error token.");
1773    ostr.println(" */");
1774    ostr.println(" public Token currentToken;");
1775    ostr.println("");
1776    ostr.println(" /**");
1777    ostr.println(" * Each entry in this array is an array of integers. Each array");
1778    ostr.println(" * of integers represents a sequence of tokens (by their ordinal");
1779    ostr.println(" * values) that is expected at this point of the parse.");
1780    ostr.println(" */");
1781    ostr.println(" public int[][] expectedTokenSequences;");
1782    ostr.println("");
1783    ostr.println(" /**");
1784    ostr.println(" * This is a reference to the \"tokenImage\" array of the generated");
1785    ostr.println(" * parser within which the parse error occurred. This array is");
1786    ostr.println(" * defined in the generated ...Constants interface.");
1787    ostr.println(" */");
1788    ostr.println(" public String[] tokenImage;");
1789    ostr.println("");
1790    ostr.println(" /**");
1791    ostr.println(" * This method has the standard behavior when this object has been");
1792    ostr.println(" * created using the standard constructors. Otherwise, it uses");
1793    ostr.println(" * \"currentToken\" and \"expectedTokenSequences\" to generate a parse");
1794    ostr.println(" * error message and returns it. If this object has been created");
1795    ostr.println(" * due to a parse error, and you do not catch it (it gets thrown");
1796    ostr.println(" * from the parser), then this method is called during the printing");
1797    ostr.println(" * of the final stack trace, and hence the correct error message");
1798    ostr.println(" * gets displayed.");
1799    ostr.println(" */");
1800    ostr.println(" public String getMessage() {");
1801    ostr.println(" if (!specialConstructor) {");
1802    ostr.println(" return super.getMessage();");
1803    ostr.println(" }");
1804    ostr.println(" StringBuffer expected = new StringBuffer();");
1805    ostr.println(" int maxSize = 0;");
1806    ostr.println(" for (int i = 0; i < expectedTokenSequences.length; i++) {");
1807    ostr.println(" if (maxSize < expectedTokenSequences[i].length) {");
1808    ostr.println(" maxSize = expectedTokenSequences[i].length;");
1809    ostr.println(" }");
1810    ostr.println(" for (int j = 0; j < expectedTokenSequences[i].length; j++) {");
1811    ostr.println(" expected.append(tokenImage[expectedTokenSequences[i][j]]).append(\" \");");
1812    ostr.println(" }");
1813    ostr.println(" if (expectedTokenSequences[i][expectedTokenSequences[i].length - 1] != 0) {");
1814    ostr.println(" expected.append(\"...\");");
1815    ostr.println(" }");
1816    ostr.println(" expected.append(eol).append(\" \");");
1817    ostr.println(" }");
1818    ostr.println(" String retval = \"Encountered \\\"\";");
1819    ostr.println(" Token tok = currentToken.next;");
1820    ostr.println(" for (int i = 0; i < maxSize; i++) {");
1821    ostr.println(" if (i != 0) retval += \" \";");
1822    ostr.println(" if (tok.kind == 0) {");
1823    ostr.println(" retval += tokenImage[0];");
1824    ostr.println(" break;");
1825    ostr.println(" }");
1826    ostr.println(" retval += add_escapes(tok.image);");
1827    ostr.println(" tok = tok.next; ");
1828    ostr.println(" }");
1829    if (OtherFilesGen.keepLineCol)
1830    {
1831    ostr.println(" retval += \"\\\" at line \" + currentToken.next.beginLine + \", column \" + currentToken.next.beginColumn;");
1832    }
1833    ostr.println(" retval += \".\" + eol;");
1834    ostr.println(" if (expectedTokenSequences.length == 1) {");
1835    ostr.println(" retval += \"Was expecting:\" + eol + \" \";");
1836    ostr.println(" } else {");
1837    ostr.println(" retval += \"Was expecting one of:\" + eol + \" \";");
1838    ostr.println(" }");
1839    ostr.println(" retval += expected.toString();");
1840    ostr.println(" return retval;");
1841    ostr.println(" }");
1842    ostr.println("");
1843    ostr.println(" /**");
1844    ostr.println(" * The end of line string for this machine.");
1845    ostr.println(" */");
1846    ostr.println(" protected String eol = System.getProperty(\"line.separator\", \"\\n\");");
1847    ostr.println(" ");
1848    ostr.println(" /**");
1849    ostr.println(" * Used to convert raw characters to their escaped version");
1850    ostr.println(" * when these raw version cannot be used as part of an ASCII");
1851    ostr.println(" * string literal.");
1852    ostr.println(" */");
1853    ostr.println(" protected String add_escapes(String str) {");
1854    ostr.println(" StringBuffer retval = new StringBuffer();");
1855    ostr.println(" char ch;");
1856    ostr.println(" for (int i = 0; i < str.length(); i++) {");
1857    ostr.println(" switch (str.charAt(i))");
1858    ostr.println(" {");
1859    ostr.println(" case 0 :");
1860    ostr.println(" continue;");
1861    ostr.println(" case '\\b':");
1862    ostr.println(" retval.append(\"\\\\b\");");
1863    ostr.println(" continue;");
1864    ostr.println(" case '\\t':");
1865    ostr.println(" retval.append(\"\\\\t\");");
1866    ostr.println(" continue;");
1867    ostr.println(" case '\\n':");
1868    ostr.println(" retval.append(\"\\\\n\");");
1869    ostr.println(" continue;");
1870    ostr.println(" case '\\f':");
1871    ostr.println(" retval.append(\"\\\\f\");");
1872    ostr.println(" continue;");
1873    ostr.println(" case '\\r':");
1874    ostr.println(" retval.append(\"\\\\r\");");
1875    ostr.println(" continue;");
1876    ostr.println(" case '\\\"':");
1877    ostr.println(" retval.append(\"\\\\\\\"\");");
1878    ostr.println(" continue;");
1879    ostr.println(" case '\\'':");
1880    ostr.println(" retval.append(\"\\\\\\'\");");
1881    ostr.println(" continue;");
1882    ostr.println(" case '\\\\':");
1883    ostr.println(" retval.append(\"\\\\\\\\\");");
1884    ostr.println(" continue;");
1885    ostr.println(" default:");
1886    ostr.println(" if ((ch = str.charAt(i)) < 0x20 || ch > 0x7e) {");
1887    ostr.println(" String s = \"0000\" + Integer.toString(ch, 16);");
1888    ostr.println(" retval.append(\"\\\\u\" + s.substring(s.length() - 4, s.length()));");
1889    ostr.println(" } else {");
1890    ostr.println(" retval.append(ch);");
1891    ostr.println(" }");
1892    ostr.println(" continue;");
1893    ostr.println(" }");
1894    ostr.println(" }");
1895    ostr.println(" return retval.toString();");
1896    ostr.println(" }");
1897    ostr.println("");
1898    ostr.println("}");
1899    ostr.close();
1900  }
1901
1902  public static void gen_TokenMgrError() {
1903    File tmp;
1904    if ((tmp = new File(Options.getOutputDirectory(), "TokenMgrError.java")).exists()) {
1905      CheckVersion("TokenMgrError.java", tokenMgrErrorVersion);
1906      return;
1907    }
1908    System.out.println("File \"TokenMgrError.java\" does not exist. Will create one.");
1909    try {
1910      ostr = new PrintWriter(
1911                new BufferedWriter(
1912                   new FileWriter(tmp),
1913                   8192
1914                )
1915             );
1916    } catch (IOException e) {
1917      JavaCCErrors.semantic_error("Could not open file TokenMgrError.java for writing.");
1918      throw new Error JavaDoc();
1919    }
1920
1921    ostr.println("/* " + getIdString(toolName, "TokenMgrError.java") + " Version " + tokenMgrErrorVersion + " */");
1922
1923    if (cu_to_insertion_point_1.size() != 0 &&
1924        ((Token)cu_to_insertion_point_1.elementAt(0)).kind == PACKAGE
1925       ) {
1926      for (int i = 1; i < cu_to_insertion_point_1.size(); i++) {
1927        if (((Token)cu_to_insertion_point_1.elementAt(i)).kind == SEMICOLON) {
1928          cline = ((Token)(cu_to_insertion_point_1.elementAt(0))).beginLine;
1929          ccol = ((Token)(cu_to_insertion_point_1.elementAt(0))).beginColumn;
1930          for (int j = 0; j <= i; j++) {
1931            printToken((Token)(cu_to_insertion_point_1.elementAt(j)), ostr);
1932          }
1933          ostr.println("");
1934          ostr.println("");
1935          break;
1936        }
1937      }
1938    }
1939    ostr.println("public class TokenMgrError extends Error");
1940    ostr.println("{");
1941    ostr.println(" /*");
1942    ostr.println(" * Ordinals for various reasons why an Error of this type can be thrown.");
1943    ostr.println(" */");
1944    ostr.println("");
1945    ostr.println(" /**");
1946    ostr.println(" * Lexical error occured.");
1947    ostr.println(" */");
1948    ostr.println(" static final int LEXICAL_ERROR = 0;");
1949    ostr.println("");
1950    ostr.println(" /**");
1951    ostr.println(" * An attempt wass made to create a second instance of a static token manager.");
1952    ostr.println(" */");
1953    ostr.println(" static final int STATIC_LEXER_ERROR = 1;");
1954    ostr.println("");
1955    ostr.println(" /**");
1956    ostr.println(" * Tried to change to an invalid lexical state.");
1957    ostr.println(" */");
1958    ostr.println(" static final int INVALID_LEXICAL_STATE = 2;");
1959    ostr.println("");
1960    ostr.println(" /**");
1961    ostr.println(" * Detected (and bailed out of) an infinite loop in the token manager.");
1962    ostr.println(" */");
1963    ostr.println(" static final int LOOP_DETECTED = 3;");
1964    ostr.println("");
1965    ostr.println(" /**");
1966    ostr.println(" * Indicates the reason why the exception is thrown. It will have");
1967    ostr.println(" * one of the above 4 values.");
1968    ostr.println(" */");
1969    ostr.println(" int errorCode;");
1970    ostr.println("");
1971    ostr.println(" /**");
1972    ostr.println(" * Replaces unprintable characters by their espaced (or unicode escaped)");
1973    ostr.println(" * equivalents in the given string");
1974    ostr.println(" */");
1975    ostr.println(" protected static final String addEscapes(String str) {");
1976    ostr.println(" StringBuffer retval = new StringBuffer();");
1977    ostr.println(" char ch;");
1978    ostr.println(" for (int i = 0; i < str.length(); i++) {");
1979    ostr.println(" switch (str.charAt(i))");
1980    ostr.println(" {");
1981    ostr.println(" case 0 :");
1982    ostr.println(" continue;");
1983    ostr.println(" case '\\b':");
1984    ostr.println(" retval.append(\"\\\\b\");");
1985    ostr.println(" continue;");
1986    ostr.println(" case '\\t':");
1987    ostr.println(" retval.append(\"\\\\t\");");
1988    ostr.println(" continue;");
1989    ostr.println(" case '\\n':");
1990    ostr.println(" retval.append(\"\\\\n\");");
1991    ostr.println(" continue;");
1992    ostr.println(" case '\\f':");
1993    ostr.println(" retval.append(\"\\\\f\");");
1994    ostr.println(" continue;");
1995    ostr.println(" case '\\r':");
1996    ostr.println(" retval.append(\"\\\\r\");");
1997    ostr.println(" continue;");
1998    ostr.println(" case '\\\"':");
1999    ostr.println(" retval.append(\"\\\\\\\"\");");
2000    ostr.println(" continue;");
2001    ostr.println(" case '\\'':");
2002    ostr.println(" retval.append(\"\\\\\\'\");");
2003    ostr.println(" continue;");
2004    ostr.println(" case '\\\\':");
2005    ostr.println(" retval.append(\"\\\\\\\\\");");
2006    ostr.println(" continue;");
2007    ostr.println(" default:");
2008    ostr.println(" if ((ch = str.charAt(i)) < 0x20 || ch > 0x7e) {");
2009    ostr.println(" String s = \"0000\" + Integer.toString(ch, 16);");
2010    ostr.println(" retval.append(\"\\\\u\" + s.substring(s.length() - 4, s.length()));");
2011    ostr.println(" } else {");
2012    ostr.println(" retval.append(ch);");
2013    ostr.println(" }");
2014    ostr.println(" continue;");
2015    ostr.println(" }");
2016    ostr.println(" }");
2017    ostr.println(" return retval.toString();");
2018    ostr.println(" }");
2019    ostr.println("");
2020    ostr.println(" /**");
2021    ostr.println(" * Returns a detailed message for the Error when it is thrown by the");
2022    ostr.println(" * token manager to indicate a lexical error.");
2023    ostr.println(" * Parameters : ");
2024    ostr.println(" * EOFSeen : indicates if EOF caused the lexicl error");
2025    ostr.println(" * curLexState : lexical state in which this error occured");
2026    ostr.println(" * errorLine : line number when the error occured");
2027    ostr.println(" * errorColumn : column number when the error occured");
2028    ostr.println(" * errorAfter : prefix that was seen before this error occured");
2029    ostr.println(" * curchar : the offending character");
2030    ostr.println(" * Note: You can customize the lexical error message by modifying this method.");
2031    ostr.println(" */");
2032    ostr.println(" protected static String LexicalError(boolean EOFSeen, int lexState, int errorLine, int errorColumn, String errorAfter, char curChar) {");
2033    ostr.println(" return(\"Lexical error at line \" +");
2034    ostr.println(" errorLine + \", column \" +");
2035    ostr.println(" errorColumn + \". Encountered: \" +");
2036    ostr.println(" (EOFSeen ? \"<EOF> \" : (\"\\\"\" + addEscapes(String.valueOf(curChar)) + \"\\\"\") + \" (\" + (int)curChar + \"), \") +");
2037    ostr.println(" \"after : \\\"\" + addEscapes(errorAfter) + \"\\\"\");");
2038    ostr.println(" }");
2039    ostr.println("");
2040    ostr.println(" /**");
2041    ostr.println(" * You can also modify the body of this method to customize your error messages.");
2042    ostr.println(" * For example, cases like LOOP_DETECTED and INVALID_LEXICAL_STATE are not");
2043    ostr.println(" * of end-users concern, so you can return something like : ");
2044    ostr.println(" *");
2045    ostr.println(" * \"Internal Error : Please file a bug report .... \"");
2046    ostr.println(" *");
2047    ostr.println(" * from this method for such cases in the release version of your parser.");
2048    ostr.println(" */");
2049    ostr.println(" public String getMessage() {");
2050    ostr.println(" return super.getMessage();");
2051    ostr.println(" }");
2052    ostr.println("");
2053    ostr.println(" /*");
2054    ostr.println(" * Constructors of various flavors follow.");
2055    ostr.println(" */");
2056    ostr.println("");
2057    ostr.println(" public TokenMgrError() {");
2058    ostr.println(" }");
2059    ostr.println("");
2060    ostr.println(" public TokenMgrError(String message, int reason) {");
2061    ostr.println(" super(message);");
2062    ostr.println(" errorCode = reason;");
2063    ostr.println(" }");
2064    ostr.println("");
2065    ostr.println(" public TokenMgrError(boolean EOFSeen, int lexState, int errorLine, int errorColumn, String errorAfter, char curChar, int reason) {");
2066    ostr.println(" this(LexicalError(EOFSeen, lexState, errorLine, errorColumn, errorAfter, curChar), reason);");
2067    ostr.println(" }");
2068    ostr.println("}");
2069    ostr.close();
2070  }
2071
2072  public static void gen_Token() {
2073    File tmp = null;
2074    if ((tmp = new File(Options.getOutputDirectory(), "Token.java")).exists()) {
2075      CheckVersion("Token.java", tokenVersion);
2076      return;
2077    }
2078    System.out.println("File \"Token.java\" does not exist. Will create one.");
2079    try {
2080      ostr = new PrintWriter(
2081                new BufferedWriter(
2082                   new FileWriter(tmp),
2083                   8192
2084                )
2085             );
2086    } catch (IOException e) {
2087      JavaCCErrors.semantic_error("Could not open file Token.java for writing.");
2088      throw new Error JavaDoc();
2089    }
2090
2091    ostr.println("/* " + getIdString(toolName, "Token.java") + " Version " + tokenVersion + " */");
2092
2093    if (cu_to_insertion_point_1.size() != 0 &&
2094        ((Token)cu_to_insertion_point_1.elementAt(0)).kind == PACKAGE
2095       ) {
2096      for (int i = 1; i < cu_to_insertion_point_1.size(); i++) {
2097        if (((Token)cu_to_insertion_point_1.elementAt(i)).kind == SEMICOLON) {
2098          cline = ((Token)(cu_to_insertion_point_1.elementAt(0))).beginLine;
2099          ccol = ((Token)(cu_to_insertion_point_1.elementAt(0))).beginColumn;
2100          for (int j = 0; j <= i; j++) {
2101            printToken((Token)(cu_to_insertion_point_1.elementAt(j)), ostr);
2102          }
2103          ostr.println("");
2104          ostr.println("");
2105          break;
2106        }
2107      }
2108    }
2109    ostr.println("/**");
2110    ostr.println(" * Describes the input token stream.");
2111    ostr.println(" */");
2112    ostr.println("");
2113    ostr.println("public class Token {");
2114    ostr.println("");
2115    ostr.println(" /**");
2116    ostr.println(" * An integer that describes the kind of this token. This numbering");
2117    ostr.println(" * system is determined by JavaCCParser, and a table of these numbers is");
2118    ostr.println(" * stored in the file ...Constants.java.");
2119    ostr.println(" */");
2120    ostr.println(" public int kind;");
2121    if (OtherFilesGen.keepLineCol)
2122    {
2123    ostr.println("");
2124    ostr.println(" /**");
2125    ostr.println(" * beginLine and beginColumn describe the position of the first character");
2126    ostr.println(" * of this token; endLine and endColumn describe the position of the");
2127    ostr.println(" * last character of this token.");
2128    ostr.println(" */");
2129    ostr.println(" public int beginLine, beginColumn, endLine, endColumn;");
2130    }
2131
2132    ostr.println("");
2133    ostr.println(" /**");
2134    ostr.println(" * The string image of the token.");
2135    ostr.println(" */");
2136    ostr.println(" public String image;");
2137    ostr.println("");
2138    ostr.println(" /**");
2139    ostr.println(" * A reference to the next regular (non-special) token from the input");
2140    ostr.println(" * stream. If this is the last token from the input stream, or if the");
2141    ostr.println(" * token manager has not read tokens beyond this one, this field is");
2142    ostr.println(" * set to null. This is true only if this token is also a regular");
2143    ostr.println(" * token. Otherwise, see below for a description of the contents of");
2144    ostr.println(" * this field.");
2145    ostr.println(" */");
2146    ostr.println(" public Token next;");
2147    ostr.println("");
2148    ostr.println(" /**");
2149    ostr.println(" * This field is used to access special tokens that occur prior to this");
2150    ostr.println(" * token, but after the immediately preceding regular (non-special) token.");
2151    ostr.println(" * If there are no such special tokens, this field is set to null.");
2152    ostr.println(" * When there are more than one such special token, this field refers");
2153    ostr.println(" * to the last of these special tokens, which in turn refers to the next");
2154    ostr.println(" * previous special token through its specialToken field, and so on");
2155    ostr.println(" * until the first special token (whose specialToken field is null).");
2156    ostr.println(" * The next fields of special tokens refer to other special tokens that");
2157    ostr.println(" * immediately follow it (without an intervening regular token). If there");
2158    ostr.println(" * is no such token, this field is null.");
2159    ostr.println(" */");
2160    ostr.println(" public Token specialToken;");
2161    ostr.println("");
2162    ostr.println(" /**");
2163    ostr.println(" * Returns the image.");
2164    ostr.println(" */");
2165    ostr.println(" public String toString()");
2166    ostr.println(" {");
2167    ostr.println(" return image;");
2168    ostr.println(" }");
2169    ostr.println("");
2170    ostr.println(" /**");
2171    ostr.println(" * Returns a new Token object, by default. However, if you want, you");
2172    ostr.println(" * can create and return subclass objects based on the value of ofKind.");
2173    ostr.println(" * Simply add the cases to the switch for all those special cases.");
2174    ostr.println(" * For example, if you have a subclass of Token called IDToken that");
2175    ostr.println(" * you want to create if ofKind is ID, simlpy add something like :");
2176    ostr.println(" *");
2177    ostr.println(" * case MyParserConstants.ID : return new IDToken();");
2178    ostr.println(" *");
2179    ostr.println(" * to the following switch statement. Then you can cast matchedToken");
2180    ostr.println(" * variable to the appropriate type and use it in your lexical actions.");
2181    ostr.println(" */");
2182    ostr.println(" public static final Token newToken(int ofKind)");
2183    ostr.println(" {");
2184    ostr.println(" switch(ofKind)");
2185    ostr.println(" {");
2186    ostr.println(" default : return new Token();");
2187    ostr.println(" }");
2188    ostr.println(" }");
2189    ostr.println("");
2190    ostr.println("}");
2191    ostr.close();
2192  }
2193
2194  public static void gen_TokenManager() {
2195    File tmp;
2196    if ((tmp = new File(Options.getOutputDirectory(), "TokenManager.java")).exists()) {
2197      CheckVersion("TokenManager.java", tokenManagerVersion);
2198      return;
2199    }
2200    System.out.println("File \"TokenManager.java\" does not exist. Will create one.");
2201    try {
2202      ostr = new PrintWriter(
2203                new BufferedWriter(
2204                   new FileWriter(tmp),
2205                   8192
2206                )
2207             );
2208    } catch (IOException e) {
2209      JavaCCErrors.semantic_error("Could not open file TokenManager.java for writing.");
2210      throw new Error JavaDoc();
2211    }
2212
2213    ostr.println("/* " + getIdString(toolName, "TokenManager.java") + " Version " + tokenManagerVersion + " */");
2214
2215    if (cu_to_insertion_point_1.size() != 0 &&
2216        ((Token)cu_to_insertion_point_1.elementAt(0)).kind == PACKAGE
2217       ) {
2218      for (int i = 1; i < cu_to_insertion_point_1.size(); i++) {
2219        if (((Token)cu_to_insertion_point_1.elementAt(i)).kind == SEMICOLON) {
2220          cline = ((Token)(cu_to_insertion_point_1.elementAt(0))).beginLine;
2221          ccol = ((Token)(cu_to_insertion_point_1.elementAt(0))).beginColumn;
2222          for (int j = 0; j <= i; j++) {
2223            printToken((Token)(cu_to_insertion_point_1.elementAt(j)), ostr);
2224          }
2225          ostr.println("");
2226          ostr.println("");
2227          break;
2228        }
2229      }
2230    }
2231    ostr.println("/**");
2232    ostr.println(" * An implementation for this interface is generated by");
2233    ostr.println(" * JavaCCParser. The user is free to use any implementation");
2234    ostr.println(" * of their choice.");
2235    ostr.println(" */");
2236    ostr.println("");
2237    ostr.println("public interface TokenManager {");
2238    ostr.println("");
2239    ostr.println(" /** This gets the next token from the input stream.");
2240    ostr.println(" * A token of kind 0 (<EOF>) should be returned on EOF.");
2241    ostr.println(" */");
2242    ostr.println(" public Token getNextToken();");
2243    ostr.println("");
2244    ostr.println("}");
2245    ostr.close();
2246  }
2247
2248   public static void reInit()
2249   {
2250      ostr = null;
2251   }
2252
2253}
2254
Popular Tags