add more more spec-compiler.jj
[cdsspec-compiler.git] / src / edu / uci / eecs / specCompiler / grammerParser / JavaCharStream.java
1 /* Generated By:JavaCC: Do not edit this line. JavaCharStream.java Version 6.0 */
2 /* JavaCCOptions:STATIC=false,SUPPORT_CLASS_VISIBILITY_PUBLIC=true */
3 package edu.uci.eecs.specCompiler.grammerParser;
4
5 /**
6  * An implementation of interface CharStream, where the stream is assumed to
7  * contain only ASCII characters (with java-like unicode escape processing).
8  */
9
10 public
11 class JavaCharStream
12 {
13   /** Whether parser is static. */
14   public static final boolean staticFlag = false;
15
16   static final int hexval(char c) throws java.io.IOException {
17     switch(c)
18     {
19        case '0' :
20           return 0;
21        case '1' :
22           return 1;
23        case '2' :
24           return 2;
25        case '3' :
26           return 3;
27        case '4' :
28           return 4;
29        case '5' :
30           return 5;
31        case '6' :
32           return 6;
33        case '7' :
34           return 7;
35        case '8' :
36           return 8;
37        case '9' :
38           return 9;
39
40        case 'a' :
41        case 'A' :
42           return 10;
43        case 'b' :
44        case 'B' :
45           return 11;
46        case 'c' :
47        case 'C' :
48           return 12;
49        case 'd' :
50        case 'D' :
51           return 13;
52        case 'e' :
53        case 'E' :
54           return 14;
55        case 'f' :
56        case 'F' :
57           return 15;
58     }
59
60     throw new java.io.IOException(); // Should never come here
61   }
62
63 /** Position in buffer. */
64   public int bufpos = -1;
65   int bufsize;
66   int available;
67   int tokenBegin;
68   protected int bufline[];
69   protected int bufcolumn[];
70
71   protected int column = 0;
72   protected int line = 1;
73
74   protected boolean prevCharIsCR = false;
75   protected boolean prevCharIsLF = false;
76
77   protected java.io.Reader inputStream;
78
79   protected char[] nextCharBuf;
80   protected char[] buffer;
81   protected int maxNextCharInd = 0;
82   protected int nextCharInd = -1;
83   protected int inBuf = 0;
84   protected int tabSize = 8;
85   protected boolean trackLineColumn = false;
86
87   public void setTabSize(int i) { tabSize = i; }
88   public int getTabSize() { return tabSize; }
89
90   protected void ExpandBuff(boolean wrapAround)
91   {
92     char[] newbuffer = new char[bufsize + 2048];
93     int newbufline[] = new int[bufsize + 2048];
94     int newbufcolumn[] = new int[bufsize + 2048];
95
96     try
97     {
98       if (wrapAround)
99       {
100         System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin);
101         System.arraycopy(buffer, 0, newbuffer, bufsize - tokenBegin, bufpos);
102         buffer = newbuffer;
103
104         System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin);
105         System.arraycopy(bufline, 0, newbufline, bufsize - tokenBegin, bufpos);
106         bufline = newbufline;
107
108         System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin);
109         System.arraycopy(bufcolumn, 0, newbufcolumn, bufsize - tokenBegin, bufpos);
110         bufcolumn = newbufcolumn;
111
112         bufpos += (bufsize - tokenBegin);
113     }
114     else
115     {
116         System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin);
117         buffer = newbuffer;
118
119         System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin);
120         bufline = newbufline;
121
122         System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin);
123         bufcolumn = newbufcolumn;
124
125         bufpos -= tokenBegin;
126       }
127     }
128     catch (Throwable t)
129     {
130       throw new Error(t.getMessage());
131     }
132
133     available = (bufsize += 2048);
134     tokenBegin = 0;
135   }
136
137   protected void FillBuff() throws java.io.IOException
138   {
139     int i;
140     if (maxNextCharInd == 4096)
141       maxNextCharInd = nextCharInd = 0;
142
143     try {
144       if ((i = inputStream.read(nextCharBuf, maxNextCharInd,
145                                           4096 - maxNextCharInd)) == -1)
146       {
147         inputStream.close();
148         throw new java.io.IOException();
149       }
150       else
151          maxNextCharInd += i;
152       return;
153     }
154     catch(java.io.IOException e) {
155       if (bufpos != 0)
156       {
157         --bufpos;
158         backup(0);
159       }
160       else
161       {
162         bufline[bufpos] = line;
163         bufcolumn[bufpos] = column;
164       }
165       throw e;
166     }
167   }
168
169   protected char ReadByte() throws java.io.IOException
170   {
171     if (++nextCharInd >= maxNextCharInd)
172       FillBuff();
173
174     return nextCharBuf[nextCharInd];
175   }
176
177 /** @return starting character for token. */
178   public char BeginToken() throws java.io.IOException
179   {
180     if (inBuf > 0)
181     {
182       --inBuf;
183
184       if (++bufpos == bufsize)
185         bufpos = 0;
186
187       tokenBegin = bufpos;
188       return buffer[bufpos];
189     }
190
191     tokenBegin = 0;
192     bufpos = -1;
193
194     return readChar();
195   }
196
197   protected void AdjustBuffSize()
198   {
199     if (available == bufsize)
200     {
201       if (tokenBegin > 2048)
202       {
203         bufpos = 0;
204         available = tokenBegin;
205       }
206       else
207         ExpandBuff(false);
208     }
209     else if (available > tokenBegin)
210       available = bufsize;
211     else if ((tokenBegin - available) < 2048)
212       ExpandBuff(true);
213     else
214       available = tokenBegin;
215   }
216
217   protected void UpdateLineColumn(char c)
218   {
219     column++;
220
221     if (prevCharIsLF)
222     {
223       prevCharIsLF = false;
224       line += (column = 1);
225     }
226     else if (prevCharIsCR)
227     {
228       prevCharIsCR = false;
229       if (c == '\n')
230       {
231         prevCharIsLF = true;
232       }
233       else
234         line += (column = 1);
235     }
236
237     switch (c)
238     {
239       case '\r' :
240         prevCharIsCR = true;
241         break;
242       case '\n' :
243         prevCharIsLF = true;
244         break;
245       case '\t' :
246         column--;
247         column += (tabSize - (column % tabSize));
248         break;
249       default :
250         break;
251     }
252
253     bufline[bufpos] = line;
254     bufcolumn[bufpos] = column;
255   }
256
257 /** Read a character. */
258   public char readChar() throws java.io.IOException
259   {
260     if (inBuf > 0)
261     {
262       --inBuf;
263
264       if (++bufpos == bufsize)
265         bufpos = 0;
266
267       return buffer[bufpos];
268     }
269
270     char c;
271
272     if (++bufpos == available)
273       AdjustBuffSize();
274
275     if ((buffer[bufpos] = c = ReadByte()) == '\\')
276     {
277       if (trackLineColumn) { UpdateLineColumn(c); }
278
279       int backSlashCnt = 1;
280
281       for (;;) // Read all the backslashes
282       {
283         if (++bufpos == available)
284           AdjustBuffSize();
285
286         try
287         {
288           if ((buffer[bufpos] = c = ReadByte()) != '\\')
289           {
290             if (trackLineColumn) { UpdateLineColumn(c); }
291             // found a non-backslash char.
292             if ((c == 'u') && ((backSlashCnt & 1) == 1))
293             {
294               if (--bufpos < 0)
295                 bufpos = bufsize - 1;
296
297               break;
298             }
299
300             backup(backSlashCnt);
301             return '\\';
302           }
303         }
304         catch(java.io.IOException e)
305         {
306           // We are returning one backslash so we should only backup (count-1)
307           if (backSlashCnt > 1)
308             backup(backSlashCnt-1);
309
310           return '\\';
311         }
312
313         if (trackLineColumn) { UpdateLineColumn(c); }
314         backSlashCnt++;
315       }
316
317       // Here, we have seen an odd number of backslash's followed by a 'u'
318       try
319       {
320         while ((c = ReadByte()) == 'u')
321           ++column;
322
323         buffer[bufpos] = c = (char)(hexval(c) << 12 |
324                                     hexval(ReadByte()) << 8 |
325                                     hexval(ReadByte()) << 4 |
326                                     hexval(ReadByte()));
327
328         column += 4;
329       }
330       catch(java.io.IOException e)
331       {
332         throw new Error("Invalid escape character at line " + line +
333                                          " column " + column + ".");
334       }
335
336       if (backSlashCnt == 1)
337         return c;
338       else
339       {
340         backup(backSlashCnt - 1);
341         return '\\';
342       }
343     }
344     else
345     {
346       UpdateLineColumn(c);
347       return c;
348     }
349   }
350
351   @Deprecated
352   /**
353    * @deprecated
354    * @see #getEndColumn
355    */
356   public int getColumn() {
357     return bufcolumn[bufpos];
358   }
359
360   @Deprecated
361   /**
362    * @deprecated
363    * @see #getEndLine
364    */
365   public int getLine() {
366     return bufline[bufpos];
367   }
368
369 /** Get end column. */
370   public int getEndColumn() {
371     return bufcolumn[bufpos];
372   }
373
374 /** Get end line. */
375   public int getEndLine() {
376     return bufline[bufpos];
377   }
378
379 /** @return column of token start */
380   public int getBeginColumn() {
381     return bufcolumn[tokenBegin];
382   }
383
384 /** @return line number of token start */
385   public int getBeginLine() {
386     return bufline[tokenBegin];
387   }
388
389 /** Retreat. */
390   public void backup(int amount) {
391
392     inBuf += amount;
393     if ((bufpos -= amount) < 0)
394       bufpos += bufsize;
395   }
396
397 /** Constructor. */
398   public JavaCharStream(java.io.Reader dstream,
399                  int startline, int startcolumn, int buffersize)
400   {
401     inputStream = dstream;
402     line = startline;
403     column = startcolumn - 1;
404
405     available = bufsize = buffersize;
406     buffer = new char[buffersize];
407     bufline = new int[buffersize];
408     bufcolumn = new int[buffersize];
409     nextCharBuf = new char[4096];
410   }
411
412 /** Constructor. */
413   public JavaCharStream(java.io.Reader dstream,
414                                         int startline, int startcolumn)
415   {
416     this(dstream, startline, startcolumn, 4096);
417   }
418
419 /** Constructor. */
420   public JavaCharStream(java.io.Reader dstream)
421   {
422     this(dstream, 1, 1, 4096);
423   }
424 /** Reinitialise. */
425   public void ReInit(java.io.Reader dstream,
426                  int startline, int startcolumn, int buffersize)
427   {
428     inputStream = dstream;
429     line = startline;
430     column = startcolumn - 1;
431
432     if (buffer == null || buffersize != buffer.length)
433     {
434       available = bufsize = buffersize;
435       buffer = new char[buffersize];
436       bufline = new int[buffersize];
437       bufcolumn = new int[buffersize];
438       nextCharBuf = new char[4096];
439     }
440     prevCharIsLF = prevCharIsCR = false;
441     tokenBegin = inBuf = maxNextCharInd = 0;
442     nextCharInd = bufpos = -1;
443   }
444
445 /** Reinitialise. */
446   public void ReInit(java.io.Reader dstream,
447                                         int startline, int startcolumn)
448   {
449     ReInit(dstream, startline, startcolumn, 4096);
450   }
451
452 /** Reinitialise. */
453   public void ReInit(java.io.Reader dstream)
454   {
455     ReInit(dstream, 1, 1, 4096);
456   }
457 /** Constructor. */
458   public JavaCharStream(java.io.InputStream dstream, String encoding, int startline,
459   int startcolumn, int buffersize) throws java.io.UnsupportedEncodingException
460   {
461     this(encoding == null ? new java.io.InputStreamReader(dstream) : new java.io.InputStreamReader(dstream, encoding), startline, startcolumn, buffersize);
462   }
463
464 /** Constructor. */
465   public JavaCharStream(java.io.InputStream dstream, int startline,
466   int startcolumn, int buffersize)
467   {
468     this(new java.io.InputStreamReader(dstream), startline, startcolumn, 4096);
469   }
470
471 /** Constructor. */
472   public JavaCharStream(java.io.InputStream dstream, String encoding, int startline,
473                         int startcolumn) throws java.io.UnsupportedEncodingException
474   {
475     this(dstream, encoding, startline, startcolumn, 4096);
476   }
477
478 /** Constructor. */
479   public JavaCharStream(java.io.InputStream dstream, int startline,
480                         int startcolumn)
481   {
482     this(dstream, startline, startcolumn, 4096);
483   }
484
485 /** Constructor. */
486   public JavaCharStream(java.io.InputStream dstream, String encoding) throws java.io.UnsupportedEncodingException
487   {
488     this(dstream, encoding, 1, 1, 4096);
489   }
490
491 /** Constructor. */
492   public JavaCharStream(java.io.InputStream dstream)
493   {
494     this(dstream, 1, 1, 4096);
495   }
496
497 /** Reinitialise. */
498   public void ReInit(java.io.InputStream dstream, String encoding, int startline,
499   int startcolumn, int buffersize) throws java.io.UnsupportedEncodingException
500   {
501     ReInit(encoding == null ? new java.io.InputStreamReader(dstream) : new java.io.InputStreamReader(dstream, encoding), startline, startcolumn, buffersize);
502   }
503
504 /** Reinitialise. */
505   public void ReInit(java.io.InputStream dstream, int startline,
506   int startcolumn, int buffersize)
507   {
508     ReInit(new java.io.InputStreamReader(dstream), startline, startcolumn, buffersize);
509   }
510 /** Reinitialise. */
511   public void ReInit(java.io.InputStream dstream, String encoding, int startline,
512                      int startcolumn) throws java.io.UnsupportedEncodingException
513   {
514     ReInit(dstream, encoding, startline, startcolumn, 4096);
515   }
516 /** Reinitialise. */
517   public void ReInit(java.io.InputStream dstream, int startline,
518                      int startcolumn)
519   {
520     ReInit(dstream, startline, startcolumn, 4096);
521   }
522 /** Reinitialise. */
523   public void ReInit(java.io.InputStream dstream, String encoding) throws java.io.UnsupportedEncodingException
524   {
525     ReInit(dstream, encoding, 1, 1, 4096);
526   }
527
528 /** Reinitialise. */
529   public void ReInit(java.io.InputStream dstream)
530   {
531     ReInit(dstream, 1, 1, 4096);
532   }
533
534   /** @return token image as String */
535   public String GetImage()
536   {
537     if (bufpos >= tokenBegin)
538       return new String(buffer, tokenBegin, bufpos - tokenBegin + 1);
539     else
540       return new String(buffer, tokenBegin, bufsize - tokenBegin) +
541                               new String(buffer, 0, bufpos + 1);
542   }
543
544   /** @return suffix */
545   public char[] GetSuffix(int len)
546   {
547     char[] ret = new char[len];
548
549     if ((bufpos + 1) >= len)
550       System.arraycopy(buffer, bufpos - len + 1, ret, 0, len);
551     else
552     {
553       System.arraycopy(buffer, bufsize - (len - bufpos - 1), ret, 0,
554                                                         len - bufpos - 1);
555       System.arraycopy(buffer, 0, ret, len - bufpos - 1, bufpos + 1);
556     }
557
558     return ret;
559   }
560
561   /** Set buffers back to null when finished. */
562   public void Done()
563   {
564     nextCharBuf = null;
565     buffer = null;
566     bufline = null;
567     bufcolumn = null;
568   }
569
570   /**
571    * Method to adjust line and column numbers for the start of a token.
572    */
573   public void adjustBeginLineColumn(int newLine, int newCol)
574   {
575     int start = tokenBegin;
576     int len;
577
578     if (bufpos >= tokenBegin)
579     {
580       len = bufpos - tokenBegin + inBuf + 1;
581     }
582     else
583     {
584       len = bufsize - tokenBegin + bufpos + 1 + inBuf;
585     }
586
587     int i = 0, j = 0, k = 0;
588     int nextColDiff = 0, columnDiff = 0;
589
590     while (i < len && bufline[j = start % bufsize] == bufline[k = ++start % bufsize])
591     {
592       bufline[j] = newLine;
593       nextColDiff = columnDiff + bufcolumn[k] - bufcolumn[j];
594       bufcolumn[j] = newCol + columnDiff;
595       columnDiff = nextColDiff;
596       i++;
597     }
598
599     if (i < len)
600     {
601       bufline[j] = newLine++;
602       bufcolumn[j] = newCol + columnDiff;
603
604       while (i++ < len)
605       {
606         if (bufline[j = start % bufsize] != bufline[++start % bufsize])
607           bufline[j] = newLine++;
608         else
609           bufline[j] = newLine;
610       }
611     }
612
613     line = bufline[j];
614     column = bufcolumn[j];
615   }
616   boolean getTrackLineColumn() { return trackLineColumn; }
617   void setTrackLineColumn(boolean trackLineColumn) { this.trackLineColumn = trackLineColumn; }
618
619 }
620 /* JavaCC - OriginalChecksum=ea8340461dd4116176ea0d0d5c055d04 (do not edit this line) */