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