Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libGDBMS / src / com / hardcode / gdbms / parser / SimpleCharStream.java @ 466

History | View | Annotate | Download (9.44 KB)

1
package com.hardcode.gdbms.parser;
2
/* Generated By:JavaCC: Do not edit this line. SimpleCharStream.java Version 3.0 */
3
/**
4
 * An implementation of interface CharStream, where the stream is assumed to
5
 * contain only ASCII characters (without unicode processing).
6
 */
7

    
8
public class SimpleCharStream
9
{
10
  public static final boolean staticFlag = false;
11
  int bufsize;
12
  int available;
13
  int tokenBegin;
14
  public int bufpos = -1;
15
  protected int bufline[];
16
  protected int bufcolumn[];
17

    
18
  protected int column = 0;
19
  protected int line = 1;
20

    
21
  protected boolean prevCharIsCR = false;
22
  protected boolean prevCharIsLF = false;
23

    
24
  protected java.io.Reader inputStream;
25

    
26
  protected char[] buffer;
27
  protected int maxNextCharInd = 0;
28
  protected int inBuf = 0;
29

    
30
  protected void ExpandBuff(boolean wrapAround)
31
  {
32
     char[] newbuffer = new char[bufsize + 2048];
33
     int newbufline[] = new int[bufsize + 2048];
34
     int newbufcolumn[] = new int[bufsize + 2048];
35

    
36
     try
37
     {
38
        if (wrapAround)
39
        {
40
           System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin);
41
           System.arraycopy(buffer, 0, newbuffer,
42
                                             bufsize - tokenBegin, bufpos);
43
           buffer = newbuffer;
44

    
45
           System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin);
46
           System.arraycopy(bufline, 0, newbufline, bufsize - tokenBegin, bufpos);
47
           bufline = newbufline;
48

    
49
           System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin);
50
           System.arraycopy(bufcolumn, 0, newbufcolumn, bufsize - tokenBegin, bufpos);
51
           bufcolumn = newbufcolumn;
52

    
53
           maxNextCharInd = (bufpos += (bufsize - tokenBegin));
54
        }
55
        else
56
        {
57
           System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin);
58
           buffer = newbuffer;
59

    
60
           System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin);
61
           bufline = newbufline;
62

    
63
           System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin);
64
           bufcolumn = newbufcolumn;
65

    
66
           maxNextCharInd = (bufpos -= tokenBegin);
67
        }
68
     }
69
     catch (Throwable t)
70
     {
71
        throw new Error(t.getMessage());
72
     }
73

    
74

    
75
     bufsize += 2048;
76
     available = bufsize;
77
     tokenBegin = 0;
78
  }
79

    
80
  protected void FillBuff() throws java.io.IOException
81
  {
82
     if (maxNextCharInd == available)
83
     {
84
        if (available == bufsize)
85
        {
86
           if (tokenBegin > 2048)
87
           {
88
              bufpos = maxNextCharInd = 0;
89
              available = tokenBegin;
90
           }
91
           else if (tokenBegin < 0)
92
              bufpos = maxNextCharInd = 0;
93
           else
94
              ExpandBuff(false);
95
        }
96
        else if (available > tokenBegin)
97
           available = bufsize;
98
        else if ((tokenBegin - available) < 2048)
99
           ExpandBuff(true);
100
        else
101
           available = tokenBegin;
102
     }
103

    
104
     int i;
105
     try {
106
        if ((i = inputStream.read(buffer, maxNextCharInd,
107
                                    available - maxNextCharInd)) == -1)
108
        {
109
           inputStream.close();
110
           throw new java.io.IOException();
111
        }
112
        else
113
           maxNextCharInd += i;
114
        return;
115
     }
116
     catch(java.io.IOException e) {
117
        --bufpos;
118
        backup(0);
119
        if (tokenBegin == -1)
120
           tokenBegin = bufpos;
121
        throw e;
122
     }
123
  }
124

    
125
  public char BeginToken() throws java.io.IOException
