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