View Javadoc

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