126
  {
127
     tokenBegin = -1;
128
     char c = readChar();
129
     tokenBegin = bufpos;
130

    
131
     return c;
132
  }
133

    
134
  protected void UpdateLineColumn(char c)
135
  {
136
     column++;
137

    
138
     if (prevCharIsLF)
139
     {
140
        prevCharIsLF = false;
141
        line += (column = 1);
142
     }
143
     else if (prevCharIsCR)
144
     {
145
        prevCharIsCR = false;
146
        if (c == '\n')
147
        {
148
           prevCharIsLF = true;
149
        }
150
        else
151
           line += (column = 1);
152
     }
153

    
154
     switch (c)
155
     {
156
        case '\r' :
157
           prevCharIsCR = true;
158
           break;
159
        case '\n' :
160
           prevCharIsLF = true;
161
           break;
162
        case '\t' :
163
           column--;
164
           column += (8 - (column & 07));
165
           break;
166
        default :
167
           break;
168
     }
169

    
170
     bufline[bufpos] = line;
171
     bufcolumn[bufpos] = column;
172
  }
173

    
174
  public char readChar() throws java.io.IOException
175
  {
176
     if (inBuf > 0)
177
     {
178
        --inBuf;
179

    
180
        if (++bufpos == bufsize)
181
           bufpos = 0;
182

    
183
        return buffer[bufpos];
184
     }
185

    
186
     if (++bufpos >= maxNextCharInd)
187
        FillBuff();
188

    
189
     char c = buffer[bufpos];
190

    
191
     UpdateLineColumn(c);
192
     return (c);
193
  }
194

    
195
  /**
196
   * @deprecated 
197
   * @see #getEndColumn
198
   */
199

    
200
  public int getColumn() {
201
     return bufcolumn[bufpos];
202
  }
203

    
204
  /**
205
   * @deprecated 
206
   * @see #getEndLine
207
   */
208

    
209
  public int getLine() {
210
     return bufline[bufpos];
211
  }
212

    
213
  public int getEndColumn() {
214
     return bufcolumn[bufpos];
215
  }
216

    
217
  public int getEndLine() {
218
     return bufline[bufpos];
219
  }
220

    
221
  public int getBeginColumn() {
222
     return bufcolumn[tokenBegin];
223
  }
224

    
225
  public int getBeginLine() {
226
     return bufline[tokenBegin];
227
  }
228

    
229
  public void backup(int amount) {
230

    
231
    inBuf += amount;
232
    if ((bufpos -= amount) < 0)
233
       bufpos += bufsize;
234
  }
235

    
236
  public SimpleCharStream(java.io.Reader dstream, int startline,
237
  int startcolumn, int buffersize)
238
  {
239
    inputStream = dstream;
240
    line = startline;
241
    column = startcolumn - 1;
242

    
243
    available = bufsize = buffersize;
244
    buffer = new char[buffersize];
245
    bufline = new int[buffersize];
246
    bufcolumn = new int[buffersize];
247
  }
248

    
249
  public SimpleCharStream(java.io.Reader dstream, int startline,
250
                                                           int startcolumn)
251
  {
252
     this(dstream, startline, startcolumn, 4096);
253
  }
254

    
255
  public SimpleCharStream(java.io.Reader dstream)
256
  {
257
     this(dstream, 1, 1, 4096);
258
  }
259
  public void ReInit(java.io.Reader dstream, int startline,
260
  int startcolumn, int buffersize)
261
  {
262
    inputStream = dstream;
263
    line = startline;
264
    column = startcolumn - 1;
265

    
266
    if (buffer == null || buffersize != buffer.length)
267
    {
268
      available = bufsize = buffersize;
269
      buffer = new char[buffersize];
270
      bufline = new int[buffersize];
271
      bufcolumn = new int[buffersize];
272
    }
273
    prevCharIsLF = prevCharIsCR = false;
274
    tokenBegin = inBuf = maxNextCharInd = 0;
275
    bufpos = -1;
276
  }
277

    
278
  public void ReInit(java.io.Reader dstream, int startline,
279
                                                           int startcolumn)
