/projects/jena-2.6.3/com/hp/hpl/jena/n3/turtle/parser/JavaCharStream.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus · Java · 622 lines · 479 code · 88 blank · 55 comment · 63 complexity · db107ca5ceecee528b9d1febc9c61a7d MD5 · raw file

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