1 /* Generated By:JavaCC: Do not edit this line. SimpleCharStream.java Version 3.0 */
2 /**
3  * An implementation of interface CharStream, where the stream is assumed to
4  * contain only ASCII characters (without unicode processing).
5  */
6 
7 public class SimpleCharStream
8 {
9   public static final boolean staticFlag = false;
10   int bufsize;
11   int available;
12   int tokenBegin;
13   public int bufpos = -1;
14   protected int bufline[];
15   protected int bufcolumn[];
16 
17   protected int column = 0;
18   protected int line = 1;
19 
20   protected boolean prevCharIsCR = false;
21   protected boolean prevCharIsLF = false;
22 
23   protected java.io.Reader inputStream;
24 
25   protected char[] buffer;
26   protected int maxNextCharInd = 0;
27   protected int inBuf = 0;
28 
ExpandBuff(boolean wrapAround)29   protected void ExpandBuff(boolean wrapAround)
30   {
31      char[] newbuffer = new char[bufsize + 2048];
32      int newbufline[] = new int[bufsize + 2048];
33      int newbufcolumn[] = new int[bufsize + 2048];
34 
35      try
36      {
37         if (wrapAround)
38         {
39            System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin);
40            System.arraycopy(buffer, 0, newbuffer,
41                                              bufsize - tokenBegin, bufpos);
42            buffer = newbuffer;
43 
44            System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin);
45            System.arraycopy(bufline, 0, newbufline, bufsize - tokenBegin, bufpos);
46            bufline = newbufline;
47 
48            System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin);
49            System.arraycopy(bufcolumn, 0, newbufcolumn, bufsize - tokenBegin, bufpos);
50            bufcolumn = newbufcolumn;
51 
52            maxNextCharInd = (bufpos += (bufsize - tokenBegin));
53         }
54         else
55         {
56            System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin);
57            buffer = newbuffer;
58 
59            System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin);
60            bufline = newbufline;
61 
62            System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin);
63            bufcolumn = newbufcolumn;
64 
65            maxNextCharInd = (bufpos -= tokenBegin);
66         }
67      }
68      catch (Throwable t)
69      {
70         throw new Error(t.getMessage());
71      }
72 
73 
74      bufsize += 2048;
75      available = bufsize;
76      tokenBegin = 0;
77   }
78 
FillBuff()79   protected void FillBuff() throws java.io.IOException
80   {
81      if (maxNextCharInd == available)
82      {
83         if (available == bufsize)
84         {
85            if (tokenBegin > 2048)
86            {
87               bufpos = maxNextCharInd = 0;
88               available = tokenBegin;
89            }
90            else if (tokenBegin < 0)
91               bufpos = maxNextCharInd = 0;
92            else
93               ExpandBuff(false);
94         }
95         else if (available > tokenBegin)
96            available = bufsize;
97         else if ((tokenBegin - available) < 2048)
98            ExpandBuff(true);
99         else
100            available = tokenBegin;
101      }
102 
103      int i;
104      try {
105         if ((i = inputStream.read(buffer, maxNextCharInd,
106                                     available - maxNextCharInd)) == -1)
107         {
108            inputStream.close();
109            throw new java.io.IOException();
110         }
111         else
112            maxNextCharInd += i;
113         return;
114      }
115      catch(java.io.IOException e) {
116         --bufpos;
117         backup(0);
118         if (tokenBegin == -1)
119            tokenBegin = bufpos;
120         throw e;
121      }
122   }
123 
BeginToken()124   public char BeginToken() throws java.io.IOException
125   {
126      tokenBegin = -1;
127      char c = readChar();
128      tokenBegin = bufpos;
129 
130      return c;
131   }
132 
UpdateLineColumn(char c)133   protected void UpdateLineColumn(char c)
134   {
135      column++;
136 
137      if (prevCharIsLF)
138      {
139         prevCharIsLF = false;
140         line += (column = 1);
141      }
142      else if (prevCharIsCR)
143      {
144         prevCharIsCR = false;
145         if (c == '\n')
146         {
147            prevCharIsLF = true;
148         }
149         else
150            line += (column = 1);
151      }
152 
153      switch (c)
154      {
155         case '\r' :
156            prevCharIsCR = true;
157            break;
158         case '\n' :
159            prevCharIsLF = true;
160            break;
161         case '\t' :
162            column--;
163            column += (8 - (column & 07));
164            break;
165         default :
166            break;
167      }
168 
169      bufline[bufpos] = line;
170      bufcolumn[bufpos] = column;
171   }
172 
readChar()173   public char readChar() throws java.io.IOException
174   {
175      if (inBuf > 0)
176      {
177         --inBuf;
178 
179         if (++bufpos == bufsize)
180            bufpos = 0;
181 
182         return buffer[bufpos];
183      }
184 
185      if (++bufpos >= maxNextCharInd)
186         FillBuff();
187 
188      char c = buffer[bufpos];
189 
190      UpdateLineColumn(c);
191      return (c);
192   }
193 
194   /**
195    * @deprecated
196    * @see #getEndColumn
197    */
198 
getColumn()199   public int getColumn() {
200      return bufcolumn[bufpos];
201   }
202 
203   /**
204    * @deprecated
205    * @see #getEndLine
206    */
207 
getLine()208   public int getLine() {
209      return bufline[bufpos];
210   }
211 
getEndColumn()212   public int getEndColumn() {
213      return bufcolumn[bufpos];
214   }
215 
getEndLine()216   public int getEndLine() {
217      return bufline[bufpos];
218   }
219 
getBeginColumn()220   public int getBeginColumn() {
221      return bufcolumn[tokenBegin];
222   }
223 
getBeginLine()224   public int getBeginLine() {
225      return bufline[tokenBegin];
226   }
227 
backup(int amount)228   public void backup(int amount) {
229 
230     inBuf += amount;
231     if ((bufpos -= amount) < 0)
232        bufpos += bufsize;
233   }
234 
SimpleCharStream(java.io.Reader dstream, int startline, int startcolumn, int buffersize)235   public SimpleCharStream(java.io.Reader dstream, int startline,
236   int startcolumn, int buffersize)
237   {
238     inputStream = dstream;
239     line = startline;
240     column = startcolumn - 1;
241 
242     available = bufsize = buffersize;
243     buffer = new char[buffersize];
244     bufline = new int[buffersize];
245     bufcolumn = new int[buffersize];
246   }
247 
SimpleCharStream(java.io.Reader dstream, int startline, int startcolumn)248   public SimpleCharStream(java.io.Reader dstream, int startline,
249                                                            int startcolumn)
250   {
251      this(dstream, startline, startcolumn, 4096);
252   }
253 
SimpleCharStream(java.io.Reader dstream)254   public SimpleCharStream(java.io.Reader dstream)
255   {
256      this(dstream, 1, 1, 4096);
257   }
ReInit(java.io.Reader dstream, int startline, int startcolumn, int buffersize)258   public void ReInit(java.io.Reader dstream, int startline,
259   int startcolumn, int buffersize)
260   {
261     inputStream = dstream;
262     line = startline;
263     column = startcolumn - 1;
264 
265     if (buffer == null || buffersize != buffer.length)
266     {
267       available = bufsize = buffersize;
268       buffer = new char[buffersize];
269       bufline = new int[buffersize];
270       bufcolumn = new int[buffersize];
271     }
272     prevCharIsLF = prevCharIsCR = false;
273     tokenBegin = inBuf = maxNextCharInd = 0;
274     bufpos = -1;
275   }
276 
ReInit(java.io.Reader dstream, int startline, int startcolumn)277   public void ReInit(java.io.Reader dstream, int startline,
278                                                            int startcolumn)
279   {
280      ReInit(dstream, startline, startcolumn, 4096);
281   }
282 
ReInit(java.io.Reader dstream)283   public void ReInit(java.io.Reader dstream)
284   {
285      ReInit(dstream, 1, 1, 4096);
286   }
SimpleCharStream(java.io.InputStream dstream, int startline, int startcolumn, int buffersize)287   public SimpleCharStream(java.io.InputStream dstream, int startline,
288   int startcolumn, int buffersize)
289   {
290      this(new java.io.InputStreamReader(dstream), startline, startcolumn, 4096);
291   }
292 
SimpleCharStream(java.io.InputStream dstream, int startline, int startcolumn)293   public SimpleCharStream(java.io.InputStream dstream, int startline,
294                                                            int startcolumn)
295   {
296      this(dstream, startline, startcolumn, 4096);
297   }
298 
SimpleCharStream(java.io.InputStream dstream)299   public SimpleCharStream(java.io.InputStream dstream)
300   {
301      this(dstream, 1, 1, 4096);
302   }
303 
ReInit(java.io.InputStream dstream, int startline, int startcolumn, int buffersize)304   public void ReInit(java.io.InputStream dstream, int startline,
305                           int startcolumn, int buffersize)
306   {
307      ReInit(new java.io.InputStreamReader(dstream), startline, startcolumn, 4096);
308   }
309 
ReInit(java.io.InputStream dstream)310   public void ReInit(java.io.InputStream dstream)
311   {
312      ReInit(dstream, 1, 1, 4096);
313   }
ReInit(java.io.InputStream dstream, int startline, int startcolumn)314   public void ReInit(java.io.InputStream dstream, int startline,
315                                                            int startcolumn)
316   {
317      ReInit(dstream, startline, startcolumn, 4096);
318   }
GetImage()319   public String GetImage()
320   {
321      if (bufpos >= tokenBegin)
322         return new String(buffer, tokenBegin, bufpos - tokenBegin + 1);
323      else
324         return new String(buffer, tokenBegin, bufsize - tokenBegin) +
325                               new String(buffer, 0, bufpos + 1);
326   }
327 
GetSuffix(int len)328   public char[] GetSuffix(int len)
329   {
330      char[] ret = new char[len];
331 
332      if ((bufpos + 1) >= len)
333         System.arraycopy(buffer, bufpos - len + 1, ret, 0, len);
334      else
335      {
336         System.arraycopy(buffer, bufsize - (len - bufpos - 1), ret, 0,
337                                                           len - bufpos - 1);
338         System.arraycopy(buffer, 0, ret, len - bufpos - 1, bufpos + 1);
339      }
340 
341      return ret;
342   }
343 
Done()344   public void Done()
345   {
346      buffer = null;
347      bufline = null;
348      bufcolumn = null;
349   }
350 
351   /**
352    * Method to adjust line and column numbers for the start of a token.
353    */
adjustBeginLineColumn(int newLine, int newCol)354   public void adjustBeginLineColumn(int newLine, int newCol)
355   {
356      int start = tokenBegin;
357      int len;
358 
359      if (bufpos >= tokenBegin)
360      {
361         len = bufpos - tokenBegin + inBuf + 1;
362      }
363      else
364      {
365         len = bufsize - tokenBegin + bufpos + 1 + inBuf;
366      }
367 
368      int i = 0, j = 0, k = 0;
369      int nextColDiff = 0, columnDiff = 0;
370 
371      while (i < len &&
372             bufline[j = start % bufsize] == bufline[k = ++start % bufsize])
373      {
374         bufline[j] = newLine;
375         nextColDiff = columnDiff + bufcolumn[k] - bufcolumn[j];
376         bufcolumn[j] = newCol + columnDiff;
377         columnDiff = nextColDiff;
378         i++;
379      }
380 
381      if (i < len)
382      {
383         bufline[j] = newLine++;
384         bufcolumn[j] = newCol + columnDiff;
385 
386         while (i++ < len)
387         {
388            if (bufline[j = start % bufsize] != bufline[++start % bufsize])
389               bufline[j] = newLine++;
390            else
391               bufline[j] = newLine;
392         }
393      }
394 
395      line = bufline[j];
396      column = bufcolumn[j];
397   }
398 
399 }
400