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