KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > pmd > cpd > cppast > SimpleCharStream


1 /* Generated By:JavaCC: Do not edit this line. SimpleCharStream.java Version 4.0 */
2 package net.sourceforge.pmd.cpd.cppast;
3
4 /**
5  * An implementation of interface CharStream, where the stream is assumed to
6  * contain only ASCII characters (without unicode processing).
7  */

8
9 public class SimpleCharStream
10 {
11   public static final boolean staticFlag = true;
12   static int bufsize;
13   static int available;
14   static int tokenBegin;
15   static public int bufpos = -1;
16   static protected int bufline[];
17   static protected int bufcolumn[];
18
19   static protected int column = 0;
20   static protected int line = 1;
21
22   static protected boolean prevCharIsCR = false;
23   static protected boolean prevCharIsLF = false;
24
25   static protected java.io.Reader JavaDoc inputStream;
26
27   static protected char[] buffer;
28   static protected int maxNextCharInd = 0;
29   static protected int inBuf = 0;
30   static protected int tabSize = 8;
31
32   static protected void setTabSize(int i) { tabSize = i; }
33   static protected int getTabSize(int i) { return tabSize; }
34
35
36   static protected void ExpandBuff(boolean wrapAround)
37   {
38      char[] newbuffer = new char[bufsize + 2048];
39      int newbufline[] = new int[bufsize + 2048];
40      int newbufcolumn[] = new int[bufsize + 2048];
41
42      try
43      {
44         if (wrapAround)
45         {
46            System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin);
47            System.arraycopy(buffer, 0, newbuffer,
48                                              bufsize - tokenBegin, bufpos);
49            buffer = newbuffer;
50
51            System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin);
52            System.arraycopy(bufline, 0, newbufline, bufsize - tokenBegin, bufpos);
53            bufline = newbufline;
54
55            System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin);
56            System.arraycopy(bufcolumn, 0, newbufcolumn, bufsize - tokenBegin, bufpos);
57            bufcolumn = newbufcolumn;
58
59            maxNextCharInd = (bufpos += (bufsize - tokenBegin));
60         }
61         else
62         {
63            System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin);
64            buffer = newbuffer;
65
66            System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin);
67            bufline = newbufline;
68
69            System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin);
70            bufcolumn = newbufcolumn;
71
72            maxNextCharInd = (bufpos -= tokenBegin);
73         }
74      }
75      catch (Throwable JavaDoc t)
76      {
77         throw new Error JavaDoc(t.getMessage());
78      }
79
80
81      bufsize += 2048;
82      available = bufsize;
83      tokenBegin = 0;
84   }
85
86   static protected void FillBuff() throws java.io.IOException JavaDoc
87   {
88      if (maxNextCharInd == available)
89      {
90         if (available == bufsize)
91         {
92            if (tokenBegin > 2048)
93            {
94               bufpos = maxNextCharInd = 0;
95               available = tokenBegin;
96            }
97            else if (tokenBegin < 0)
98               bufpos = maxNextCharInd = 0;
99            else
100               ExpandBuff(false);
101         }
102         else if (available > tokenBegin)
103            available = bufsize;
104         else if ((tokenBegin - available) < 2048)
105            ExpandBuff(true);
106         else
107            available = tokenBegin;
108      }
109
110      int i;
111      try {
112         if ((i = inputStream.read(buffer, maxNextCharInd,
113                                     available - maxNextCharInd)) == -1)
114         {
115            inputStream.close();
116            throw new java.io.IOException JavaDoc();
117         }
118         else
119            maxNextCharInd += i;
120         return;
121      }
122      catch(java.io.IOException JavaDoc e) {
123         --bufpos;
124         backup(0);
125         if (tokenBegin == -1)
126            tokenBegin = bufpos;
127         throw e;
128      }
129   }
130
131   static public char BeginToken() throws java.io.IOException JavaDoc
132   {
133      tokenBegin = -1;
134      char c = readChar();
135      tokenBegin = bufpos;
136
137      return c;
138   }
139
140   static protected void UpdateLineColumn(char c)
141   {
142      column++;
143
144      if (prevCharIsLF)
145      {
146         prevCharIsLF = false;
147         line += (column = 1);
148      }
149      else if (prevCharIsCR)
150      {
151         prevCharIsCR = false;
152         if (c == '\n')
153         {
154            prevCharIsLF = true;
155         }
156         else
157            line += (column = 1);
158      }
159
160      switch (c)
161      {
162         case '\r' :
163            prevCharIsCR = true;
164            break;
165         case '\n' :
166            prevCharIsLF = true;
167            break;
168         case '\t' :
169            column--;
170            column += (tabSize - (column % tabSize));
171            break;
172         default :
173            break;
174      }
175
176      bufline[bufpos] = line;
177      bufcolumn[bufpos] = column;
178   }
179
180   static public char readChar() throws java.io.IOException JavaDoc
181   {
182      if (inBuf > 0)
183      {
184         --inBuf;
185
186         if (++bufpos == bufsize)
187            bufpos = 0;
188
189         return buffer[bufpos];
190      }
191
192      if (++bufpos >= maxNextCharInd)
193         FillBuff();
194
195      char c = buffer[bufpos];
196
197      UpdateLineColumn(c);
198      return (c);
199   }
200
201   /**
202    * @deprecated
203    * @see #getEndColumn
204    */

