PageRenderTime 43ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/bundles/plugins-trunk/XML/sidekick/ecmascript/parser/JavaCharStream.java

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