View Javadoc

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