View Javadoc

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