/src/minijava/main/JavaCharStream.java

http://antonyminijava.googlecode.com/ · Java · 616 lines · 479 code · 86 blank · 51 comment · 63 complexity · 0133b1f95e24a1f533e8c60cad8d271f MD5 · raw file

  1. package minijava.main;
  2. /* Generated By:JavaCC: Do not edit this line. JavaCharStream.java Version 5.0 */
  3. /* JavaCCOptions:STATIC=false,SUPPORT_CLASS_VISIBILITY_PUBLIC=true */
  4. /**
  5. * An implementation of interface CharStream, where the stream is assumed to
  6. * contain only ASCII characters (with java-like unicode escape processing).
  7. */
  8. public
  9. class JavaCharStream
  10. {
  11. /** Whether parser is static. */
  12. public static final boolean staticFlag = false;
  13. static final int hexval(char c) throws java.io.IOException {
  14. switch(c)
  15. {
  16. case '0' :
  17. return 0;
  18. case '1' :
  19. return 1;
  20. case '2' :
  21. return 2;
  22. case '3' :
  23. return 3;
  24. case '4' :
  25. return 4;
  26. case '5' :
  27. return 5;
  28. case '6' :
  29. return 6;
  30. case '7' :
  31. return 7;
  32. case '8' :
  33. return 8;
  34. case '9' :
  35. return 9;
  36. case 'a' :
  37. case 'A' :
  38. return 10;
  39. case 'b' :
  40. case 'B' :
  41. return 11;
  42. case 'c' :
  43. case 'C' :
  44. return 12;
  45. case 'd' :
  46. case 'D' :
  47. return 13;
  48. case 'e' :
  49. case 'E' :
  50. return 14;
  51. case 'f' :
  52. case 'F' :
  53. return 15;
  54. }
  55. throw new java.io.IOException(); // Should never come here
  56. }
  57. /** Position in buffer. */
  58. public int bufpos = -1;
  59. int bufsize;
  60. int available;
  61. int tokenBegin;
  62. protected int bufline[];
  63. protected int bufcolumn[];
  64. protected int column = 0;
  65. protected int line = 1;
  66. protected boolean prevCharIsCR = false;
  67. protected boolean prevCharIsLF = false;
  68. protected java.io.Reader inputStream;
  69. protected char[] nextCharBuf;
  70. protected char[] buffer;
  71. protected int maxNextCharInd = 0;
  72. protected int nextCharInd = -1;
  73. protected int inBuf = 0;
  74. protected int tabSize = 8;
  75. protected void setTabSize(int i) { tabSize = i; }
  76. protected int getTabSize(int i) { return tabSize; }
  77. protected void ExpandBuff(boolean wrapAround)
  78. {
  79. char[] newbuffer = new char[bufsize + 2048];
  80. int newbufline[] = new int[bufsize + 2048];
  81. int newbufcolumn[] = new int[bufsize + 2048];
  82. try
  83. {
  84. if (wrapAround)
  85. {
  86. System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin);
  87. System.arraycopy(buffer, 0, newbuffer, bufsize - tokenBegin, bufpos);
  88. buffer = newbuffer;
  89. System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin);
  90. System.arraycopy(bufline, 0, newbufline, bufsize - tokenBegin, bufpos);
  91. bufline = newbufline;
  92. System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin);
  93. System.arraycopy(bufcolumn, 0, newbufcolumn, bufsize - tokenBegin, bufpos);
  94. bufcolumn = newbufcolumn;
  95. bufpos += (bufsize - tokenBegin);
  96. }
  97. else
  98. {
  99. System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin);
  100. buffer = newbuffer;
  101. System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin);
  102. bufline = newbufline;
  103. System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin);
  104. bufcolumn = newbufcolumn;
  105. bufpos -= tokenBegin;
  106. }
  107. }
  108. catch (Throwable t)
  109. {
  110. throw new Error(t.getMessage());
  111. }
  112. available = (bufsize += 2048);
  113. tokenBegin = 0;
  114. }
  115. protected void FillBuff() throws java.io.IOException
  116. {
  117. int i;
  118. if (maxNextCharInd == 4096)
  119. maxNextCharInd = nextCharInd = 0;
  120. try {
  121. if ((i = inputStream.read(nextCharBuf, maxNextCharInd,
  122. 4096 - maxNextCharInd)) == -1)
  123. {
  124. inputStream.close();
  125. throw new java.io.IOException();
  126. }
  127. else
  128. maxNextCharInd += i;
  129. return;
  130. }
  131. catch(java.io.IOException e) {
  132. if (bufpos != 0)
  133. {
  134. --bufpos;
  135. backup(0);
  136. }
  137. else
  138. {
  139. bufline[bufpos] = line;
  140. bufcolumn[bufpos] = column;
  141. }
  142. throw e;
  143. }
  144. }
  145. protected char ReadByte() throws java.io.IOException
  146. {
  147. if (++nextCharInd >= maxNextCharInd)
  148. FillBuff();
  149. return nextCharBuf[nextCharInd];
  150. }
  151. /** @return starting character for token. */
  152. public char BeginToken() throws java.io.IOException
  153. {
  154. if (inBuf > 0)
  155. {
  156. --inBuf;
  157. if (++bufpos == bufsize)
  158. bufpos = 0;
  159. tokenBegin = bufpos;
  160. return buffer[bufpos];
  161. }
  162. tokenBegin = 0;
  163. bufpos = -1;
  164. return readChar();
  165. }
  166. protected void AdjustBuffSize()
  167. {
  168. if (available == bufsize)
  169. {
  170. if (tokenBegin > 2048)
  171. {
  172. bufpos = 0;
  173. available = tokenBegin;
  174. }
  175. else
  176. ExpandBuff(false);
  177. }
  178. else if (available > tokenBegin)
  179. available = bufsize;
  180. else if ((tokenBegin - available) < 2048)
  181. ExpandBuff(true);
  182. else
  183. available = tokenBegin;
  184. }
  185. protected void UpdateLineColumn(char c)
  186. {
  187. column++;
  188. if (prevCharIsLF)
  189. {
  190. prevCharIsLF = false;
  191. line += (column = 1);
  192. }
  193. else if (prevCharIsCR)
  194. {
  195. prevCharIsCR = false;
  196. if (c == '\n')
  197. {
  198. prevCharIsLF = true;
  199. }
  200. else
  201. line += (column = 1);
  202. }
  203. switch (c)
  204. {
  205. case '\r' :
  206. prevCharIsCR = true;
  207. break;
  208. case '\n' :
  209. prevCharIsLF = true;
  210. break;
  211. case '\t' :
  212. column--;
  213. column += (tabSize - (column % tabSize));
  214. break;
  215. default :
  216. break;
  217. }
  218. bufline[bufpos] = line;
  219. bufcolumn[bufpos] = column;
  220. }
  221. /** Read a character. */
  222. public char readChar() throws java.io.IOException
  223. {
  224. if (inBuf > 0)
  225. {
  226. --inBuf;
  227. if (++bufpos == bufsize)
  228. bufpos = 0;
  229. return buffer[bufpos];
  230. }
  231. char c;
  232. if (++bufpos == available)
  233. AdjustBuffSize();
  234. if ((buffer[bufpos] = c = ReadByte()) == '\\')
  235. {
  236. UpdateLineColumn(c);
  237. int backSlashCnt = 1;
  238. for (;;) // Read all the backslashes
  239. {
  240. if (++bufpos == available)
  241. AdjustBuffSize();
  242. try
  243. {
  244. if ((buffer[bufpos] = c = ReadByte()) != '\\')
  245. {
  246. UpdateLineColumn(c);
  247. // found a non-backslash char.
  248. if ((c == 'u') && ((backSlashCnt & 1) == 1))
  249. {
  250. if (--bufpos < 0)
  251. bufpos = bufsize - 1;
  252. break;
  253. }
  254. backup(backSlashCnt);
  255. return '\\';
  256. }
  257. }
  258. catch(java.io.IOException e)
  259. {
  260. // We are returning one backslash so we should only backup (count-1)
  261. if (backSlashCnt > 1)
  262. backup(backSlashCnt-1);
  263. return '\\';
  264. }
  265. UpdateLineColumn(c);
  266. backSlashCnt++;
  267. }
  268. // Here, we have seen an odd number of backslash's followed by a 'u'
  269. try
  270. {
  271. while ((c = ReadByte()) == 'u')
  272. ++column;
  273. buffer[bufpos] = c = (char)(hexval(c) << 12 |
  274. hexval(ReadByte()) << 8 |
  275. hexval(ReadByte()) << 4 |
  276. hexval(ReadByte()));
  277. column += 4;
  278. }
  279. catch(java.io.IOException e)
  280. {
  281. throw new Error("Invalid escape character at line " + line +
  282. " column " + column + ".");
  283. }
  284. if (backSlashCnt == 1)
  285. return c;
  286. else
  287. {
  288. backup(backSlashCnt - 1);
  289. return '\\';
  290. }
  291. }
  292. else
  293. {
  294. UpdateLineColumn(c);
  295. return c;
  296. }
  297. }
  298. @Deprecated
  299. /**
  300. * @deprecated
  301. * @see #getEndColumn
  302. */
  303. public int getColumn() {
  304. return bufcolumn[bufpos];
  305. }
  306. @Deprecated
  307. /**
  308. * @deprecated
  309. * @see #getEndLine
  310. */
  311. public int getLine() {
  312. return bufline[bufpos];
  313. }
  314. /** Get end column. */
  315. public int getEndColumn() {
  316. return bufcolumn[bufpos];
  317. }
  318. /** Get end line. */
  319. public int getEndLine() {
  320. return bufline[bufpos];
  321. }
  322. /** @return column of token start */
  323. public int getBeginColumn() {
  324. return bufcolumn[tokenBegin];
  325. }
  326. /** @return line number of token start */
  327. public int getBeginLine() {
  328. return bufline[tokenBegin];
  329. }
  330. /** Retreat. */
  331. public void backup(int amount) {
  332. inBuf += amount;
  333. if ((bufpos -= amount) < 0)
  334. bufpos += bufsize;
  335. }
  336. /** Constructor. */
  337. public JavaCharStream(java.io.Reader dstream,
  338. int startline, int startcolumn, int buffersize)
  339. {
  340. inputStream = dstream;
  341. line = startline;
  342. column = startcolumn - 1;
  343. available = bufsize = buffersize;
  344. buffer = new char[buffersize];
  345. bufline = new int[buffersize];
  346. bufcolumn = new int[buffersize];
  347. nextCharBuf = new char[4096];
  348. }
  349. /** Constructor. */
  350. public JavaCharStream(java.io.Reader dstream,
  351. int startline, int startcolumn)
  352. {
  353. this(dstream, startline, startcolumn, 4096);
  354. }
  355. /** Constructor. */
  356. public JavaCharStream(java.io.Reader dstream)
  357. {
  358. this(dstream, 1, 1, 4096);
  359. }
  360. /** Reinitialise. */
  361. public void ReInit(java.io.Reader dstream,
  362. int startline, int startcolumn, int buffersize)
  363. {
  364. inputStream = dstream;
  365. line = startline;
  366. column = startcolumn - 1;
  367. if (buffer == null || buffersize != buffer.length)
  368. {
  369. available = bufsize = buffersize;
  370. buffer = new char[buffersize];
  371. bufline = new int[buffersize];
  372. bufcolumn = new int[buffersize];
  373. nextCharBuf = new char[4096];
  374. }
  375. prevCharIsLF = prevCharIsCR = false;
  376. tokenBegin = inBuf = maxNextCharInd = 0;
  377. nextCharInd = bufpos = -1;
  378. }
  379. /** Reinitialise. */
  380. public void ReInit(java.io.Reader dstream,
  381. int startline, int startcolumn)
  382. {
  383. ReInit(dstream, startline, startcolumn, 4096);
  384. }
  385. /** Reinitialise. */
  386. public void ReInit(java.io.Reader dstream)
  387. {
  388. ReInit(dstream, 1, 1, 4096);
  389. }
  390. /** Constructor. */
  391. public JavaCharStream(java.io.InputStream dstream, String encoding, int startline,
  392. int startcolumn, int buffersize) throws java.io.UnsupportedEncodingException
  393. {
  394. this(encoding == null ? new java.io.InputStreamReader(dstream) : new java.io.InputStreamReader(dstream, encoding), startline, startcolumn, buffersize);
  395. }
  396. /** Constructor. */
  397. public JavaCharStream(java.io.InputStream dstream, int startline,
  398. int startcolumn, int buffersize)
  399. {
  400. this(new java.io.InputStreamReader(dstream), startline, startcolumn, 4096);
  401. }
  402. /** Constructor. */
  403. public JavaCharStream(java.io.InputStream dstream, String encoding, int startline,
  404. int startcolumn) throws java.io.UnsupportedEncodingException
  405. {
  406. this(dstream, encoding, startline, startcolumn, 4096);
  407. }
  408. /** Constructor. */
  409. public JavaCharStream(java.io.InputStream dstream, int startline,
  410. int startcolumn)
  411. {
  412. this(dstream, startline, startcolumn, 4096);
  413. }
  414. /** Constructor. */
  415. public JavaCharStream(java.io.InputStream dstream, String encoding) throws java.io.UnsupportedEncodingException
  416. {
  417. this(dstream, encoding, 1, 1, 4096);
  418. }
  419. /** Constructor. */
  420. public JavaCharStream(java.io.InputStream dstream)
  421. {
  422. this(dstream, 1, 1, 4096);
  423. }
  424. /** Reinitialise. */
  425. public void ReInit(java.io.InputStream dstream, String encoding, int startline,
  426. int startcolumn, int buffersize) throws java.io.UnsupportedEncodingException
  427. {
  428. ReInit(encoding == null ? new java.io.InputStreamReader(dstream) : new java.io.InputStreamReader(dstream, encoding), startline, startcolumn, buffersize);
  429. }
  430. /** Reinitialise. */
  431. public void ReInit(java.io.InputStream dstream, int startline,
  432. int startcolumn, int buffersize)
  433. {
  434. ReInit(new java.io.InputStreamReader(dstream), startline, startcolumn, buffersize);
  435. }
  436. /** Reinitialise. */
  437. public void ReInit(java.io.InputStream dstream, String encoding, int startline,
  438. int startcolumn) throws java.io.UnsupportedEncodingException
  439. {
  440. ReInit(dstream, encoding, startline, startcolumn, 4096);
  441. }
  442. /** Reinitialise. */
  443. public void ReInit(java.io.InputStream dstream, int startline,
  444. int startcolumn)
  445. {
  446. ReInit(dstream, startline, startcolumn, 4096);
  447. }
  448. /** Reinitialise. */
  449. public void ReInit(java.io.InputStream dstream, String encoding) throws java.io.UnsupportedEncodingException
  450. {
  451. ReInit(dstream, encoding, 1, 1, 4096);
  452. }
  453. /** Reinitialise. */
  454. public void ReInit(java.io.InputStream dstream)
  455. {
  456. ReInit(dstream, 1, 1, 4096);
  457. }
  458. /** @return token image as String */
  459. public String GetImage()
  460. {
  461. if (bufpos >= tokenBegin)
  462. return new String(buffer, tokenBegin, bufpos - tokenBegin + 1);
  463. else
  464. return new String(buffer, tokenBegin, bufsize - tokenBegin) +
  465. new String(buffer, 0, bufpos + 1);
  466. }
  467. /** @return suffix */
  468. public char[] GetSuffix(int len)
  469. {
  470. char[] ret = new char[len];
  471. if ((bufpos + 1) >= len)
  472. System.arraycopy(buffer, bufpos - len + 1, ret, 0, len);
  473. else
  474. {
  475. System.arraycopy(buffer, bufsize - (len - bufpos - 1), ret, 0,
  476. len - bufpos - 1);
  477. System.arraycopy(buffer, 0, ret, len - bufpos - 1, bufpos + 1);
  478. }
  479. return ret;
  480. }
  481. /** Set buffers back to null when finished. */
  482. public void Done()
  483. {
  484. nextCharBuf = null;
  485. buffer = null;
  486. bufline = null;
  487. bufcolumn = null;
  488. }
  489. /**
  490. * Method to adjust line and column numbers for the start of a token.
  491. */
  492. public void adjustBeginLineColumn(int newLine, int newCol)
  493. {
  494. int start = tokenBegin;
  495. int len;
  496. if (bufpos >= tokenBegin)
  497. {
  498. len = bufpos - tokenBegin + inBuf + 1;
  499. }
  500. else
  501. {
  502. len = bufsize - tokenBegin + bufpos + 1 + inBuf;
  503. }
  504. int i = 0, j = 0, k = 0;
  505. int nextColDiff = 0, columnDiff = 0;
  506. while (i < len && bufline[j = start % bufsize] == bufline[k = ++start % bufsize])
  507. {
  508. bufline[j] = newLine;
  509. nextColDiff = columnDiff + bufcolumn[k] - bufcolumn[j];
  510. bufcolumn[j] = newCol + columnDiff;
  511. columnDiff = nextColDiff;
  512. i++;
  513. }
  514. if (i < len)
  515. {
  516. bufline[j] = newLine++;
  517. bufcolumn[j] = newCol + columnDiff;
  518. while (i++ < len)
  519. {
  520. if (bufline[j = start % bufsize] != bufline[++start % bufsize])
  521. bufline[j] = newLine++;
  522. else
  523. bufline[j] = newLine;
  524. }
  525. }
  526. line = bufline[j];
  527. column = bufcolumn[j];
  528. }
  529. }
  530. /* JavaCC - OriginalChecksum=5d1b44b58db960d9e710bf035a698c9b (do not edit this line) */