PageRenderTime 42ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/jEdit/tags/jedit-4-0-pre5/bsh/ASCII_UCodeESC_CharStream.java

#
Java | 520 lines | 427 code | 75 blank | 18 comment | 58 complexity | 86c4c1732ea7a9c8b37b43be249ab7bb 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. ASCII_UCodeESC_CharStream.java Version 0.7pre6 */
  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. final class ASCII_UCodeESC_CharStream
  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. private int bufline[];
  59. private int bufcolumn[];
  60. private int column = 0;
  61. private int line = 1;
  62. private java.io.Reader inputStream;
  63. private boolean prevCharIsCR = false;
  64. private boolean prevCharIsLF = false;
  65. private char[] nextCharBuf;
  66. private char[] buffer;
  67. private int maxNextCharInd = 0;
  68. private int nextCharInd = -1;
  69. private int inBuf = 0;
  70. private final 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. private final 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. private final char ReadByte() throws java.io.IOException
  140. {
  141. if (++nextCharInd >= maxNextCharInd)
  142. FillBuff();
  143. return nextCharBuf[nextCharInd];
  144. }
  145. public final char BeginToken() throws java.io.IOException
  146. {
  147. if (inBuf > 0)
  148. {
  149. --inBuf;
  150. return buffer[tokenBegin = (bufpos == bufsize - 1) ? (bufpos = 0)
  151. : ++bufpos];
  152. }
  153. tokenBegin = 0;
  154. bufpos = -1;
  155. return readChar();
  156. }
  157. private final void AdjustBuffSize()
  158. {
  159. if (available == bufsize)
  160. {
  161. if (tokenBegin > 2048)
  162. {
  163. bufpos = 0;
  164. available = tokenBegin;
  165. }
  166. else
  167. ExpandBuff(false);
  168. }
  169. else if (available > tokenBegin)
  170. available = bufsize;
  171. else if ((tokenBegin - available) < 2048)
  172. ExpandBuff(true);
  173. else
  174. available = tokenBegin;
  175. }
  176. private final void UpdateLineColumn(char c)
  177. {
  178. column++;
  179. if (prevCharIsLF)
  180. {
  181. prevCharIsLF = false;
  182. line += (column = 1);
  183. }
  184. else if (prevCharIsCR)
  185. {
  186. prevCharIsCR = false;
  187. if (c == '\n')
  188. {
  189. prevCharIsLF = true;
  190. }
  191. else
  192. line += (column = 1);
  193. }
  194. switch (c)
  195. {
  196. case '\r' :
  197. prevCharIsCR = true;
  198. break;
  199. case '\n' :
  200. prevCharIsLF = true;
  201. break;
  202. case '\t' :
  203. column--;
  204. column += (8 - (column & 07));
  205. break;
  206. default :
  207. break;
  208. }
  209. bufline[bufpos] = line;
  210. bufcolumn[bufpos] = column;
  211. }
  212. public final char readChar() throws java.io.IOException
  213. {
  214. if (inBuf > 0)
  215. {
  216. --inBuf;
  217. return buffer[(bufpos == bufsize - 1) ? (bufpos = 0) : ++bufpos];
  218. }
  219. char c;
  220. if (++bufpos == available)
  221. AdjustBuffSize();
  222. if (((buffer[bufpos] = c = (char)((char)0xff & ReadByte())) == '\\'))
  223. {
  224. UpdateLineColumn(c);
  225. int backSlashCnt = 1;
  226. for (;;) // Read all the backslashes
  227. {
  228. if (++bufpos == available)
  229. AdjustBuffSize();
  230. try
  231. {
  232. if ((buffer[bufpos] = c = (char)((char)0xff & ReadByte())) != '\\')
  233. {
  234. UpdateLineColumn(c);
  235. // found a non-backslash char.
  236. if ((c == 'u') && ((backSlashCnt & 1) == 1))
  237. {
  238. if (--bufpos < 0)
  239. bufpos = bufsize - 1;
  240. break;
  241. }
  242. backup(backSlashCnt);
  243. return '\\';
  244. }
  245. }
  246. catch(java.io.IOException e)
  247. {
  248. if (backSlashCnt > 1)
  249. backup(backSlashCnt);
  250. return '\\';
  251. }
  252. UpdateLineColumn(c);
  253. backSlashCnt++;
  254. }
  255. // Here, we have seen an odd number of backslash's followed by a 'u'
  256. try
  257. {
  258. while ((c = (char)((char)0xff & ReadByte())) == 'u')
  259. ++column;
  260. buffer[bufpos] = c = (char)(hexval(c) << 12 |
  261. hexval((char)((char)0xff & ReadByte())) << 8 |
  262. hexval((char)((char)0xff & ReadByte())) << 4 |
  263. hexval((char)((char)0xff & ReadByte())));
  264. column += 4;
  265. }
  266. catch(java.io.IOException e)
  267. {
  268. throw new Error("Invalid escape character at line " + line +
  269. " column " + column + ".");
  270. }
  271. if (backSlashCnt == 1)
  272. return c;
  273. else
  274. {
  275. backup(backSlashCnt - 1);
  276. return '\\';
  277. }
  278. }
  279. else
  280. {
  281. UpdateLineColumn(c);
  282. return (c);
  283. }
  284. }
  285. /**
  286. * @deprecated
  287. * @see #getEndColumn
  288. */
  289. public final int getColumn() {
  290. return bufcolumn[bufpos];
  291. }
  292. /**
  293. * @deprecated
  294. * @see #getEndLine
  295. */
  296. public final int getLine() {
  297. return bufline[bufpos];
  298. }
  299. public final int getEndColumn() {
  300. return bufcolumn[bufpos];
  301. }
  302. public final int getEndLine() {
  303. return bufline[bufpos];
  304. }
  305. public final int getBeginColumn() {
  306. return bufcolumn[tokenBegin];
  307. }
  308. public final int getBeginLine() {
  309. return bufline[tokenBegin];
  310. }
  311. public final void backup(int amount) {
  312. inBuf += amount;
  313. if ((bufpos -= amount) < 0)
  314. bufpos += bufsize;
  315. }
  316. public ASCII_UCodeESC_CharStream(java.io.Reader dstream,
  317. int startline, int startcolumn, int buffersize)
  318. {
  319. inputStream = dstream;
  320. line = startline;
  321. column = startcolumn - 1;
  322. available = bufsize = buffersize;
  323. buffer = new char[buffersize];
  324. bufline = new int[buffersize];
  325. bufcolumn = new int[buffersize];
  326. nextCharBuf = new char[4096];
  327. }
  328. public ASCII_UCodeESC_CharStream(java.io.Reader dstream,
  329. int startline, int startcolumn)
  330. {
  331. this(dstream, startline, startcolumn, 4096);
  332. }
  333. public void ReInit(java.io.Reader dstream,
  334. int startline, int startcolumn, int buffersize)
  335. {
  336. inputStream = dstream;
  337. line = startline;
  338. column = startcolumn - 1;
  339. if (buffer == null || buffersize != buffer.length)
  340. {
  341. available = bufsize = buffersize;
  342. buffer = new char[buffersize];
  343. bufline = new int[buffersize];
  344. bufcolumn = new int[buffersize];
  345. nextCharBuf = new char[4096];
  346. }
  347. prevCharIsLF = prevCharIsCR = false;
  348. tokenBegin = inBuf = maxNextCharInd = 0;
  349. nextCharInd = bufpos = -1;
  350. }
  351. public void ReInit(java.io.Reader dstream,
  352. int startline, int startcolumn)
  353. {
  354. ReInit(dstream, startline, startcolumn, 4096);
  355. }
  356. public ASCII_UCodeESC_CharStream(java.io.InputStream dstream, int startline,
  357. int startcolumn, int buffersize)
  358. {
  359. this(new java.io.InputStreamReader(dstream), startline, startcolumn, 4096);
  360. }
  361. public ASCII_UCodeESC_CharStream(java.io.InputStream dstream, int startline,
  362. int startcolumn)
  363. {
  364. this(dstream, startline, startcolumn, 4096);
  365. }
  366. public void ReInit(java.io.InputStream dstream, int startline,
  367. int startcolumn, int buffersize)
  368. {
  369. ReInit(new java.io.InputStreamReader(dstream), startline, startcolumn, 4096);
  370. }
  371. public void ReInit(java.io.InputStream dstream, int startline,
  372. int startcolumn)
  373. {
  374. ReInit(dstream, startline, startcolumn, 4096);
  375. }
  376. public final String GetImage()
  377. {
  378. if (bufpos >= tokenBegin)
  379. return new String(buffer, tokenBegin, bufpos - tokenBegin + 1);
  380. else
  381. return new String(buffer, tokenBegin, bufsize - tokenBegin) +
  382. new String(buffer, 0, bufpos + 1);
  383. }
  384. public final char[] GetSuffix(int len)
  385. {
  386. char[] ret = new char[len];
  387. if ((bufpos + 1) >= len)
  388. System.arraycopy(buffer, bufpos - len + 1, ret, 0, len);
  389. else
  390. {
  391. System.arraycopy(buffer, bufsize - (len - bufpos - 1), ret, 0,
  392. len - bufpos - 1);
  393. System.arraycopy(buffer, 0, ret, len - bufpos - 1, bufpos + 1);
  394. }
  395. return ret;
  396. }
  397. public void Done()
  398. {
  399. nextCharBuf = null;
  400. buffer = null;
  401. bufline = null;
  402. bufcolumn = null;
  403. }
  404. /**
  405. * Method to adjust line and column numbers for the start of a token.<BR>
  406. */
  407. public void adjustBeginLineColumn(int newLine, int newCol)
  408. {
  409. int start = tokenBegin;
  410. int len;
  411. if (bufpos >= tokenBegin)
  412. {
  413. len = bufpos - tokenBegin + inBuf + 1;
  414. }
  415. else
  416. {
  417. len = bufsize - tokenBegin + bufpos + 1 + inBuf;
  418. }
  419. int i = 0, j = 0, k = 0;
  420. int nextColDiff = 0, columnDiff = 0;
  421. while (i < len &&
  422. bufline[j = start % bufsize] == bufline[k = ++start % bufsize])
  423. {
  424. bufline[j] = newLine;
  425. nextColDiff = columnDiff + bufcolumn[k] - bufcolumn[j];
  426. bufcolumn[j] = newCol + columnDiff;
  427. columnDiff = nextColDiff;
  428. i++;
  429. }
  430. if (i < len)
  431. {
  432. bufline[j] = newLine++;
  433. bufcolumn[j] = newCol + columnDiff;
  434. while (i++ < len)
  435. {
  436. if (bufline[j = start % bufsize] != bufline[++start % bufsize])
  437. bufline[j] = newLine++;
  438. else
  439. bufline[j] = newLine;
  440. }
  441. }
  442. line = bufline[j];
  443. column = bufcolumn[j];
  444. }
  445. }