280
  {
281
     ReInit(dstream, startline, startcolumn, 4096);
282
  }
283

    
284
  public void ReInit(java.io.Reader dstream)
285
  {
286
     ReInit(dstream, 1, 1, 4096);
287
  }
288
  public SimpleCharStream(java.io.InputStream dstream, int startline,
289
  int startcolumn, int buffersize)
290
  {
291
     this(new java.io.InputStreamReader(dstream), startline, startcolumn, 4096);
292
  }
293

    
294
  public SimpleCharStream(java.io.InputStream dstream, int startline,
295
                                                           int startcolumn)
296
  {
297
     this(dstream, startline, startcolumn, 4096);
298
  }
299

    
300
  public SimpleCharStream(java.io.InputStream dstream)
301
  {
302
     this(dstream, 1, 1, 4096);
303
  }
304

    
305
  public void ReInit(java.io.InputStream dstream, int startline,
306
                          int startcolumn, int buffersize)
307
  {
308
     ReInit(new java.io.InputStreamReader(dstream), startline, startcolumn, 4096);
309
  }
310

    
311
  public void ReInit(java.io.InputStream dstream)
312
  {
313
     ReInit(dstream, 1, 1, 4096);
314
  }
315
  public void ReInit(java.io.InputStream dstream, int startline,
316
                                                           int startcolumn)
317
  {
318
     ReInit(dstream, startline, startcolumn, 4096);
319
  }
320
  public String GetImage()
321
  {
322
     if (bufpos >= tokenBegin)
323
        return new String(buffer, tokenBegin, bufpos - tokenBegin + 1);
324
     else
325
        return new String(buffer, tokenBegin, bufsize - tokenBegin) +
326
                              new String(buffer, 0, bufpos + 1);
327
  }
328

    
329
  public char[] GetSuffix(int len)
330
  {
331
     char[] ret = new char[len];
332

    
333
     if ((bufpos + 1) >= len)
334
        System.arraycopy(buffer, bufpos - len + 1, ret, 0, len);
335
     else
336
     {
337
        System.arraycopy(buffer, bufsize - (len - bufpos - 1), ret, 0,
338
                                                          len - bufpos - 1);
339
        System.arraycopy(buffer, 0, ret, len - bufpos - 1, bufpos + 1);
340
     }
341

    
342
     return ret;
343
  }
344

    
345
  public void Done()
346
  {
347
     buffer = null;
348
     bufline = null;
349
     bufcolumn = null;
350
  }
351

    
352
  /**
353
   * Method to adjust line and column numbers for the start of a token.
354
   */
355
  public void adjustBeginLineColumn(int newLine, int newCol)
356
  {
357
     int start = tokenBegin;
358
     int len;
359

    
360
     if (bufpos >= tokenBegin)
361
     {
362
        len = bufpos - tokenBegin + inBuf + 1;
363
     }
364
     else
365
     {
366
        len = bufsize - tokenBegin + bufpos + 1 + inBuf;
367
     }
368

    
369
     int i = 0, j = 0, k = 0;
370
     int nextColDiff = 0, columnDiff = 0;
371

    
372
     while (i < len &&
373
            bufline[j = start % bufsize] == bufline[k = ++start % bufsize])
374
     {
375
        bufline[j] = newLine;
376
        nextColDiff = columnDiff + bufcolumn[k] - bufcolumn[j];
377
        bufcolumn[j] = newCol + columnDiff;
378
        columnDiff = nextColDiff;
379
        i++;
380
     } 
381

    
382
     if (i < len)
383
     {
384
        bufline[j] = newLine++;
385
        bufcolumn[j] = newCol + columnDiff;
386

    
387
        while (i++ < len)
388
        {
389
           if (bufline[j = start % bufsize] != bufline[++start % bufsize])
390
              bufline[j] = newLine++;
391
           else
392
              bufline[j] = newLine;
393
        }
394
     }
395

    
396
     line = bufline[j];
397
     column = bufcolumn[j];
398
  }
399

    
400
}