001 /*
002 * Sonar, open source software quality management tool.
003 * Copyright (C) 2008-2011 SonarSource
004 * mailto:contact AT sonarsource DOT com
005 *
006 * Sonar is free software; you can redistribute it and/or
007 * modify it under the terms of the GNU Lesser General Public
008 * License as published by the Free Software Foundation; either
009 * version 3 of the License, or (at your option) any later version.
010 *
011 * Sonar is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014 * Lesser General Public License for more details.
015 *
016 * You should have received a copy of the GNU Lesser General Public
017 * License along with Sonar; if not, write to the Free Software
018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
019 */
020
021 /* Generated By:JavaCC: Do not edit this line. SimpleCharStream.java Version 4.1 */
022 /* JavaCCOptions:STATIC=true */
023 package net.sourceforge.pmd.cpd.cppast;
024
025 /**
026 * An implementation of interface CharStream, where the stream is assumed to
027 * contain only ASCII characters (without unicode processing).
028 */
029
030 public class SimpleCharStream
031 {
032 /** Whether parser is static. */
033 public static final boolean staticFlag = true;
034 static int bufsize;
035 static int available;
036 static int tokenBegin;
037 /** Position in buffer. */
038 static public int bufpos = -1;
039 static protected int bufline[];
040 static protected int bufcolumn[];
041
042 static protected int column = 0;
043 static protected int line = 1;
044
045 static protected boolean prevCharIsCR = false;
046 static protected boolean prevCharIsLF = false;
047
048 static protected java.io.Reader inputStream;
049
050 static protected char[] buffer;
051 static protected int maxNextCharInd = 0;
052 static protected int inBuf = 0;
053 static protected int tabSize = 8;
054
055 static protected void setTabSize(int i) { tabSize = i; }
056 static protected int getTabSize(int i) { return tabSize; }
057
058
059 static protected void ExpandBuff(boolean wrapAround)
060 {
061 char[] newbuffer = new char[bufsize + 2048];
062 int newbufline[] = new int[bufsize + 2048];
063 int newbufcolumn[] = new int[bufsize + 2048];
064
065 try
066 {
067 if (wrapAround)
068 {
069 System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin);
070 System.arraycopy(buffer, 0, newbuffer,
071 bufsize - tokenBegin, bufpos);
072 buffer = newbuffer;
073
074 System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin);
075 System.arraycopy(bufline, 0, newbufline, bufsize - tokenBegin, bufpos);
076 bufline = newbufline;
077
078 System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin);
079 System.arraycopy(bufcolumn, 0, newbufcolumn, bufsize - tokenBegin, bufpos);
080 bufcolumn = newbufcolumn;
081
082 maxNextCharInd = (bufpos += (bufsize - tokenBegin));
083 }
084 else
085 {
086 System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin);
087 buffer = newbuffer;
088
089 System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin);
090 bufline = newbufline;
091
092 System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin);
093 bufcolumn = newbufcolumn;
094
095 maxNextCharInd = (bufpos -= tokenBegin);
096 }
097 }
098 catch (Throwable t)
099 {
100 throw new Error(t.getMessage());
101 }
102
103
104 bufsize += 2048;
105 available = bufsize;
106 tokenBegin = 0;
107 }
108
109 static protected void FillBuff() throws java.io.IOException
110 {
111 if (maxNextCharInd == available)
112 {
113 if (available == bufsize)
114 {
115 if (tokenBegin > 2048)
116 {
117 bufpos = maxNextCharInd = 0;
118 available = tokenBegin;
119 }
120 else if (tokenBegin < 0)
121 bufpos = maxNextCharInd = 0;
122 else
123 ExpandBuff(false);
124 }
125 else if (available > tokenBegin)
126 available = bufsize;
127 else if ((tokenBegin - available) < 2048)
128 ExpandBuff(true);
129 else
130 available = tokenBegin;
131 }
132
133 int i;
134 try {
135 if ((i = inputStream.read(buffer, maxNextCharInd,
136 available - maxNextCharInd)) == -1)
137 {
138 inputStream.close();
139 throw new java.io.IOException();
140 }
141 else
142 maxNextCharInd += i;
143 return;
144 }
145 catch(java.io.IOException e) {
146 --bufpos;
147 backup(0);
148 if (tokenBegin == -1)
149 tokenBegin = bufpos;
150 throw e;
151 }
152 }
153
154 /** Start. */
155 static public char BeginToken() throws java.io.IOException
156 {
157 tokenBegin = -1;
158 char c = readChar();
159 tokenBegin = bufpos;
160
161 return c;
162 }
163
164 static protected void UpdateLineColumn(char c)
165 {
166 column++;
167
168 if (prevCharIsLF)
169 {
170 prevCharIsLF = false;
171 line += (column = 1);
172 }
173 else if (prevCharIsCR)
174 {
175 prevCharIsCR = false;
176 if (c == '\n')
177 {
178 prevCharIsLF = true;
179 }
180 else
181 line += (column = 1);
182 }
183
184 switch (c)
185 {
186 case '\r' :
187 prevCharIsCR = true;
188 break;
189 case '\n' :
190 prevCharIsLF = true;
191 break;
192 case '\t' :
193 column--;
194 column += (tabSize - (column % tabSize));
195 break;
196 default :
197 break;
198 }
199
200 bufline[bufpos] = line;
201 bufcolumn[bufpos] = column;
202 }
203
204 /** Read a character. */
205 static public char readChar() throws java.io.IOException
206 {
207 if (inBuf > 0)
208 {
209 --inBuf;
210
211 if (++bufpos == bufsize)
212 bufpos = 0;
213
214 return buffer[bufpos];
215 }
216
217 if (++bufpos >= maxNextCharInd)
218 FillBuff();
219
220 char c = buffer[bufpos];
221
222 UpdateLineColumn(c);
223 return c;
224 }
225
226 /**
227 * @deprecated
228 * @see #getEndColumn
229 */
230
231 static public int getColumn() {
232 return bufcolumn[bufpos];
233 }
234
235 /**
236 * @deprecated
237 * @see #getEndLine
238 */
239
240 static public int getLine() {
241 return bufline[bufpos];
242 }
243
244 /** Get token end column number. */
245 static public int getEndColumn() {
246 return bufcolumn[bufpos];
247 }
248
249 /** Get token end line number. */
250 static public int getEndLine() {
251 return bufline[bufpos];
252 }
253
254 /** Get token beginning column number. */
255 static public int getBeginColumn() {
256 return bufcolumn[tokenBegin];
257 }
258
259 /** Get token beginning line number. */
260 static public int getBeginLine() {
261 return bufline[tokenBegin];
262 }
263
264 /** Backup a number of characters. */
265 static public void backup(int amount) {
266
267 inBuf += amount;
268 if ((bufpos -= amount) < 0)
269 bufpos += bufsize;
270 }
271
272 /** Constructor. */
273 public SimpleCharStream(java.io.Reader dstream, int startline,
274 int startcolumn, int buffersize)
275 {
276 if (inputStream != null)
277 throw new Error("\n ERROR: Second call to the constructor of a static SimpleCharStream.\n" +
278 " You must either use ReInit() or set the JavaCC option STATIC to false\n" +
279 " during the generation of this class.");
280 inputStream = dstream;
281 line = startline;
282 column = startcolumn - 1;
283
284 available = bufsize = buffersize;
285 buffer = new char[buffersize];
286 bufline = new int[buffersize];
287 bufcolumn = new int[buffersize];
288 }
289
290 /** Constructor. */
291 public SimpleCharStream(java.io.Reader dstream, int startline,
292 int startcolumn)
293 {
294 this(dstream, startline, startcolumn, 4096);
295 }
296
297 /** Constructor. */
298 public SimpleCharStream(java.io.Reader dstream)
299 {
300 this(dstream, 1, 1, 4096);
301 }
302
303 /** Reinitialise. */
304 public void ReInit(java.io.Reader dstream, int startline,
305 int startcolumn, int buffersize)
306 {
307 inputStream = dstream;
308 line = startline;
309 column = startcolumn - 1;
310
311 if (buffer == null || buffersize != buffer.length)
312 {
313 available = bufsize = buffersize;
314 buffer = new char[buffersize];
315 bufline = new int[buffersize];
316 bufcolumn = new int[buffersize];
317 }
318 prevCharIsLF = prevCharIsCR = false;
319 tokenBegin = inBuf = maxNextCharInd = 0;
320 bufpos = -1;
321 }
322
323 /** Reinitialise. */
324 public void ReInit(java.io.Reader dstream, int startline,
325 int startcolumn)
326 {
327 ReInit(dstream, startline, startcolumn, 4096);
328 }
329
330 /** Reinitialise. */
331 public void ReInit(java.io.Reader dstream)
332 {
333 ReInit(dstream, 1, 1, 4096);
334 }
335 /** Constructor. */
336 public SimpleCharStream(java.io.InputStream dstream, String encoding, int startline,
337 int startcolumn, int buffersize) throws java.io.UnsupportedEncodingException
338 {
339 this(encoding == null ? new java.io.InputStreamReader(dstream) : new java.io.InputStreamReader(dstream, encoding), startline, startcolumn, buffersize);
340 }
341
342 /** Constructor. */
343 public SimpleCharStream(java.io.InputStream dstream, int startline,
344 int startcolumn, int buffersize)
345 {
346 this(new java.io.InputStreamReader(dstream), startline, startcolumn, buffersize);
347 }
348
349 /** Constructor. */
350 public SimpleCharStream(java.io.InputStream dstream, String encoding, int startline,
351 int startcolumn) throws java.io.UnsupportedEncodingException
352 {
353 this(dstream, encoding, startline, startcolumn, 4096);
354 }
355
356 /** Constructor. */
357 public SimpleCharStream(java.io.InputStream dstream, int startline,
358 int startcolumn)
359 {
360 this(dstream, startline, startcolumn, 4096);
361 }
362
363 /** Constructor. */
364 public SimpleCharStream(java.io.InputStream dstream, String encoding) throws java.io.UnsupportedEncodingException
365 {
366 this(dstream, encoding, 1, 1, 4096);
367 }
368
369 /** Constructor. */
370 public SimpleCharStream(java.io.InputStream dstream)
371 {
372 this(dstream, 1, 1, 4096);
373 }
374
375 /** Reinitialise. */
376 public void ReInit(java.io.InputStream dstream, String encoding, int startline,
377 int startcolumn, int buffersize) throws java.io.UnsupportedEncodingException
378 {
379 ReInit(encoding == null ? new java.io.InputStreamReader(dstream) : new java.io.InputStreamReader(dstream, encoding), startline, startcolumn, buffersize);
380 }
381
382 /** Reinitialise. */
383 public void ReInit(java.io.InputStream dstream, int startline,
384 int startcolumn, int buffersize)
385 {
386 ReInit(new java.io.InputStreamReader(dstream), startline, startcolumn, buffersize);
387 }
388
389 /** Reinitialise. */
390 public void ReInit(java.io.InputStream dstream, String encoding) throws java.io.UnsupportedEncodingException
391 {
392 ReInit(dstream, encoding, 1, 1, 4096);
393 }
394
395 /** Reinitialise. */
396 public void ReInit(java.io.InputStream dstream)
397 {
398 ReInit(dstream, 1, 1, 4096);
399 }
400 /** Reinitialise. */
401 public void ReInit(java.io.InputStream dstream, String encoding, int startline,
402 int startcolumn) throws java.io.UnsupportedEncodingException
403 {
404 ReInit(dstream, encoding, startline, startcolumn, 4096);
405 }
406 /** Reinitialise. */
407 public void ReInit(java.io.InputStream dstream, int startline,
408 int startcolumn)
409 {
410 ReInit(dstream, startline, startcolumn, 4096);
411 }
412 /** Get token literal value. */
413 static public String GetImage()
414 {
415 if (bufpos >= tokenBegin)
416 return new String(buffer, tokenBegin, bufpos - tokenBegin + 1);
417 else
418 return new String(buffer, tokenBegin, bufsize - tokenBegin) +
419 new String(buffer, 0, bufpos + 1);
420 }
421
422 /** Get the suffix. */
423 static public char[] GetSuffix(int len)
424 {
425 char[] ret = new char[len];
426
427 if ((bufpos + 1) >= len)
428 System.arraycopy(buffer, bufpos - len + 1, ret, 0, len);
429 else
430 {
431 System.arraycopy(buffer, bufsize - (len - bufpos - 1), ret, 0,
432 len - bufpos - 1);
433 System.arraycopy(buffer, 0, ret, len - bufpos - 1, bufpos + 1);
434 }
435
436 return ret;
437 }
438
439 /** Reset buffer when finished. */
440 static public void Done()
441 {
442 buffer = null;
443 bufline = null;
444 bufcolumn = null;
445 }
446
447 /**
448 * Method to adjust line and column numbers for the start of a token.
449 */
450 static public void adjustBeginLineColumn(int newLine, int newCol)
451 {
452 int start = tokenBegin;
453 int len;
454
455 if (bufpos >= tokenBegin)
456 {
457 len = bufpos - tokenBegin + inBuf + 1;
458 }
459 else
460 {
461 len = bufsize - tokenBegin + bufpos + 1 + inBuf;
462 }
463
464 int i = 0, j = 0, k = 0;
465 int nextColDiff = 0, columnDiff = 0;
466
467 while (i < len &&
468 bufline[j = start % bufsize] == bufline[k = ++start % bufsize])
469 {
470 bufline[j] = newLine;
471 nextColDiff = columnDiff + bufcolumn[k] - bufcolumn[j];
472 bufcolumn[j] = newCol + columnDiff;
473 columnDiff = nextColDiff;
474 i++;
475 }
476
477 if (i < len)
478 {
479 bufline[j] = newLine++;
480 bufcolumn[j] = newCol + columnDiff;
481
482 while (i++ < len)
483 {
484 if (bufline[j = start % bufsize] != bufline[++start % bufsize])
485 bufline[j] = newLine++;
486 else
487 bufline[j] = newLine;
488 }
489 }
490
491 line = bufline[j];
492 column = bufcolumn[j];
493 }
494
495 }
496 /* JavaCC - OriginalChecksum=9bbb8cb4295bb8f7d58e31ce57dc2e0f (do not edit this line) */