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