source: java/FramclipsePlugin/src/main/java/com/framsticks/framclipse/internal/parser/CharStreamWithOffset.java @ 193

Last change on this file since 193 was 193, checked in by Maciej Komosinski, 10 years ago

Set svn:eol-style native for all textual files

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/plain
File size: 11.2 KB
Line 
1/* Generated By:JavaCC: Do not edit this line. SimpleCharStream.java Version 4.0 */
2package com.framsticks.framclipse.internal.parser;
3
4/**
5 * An implementation of interface CharStream, where the stream is assumed to
6 * contain only ASCII characters (without unicode processing).
7 */
8
9public class CharStreamWithOffset implements CharStream
10{
11  public static final boolean staticFlag = false;
12  int bufsize;
13  int available;
14  int tokenBegin;
15  public int bufpos = -1;
16  protected int bufline[];
17  protected int bufcolumn[];
18  int charOffset = 0;
19
20  protected int column = 0;
21  protected int line = 1;
22
23  protected boolean prevCharIsCR = false;
24  protected boolean prevCharIsLF = false;
25
26  protected java.io.Reader inputStream;
27
28  protected char[] buffer;
29  protected int maxNextCharInd = 0;
30  protected int inBuf = 0;
31  protected int tabSize = 8;
32
33  protected void setTabSize(int i) { tabSize = i; }
34  protected int getTabSize(int i) { return tabSize; }
35
36
37  protected void ExpandBuff(boolean wrapAround)
38  {
39     char[] newbuffer = new char[bufsize + 2048];
40     int newbufline[] = new int[bufsize + 2048];
41     int newbufcolumn[] = new int[bufsize + 2048];
42
43     try
44     {
45        if (wrapAround)
46        {
47           System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin);
48           System.arraycopy(buffer, 0, newbuffer,
49                                             bufsize - tokenBegin, bufpos);
50           buffer = newbuffer;
51
52           System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin);
53           System.arraycopy(bufline, 0, newbufline, bufsize - tokenBegin, bufpos);
54           bufline = newbufline;
55
56           System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin);
57           System.arraycopy(bufcolumn, 0, newbufcolumn, bufsize - tokenBegin, bufpos);
58           bufcolumn = newbufcolumn;
59
60           maxNextCharInd = (bufpos += (bufsize - tokenBegin));
61        }
62        else
63        {
64           System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin);
65           buffer = newbuffer;
66
67           System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin);
68           bufline = newbufline;
69
70           System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin);
71           bufcolumn = newbufcolumn;
72
73           maxNextCharInd = (bufpos -= tokenBegin);
74        }
75     }
76     catch (Throwable t)
77     {
78        throw new Error(t.getMessage());
79     }
80
81
82     bufsize += 2048;
83     available = bufsize;
84     tokenBegin = 0;
85  }
86
87  protected void FillBuff() throws java.io.IOException
88  {
89     if (maxNextCharInd == available)
90     {
91        if (available == bufsize)
92        {
93           if (tokenBegin > 2048)
94           {
95              bufpos = maxNextCharInd = 0;
96              available = tokenBegin;
97           }
98           else if (tokenBegin < 0)
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        {
121           maxNextCharInd += i;
122           charOffset += i;
123        }
124        return;
125     }
126     catch(java.io.IOException e) {
127        --bufpos;
128        backup(0);
129        if (tokenBegin == -1)
130           tokenBegin = bufpos;
131        throw e;
132     }
133  }
134
135  public char BeginToken() throws java.io.IOException
136  {
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 += (tabSize - (column % tabSize));
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     if (inBuf > 0)
187     {
188        --inBuf;
189
190        if (++bufpos == bufsize)
191           bufpos = 0;
192
193        return buffer[bufpos];
194     }
195
196     if (++bufpos >= maxNextCharInd)
197        FillBuff();
198
199     char c = buffer[bufpos];
200
201     UpdateLineColumn(c);
202     return (c);
203  }
204
205  /**
206   * @deprecated
207   * @see #getEndColumn
208   */
209
210  public int getColumn() {
211     return bufcolumn[bufpos];
212  }
213
214  /**
215   * @deprecated
216   * @see #getEndLine
217   */
218
219  public int getLine() {
220     return bufline[bufpos];
221  }
222
223  public int getEndColumn() {
224     return bufcolumn[bufpos];
225  }
226
227  public int getEndLine() {
228     return bufline[bufpos];
229  }
230 
231  public int getBeginOffset() {
232           return charOffset - maxNextCharInd + tokenBegin;
233  }
234
235   public int getOffset() {
236           return charOffset - maxNextCharInd + bufpos + 1;
237  }
238 
239
240  public int getBeginColumn() {
241     return bufcolumn[tokenBegin];
242  }
243
244  public int getBeginLine() {
245     return bufline[tokenBegin];
246  }
247
248  public void backup(int amount) {
249
250    inBuf += amount;
251    if ((bufpos -= amount) < 0)
252       bufpos += bufsize;
253   
254  }
255
256  public CharStreamWithOffset(java.io.Reader dstream, int startline,
257  int startcolumn, int buffersize)
258  {
259    inputStream = dstream;
260    line = startline;
261    column = startcolumn - 1;
262
263    available = bufsize = buffersize;
264    buffer = new char[buffersize];
265    bufline = new int[buffersize];
266    bufcolumn = new int[buffersize];
267  }
268
269  public CharStreamWithOffset(java.io.Reader dstream, int startline,
270                          int startcolumn)
271  {
272     this(dstream, startline, startcolumn, 4096);
273  }
274
275  public CharStreamWithOffset(java.io.Reader dstream)
276  {
277     this(dstream, 1, 1, 4096);
278  }
279  public void ReInit(java.io.Reader dstream, int startline,
280  int startcolumn, int buffersize)
281  {
282    inputStream = dstream;
283    line = startline;
284    column = startcolumn - 1;
285
286    if (buffer == null || buffersize != buffer.length)
287    {
288      available = bufsize = buffersize;
289      buffer = new char[buffersize];
290      bufline = new int[buffersize];
291      bufcolumn = new int[buffersize];
292    }
293    prevCharIsLF = prevCharIsCR = false;
294    tokenBegin = inBuf = maxNextCharInd = 0;
295    bufpos = -1;
296  }
297
298  public void ReInit(java.io.Reader dstream, int startline,
299                     int startcolumn)
300  {
301     ReInit(dstream, startline, startcolumn, 4096);
302  }
303
304  public void ReInit(java.io.Reader dstream)
305  {
306     ReInit(dstream, 1, 1, 4096);
307  }
308  public CharStreamWithOffset(java.io.InputStream dstream, String encoding, int startline,
309  int startcolumn, int buffersize) throws java.io.UnsupportedEncodingException
310  {
311     this(encoding == null ? new java.io.InputStreamReader(dstream) : new java.io.InputStreamReader(dstream, encoding), startline, startcolumn, buffersize);
312  }
313
314  public CharStreamWithOffset(java.io.InputStream dstream, int startline,
315  int startcolumn, int buffersize)
316  {
317     this(new java.io.InputStreamReader(dstream), startline, startcolumn, buffersize);
318  }
319
320  public CharStreamWithOffset(java.io.InputStream dstream, String encoding, int startline,
321                          int startcolumn) throws java.io.UnsupportedEncodingException
322  {
323     this(dstream, encoding, startline, startcolumn, 4096);
324  }
325
326  public CharStreamWithOffset(java.io.InputStream dstream, int startline,
327                          int startcolumn)
328  {
329     this(dstream, startline, startcolumn, 4096);
330  }
331
332  public CharStreamWithOffset(java.io.InputStream dstream, String encoding) throws java.io.UnsupportedEncodingException
333  {
334     this(dstream, encoding, 1, 1, 4096);
335  }
336
337  public CharStreamWithOffset(java.io.InputStream dstream)
338  {
339     this(dstream, 1, 1, 4096);
340  }
341
342  public void ReInit(java.io.InputStream dstream, String encoding, int startline,
343                          int startcolumn, int buffersize) throws java.io.UnsupportedEncodingException
344  {
345     ReInit(encoding == null ? new java.io.InputStreamReader(dstream) : new java.io.InputStreamReader(dstream, encoding), startline, startcolumn, buffersize);
346  }
347
348  public void ReInit(java.io.InputStream dstream, int startline,
349                          int startcolumn, int buffersize)
350  {
351     ReInit(new java.io.InputStreamReader(dstream), startline, startcolumn, buffersize);
352  }
353
354  public void ReInit(java.io.InputStream dstream, String encoding) throws java.io.UnsupportedEncodingException
355  {
356     ReInit(dstream, encoding, 1, 1, 4096);
357  }
358
359  public void ReInit(java.io.InputStream dstream)
360  {
361     ReInit(dstream, 1, 1, 4096);
362  }
363  public void ReInit(java.io.InputStream dstream, String encoding, int startline,
364                     int startcolumn) throws java.io.UnsupportedEncodingException
365  {
366     ReInit(dstream, encoding, startline, startcolumn, 4096);
367  }
368  public void ReInit(java.io.InputStream dstream, int startline,
369                     int startcolumn)
370  {
371     ReInit(dstream, startline, startcolumn, 4096);
372  }
373  public String GetImage()
374  {
375     if (bufpos >= tokenBegin)
376        return new String(buffer, tokenBegin, bufpos - tokenBegin + 1);
377     else
378        return new String(buffer, tokenBegin, bufsize - tokenBegin) +
379                              new String(buffer, 0, bufpos + 1);
380  }
381
382  public char[] GetSuffix(int len)
383  {
384     char[] ret = new char[len];
385
386     if ((bufpos + 1) >= len)
387        System.arraycopy(buffer, bufpos - len + 1, ret, 0, len);
388     else
389     {
390        System.arraycopy(buffer, bufsize - (len - bufpos - 1), ret, 0,
391                                                          len - bufpos - 1);
392        System.arraycopy(buffer, 0, ret, len - bufpos - 1, bufpos + 1);
393     }
394
395     return ret;
396  }
397
398  public void Done()
399  {
400     buffer = null;
401     bufline = null;
402     bufcolumn = null;
403  }
404
405  /**
406   * Method to adjust line and column numbers for the start of a token.
407   */
408  public void adjustBeginLineColumn(int newLine, int newCol)
409  {
410     int start = tokenBegin;
411     int len;
412
413     if (bufpos >= tokenBegin)
414     {
415        len = bufpos - tokenBegin + inBuf + 1;
416     }
417     else
418     {
419        len = bufsize - tokenBegin + bufpos + 1 + inBuf;
420     }
421
422     int i = 0, j = 0, k = 0;
423     int nextColDiff = 0, columnDiff = 0;
424
425     while (i < len &&
426            bufline[j = start % bufsize] == bufline[k = ++start % bufsize])
427     {
428        bufline[j] = newLine;
429        nextColDiff = columnDiff + bufcolumn[k] - bufcolumn[j];
430        bufcolumn[j] = newCol + columnDiff;
431        columnDiff = nextColDiff;
432        i++;
433     }
434
435     if (i < len)
436     {
437        bufline[j] = newLine++;
438        bufcolumn[j] = newCol + columnDiff;
439
440        while (i++ < len)
441        {
442           if (bufline[j = start % bufsize] != bufline[++start % bufsize])
443              bufline[j] = newLine++;
444           else
445              bufline[j] = newLine;
446        }
447     }
448
449     line = bufline[j];
450     column = bufcolumn[j];
451  }
452
453}
Note: See TracBrowser for help on using the repository browser.