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