/bundles/plugins-trunk/PHPParser/src/gatchan/phpparser/parser/JavaCharStream.java

# · Java · 636 lines · 498 code · 89 blank · 49 comment · 63 complexity · caeaba941b868ea95e3f80528997ed0c MD5 · raw file

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