PageRenderTime 47ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 1ms

/jEdit/tags/jedit-4-3-pre5/bsh/JavaCharStream.java

#
Java | 547 lines | 447 code | 82 blank | 18 comment | 60 complexity | a486501085bee7d5e819cd0a943015eb 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 3.0 */
  2. package bsh;
  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 void ExpandBuff(boolean wrapAround)
  71. {
  72. char[] newbuffer = new char[bufsize + 2048];
  73. int newbufline[] = new int[bufsize + 2048];
  74. int newbufcolumn[] = new int[bufsize + 2048];
  75. try
  76. {
  77. if (wrapAround)
  78. {
  79. System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin);
  80. System.arraycopy(buffer, 0, newbuffer,
  81. bufsize - tokenBegin, bufpos);
  82. buffer = newbuffer;
  83. System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin);
  84. System.arraycopy(bufline, 0, newbufline, bufsize - tokenBegin, bufpos);
  85. bufline = newbufline;
  86. System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin);
  87. System.arraycopy(bufcolumn, 0, newbufcolumn, bufsize - tokenBegin, bufpos);
  88. bufcolumn = newbufcolumn;
  89. bufpos += (bufsize - tokenBegin);
  90. }
  91. else
  92. {
  93. System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin);
  94. buffer = newbuffer;
  95. System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin);
  96. bufline = newbufline;
  97. System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin);
  98. bufcolumn = newbufcolumn;
  99. bufpos -= tokenBegin;
  100. }
  101. }
  102. catch (Throwable t)
  103. {
  104. throw new Error(t.getMessage());
  105. }
  106. available = (bufsize += 2048);
  107. tokenBegin = 0;
  108. }
  109. protected void FillBuff() throws java.io.IOException
  110. {
  111. int i;
  112. if (maxNextCharInd == 4096)
  113. maxNextCharInd = nextCharInd = 0;
  114. try {
  115. if ((i = inputStream.read(nextCharBuf, maxNextCharInd,
  116. 4096 - maxNextCharInd)) == -1)
  117. {
  118. inputStream.close();
  119. throw new java.io.IOException();
  120. }
  121. else
  122. maxNextCharInd += i;
  123. return;
  124. }
  125. catch(java.io.IOException e) {
  126. if (bufpos != 0)
  127. {
  128. --bufpos;
  129. backup(0);
  130. }
  131. else
  132. {
  133. bufline[bufpos] = line;
  134. bufcolumn[bufpos] = column;
  135. }
  136. throw e;
  137. }
  138. }
  139. protected char ReadByte() throws java.io.IOException
  140. {
  141. if (++nextCharInd >= maxNextCharInd)
  142. FillBuff();
  143. return nextCharBuf[nextCharInd];
  144. }
  145. public char BeginToken() throws java.io.IOException
  146. {
  147. if (inBuf > 0)
  148. {
  149. --inBuf;
  150. if (++bufpos == bufsize)
  151. bufpos = 0;
  152. tokenBegin = bufpos;
  153. return buffer[bufpos];
  154. }
  155. tokenBegin = 0;
  156. bufpos = -1;
  157. return readChar();
  158. }
  159. protected void AdjustBuffSize()
  160. {
  161. if (available == bufsize)
  162. {
  163. if (tokenBegin > 2048)
  164. {
  165. bufpos = 0;
  166. available = tokenBegin;
  167. }
  168. else
  169. ExpandBuff(false);
  170. }
  171. else if (available > tokenBegin)
  172. available = bufsize;
  173. else if ((tokenBegin - available) < 2048)
  174. ExpandBuff(true);
  175. else
  176. available = tokenBegin;
  177. }
  178. protected void UpdateLineColumn(char c)
  179. {
  180. column++;
  181. if (prevCharIsLF)
  182. {
  183. prevCharIsLF = false;
  184. line += (column = 1);
  185. }
  186. else if (prevCharIsCR)
  187. {
  188. prevCharIsCR = false;
  189. if (c == '\n')
  190. {
  191. prevCharIsLF = true;
  192. }
  193. else
  194. line += (column = 1);
  195. }
  196. switch (c)
  197. {
  198. case '\r' :
  199. prevCharIsCR = true;
  200. break;
  201. case '\n' :
  202. prevCharIsLF = true;
  203. break;
  204. case '\t' :
  205. column--;
  206. column += (8 - (column & 07));
  207. break;
  208. default :
  209. break;
  210. }
  211. bufline[bufpos] = line;
  212. bufcolumn[bufpos] = column;
  213. }
  214. public char readChar() throws java.io.IOException
  215. {
  216. if (inBuf > 0)
  217. {
  218. --inBuf;
  219. if (++bufpos == bufsize)
  220. bufpos = 0;
  221. return buffer[bufpos];
  222. }
  223. char c;
  224. if (++bufpos == available)
  225. AdjustBuffSize();
  226. if ((buffer[bufpos] = c = ReadByte()) == '\\')
  227. {
  228. UpdateLineColumn(c);
  229. int backSlashCnt = 1;
  230. for (;;) // Read all the backslashes
  231. {
  232. if (++bufpos == available)
  233. AdjustBuffSize();
  234. try
  235. {
  236. if ((buffer[bufpos] = c = ReadByte()) != '\\')
  237. {
  238. UpdateLineColumn(c);
  239. // found a non-backslash char.
  240. if ((c == 'u') && ((backSlashCnt & 1) == 1))
  241. {
  242. if (--bufpos < 0)
  243. bufpos = bufsize - 1;
  244. break;
  245. }
  246. backup(backSlashCnt);
  247. return '\\';
  248. }
  249. }
  250. catch(java.io.IOException e)
  251. {
  252. if (backSlashCnt > 1)
  253. backup(backSlashCnt);
  254. return '\\';
  255. }
  256. UpdateLineColumn(c);
  257. backSlashCnt++;
  258. }
  259. // Here, we have seen an odd number of backslash's followed by a 'u'
  260. try
  261. {
  262. while ((c = ReadByte()) == 'u')
  263. ++column;
  264. buffer[bufpos] = c = (char)(hexval(c) << 12 |
  265. hexval(ReadByte()) << 8 |
  266. hexval(ReadByte()) << 4 |
  267. hexval(ReadByte()));
  268. column += 4;
  269. }
  270. catch(java.io.IOException e)
  271. {
  272. throw new Error("Invalid escape character at line " + line +
  273. " column " + column + ".");
  274. }
  275. if (backSlashCnt == 1)
  276. return c;
  277. else
  278. {
  279. backup(backSlashCnt - 1);
  280. return '\\';
  281. }
  282. }
  283. else
  284. {
  285. UpdateLineColumn(c);
  286. return (c);
  287. }
  288. }
  289. /**
  290. * @deprecated
  291. * @see #getEndColumn
  292. */
  293. public int getColumn() {
  294. return bufcolumn[bufpos];
  295. }
  296. /**
  297. * @deprecated
  298. * @see #getEndLine
  299. */
  300. public int getLine() {
  301. return bufline[bufpos];
  302. }
  303. public int getEndColumn() {
  304. return bufcolumn[bufpos];
  305. }
  306. public int getEndLine() {
  307. return bufline[bufpos];
  308. }
  309. public int getBeginColumn() {
  310. return bufcolumn[tokenBegin];
  311. }
  312. public int getBeginLine() {
  313. return bufline[tokenBegin];
  314. }
  315. public void backup(int amount) {
  316. inBuf += amount;
  317. if ((bufpos -= amount) < 0)
  318. bufpos += bufsize;
  319. }
  320. public JavaCharStream(java.io.Reader dstream,
  321. int startline, int startcolumn, int buffersize)
  322. {
  323. inputStream = dstream;
  324. line = startline;
  325. column = startcolumn - 1;
  326. available = bufsize = buffersize;
  327. buffer = new char[buffersize];
  328. bufline = new int[buffersize];
  329. bufcolumn = new int[buffersize];
  330. nextCharBuf = new char[4096];
  331. }
  332. public JavaCharStream(java.io.Reader dstream,
  333. int startline, int startcolumn)
  334. {
  335. this(dstream, startline, startcolumn, 4096);
  336. }
  337. public JavaCharStream(java.io.Reader dstream)
  338. {
  339. this(dstream, 1, 1, 4096);
  340. }
  341. public void ReInit(java.io.Reader dstream,
  342. int startline, int startcolumn, int buffersize)
  343. {
  344. inputStream = dstream;
  345. line = startline;
  346. column = startcolumn - 1;
  347. if (buffer == null || buffersize != buffer.length)
  348. {
  349. available = bufsize = buffersize;
  350. buffer = new char[buffersize];
  351. bufline = new int[buffersize];
  352. bufcolumn = new int[buffersize];
  353. nextCharBuf = new char[4096];
  354. }
  355. prevCharIsLF = prevCharIsCR = false;
  356. tokenBegin = inBuf = maxNextCharInd = 0;
  357. nextCharInd = bufpos = -1;
  358. }
  359. public void ReInit(java.io.Reader dstream,
  360. int startline, int startcolumn)
  361. {
  362. ReInit(dstream, startline, startcolumn, 4096);
  363. }
  364. public void ReInit(java.io.Reader dstream)
  365. {
  366. ReInit(dstream, 1, 1, 4096);
  367. }
  368. public JavaCharStream(java.io.InputStream dstream, int startline,
  369. int startcolumn, int buffersize)
  370. {
  371. this(new java.io.InputStreamReader(dstream), startline, startcolumn, 4096);
  372. }
  373. public JavaCharStream(java.io.InputStream dstream, int startline,
  374. int startcolumn)
  375. {
  376. this(dstream, startline, startcolumn, 4096);
  377. }
  378. public JavaCharStream(java.io.InputStream dstream)
  379. {
  380. this(dstream, 1, 1, 4096);
  381. }
  382. public void ReInit(java.io.InputStream dstream, int startline,
  383. int startcolumn, int buffersize)
  384. {
  385. ReInit(new java.io.InputStreamReader(dstream), startline, startcolumn, 4096);
  386. }
  387. public void ReInit(java.io.InputStream dstream, int startline,
  388. int startcolumn)
  389. {
  390. ReInit(dstream, startline, startcolumn, 4096);
  391. }
  392. public void ReInit(java.io.InputStream dstream)
  393. {
  394. ReInit(dstream, 1, 1, 4096);
  395. }
  396. public String GetImage()
  397. {
  398. if (bufpos >= tokenBegin)
  399. return new String(buffer, tokenBegin, bufpos - tokenBegin + 1);
  400. else
  401. return new String(buffer, tokenBegin, bufsize - tokenBegin) +
  402. new String(buffer, 0, bufpos + 1);
  403. }
  404. public char[] GetSuffix(int len)
  405. {
  406. char[] ret = new char[len];
  407. if ((bufpos + 1) >= len)
  408. System.arraycopy(buffer, bufpos - len + 1, ret, 0, len);
  409. else
  410. {
  411. System.arraycopy(buffer, bufsize - (len - bufpos - 1), ret, 0,
  412. len - bufpos - 1);
  413. System.arraycopy(buffer, 0, ret, len - bufpos - 1, bufpos + 1);
  414. }
  415. return ret;
  416. }
  417. public void Done()
  418. {
  419. nextCharBuf = null;
  420. buffer = null;
  421. bufline = null;
  422. bufcolumn = null;
  423. }
  424. /**
  425. * Method to adjust line and column numbers for the start of a token.<BR>
  426. */
  427. public void adjustBeginLineColumn(int newLine, int newCol)
  428. {
  429. int start = tokenBegin;
  430. int len;
  431. if (bufpos >= tokenBegin)
  432. {
  433. len = bufpos - tokenBegin + inBuf + 1;
  434. }
  435. else
  436. {
  437. len = bufsize - tokenBegin + bufpos + 1 + inBuf;
  438. }
  439. int i = 0, j = 0, k = 0;
  440. int nextColDiff = 0, columnDiff = 0;
  441. while (i < len &&
  442. bufline[j = start % bufsize] == bufline[k = ++start % bufsize])
  443. {
  444. bufline[j] = newLine;
  445. nextColDiff = columnDiff + bufcolumn[k] - bufcolumn[j];
  446. bufcolumn[j] = newCol + columnDiff;
  447. columnDiff = nextColDiff;
  448. i++;
  449. }
  450. if (i < len)
  451. {
  452. bufline[j] = newLine++;
  453. bufcolumn[j] = newCol + columnDiff;
  454. while (i++ < len)
  455. {
  456. if (bufline[j = start % bufsize] != bufline[++start % bufsize])
  457. bufline[j] = newLine++;
  458. else
  459. bufline[j] = newLine;
  460. }
  461. }
  462. line = bufline[j];
  463. column = bufcolumn[j];
  464. }
  465. }