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