OpenCores
URL https://opencores.org/ocsvn/fpga-cf/fpga-cf/trunk

Subversion Repositories fpga-cf

[/] [fpga-cf/] [trunk/] [java/] [src/] [edu/] [byu/] [cc/] [plieber/] [fpgaenet/] [debug/] [llparse/] [SimpleCharStream.java] - Blame information for rev 2

Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.