205
206   static public int getColumn() {
207      return bufcolumn[bufpos];
208   }
209
210   /**
211    * @deprecated
212    * @see #getEndLine
213    */

214
215   static public int getLine() {
216      return bufline[bufpos];
217   }
218
219   static public int getEndColumn() {
220      return bufcolumn[bufpos];
221   }
222
223   static public int getEndLine() {
224      return bufline[bufpos];
225   }
226
227   static public int getBeginColumn() {
228      return bufcolumn[tokenBegin];
229   }
230
231   static public int getBeginLine() {
232      return bufline[tokenBegin];
233   }
234
235   static public void backup(int amount) {
236
237     inBuf += amount;
238     if ((bufpos -= amount) < 0)
239        bufpos += bufsize;
240   }
241
242   public SimpleCharStream(java.io.Reader JavaDoc dstream, int startline,
243   int startcolumn, int buffersize)
244   {
245     if (inputStream != null)
246        throw new Error JavaDoc("\n ERROR: Second call to the constructor of a static SimpleCharStream. You must\n" +
247        " either use ReInit() or set the JavaCC option STATIC to false\n" +
248        " during the generation of this class.");
249     inputStream = dstream;
250     line = startline;
251     column = startcolumn - 1;
252
253     available = bufsize = buffersize;
254     buffer = new char[buffersize];
255     bufline = new int[buffersize];
256     bufcolumn = new int[buffersize];
257   }
258
259   public SimpleCharStream(java.io.Reader JavaDoc dstream, int startline,
260                           int startcolumn)
261   {
262      this(dstream, startline, startcolumn, 4096);
263   }
264
265   public SimpleCharStream(java.io.Reader JavaDoc dstream)
266   {
267      this(dstream, 1, 1, 4096);
268   }
269   public void ReInit(java.io.Reader JavaDoc dstream, int startline,
270   int startcolumn, int buffersize)
271   {
272     inputStream = dstream;
273     line = startline;
274     column = startcolumn - 1;
275
276     if (buffer == null || buffersize != buffer.length)
277     {
278       available = bufsize = buffersize;
279       buffer = new char[buffersize];
280       bufline = new int[buffersize];
281       bufcolumn = new int[buffersize];
282     }
283     prevCharIsLF = prevCharIsCR = false;
284     tokenBegin = inBuf = maxNextCharInd = 0;
285     bufpos = -1;
286   }
287
288   public void ReInit(java.io.Reader JavaDoc dstream, int startline,
289                      int startcolumn)
290   {
291      ReInit(dstream, startline, startcolumn, 4096);
292   }
293
294   public void ReInit(java.io.Reader JavaDoc dstream)
295   {
296      ReInit(dstream, 1, 1, 4096);
297   }
298   public SimpleCharStream(java.io.InputStream JavaDoc dstream, String JavaDoc encoding, int startline,
299   int startcolumn, int buffersize) throws java.io.UnsupportedEncodingException JavaDoc
300   {
301      this(encoding == null ? new java.io.InputStreamReader JavaDoc(dstream) : new java.io.InputStreamReader JavaDoc(dstream, encoding), startline, startcolumn, buffersize);
302   }
303
304   public SimpleCharStream(java.io.InputStream JavaDoc dstream, int startline,
305   int startcolumn, int buffersize)
306   {
307      this(new java.io.InputStreamReader JavaDoc(dstream), startline, startcolumn, buffersize);
308   }
309
310   public SimpleCharStream(java.io.InputStream JavaDoc dstream, String JavaDoc encoding, int startline,
311                           int startcolumn) throws java.io.UnsupportedEncodingException JavaDoc
312   {
313      this(dstream, encoding, startline, startcolumn, 4096);
314   }
315
316   public SimpleCharStream(java.io.InputStream JavaDoc dstream, int startline,
317                           int startcolumn)
318   {
319      this(dstream, startline, startcolumn, 4096);
320   }
321
322   public SimpleCharStream(java.io.InputStream JavaDoc dstream, String JavaDoc encoding) throws java.io.UnsupportedEncodingException JavaDoc
323   {
324      this(dstream, encoding, 1, 1, 4096);
325   }
326
327   public SimpleCharStream(java.io.InputStream JavaDoc dstream)
328   {
329      this(dstream, 1, 1, 4096);
330   }
331
332   public void ReInit(java.io.InputStream JavaDoc dstream, String JavaDoc encoding, int startline,
333                           int startcolumn, int buffersize) throws java.io.UnsupportedEncodingException JavaDoc
334   {
335      ReInit(encoding == null ? new java.io.InputStreamReader JavaDoc(dstream) : new java.io.InputStreamReader JavaDoc(dstream, encoding), startline, startcolumn, buffersize);
336   }
337
338   public void ReInit(java.io.InputStream JavaDoc dstream, int startline,
339                           int startcolumn, int buffersize)
340   {
341      ReInit(new java.io.InputStreamReader JavaDoc(dstream), startline, startcolumn, buffersize);
342   }
343
344   public void ReInit(java.io.InputStream JavaDoc dstream, String JavaDoc encoding) throws java.io.UnsupportedEncodingException JavaDoc
345   {
346      ReInit(dstream, encoding, 1, 1, 4096);
347   }
348
349   public void ReInit(java.io.InputStream JavaDoc dstream)
350   {
351      ReInit(dstream, 1, 1, 4096);
352   }
353   public void ReInit(java.io.InputStream JavaDoc dstream, String JavaDoc encoding, int startline,
354                      int startcolumn) throws java.io.UnsupportedEncodingException JavaDoc
355   {
356      ReInit(dstream, encoding, startline, startcolumn, 4096);
357   }
358   public void ReInit(java.io.InputStream JavaDoc dstream, int startline,
359                      int startcolumn)
360   {
361      ReInit(dstream, startline, startcolumn, 4096);
362   }
363   static public String JavaDoc GetImage()
364   {
365      if (bufpos >= tokenBegin)
366         return new String JavaDoc(buffer, tokenBegin, bufpos - tokenBegin + 1);
367      else
368         return new String JavaDoc(buffer, tokenBegin, bufsize - tokenBegin) +
369                               new String JavaDoc(buffer, 0, bufpos + 1);
370   }
371
372   static public char[] GetSuffix(int len)
373   {
374      char[] ret = new char[len];
375
376      if ((bufpos + 1) >= len)
377         System.arraycopy(buffer, bufpos - len + 1, ret, 0, len);
378      else
379      {
380         System.arraycopy(buffer, bufsize - (len - bufpos - 1), ret, 0,
381                                                           len - bufpos - 1);
382         System.arraycopy(buffer, 0, ret, len - bufpos - 1, bufpos + 1);
383      }
384
385      return ret;
386   }
387
388   static public void Done()
389   {
390      buffer = null;
391      bufline = null;
392      bufcolumn = null;
393   }
394
395   /**
396    * Method to adjust line and column numbers for the start of a token.
397    */

