/projects/pmd-4.2.5/src/net/sourceforge/pmd/ast/JavaCharStream.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus · Java · 616 lines · 480 code · 86 blank · 50 comment · 62 complexity · 693d3294971d6eb114534592cb5488f4 MD5 · raw file

  1. /* Generated By:JavaCC: Do not edit this line. JavaCharStream.java Version 4.1 */
  2. /* JavaCCOptions:STATIC=false */
  3. package net.sourceforge.pmd.ast;
  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 class JavaCharStream implements CharStream
  9. {
  10. /** Whether parser is static. */
  11. public static final boolean staticFlag = false;
  12. static final int hexval(char c) throws java.io.IOException {
  13. switch(c)
  14. {
  15. case '0' :
  16. return 0;
  17. case '1' :
  18. return 1;
  19. case '2' :
  20. return 2;
  21. case '3' :
  22. return 3;
  23. case '4' :
  24. return 4;
  25. case '5' :
  26. return 5;
  27. case '6' :
  28. return 6;
  29. case '7' :
  30. return 7;
  31. case '8' :
  32. return 8;
  33. case '9' :
  34. return 9;
  35. case 'a' :
  36. case 'A' :
  37. return 10;
  38. case 'b' :
  39. case 'B' :
  40. return 11;
  41. case 'c' :
  42. case 'C' :
  43. return 12;
  44. case 'd' :
  45. case 'D' :
  46. return 13;
  47. case 'e' :
  48. case 'E' :
  49. return 14;
  50. case 'f' :
  51. case 'F' :
  52. return 15;
  53. }
  54. throw new java.io.IOException(); // Should never come here
  55. }
  56. /** Position in buffer. */
  57. public int bufpos = -1;
  58. int bufsize;
  59. int available;
  60. int tokenBegin;
  61. protected int bufline[];
  62. protected int bufcolumn[];
  63. protected int column = 0;
  64. protected int line = 1;
  65. protected boolean prevCharIsCR = false;
  66. protected boolean prevCharIsLF = false;
  67. protected java.io.Reader inputStream;
  68. protected char[] nextCharBuf;
  69. protected char[] buffer;
  70. protected int maxNextCharInd = 0;
  71. protected int nextCharInd = -1;
  72. protected int inBuf = 0;
  73. protected int tabSize = 8;
  74. protected void setTabSize(int i) { tabSize = i; }
  75. protected int getTabSize(int i) { return tabSize; }
  76. protected void ExpandBuff(boolean wrapAround)
  77. {
  78. char[] newbuffer = new char[bufsize + 2048];
  79. int newbufline[] = new int[bufsize + 2048];
  80. int newbufcolumn[] = new int[bufsize + 2048];
  81. try
  82. {
  83. if (wrapAround)
  84. {
  85. System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin);
  86. System.arraycopy(buffer, 0, newbuffer,
  87. 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 RuntimeException(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. if (backSlashCnt > 1)
  261. backup(backSlashCnt-1);
  262. return '\\';
  263. }
  264. UpdateLineColumn(c);
  265. backSlashCnt++;
  266. }
  267. // Here, we have seen an odd number of backslash's followed by a 'u'
  268. try
  269. {
  270. while ((c = ReadByte()) == 'u')
  271. ++column;
  272. buffer[bufpos] = c = (char)(hexval(c) << 12 |
  273. hexval(ReadByte()) << 8 |
  274. hexval(ReadByte()) << 4 |
  275. hexval(ReadByte()));
  276. column += 4;
  277. }
  278. catch(java.io.IOException e)
  279. {
  280. throw new RuntimeException("Invalid escape character at line " + line +
  281. " column " + column + ".");
  282. }
  283. if (backSlashCnt == 1)
  284. return c;
  285. else
  286. {
  287. backup(backSlashCnt - 1);
  288. return '\\';
  289. }
  290. }
  291. else
  292. {
  293. UpdateLineColumn(c);
  294. return c;
  295. }
  296. }
  297. @Deprecated
  298. /**
  299. * @deprecated
  300. * @see #getEndColumn
  301. */
  302. public int getColumn() {
  303. return bufcolumn[bufpos];
  304. }
  305. @Deprecated
  306. /**
  307. * @deprecated
  308. * @see #getEndLine
  309. */
  310. public int getLine() {
  311. return bufline[bufpos];
  312. }
  313. /** Get end column. */
  314. public int getEndColumn() {
  315. return bufcolumn[bufpos];
  316. }
  317. /** Get end line. */
  318. public int getEndLine() {
  319. return bufline[bufpos];
  320. }
  321. /** @return column of token start */
  322. public int getBeginColumn() {
  323. return bufcolumn[tokenBegin];
  324. }
  325. /** @return line number of token start */
  326. public int getBeginLine() {
  327. return bufline[tokenBegin];
  328. }
  329. /** Retreat. */
  330. public void backup(int amount) {
  331. inBuf += amount;
  332. if ((bufpos -= amount) < 0)
  333. bufpos += bufsize;
  334. }
  335. /** Constructor. */
  336. public JavaCharStream(java.io.Reader dstream,
  337. int startline, int startcolumn, int buffersize)
  338. {
  339. inputStream = dstream;
  340. line = startline;
  341. column = startcolumn - 1;
  342. available = bufsize = buffersize;
  343. buffer = new char[buffersize];
  344. bufline = new int[buffersize];
  345. bufcolumn = new int[buffersize];
  346. nextCharBuf = new char[4096];
  347. }
  348. /** Constructor. */
  349. public JavaCharStream(java.io.Reader dstream,
  350. int startline, int startcolumn)
  351. {
  352. this(dstream, startline, startcolumn, 4096);
  353. }
  354. /** Constructor. */
  355. public JavaCharStream(java.io.Reader dstream)
  356. {
  357. this(dstream, 1, 1, 4096);
  358. }
  359. /** Reinitialise. */
  360. public void ReInit(java.io.Reader dstream,
  361. int startline, int startcolumn, int buffersize)
  362. {
  363. inputStream = dstream;
  364. line = startline;
  365. column = startcolumn - 1;
  366. if (buffer == null || buffersize != buffer.length)
  367. {
  368. available = bufsize = buffersize;
  369. buffer = new char[buffersize];
  370. bufline = new int[buffersize];
  371. bufcolumn = new int[buffersize];
  372. nextCharBuf = new char[4096];
  373. }
  374. prevCharIsLF = prevCharIsCR = false;
  375. tokenBegin = inBuf = maxNextCharInd = 0;
  376. nextCharInd = bufpos = -1;
  377. }
  378. /** Reinitialise. */
  379. public void ReInit(java.io.Reader dstream,
  380. int startline, int startcolumn)
  381. {
  382. ReInit(dstream, startline, startcolumn, 4096);
  383. }
  384. /** Reinitialise. */
  385. public void ReInit(java.io.Reader dstream)
  386. {
  387. ReInit(dstream, 1, 1, 4096);
  388. }
  389. /** Constructor. */
  390. public JavaCharStream(java.io.InputStream dstream, String encoding, int startline,
  391. int startcolumn, int buffersize) throws java.io.UnsupportedEncodingException
  392. {
  393. this(encoding == null ? new java.io.InputStreamReader(dstream) : new java.io.InputStreamReader(dstream, encoding), startline, startcolumn, buffersize);
  394. }
  395. /** Constructor. */
  396. public JavaCharStream(java.io.InputStream dstream, int startline,
  397. int startcolumn, int buffersize)
  398. {
  399. this(new java.io.InputStreamReader(dstream), startline, startcolumn, 4096);
  400. }
  401. /** Constructor. */
  402. public JavaCharStream(java.io.InputStream dstream, String encoding, int startline,
  403. int startcolumn) throws java.io.UnsupportedEncodingException
  404. {
  405. this(dstream, encoding, startline, startcolumn, 4096);
  406. }
  407. /** Constructor. */
  408. public JavaCharStream(java.io.InputStream dstream, int startline,
  409. int startcolumn)
  410. {
  411. this(dstream, startline, startcolumn, 4096);
  412. }
  413. /** Constructor. */
  414. public JavaCharStream(java.io.InputStream dstream, String encoding) throws java.io.UnsupportedEncodingException
  415. {
  416. this(dstream, encoding, 1, 1, 4096);
  417. }
  418. /** Constructor. */
  419. public JavaCharStream(java.io.InputStream dstream)
  420. {
  421. this(dstream, 1, 1, 4096);
  422. }
  423. /** Reinitialise. */
  424. public void ReInit(java.io.InputStream dstream, String encoding, int startline,
  425. int startcolumn, int buffersize) throws java.io.UnsupportedEncodingException
  426. {
  427. ReInit(encoding == null ? new java.io.InputStreamReader(dstream) : new java.io.InputStreamReader(dstream, encoding), startline, startcolumn, buffersize);
  428. }
  429. /** Reinitialise. */
  430. public void ReInit(java.io.InputStream dstream, int startline,
  431. int startcolumn, int buffersize)
  432. {
  433. ReInit(new java.io.InputStreamReader(dstream), startline, startcolumn, buffersize);
  434. }
  435. /** Reinitialise. */
  436. public void ReInit(java.io.InputStream dstream, String encoding, int startline,
  437. int startcolumn) throws java.io.UnsupportedEncodingException
  438. {
  439. ReInit(dstream, encoding, startline, startcolumn, 4096);
  440. }
  441. /** Reinitialise. */
  442. public void ReInit(java.io.InputStream dstream, int startline,
  443. int startcolumn)
  444. {
  445. ReInit(dstream, startline, startcolumn, 4096);
  446. }
  447. /** Reinitialise. */
  448. public void ReInit(java.io.InputStream dstream, String encoding) throws java.io.UnsupportedEncodingException
  449. {
  450. ReInit(dstream, encoding, 1, 1, 4096);
  451. }
  452. /** Reinitialise. */
  453. public void ReInit(java.io.InputStream dstream)
  454. {
  455. ReInit(dstream, 1, 1, 4096);
  456. }
  457. /** @return token image as String */
  458. public String GetImage()
  459. {
  460. if (bufpos >= tokenBegin)
  461. return new String(buffer, tokenBegin, bufpos - tokenBegin + 1);
  462. else
  463. return new String(buffer, tokenBegin, bufsize - tokenBegin) +
  464. new String(buffer, 0, bufpos + 1);
  465. }
  466. /** @return suffix */
  467. public char[] GetSuffix(int len)
  468. {
  469. char[] ret = new char[len];
  470. if ((bufpos + 1) >= len)
  471. System.arraycopy(buffer, bufpos - len + 1, ret, 0, len);
  472. else
  473. {
  474. System.arraycopy(buffer, bufsize - (len - bufpos - 1), ret, 0,
  475. len - bufpos - 1);
  476. System.arraycopy(buffer, 0, ret, len - bufpos - 1, bufpos + 1);
  477. }
  478. return ret;
  479. }
  480. /** Set buffers back to null when finished. */
  481. public void Done()
  482. {
  483. nextCharBuf = null;
  484. buffer = null;
  485. bufline = null;
  486. bufcolumn = null;
  487. }
  488. /**
  489. * Method to adjust line and column numbers for the start of a token.
  490. */
  491. public void adjustBeginLineColumn(int newLine, int newCol)
  492. {
  493. int start = tokenBegin;
  494. int len;
  495. if (bufpos >= tokenBegin)
  496. {
  497. len = bufpos - tokenBegin + inBuf + 1;
  498. }
  499. else
  500. {
  501. len = bufsize - tokenBegin + bufpos + 1 + inBuf;
  502. }
  503. int i = 0, j = 0, k = 0;
  504. int nextColDiff = 0, columnDiff = 0;
  505. while (i < len &&
  506. 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=f18b7eb3e3e62d2724487ac20028cf2f (do not edit this line) */