Source code: org/objectstyle/cayenne/wocompat/parser/ASCII_CharStream.java
1 /* Generated By:JavaCC: Do not edit this line. ASCII_CharStream.java Version 0.7pre6 */
2 package org.objectstyle.cayenne.wocompat.parser;
3
4 /**
5 * An implementation of interface CharStream, where the stream is assumed to
6 * contain only ASCII characters (without unicode processing).
7 */
8 public final class ASCII_CharStream
9 {
10 public static final boolean staticFlag = false;
11 int bufsize;
12 int available;
13 int tokenBegin;
14 public int bufpos = -1;
15 private int bufline[];
16 private int bufcolumn[];
17
18 private int column = 0;
19 private int line = 1;
20
21 private boolean prevCharIsCR = false;
22 private boolean prevCharIsLF = false;
23
24 private java.io.Reader inputStream;
25
26 private char[] buffer;
27 private int maxNextCharInd = 0;
28 private int inBuf = 0;
29
30 private final void ExpandBuff(boolean wrapAround)
31 {
32 char[] newbuffer = new char[bufsize + 2048];
33 int newbufline[] = new int[bufsize + 2048];
34 int newbufcolumn[] = new int[bufsize + 2048];
35
36 try
37 {
38 if (wrapAround)
39 {
40 System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin);
41 System.arraycopy(buffer, 0, newbuffer,
42 bufsize - tokenBegin, bufpos);
43 buffer = newbuffer;
44
45 System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin);
46 System.arraycopy(bufline, 0, newbufline, bufsize - tokenBegin, bufpos);
47 bufline = newbufline;
48
49 System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin);
50 System.arraycopy(bufcolumn, 0, newbufcolumn, bufsize - tokenBegin, bufpos);
51 bufcolumn = newbufcolumn;
52
53 maxNextCharInd = (bufpos += (bufsize - tokenBegin));
54 }
55 else
56 {
57 System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin);
58 buffer = newbuffer;
59
60 System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin);
61 bufline = newbufline;
62
63 System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin);
64 bufcolumn = newbufcolumn;
65
66 maxNextCharInd = (bufpos -= tokenBegin);
67 }
68 }
69 catch (Throwable t)
70 {
71 throw new Error(t.getMessage());
72 }
73
74
75 bufsize += 2048;
76 available = bufsize;
77 tokenBegin = 0;
78 }
79
80 private final void FillBuff() throws java.io.IOException
81 {
82 if (maxNextCharInd == available)
83 {
84 if (available == bufsize)
85 {
86 if (tokenBegin > 2048)
87 {
88 bufpos = maxNextCharInd = 0;
89 available = tokenBegin;
90 }
91 else if (tokenBegin < 0)
92 bufpos = maxNextCharInd = 0;
93 else
94 ExpandBuff(false);
95 }
96 else if (available > tokenBegin)
97 available = bufsize;
98 else if ((tokenBegin - available) < 2048)
99 ExpandBuff(true);
100 else
101 available = tokenBegin;
102 }
103
104 int i;
105 try {
106 if ((i = inputStream.read(buffer, maxNextCharInd,
107 available - maxNextCharInd)) == -1)
108 {
109 inputStream.close();
110 throw new java.io.IOException();
111 }
112 else
113 maxNextCharInd += i;
114 return;
115 }
116 catch(java.io.IOException e) {
117 --bufpos;
118 backup(0);
119 if (tokenBegin == -1)
120 tokenBegin = bufpos;
121 throw e;
122 }
123 }
124
125 public final char BeginToken() throws java.io.IOException
126 {
127 tokenBegin = -1;
128 char c = readChar();
129 tokenBegin = bufpos;
130
131 return c;
132 }
133
134 private final void UpdateLineColumn(char c)
135 {
136 column++;
137
138 if (prevCharIsLF)
139 {
140 prevCharIsLF = false;
141 line += (column = 1);
142 }
143 else if (prevCharIsCR)
144 {
145 prevCharIsCR = false;
146 if (c == '\n')
147 {
148 prevCharIsLF = true;
149 }
150 else
151 line += (column = 1);
152 }
153
154 switch (c)
155 {
156 case '\r' :
157 prevCharIsCR = true;
158 break;
159 case '\n' :
160 prevCharIsLF = true;
161 break;
162 case '\t' :
163 column--;
164 column += (8 - (column & 07));
165 break;
166 default :
167 break;
168 }
169
170 bufline[bufpos] = line;
171 bufcolumn[bufpos] = column;
172 }
173
174 public final char readChar() throws java.io.IOException
175 {
176 if (inBuf > 0)
177 {
178 --inBuf;
179 return (char)((char)0xff & buffer[(bufpos == bufsize - 1) ? (bufpos = 0) : ++bufpos]);
180 }
181
182 if (++bufpos >= maxNextCharInd)
183 FillBuff();
184
185 char c = (char)((char)0xff & buffer[bufpos]);
186
187 UpdateLineColumn(c);
188 return (c);
189 }
190
191 public final int getEndColumn() {
192 return bufcolumn[bufpos];
193 }
194
195 public final int getEndLine() {
196 return bufline[bufpos];
197 }
198
199 public final int getBeginColumn() {
200 return bufcolumn[tokenBegin];
201 }
202
203 public final int getBeginLine() {
204 return bufline[tokenBegin];
205 }
206
207 public final void backup(int amount) {
208
209 inBuf += amount;
210 if ((bufpos -= amount) < 0)
211 bufpos += bufsize;
212 }
213
214 public ASCII_CharStream(java.io.Reader dstream, int startline,
215 int startcolumn, int buffersize)
216 {
217 inputStream = dstream;
218 line = startline;
219 column = startcolumn - 1;
220
221 available = bufsize = buffersize;
222 buffer = new char[buffersize];
223 bufline = new int[buffersize];
224 bufcolumn = new int[buffersize];
225 }
226
227 public ASCII_CharStream(java.io.Reader dstream, int startline,
228 int startcolumn)
229 {
230 this(dstream, startline, startcolumn, 4096);
231 }
232 public void ReInit(java.io.Reader dstream, int startline,
233 int startcolumn, int buffersize)
234 {
235 inputStream = dstream;
236 line = startline;
237 column = startcolumn - 1;
238
239 if (buffer == null || buffersize != buffer.length)
240 {
241 available = bufsize = buffersize;
242 buffer = new char[buffersize];
243 bufline = new int[buffersize];
244 bufcolumn = new int[buffersize];
245 }
246 prevCharIsLF = prevCharIsCR = false;
247 tokenBegin = inBuf = maxNextCharInd = 0;
248 bufpos = -1;
249 }
250
251 public void ReInit(java.io.Reader dstream, int startline,
252 int startcolumn)
253 {
254 ReInit(dstream, startline, startcolumn, 4096);
255 }
256 public ASCII_CharStream(java.io.InputStream dstream, int startline,
257 int startcolumn, int buffersize)
258 {
259 this(new java.io.InputStreamReader(dstream), startline, startcolumn, 4096);
260 }
261
262 public ASCII_CharStream(java.io.InputStream dstream, int startline,
263 int startcolumn)
264 {
265 this(dstream, startline, startcolumn, 4096);
266 }
267
268 public void ReInit(java.io.InputStream dstream, int startline,
269 int startcolumn, int buffersize)
270 {
271 ReInit(new java.io.InputStreamReader(dstream), startline, startcolumn, 4096);
272 }
273 public void ReInit(java.io.InputStream dstream, int startline,
274 int startcolumn)
275 {
276 ReInit(dstream, startline, startcolumn, 4096);
277 }
278 public final String GetImage()
279 {
280 if (bufpos >= tokenBegin)
281 return new String(buffer, tokenBegin, bufpos - tokenBegin + 1);
282 else
283 return new String(buffer, tokenBegin, bufsize - tokenBegin) +
284 new String(buffer, 0, bufpos + 1);
285 }
286
287 public final char[] GetSuffix(int len)
288 {
289 char[] ret = new char[len];
290
291 if ((bufpos + 1) >= len)
292 System.arraycopy(buffer, bufpos - len + 1, ret, 0, len);
293 else
294 {
295 System.arraycopy(buffer, bufsize - (len - bufpos - 1), ret, 0,
296 len - bufpos - 1);
297 System.arraycopy(buffer, 0, ret, len - bufpos - 1, bufpos + 1);
298 }
299
300 return ret;
301 }
302
303 public void Done()
304 {
305 buffer = null;
306 bufline = null;
307 bufcolumn = null;
308 }
309
310 /**
311 * Adjusts line and column numbers for the start of a token.
312 */
313 public void adjustBeginLineColumn(int newLine, int newCol)
314 {
315 int start = tokenBegin;
316 int len;
317
318 if (bufpos >= tokenBegin)
319 {
320 len = bufpos - tokenBegin + inBuf + 1;
321 }
322 else
323 {
324 len = bufsize - tokenBegin + bufpos + 1 + inBuf;
325 }
326
327 int i = 0, j = 0, k = 0;
328 int nextColDiff = 0, columnDiff = 0;
329
330 while (i < len &&
331 bufline[j = start % bufsize] == bufline[k = ++start % bufsize])
332 {
333 bufline[j] = newLine;
334 nextColDiff = columnDiff + bufcolumn[k] - bufcolumn[j];
335 bufcolumn[j] = newCol + columnDiff;
336 columnDiff = nextColDiff;
337 i++;
338 }
339
340 if (i < len)
341 {
342 bufline[j] = newLine++;
343 bufcolumn[j] = newCol + columnDiff;
344
345 while (i++ < len)
346 {
347 if (bufline[j = start % bufsize] != bufline[++start % bufsize])
348 bufline[j] = newLine++;
349 else
350 bufline[j] = newLine;
351 }
352 }
353
354 line = bufline[j];
355 column = bufcolumn[j];
356 }
357
358 }