398   static public void adjustBeginLineColumn(int newLine, int newCol)
399   {
400      int start = tokenBegin;
401      int len;
402
403      if (bufpos >= tokenBegin)
404      {
405         len = bufpos - tokenBegin + inBuf + 1;
406      }
407      else
408      {
409         len = bufsize - tokenBegin + bufpos + 1 + inBuf;
410      }
411
412      int i = 0, j = 0, k = 0;
413      int nextColDiff = 0, columnDiff = 0;
414
415      while (i < len &&
416             bufline[j = start % bufsize] == bufline[k = ++start % bufsize])
417      {
418         bufline[j] = newLine;
419         nextColDiff = columnDiff + bufcolumn[k] - bufcolumn[j];
420         bufcolumn[j] = newCol + columnDiff;
421         columnDiff = nextColDiff;
422         i++;
423      }
424
425      if (i < len)
426      {
427         bufline[j] = newLine++;
428         bufcolumn[j] = newCol + columnDiff;
429
430         while (i++ < len)
431         {
432            if (bufline[j = start % bufsize] != bufline[++start % bufsize])
433               bufline[j] = newLine++;
434            else
435               bufline[j] = newLine;
436         }
437      }
438
439      line = bufline[j];
440      column = bufcolumn[j];
441   }
442
443 }
444
Popular Tags