001 /* Generated By:JavaCC: Do not edit this line. ASCII_CharStream.java Version 0.7pre6 */
002 package gate.jape.parser;
003
004 /**
005 * An implementation of interface CharStream, where the stream is assumed to
006 * contain only ASCII characters (without unicode processing).
007 */
008
009 public final class ASCII_CharStream
010 {
011 public static final boolean staticFlag = false;
012 int bufsize;
013 int available;
014 int tokenBegin;
015 public int bufpos = -1;
016 private int bufline[];
017 private int bufcolumn[];
018
019 private int column = 0;
020 private int line = 1;
021
022 private boolean prevCharIsCR = false;
023 private boolean prevCharIsLF = false;
024
025 private java.io.Reader inputStream;
026
027 private char[] buffer;
028 private int maxNextCharInd = 0;
029 private int inBuf = 0;
030
031 private final void ExpandBuff(boolean wrapAround)
032 {
033 char[] newbuffer = new char[bufsize + 2048];
034 int newbufline[] = new int[bufsize + 2048];
035 int newbufcolumn[] = new int[bufsize + 2048];
036
037 try
038 {
039 if (wrapAround)
040 {
041 System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin);
042 System.arraycopy(buffer, 0, newbuffer,
043 bufsize - tokenBegin, bufpos);
044 buffer = newbuffer;
045
046 System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin);
047 System.arraycopy(bufline, 0, newbufline, bufsize - tokenBegin, bufpos);
048 bufline = newbufline;
049
050 System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin);
051 System.arraycopy(bufcolumn, 0, newbufcolumn, bufsize - tokenBegin, bufpos);
052 bufcolumn = newbufcolumn;
053
054 maxNextCharInd = (bufpos += (bufsize - tokenBegin));
055 }
056 else
057 {
058 System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin);
059 buffer = newbuffer;
060
061 System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin);
062 bufline = newbufline;
063
064 System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin);
065 bufcolumn = newbufcolumn;
066
067 maxNextCharInd = (bufpos -= tokenBegin);
068 }
069 }
070 catch (Throwable t)
071 {
072 throw new Error(t.getMessage());
073 }
074
075
076 bufsize += 2048;
077 available = bufsize;
078 tokenBegin = 0;
079 }
080
081 private final void FillBuff() throws java.io.IOException
082 {
083 if (maxNextCharInd == available)
084 {
085 if (available == bufsize)
086 {
087 if (tokenBegin > 2048)
088 {
089 bufpos = maxNextCharInd = 0;
090 available = tokenBegin;
091 }
092 else if (tokenBegin < 0)
093 bufpos = maxNextCharInd = 0;
094 else
095 ExpandBuff(false);
096 }
097 else if (available > tokenBegin)
098 available = bufsize;
099 else if ((tokenBegin - available) < 2048)
100 ExpandBuff(true);
101 else
102 available = tokenBegin;
103 }
104
105 int i;
106 try {
107 if ((i = inputStream.read(buffer, maxNextCharInd,
108 available - maxNextCharInd)) == -1)
109 {
110 inputStream.close();
111 throw new java.io.IOException();
112 }
113 else
114 maxNextCharInd += i;
115 return;
116 }
117 catch(java.io.IOException e) {
118 --bufpos;
119 backup(0);
120 if (tokenBegin == -1)
121 tokenBegin = bufpos;
122 throw e;
123 }
124 }
125
126 public final char BeginToken() throws java.io.IOException
127 {
128 tokenBegin = -1;
129 char c = readChar();
130 tokenBegin = bufpos;
131
132 return c;
133 }
134
135 private final void UpdateLineColumn(char c)
136 {
137 column++;
138
139 if (prevCharIsLF)
140 {
141 prevCharIsLF = false;
142 line += (column = 1);
143 }
144 else if (prevCharIsCR)
145 {
146 prevCharIsCR = false;
147 if (c == '\n')
148 {
149 prevCharIsLF = true;
150 }
151 else
152 line += (column = 1);
153 }
154
155 switch (c)
156 {
157 case '\r' :
158 prevCharIsCR = true;
159 break;
160 case '\n' :
161 prevCharIsLF = true;
162 break;
163 case '\t' :
164 column--;
165 column += (8 - (column & 07));
166 break;
167 default :
168 break;
169 }
170
171 bufline[bufpos] = line;
172 bufcolumn[bufpos] = column;
173 }
174
175 public final char readChar() throws java.io.IOException
176 {
177 if (inBuf > 0)
178 {
179 --inBuf;
180 return (char)((char)0xff & buffer[(bufpos == bufsize - 1) ? (bufpos = 0) : ++bufpos]);
181 }
182
183 if (++bufpos >= maxNextCharInd)
184 FillBuff();
185
186 char c = (char)((char)0xff & buffer[bufpos]);
187
188 UpdateLineColumn(c);
189 return (c);
190 }
191
192 /**
193 * @deprecated
194 * @see #getEndColumn
195 */
196
197 public final int getColumn() {
198 return bufcolumn[bufpos];
199 }
200
201 /**
202 * @deprecated
203 * @see #getEndLine
204 */
205
206 public final int getLine() {
207 return bufline[bufpos];
208 }
209
210 public final int getEndColumn() {
211 return bufcolumn[bufpos];
212 }
213
214 public final int getEndLine() {
215 return bufline[bufpos];
216 }
217
218 public final int getBeginColumn() {
219 return bufcolumn[tokenBegin];
220 }
221
222 public final int getBeginLine() {
223 return bufline[tokenBegin];
224 }
225
226 public final void backup(int amount) {
227
228 inBuf += amount;
229 if ((bufpos -= amount) < 0)
230 bufpos += bufsize;
231 }
232
233 public ASCII_CharStream(java.io.Reader dstream, int startline,
234 int startcolumn, int buffersize)
235 {
236 inputStream = dstream;
237 line = startline;
238 column = startcolumn - 1;
239
240 available = bufsize = buffersize;
241 buffer = new char[buffersize];
242 bufline = new int[buffersize];
243 bufcolumn = new int[buffersize];
244 }
245
246 public ASCII_CharStream(java.io.Reader dstream, int startline,
247 int startcolumn)
248 {
249 this(dstream, startline, startcolumn, 4096);
250 }
251 public void ReInit(java.io.Reader dstream, int startline,
252 int startcolumn, int buffersize)
253 {
254 inputStream = dstream;
255 line = startline;
256 column = startcolumn - 1;
257
258 if (buffer == null || buffersize != buffer.length)
259 {
260 available = bufsize = buffersize;
261 buffer = new char[buffersize];
262 bufline = new int[buffersize];
263 bufcolumn = new int[buffersize];
264 }
265 prevCharIsLF = prevCharIsCR = false;
266 tokenBegin = inBuf = maxNextCharInd = 0;
267 bufpos = -1;
268 }
269
270 public void ReInit(java.io.Reader dstream, int startline,
271 int startcolumn)
272 {
273 ReInit(dstream, startline, startcolumn, 4096);
274 }
275 public ASCII_CharStream(java.io.InputStream dstream, int startline,
276 int startcolumn, int buffersize)
277 {
278 this(new java.io.InputStreamReader(dstream), startline, startcolumn, 4096);
279 }
280
281 public ASCII_CharStream(java.io.InputStream dstream, int startline,
282 int startcolumn)
283 {
284 this(dstream, startline, startcolumn, 4096);
285 }
286
287 public void ReInit(java.io.InputStream dstream, int startline,
288 int startcolumn, int buffersize)
289 {
290 ReInit(new java.io.InputStreamReader(dstream), startline, startcolumn, 4096);
291 }
292 public void ReInit(java.io.InputStream dstream, int startline,
293 int startcolumn)
294 {
295 ReInit(dstream, startline, startcolumn, 4096);
296 }
297 public final String GetImage()
298 {
299 if (bufpos >= tokenBegin)
300 return new String(buffer, tokenBegin, bufpos - tokenBegin + 1);
301 else
302 return new String(buffer, tokenBegin, bufsize - tokenBegin) +
303 new String(buffer, 0, bufpos + 1);
304 }
305
306 public final char[] GetSuffix(int len)
307 {
308 char[] ret = new char[len];
309
310 if ((bufpos + 1) >= len)
311 System.arraycopy(buffer, bufpos - len + 1, ret, 0, len);
312 else
313 {
314 System.arraycopy(buffer, bufsize - (len - bufpos - 1), ret, 0,
315 len - bufpos - 1);
316 System.arraycopy(buffer, 0, ret, len - bufpos - 1, bufpos + 1);
317 }
318
319 return ret;
320 }
321
322 public void Done()
323 {
324 buffer = null;
325 bufline = null;
326 bufcolumn = null;
327 }
328
329 /**
330 * Method to adjust line and column numbers for the start of a token.<BR>
331 */
332 public void adjustBeginLineColumn(int newLine, int newCol)
333 {
334 int start = tokenBegin;
335 int len;
336
337 if (bufpos >= tokenBegin)
338 {
339 len = bufpos - tokenBegin + inBuf + 1;
340 }
341 else
342 {
343 len = bufsize - tokenBegin + bufpos + 1 + inBuf;
344 }
345
346 int i = 0, j = 0, k = 0;
347 int nextColDiff = 0, columnDiff = 0;
348
349 while (i < len &&
350 bufline[j = start % bufsize] == bufline[k = ++start % bufsize])
351 {
352 bufline[j] = newLine;
353 nextColDiff = columnDiff + bufcolumn[k] - bufcolumn[j];
354 bufcolumn[j] = newCol + columnDiff;
355 columnDiff = nextColDiff;
356 i++;
357 }
358
359 if (i < len)
360 {
361 bufline[j] = newLine++;
362 bufcolumn[j] = newCol + columnDiff;
363
364 while (i++ < len)
365 {
366 if (bufline[j = start % bufsize] != bufline[++start % bufsize])
367 bufline[j] = newLine++;
368 else
369 bufline[j] = newLine;
370 }
371 }
372
373 line = bufline[j];
374 column = bufcolumn[j];
375 }
376
377 }
|