1 /* Generated By:JavaCC: Do not edit this line. JavaCharStream.java Version 6.0 */
2 /* JavaCCOptions:STATIC=false,SUPPORT_CLASS_VISIBILITY_PUBLIC=true */
3 package edu.uci.eecs.specCompiler.grammerParser;
6 * An implementation of interface CharStream, where the stream is assumed to
7 * contain only ASCII characters (with java-like unicode escape processing).
13 /** Whether parser is static. */
14 public static final boolean staticFlag = false;
16 static final int hexval(char c) throws java.io.IOException {
60 throw new java.io.IOException(); // Should never come here
63 /** Position in buffer. */
64 public int bufpos = -1;
68 protected int bufline[];
69 protected int bufcolumn[];
71 protected int column = 0;
72 protected int line = 1;
74 protected boolean prevCharIsCR = false;
75 protected boolean prevCharIsLF = false;
77 protected java.io.Reader inputStream;
79 protected char[] nextCharBuf;
80 protected char[] buffer;
81 protected int maxNextCharInd = 0;
82 protected int nextCharInd = -1;
83 protected int inBuf = 0;
84 protected int tabSize = 8;
85 protected boolean trackLineColumn = false;
87 public void setTabSize(int i) { tabSize = i; }
88 public int getTabSize() { return tabSize; }
90 protected void ExpandBuff(boolean wrapAround)
92 char[] newbuffer = new char[bufsize + 2048];
93 int newbufline[] = new int[bufsize + 2048];
94 int newbufcolumn[] = new int[bufsize + 2048];
100 System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin);
101 System.arraycopy(buffer, 0, newbuffer, bufsize - tokenBegin, bufpos);
104 System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin);
105 System.arraycopy(bufline, 0, newbufline, bufsize - tokenBegin, bufpos);
106 bufline = newbufline;
108 System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin);
109 System.arraycopy(bufcolumn, 0, newbufcolumn, bufsize - tokenBegin, bufpos);
110 bufcolumn = newbufcolumn;
112 bufpos += (bufsize - tokenBegin);
116 System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin);
119 System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin);
120 bufline = newbufline;
122 System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin);
123 bufcolumn = newbufcolumn;
125 bufpos -= tokenBegin;
130 throw new Error(t.getMessage());
133 available = (bufsize += 2048);
137 protected void FillBuff() throws java.io.IOException
140 if (maxNextCharInd == 4096)
141 maxNextCharInd = nextCharInd = 0;
144 if ((i = inputStream.read(nextCharBuf, maxNextCharInd,
145 4096 - maxNextCharInd)) == -1)
148 throw new java.io.IOException();
154 catch(java.io.IOException e) {
162 bufline[bufpos] = line;
163 bufcolumn[bufpos] = column;
169 protected char ReadByte() throws java.io.IOException
171 if (++nextCharInd >= maxNextCharInd)
174 return nextCharBuf[nextCharInd];
177 /** @return starting character for token. */
178 public char BeginToken() throws java.io.IOException
184 if (++bufpos == bufsize)
188 return buffer[bufpos];
197 protected void AdjustBuffSize()
199 if (available == bufsize)
201 if (tokenBegin > 2048)
204 available = tokenBegin;
209 else if (available > tokenBegin)
211 else if ((tokenBegin - available) < 2048)
214 available = tokenBegin;
217 protected void UpdateLineColumn(char c)
223 prevCharIsLF = false;
224 line += (column = 1);
226 else if (prevCharIsCR)
228 prevCharIsCR = false;
234 line += (column = 1);
247 column += (tabSize - (column % tabSize));
253 bufline[bufpos] = line;
254 bufcolumn[bufpos] = column;
257 /** Read a character. */
258 public char readChar() throws java.io.IOException
264 if (++bufpos == bufsize)
267 return buffer[bufpos];
272 if (++bufpos == available)
275 if ((buffer[bufpos] = c = ReadByte()) == '\\')
277 if (trackLineColumn) { UpdateLineColumn(c); }
279 int backSlashCnt = 1;
281 for (;;) // Read all the backslashes
283 if (++bufpos == available)
288 if ((buffer[bufpos] = c = ReadByte()) != '\\')
290 if (trackLineColumn) { UpdateLineColumn(c); }
291 // found a non-backslash char.
292 if ((c == 'u') && ((backSlashCnt & 1) == 1))
295 bufpos = bufsize - 1;
300 backup(backSlashCnt);
304 catch(java.io.IOException e)
306 // We are returning one backslash so we should only backup (count-1)
307 if (backSlashCnt > 1)
308 backup(backSlashCnt-1);
313 if (trackLineColumn) { UpdateLineColumn(c); }
317 // Here, we have seen an odd number of backslash's followed by a 'u'
320 while ((c = ReadByte()) == 'u')
323 buffer[bufpos] = c = (char)(hexval(c) << 12 |
324 hexval(ReadByte()) << 8 |
325 hexval(ReadByte()) << 4 |
330 catch(java.io.IOException e)
332 throw new Error("Invalid escape character at line " + line +
333 " column " + column + ".");
336 if (backSlashCnt == 1)
340 backup(backSlashCnt - 1);
356 public int getColumn() {
357 return bufcolumn[bufpos];
365 public int getLine() {
366 return bufline[bufpos];
369 /** Get end column. */
370 public int getEndColumn() {
371 return bufcolumn[bufpos];
375 public int getEndLine() {
376 return bufline[bufpos];
379 /** @return column of token start */
380 public int getBeginColumn() {
381 return bufcolumn[tokenBegin];
384 /** @return line number of token start */
385 public int getBeginLine() {
386 return bufline[tokenBegin];
390 public void backup(int amount) {
393 if ((bufpos -= amount) < 0)
398 public JavaCharStream(java.io.Reader dstream,
399 int startline, int startcolumn, int buffersize)
401 inputStream = dstream;
403 column = startcolumn - 1;
405 available = bufsize = buffersize;
406 buffer = new char[buffersize];
407 bufline = new int[buffersize];
408 bufcolumn = new int[buffersize];
409 nextCharBuf = new char[4096];
413 public JavaCharStream(java.io.Reader dstream,
414 int startline, int startcolumn)
416 this(dstream, startline, startcolumn, 4096);
420 public JavaCharStream(java.io.Reader dstream)
422 this(dstream, 1, 1, 4096);
425 public void ReInit(java.io.Reader dstream,
426 int startline, int startcolumn, int buffersize)
428 inputStream = dstream;
430 column = startcolumn - 1;
432 if (buffer == null || buffersize != buffer.length)
434 available = bufsize = buffersize;
435 buffer = new char[buffersize];
436 bufline = new int[buffersize];
437 bufcolumn = new int[buffersize];
438 nextCharBuf = new char[4096];
440 prevCharIsLF = prevCharIsCR = false;
441 tokenBegin = inBuf = maxNextCharInd = 0;
442 nextCharInd = bufpos = -1;
446 public void ReInit(java.io.Reader dstream,
447 int startline, int startcolumn)
449 ReInit(dstream, startline, startcolumn, 4096);
453 public void ReInit(java.io.Reader dstream)
455 ReInit(dstream, 1, 1, 4096);
458 public JavaCharStream(java.io.InputStream dstream, String encoding, int startline,
459 int startcolumn, int buffersize) throws java.io.UnsupportedEncodingException
461 this(encoding == null ? new java.io.InputStreamReader(dstream) : new java.io.InputStreamReader(dstream, encoding), startline, startcolumn, buffersize);
465 public JavaCharStream(java.io.InputStream dstream, int startline,
466 int startcolumn, int buffersize)
468 this(new java.io.InputStreamReader(dstream), startline, startcolumn, 4096);
472 public JavaCharStream(java.io.InputStream dstream, String encoding, int startline,
473 int startcolumn) throws java.io.UnsupportedEncodingException
475 this(dstream, encoding, startline, startcolumn, 4096);
479 public JavaCharStream(java.io.InputStream dstream, int startline,
482 this(dstream, startline, startcolumn, 4096);
486 public JavaCharStream(java.io.InputStream dstream, String encoding) throws java.io.UnsupportedEncodingException
488 this(dstream, encoding, 1, 1, 4096);
492 public JavaCharStream(java.io.InputStream dstream)
494 this(dstream, 1, 1, 4096);
498 public void ReInit(java.io.InputStream dstream, String encoding, int startline,
499 int startcolumn, int buffersize) throws java.io.UnsupportedEncodingException
501 ReInit(encoding == null ? new java.io.InputStreamReader(dstream) : new java.io.InputStreamReader(dstream, encoding), startline, startcolumn, buffersize);
505 public void ReInit(java.io.InputStream dstream, int startline,
506 int startcolumn, int buffersize)
508 ReInit(new java.io.InputStreamReader(dstream), startline, startcolumn, buffersize);
511 public void ReInit(java.io.InputStream dstream, String encoding, int startline,
512 int startcolumn) throws java.io.UnsupportedEncodingException
514 ReInit(dstream, encoding, startline, startcolumn, 4096);
517 public void ReInit(java.io.InputStream dstream, int startline,
520 ReInit(dstream, startline, startcolumn, 4096);
523 public void ReInit(java.io.InputStream dstream, String encoding) throws java.io.UnsupportedEncodingException
525 ReInit(dstream, encoding, 1, 1, 4096);
529 public void ReInit(java.io.InputStream dstream)
531 ReInit(dstream, 1, 1, 4096);
534 /** @return token image as String */
535 public String GetImage()
537 if (bufpos >= tokenBegin)
538 return new String(buffer, tokenBegin, bufpos - tokenBegin + 1);
540 return new String(buffer, tokenBegin, bufsize - tokenBegin) +
541 new String(buffer, 0, bufpos + 1);
544 /** @return suffix */
545 public char[] GetSuffix(int len)
547 char[] ret = new char[len];
549 if ((bufpos + 1) >= len)
550 System.arraycopy(buffer, bufpos - len + 1, ret, 0, len);
553 System.arraycopy(buffer, bufsize - (len - bufpos - 1), ret, 0,
555 System.arraycopy(buffer, 0, ret, len - bufpos - 1, bufpos + 1);
561 /** Set buffers back to null when finished. */
571 * Method to adjust line and column numbers for the start of a token.
573 public void adjustBeginLineColumn(int newLine, int newCol)
575 int start = tokenBegin;
578 if (bufpos >= tokenBegin)
580 len = bufpos - tokenBegin + inBuf + 1;
584 len = bufsize - tokenBegin + bufpos + 1 + inBuf;
587 int i = 0, j = 0, k = 0;
588 int nextColDiff = 0, columnDiff = 0;
590 while (i < len && bufline[j = start % bufsize] == bufline[k = ++start % bufsize])
592 bufline[j] = newLine;
593 nextColDiff = columnDiff + bufcolumn[k] - bufcolumn[j];
594 bufcolumn[j] = newCol + columnDiff;
595 columnDiff = nextColDiff;
601 bufline[j] = newLine++;
602 bufcolumn[j] = newCol + columnDiff;
606 if (bufline[j = start % bufsize] != bufline[++start % bufsize])
607 bufline[j] = newLine++;
609 bufline[j] = newLine;
614 column = bufcolumn[j];
616 boolean getTrackLineColumn() { return trackLineColumn; }
617 void setTrackLineColumn(boolean trackLineColumn) { this.trackLineColumn = trackLineColumn; }
620 /* JavaCC - OriginalChecksum=ea8340461dd4116176ea0d0d5c055d04 (do not edit this line) */