1 |
| |
2 |
| package net.sourceforge.pmd.cpd.cppast; |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
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 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 |
0
| static protected void setTabSize(int i) { tabSize = i; }
|
33 |
0
| static protected int getTabSize(int i) { return tabSize; }
|
34 |
| |
35 |
| |
36 |
0
| static protected void ExpandBuff(boolean wrapAround)
|
37 |
| { |
38 |
0
| char[] newbuffer = new char[bufsize + 2048];
|
39 |
0
| int newbufline[] = new int[bufsize + 2048];
|
40 |
0
| int newbufcolumn[] = new int[bufsize + 2048];
|
41 |
| |
42 |
0
| try
|
43 |
| { |
44 |
0
| if (wrapAround)
|
45 |
| { |
46 |
0
| System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin);
|
47 |
0
| System.arraycopy(buffer, 0, newbuffer,
|
48 |
| bufsize - tokenBegin, bufpos); |
49 |
0
| buffer = newbuffer;
|
50 |
| |
51 |
0
| System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin);
|
52 |
0
| System.arraycopy(bufline, 0, newbufline, bufsize - tokenBegin, bufpos);
|
53 |
0
| bufline = newbufline;
|
54 |
| |
55 |
0
| System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin);
|
56 |
0
| System.arraycopy(bufcolumn, 0, newbufcolumn, bufsize - tokenBegin, bufpos);
|
57 |
0
| bufcolumn = newbufcolumn;
|
58 |
| |
59 |
0
| maxNextCharInd = (bufpos += (bufsize - tokenBegin));
|
60 |
| } |
61 |
| else |
62 |
| { |
63 |
0
| System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin);
|
64 |
0
| buffer = newbuffer;
|
65 |
| |
66 |
0
| System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin);
|
67 |
0
| bufline = newbufline;
|
68 |
| |
69 |
0
| System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin);
|
70 |
0
| bufcolumn = newbufcolumn;
|
71 |
| |
72 |
0
| maxNextCharInd = (bufpos -= tokenBegin);
|
73 |
| } |
74 |
| } |
75 |
| catch (Throwable t) |
76 |
| { |
77 |
0
| throw new Error(t.getMessage());
|
78 |
| } |
79 |
| |
80 |
| |
81 |
0
| bufsize += 2048;
|
82 |
0
| available = bufsize;
|
83 |
0
| tokenBegin = 0;
|
84 |
| } |
85 |
| |
86 |
12
| static protected void FillBuff() throws java.io.IOException
|
87 |
| { |
88 |
12
| if (maxNextCharInd == available)
|
89 |
| { |
90 |
0
| if (available == bufsize)
|
91 |
| { |
92 |
0
| if (tokenBegin > 2048)
|
93 |
| { |
94 |
0
| bufpos = maxNextCharInd = 0;
|
95 |
0
| available = tokenBegin;
|
96 |
| } |
97 |
0
| else if (tokenBegin < 0)
|
98 |
0
| bufpos = maxNextCharInd = 0;
|
99 |
| else |
100 |
0
| ExpandBuff(false);
|
101 |
| } |
102 |
0
| else if (available > tokenBegin)
|
103 |
0
| available = bufsize;
|
104 |
0
| else if ((tokenBegin - available) < 2048)
|
105 |
0
| ExpandBuff(true);
|
106 |
| else |
107 |
0
| available = tokenBegin;
|
108 |
| } |
109 |
| |
110 |
12
| int i;
|
111 |
12
| try {
|
112 |
?
| if ((i = inputStream.read(buffer, maxNextCharInd,
|
113 |
| available - maxNextCharInd)) == -1) |
114 |
| { |
115 |
4
| inputStream.close();
|
116 |
4
| throw new java.io.IOException();
|
117 |
| } |
118 |
| else |
119 |
4
| maxNextCharInd += i;
|
120 |
4
| return;
|
121 |
| } |
122 |
| catch(java.io.IOException e) { |
123 |
8
| --bufpos;
|
124 |
8
| backup(0);
|
125 |
8
| if (tokenBegin == -1)
|
126 |
8
| tokenBegin = bufpos;
|
127 |
8
| throw e;
|
128 |
| } |
129 |
| } |
130 |
| |
131 |
80
| static public char BeginToken() throws java.io.IOException
|
132 |
| { |
133 |
80
| tokenBegin = -1;
|
134 |
80
| char c = readChar();
|
135 |
72
| tokenBegin = bufpos;
|
136 |
| |
137 |
72
| return c;
|
138 |
| } |
139 |
| |
140 |
225
| static protected void UpdateLineColumn(char c)
|
141 |
| { |
142 |
225
| column++;
|
143 |
| |
144 |
225
| if (prevCharIsLF)
|
145 |
| { |
146 |
7
| prevCharIsLF = false;
|
147 |
7
| line += (column = 1);
|
148 |
| } |
149 |
218
| else if (prevCharIsCR)
|
150 |
| { |
151 |
0
| prevCharIsCR = false;
|
152 |
0
| if (c == '\n')
|
153 |
| { |
154 |
0
| prevCharIsLF = true;
|
155 |
| } |
156 |
| else |
157 |
0
| line += (column = 1);
|
158 |
| } |
159 |
| |
160 |
225
| switch (c)
|
161 |
| { |
162 |
0
| case '\r' :
|
163 |
0
| prevCharIsCR = true;
|
164 |
0
| break;
|
165 |
11
| case '\n' :
|
166 |
11
| prevCharIsLF = true;
|
167 |
11
| break;
|
168 |
0
| case '\t' :
|
169 |
0
| column--;
|
170 |
0
| column += (tabSize - (column % tabSize));
|
171 |
0
| break;
|
172 |
214
| default :
|
173 |
214
| break;
|
174 |
| } |
175 |
| |
176 |
225
| bufline[bufpos] = line;
|
177 |
225
| bufcolumn[bufpos] = column;
|
178 |
| } |
179 |
| |
180 |
252
| static public char readChar() throws java.io.IOException
|
181 |
| { |
182 |
252
| if (inBuf > 0)
|
183 |
| { |
184 |
19
| --inBuf;
|
185 |
| |
186 |
19
| if (++bufpos == bufsize)
|
187 |
0
| bufpos = 0;
|
188 |
| |
189 |
19
| return buffer[bufpos];
|
190 |
| } |
191 |
| |
192 |
233
| if (++bufpos >= maxNextCharInd)
|
193 |
12
| FillBuff();
|
194 |
| |
195 |
225
| char c = buffer[bufpos];
|
196 |
| |
197 |
225
| UpdateLineColumn(c);
|
198 |
225
| return (c);
|
199 |
| } |
200 |
| |
201 |
| |
202 |
| |
203 |
| |
204 |
| |
205 |
| |
206 |
0
| static public int getColumn() {
|
207 |
0
| return bufcolumn[bufpos];
|
208 |
| } |
209 |
| |
210 |
| |
211 |
| |
212 |
| |
213 |
| |
214 |
| |
215 |
0
| static public int getLine() {
|
216 |
0
| return bufline[bufpos];
|
217 |
| } |
218 |
| |
219 |
43
| static public int getEndColumn() {
|
220 |
43
| return bufcolumn[bufpos];
|
221 |
| } |
222 |
| |
223 |
43
| static public int getEndLine() {
|
224 |
43
| return bufline[bufpos];
|
225 |
| } |
226 |
| |
227 |
43
| static public int getBeginColumn() {
|
228 |
43
| return bufcolumn[tokenBegin];
|
229 |
| } |
230 |
| |
231 |
43
| static public int getBeginLine() {
|
232 |
43
| return bufline[tokenBegin];
|
233 |
| } |
234 |
| |
235 |
71
| static public void backup(int amount) {
|
236 |
| |
237 |
71
| inBuf += amount;
|
238 |
71
| if ((bufpos -= amount) < 0)
|
239 |
0
| bufpos += bufsize;
|
240 |
| } |
241 |
| |
242 |
1
| public SimpleCharStream(java.io.Reader dstream, int startline,
|
243 |
| int startcolumn, int buffersize) |
244 |
| { |
245 |
1
| if (inputStream != null)
|
246 |
0
| throw new Error("\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 |
1
| inputStream = dstream;
|
250 |
1
| line = startline;
|
251 |
1
| column = startcolumn - 1;
|
252 |
| |
253 |
1
| available = bufsize = buffersize;
|
254 |
1
| buffer = new char[buffersize];
|
255 |
1
| bufline = new int[buffersize];
|
256 |
1
| bufcolumn = new int[buffersize];
|
257 |
| } |
258 |
| |
259 |
0
| public SimpleCharStream(java.io.Reader dstream, int startline,
|
260 |
| int startcolumn) |
261 |
| { |
262 |
0
| this(dstream, startline, startcolumn, 4096);
|
263 |
| } |
264 |
| |
265 |
1
| public SimpleCharStream(java.io.Reader dstream)
|
266 |
| { |
267 |
1
| this(dstream, 1, 1, 4096);
|
268 |
| } |
269 |
3
| public void ReInit(java.io.Reader dstream, int startline,
|
270 |
| int startcolumn, int buffersize) |
271 |
| { |
272 |
3
| inputStream = dstream;
|
273 |
3
| line = startline;
|
274 |
3
| column = startcolumn - 1;
|
275 |
| |
276 |
3
| if (buffer == null || buffersize != buffer.length)
|
277 |
| { |
278 |
0
| available = bufsize = buffersize;
|
279 |
0
| buffer = new char[buffersize];
|
280 |
0
| bufline = new int[buffersize];
|
281 |
0
| bufcolumn = new int[buffersize];
|
282 |
| } |
283 |
3
| prevCharIsLF = prevCharIsCR = false;
|
284 |
3
| tokenBegin = inBuf = maxNextCharInd = 0;
|
285 |
3
| bufpos = -1;
|
286 |
| } |
287 |
| |
288 |
0
| public void ReInit(java.io.Reader dstream, int startline,
|
289 |
| int startcolumn) |
290 |
| { |
291 |
0
| ReInit(dstream, startline, startcolumn, 4096);
|
292 |
| } |
293 |
| |
294 |
3
| public void ReInit(java.io.Reader dstream)
|
295 |
| { |
296 |
3
| ReInit(dstream, 1, 1, 4096);
|
297 |
| } |
298 |
0
| public SimpleCharStream(java.io.InputStream dstream, String encoding, int startline,
|
299 |
| int startcolumn, int buffersize) throws java.io.UnsupportedEncodingException |
300 |
| { |
301 |
0
| this(encoding == null ? new java.io.InputStreamReader(dstream) : new java.io.InputStreamReader(dstream, encoding), startline, startcolumn, buffersize);
|
302 |
| } |
303 |
| |
304 |
0
| public SimpleCharStream(java.io.InputStream dstream, int startline,
|
305 |
| int startcolumn, int buffersize) |
306 |
| { |
307 |
0
| this(new java.io.InputStreamReader(dstream), startline, startcolumn, buffersize);
|
308 |
| } |
309 |
| |
310 |
0
| public SimpleCharStream(java.io.InputStream dstream, String encoding, int startline,
|
311 |
| int startcolumn) throws java.io.UnsupportedEncodingException |
312 |
| { |
313 |
0
| this(dstream, encoding, startline, startcolumn, 4096);
|
314 |
| } |
315 |
| |
316 |
0
| public SimpleCharStream(java.io.InputStream dstream, int startline,
|
317 |
| int startcolumn) |
318 |
| { |
319 |
0
| this(dstream, startline, startcolumn, 4096);
|
320 |
| } |
321 |
| |
322 |
0
| public SimpleCharStream(java.io.InputStream dstream, String encoding) throws java.io.UnsupportedEncodingException
|
323 |
| { |
324 |
0
| this(dstream, encoding, 1, 1, 4096);
|
325 |
| } |
326 |
| |
327 |
0
| public SimpleCharStream(java.io.InputStream dstream)
|
328 |
| { |
329 |
0
| this(dstream, 1, 1, 4096);
|
330 |
| } |
331 |
| |
332 |
0
| public void ReInit(java.io.InputStream dstream, String encoding, int startline,
|
333 |
| int startcolumn, int buffersize) throws java.io.UnsupportedEncodingException |
334 |
| { |
335 |
0
| ReInit(encoding == null ? new java.io.InputStreamReader(dstream) : new java.io.InputStreamReader(dstream, encoding), startline, startcolumn, buffersize);
|
336 |
| } |
337 |
| |
338 |
0
| public void ReInit(java.io.InputStream dstream, int startline,
|
339 |
| int startcolumn, int buffersize) |
340 |
| { |
341 |
0
| ReInit(new java.io.InputStreamReader(dstream), startline, startcolumn, buffersize);
|
342 |
| } |
343 |
| |
344 |
0
| public void ReInit(java.io.InputStream dstream, String encoding) throws java.io.UnsupportedEncodingException
|
345 |
| { |
346 |
0
| ReInit(dstream, encoding, 1, 1, 4096);
|
347 |
| } |
348 |
| |
349 |
0
| public void ReInit(java.io.InputStream dstream)
|
350 |
| { |
351 |
0
| ReInit(dstream, 1, 1, 4096);
|
352 |
| } |
353 |
0
| public void ReInit(java.io.InputStream dstream, String encoding, int startline,
|
354 |
| int startcolumn) throws java.io.UnsupportedEncodingException |
355 |
| { |
356 |
0
| ReInit(dstream, encoding, startline, startcolumn, 4096);
|
357 |
| } |
358 |
0
| public void ReInit(java.io.InputStream dstream, int startline,
|
359 |
| int startcolumn) |
360 |
| { |
361 |
0
| ReInit(dstream, startline, startcolumn, 4096);
|
362 |
| } |
363 |
10
| static public String GetImage()
|
364 |
| { |
365 |
10
| if (bufpos >= tokenBegin)
|
366 |
10
| return new String(buffer, tokenBegin, bufpos - tokenBegin + 1);
|
367 |
| else |
368 |
0
| return new String(buffer, tokenBegin, bufsize - tokenBegin) +
|
369 |
| new String(buffer, 0, bufpos + 1); |
370 |
| } |
371 |
| |
372 |
0
| static public char[] GetSuffix(int len)
|
373 |
| { |
374 |
0
| char[] ret = new char[len];
|
375 |
| |
376 |
0
| if ((bufpos + 1) >= len)
|
377 |
0
| System.arraycopy(buffer, bufpos - len + 1, ret, 0, len);
|
378 |
| else |
379 |
| { |
380 |
0
| System.arraycopy(buffer, bufsize - (len - bufpos - 1), ret, 0,
|
381 |
| len - bufpos - 1); |
382 |
0
| System.arraycopy(buffer, 0, ret, len - bufpos - 1, bufpos + 1);
|
383 |
| } |
384 |
| |
385 |
0
| return ret;
|
386 |
| } |
387 |
| |
388 |
0
| static public void Done()
|
389 |
| { |
390 |
0
| buffer = null;
|
391 |
0
| bufline = null;
|
392 |
0
| bufcolumn = null;
|
393 |
| } |
394 |
| |
395 |
| |
396 |
| |
397 |
| |
398 |
0
| static public void adjustBeginLineColumn(int newLine, int newCol)
|
399 |
| { |
400 |
0
| int start = tokenBegin;
|
401 |
0
| int len;
|
402 |
| |
403 |
0
| if (bufpos >= tokenBegin)
|
404 |
| { |
405 |
0
| len = bufpos - tokenBegin + inBuf + 1;
|
406 |
| } |
407 |
| else |
408 |
| { |
409 |
0
| len = bufsize - tokenBegin + bufpos + 1 + inBuf;
|
410 |
| } |
411 |
| |
412 |
0
| int i = 0, j = 0, k = 0;
|
413 |
0
| int nextColDiff = 0, columnDiff = 0;
|
414 |
| |
415 |
0
| while (i < len &&
|
416 |
| bufline[j = start % bufsize] == bufline[k = ++start % bufsize]) |
417 |
| { |
418 |
0
| bufline[j] = newLine;
|
419 |
0
| nextColDiff = columnDiff + bufcolumn[k] - bufcolumn[j];
|
420 |
0
| bufcolumn[j] = newCol + columnDiff;
|
421 |
0
| columnDiff = nextColDiff;
|
422 |
0
| i++;
|
423 |
| } |
424 |
| |
425 |
0
| if (i < len)
|
426 |
| { |
427 |
0
| bufline[j] = newLine++;
|
428 |
0
| bufcolumn[j] = newCol + columnDiff;
|
429 |
| |
430 |
0
| while (i++ < len)
|
431 |
| { |
432 |
0
| if (bufline[j = start % bufsize] != bufline[++start % bufsize])
|
433 |
0
| bufline[j] = newLine++;
|
434 |
| else |
435 |
0
| bufline[j] = newLine;
|
436 |
| } |
437 |
| } |
438 |
| |
439 |
0
| line = bufline[j];
|
440 |
0
| column = bufcolumn[j];
|
441 |
| } |
442 |
| |
443 |
| } |