PageRenderTime 57ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/CPSC411/ir/src/ir/parser/SimpleCharStream.java

https://gitlab.com/isometric/Coursework
Java | 472 lines | 353 code | 71 blank | 48 comment | 43 complexity | c62ba54ef89fed23a0b8d9a218842b39 MD5 | raw file
  1. /* Generated By:JavaCC: Do not edit this line. SimpleCharStream.java Version 5.0 */
  2. /* JavaCCOptions:STATIC=false,SUPPORT_CLASS_VISIBILITY_PUBLIC=true */
  3. package ir.parser;
  4. /**
  5. * An implementation of interface CharStream, where the stream is assumed to
  6. * contain only ASCII characters (without unicode processing).
  7. */
  8. @SuppressWarnings("all")
  9. public class SimpleCharStream
  10. {
  11. /** Whether parser is static. */
  12. public static final boolean staticFlag = false;
  13. int bufsize;
  14. int available;
  15. int tokenBegin;
  16. /** Position in buffer. */
  17. public int bufpos = -1;
  18. protected int bufline[];
  19. protected int bufcolumn[];
  20. protected int column = 0;
  21. protected int line = 1;
  22. protected boolean prevCharIsCR = false;
  23. protected boolean prevCharIsLF = false;
  24. protected java.io.Reader inputStream;
  25. protected char[] buffer;
  26. protected int maxNextCharInd = 0;
  27. protected int inBuf = 0;
  28. protected int tabSize = 8;
  29. protected void setTabSize(int i) { tabSize = i; }
  30. protected int getTabSize(int i) { return tabSize; }
  31. protected void ExpandBuff(boolean wrapAround)
  32. {
  33. char[] newbuffer = new char[bufsize + 2048];
  34. int newbufline[] = new int[bufsize + 2048];
  35. int newbufcolumn[] = new int[bufsize + 2048];
  36. try
  37. {
  38. if (wrapAround)
  39. {
  40. System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin);
  41. System.arraycopy(buffer, 0, newbuffer, bufsize - tokenBegin, bufpos);
  42. buffer = newbuffer;
  43. System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin);
  44. System.arraycopy(bufline, 0, newbufline, bufsize - tokenBegin, bufpos);
  45. bufline = newbufline;
  46. System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin);
  47. System.arraycopy(bufcolumn, 0, newbufcolumn, bufsize - tokenBegin, bufpos);
  48. bufcolumn = newbufcolumn;
  49. maxNextCharInd = (bufpos += (bufsize - tokenBegin));
  50. }
  51. else
  52. {
  53. System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin);
  54. buffer = newbuffer;
  55. System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin);
  56. bufline = newbufline;
  57. System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin);
  58. bufcolumn = newbufcolumn;
  59. maxNextCharInd = (bufpos -= tokenBegin);
  60. }
  61. }
  62. catch (Throwable t)
  63. {
  64. throw new Error(t.getMessage());
  65. }
  66. bufsize += 2048;
  67. available = bufsize;
  68. tokenBegin = 0;
  69. }
  70. protected void FillBuff() throws java.io.IOException
  71. {
  72. if (maxNextCharInd == available)
  73. {
  74. if (available == bufsize)
  75. {
  76. if (tokenBegin > 2048)
  77. {
  78. bufpos = maxNextCharInd = 0;
  79. available = tokenBegin;
  80. }
  81. else if (tokenBegin < 0)
  82. bufpos = maxNextCharInd = 0;
  83. else
  84. ExpandBuff(false);
  85. }
  86. else if (available > tokenBegin)
  87. available = bufsize;
  88. else if ((tokenBegin - available) < 2048)
  89. ExpandBuff(true);
  90. else
  91. available = tokenBegin;
  92. }
  93. int i;
  94. try {
  95. if ((i = inputStream.read(buffer, maxNextCharInd, available - maxNextCharInd)) == -1)
  96. {
  97. inputStream.close();
  98. throw new java.io.IOException();
  99. }
  100. else
  101. maxNextCharInd += i;
  102. return;
  103. }
  104. catch(java.io.IOException e) {
  105. --bufpos;
  106. backup(0);
  107. if (tokenBegin == -1)
  108. tokenBegin = bufpos;
  109. throw e;
  110. }
  111. }
  112. /** Start. */
  113. public char BeginToken() throws java.io.IOException
  114. {
  115. tokenBegin = -1;
  116. char c = readChar();
  117. tokenBegin = bufpos;
  118. return c;
  119. }
  120. protected void UpdateLineColumn(char c)
  121. {
  122. column++;
  123. if (prevCharIsLF)
  124. {
  125. prevCharIsLF = false;
  126. line += (column = 1);
  127. }
  128. else if (prevCharIsCR)
  129. {
  130. prevCharIsCR = false;
  131. if (c == '\n')
  132. {
  133. prevCharIsLF = true;
  134. }
  135. else
  136. line += (column = 1);
  137. }
  138. switch (c)
  139. {
  140. case '\r' :
  141. prevCharIsCR = true;
  142. break;
  143. case '\n' :
  144. prevCharIsLF = true;
  145. break;
  146. case '\t' :
  147. column--;
  148. column += (tabSize - (column % tabSize));
  149. break;
  150. default :
  151. break;
  152. }
  153. bufline[bufpos] = line;
  154. bufcolumn[bufpos] = column;
  155. }
  156. /** Read a character. */
  157. public char readChar() throws java.io.IOException
  158. {
  159. if (inBuf > 0)
  160. {
  161. --inBuf;
  162. if (++bufpos == bufsize)
  163. bufpos = 0;
  164. return buffer[bufpos];
  165. }
  166. if (++bufpos >= maxNextCharInd)
  167. FillBuff();
  168. char c = buffer[bufpos];
  169. UpdateLineColumn(c);
  170. return c;
  171. }
  172. @Deprecated
  173. /**
  174. * @deprecated
  175. * @see #getEndColumn
  176. */
  177. public int getColumn() {
  178. return bufcolumn[bufpos];
  179. }
  180. @Deprecated
  181. /**
  182. * @deprecated
  183. * @see #getEndLine
  184. */
  185. public int getLine() {
  186. return bufline[bufpos];
  187. }
  188. /** Get token end column number. */
  189. public int getEndColumn() {
  190. return bufcolumn[bufpos];
  191. }
  192. /** Get token end line number. */
  193. public int getEndLine() {
  194. return bufline[bufpos];
  195. }
  196. /** Get token beginning column number. */
  197. public int getBeginColumn() {
  198. return bufcolumn[tokenBegin];
  199. }
  200. /** Get token beginning line number. */
  201. public int getBeginLine() {
  202. return bufline[tokenBegin];
  203. }
  204. /** Backup a number of characters. */
  205. public void backup(int amount) {
  206. inBuf += amount;
  207. if ((bufpos -= amount) < 0)
  208. bufpos += bufsize;
  209. }
  210. /** Constructor. */
  211. public SimpleCharStream(java.io.Reader dstream, int startline,
  212. int startcolumn, int buffersize)
  213. {
  214. inputStream = dstream;
  215. line = startline;
  216. column = startcolumn - 1;
  217. available = bufsize = buffersize;
  218. buffer = new char[buffersize];
  219. bufline = new int[buffersize];
  220. bufcolumn = new int[buffersize];
  221. }
  222. /** Constructor. */
  223. public SimpleCharStream(java.io.Reader dstream, int startline,
  224. int startcolumn)
  225. {
  226. this(dstream, startline, startcolumn, 4096);
  227. }
  228. /** Constructor. */
  229. public SimpleCharStream(java.io.Reader dstream)
  230. {
  231. this(dstream, 1, 1, 4096);
  232. }
  233. /** Reinitialise. */
  234. public void ReInit(java.io.Reader dstream, int startline,
  235. int startcolumn, int buffersize)
  236. {
  237. inputStream = dstream;
  238. line = startline;
  239. column = startcolumn - 1;
  240. if (buffer == null || buffersize != buffer.length)
  241. {
  242. available = bufsize = buffersize;
  243. buffer = new char[buffersize];
  244. bufline = new int[buffersize];
  245. bufcolumn = new int[buffersize];
  246. }
  247. prevCharIsLF = prevCharIsCR = false;
  248. tokenBegin = inBuf = maxNextCharInd = 0;
  249. bufpos = -1;
  250. }
  251. /** Reinitialise. */
  252. public void ReInit(java.io.Reader dstream, int startline,
  253. int startcolumn)
  254. {
  255. ReInit(dstream, startline, startcolumn, 4096);
  256. }
  257. /** Reinitialise. */
  258. public void ReInit(java.io.Reader dstream)
  259. {
  260. ReInit(dstream, 1, 1, 4096);
  261. }
  262. /** Constructor. */
  263. public SimpleCharStream(java.io.InputStream dstream, String encoding, int startline,
  264. int startcolumn, int buffersize) throws java.io.UnsupportedEncodingException
  265. {
  266. this(encoding == null ? new java.io.InputStreamReader(dstream) : new java.io.InputStreamReader(dstream, encoding), startline, startcolumn, buffersize);
  267. }
  268. /** Constructor. */
  269. public SimpleCharStream(java.io.InputStream dstream, int startline,
  270. int startcolumn, int buffersize)
  271. {
  272. this(new java.io.InputStreamReader(dstream), startline, startcolumn, buffersize);
  273. }
  274. /** Constructor. */
  275. public SimpleCharStream(java.io.InputStream dstream, String encoding, int startline,
  276. int startcolumn) throws java.io.UnsupportedEncodingException
  277. {
  278. this(dstream, encoding, startline, startcolumn, 4096);
  279. }
  280. /** Constructor. */
  281. public SimpleCharStream(java.io.InputStream dstream, int startline,
  282. int startcolumn)
  283. {
  284. this(dstream, startline, startcolumn, 4096);
  285. }
  286. /** Constructor. */
  287. public SimpleCharStream(java.io.InputStream dstream, String encoding) throws java.io.UnsupportedEncodingException
  288. {
  289. this(dstream, encoding, 1, 1, 4096);
  290. }
  291. /** Constructor. */
  292. public SimpleCharStream(java.io.InputStream dstream)
  293. {
  294. this(dstream, 1, 1, 4096);
  295. }
  296. /** Reinitialise. */
  297. public void ReInit(java.io.InputStream dstream, String encoding, int startline,
  298. int startcolumn, int buffersize) throws java.io.UnsupportedEncodingException
  299. {
  300. ReInit(encoding == null ? new java.io.InputStreamReader(dstream) : new java.io.InputStreamReader(dstream, encoding), startline, startcolumn, buffersize);
  301. }
  302. /** Reinitialise. */
  303. public void ReInit(java.io.InputStream dstream, int startline,
  304. int startcolumn, int buffersize)
  305. {
  306. ReInit(new java.io.InputStreamReader(dstream), startline, startcolumn, buffersize);
  307. }
  308. /** Reinitialise. */
  309. public void ReInit(java.io.InputStream dstream, String encoding) throws java.io.UnsupportedEncodingException
  310. {
  311. ReInit(dstream, encoding, 1, 1, 4096);
  312. }
  313. /** Reinitialise. */
  314. public void ReInit(java.io.InputStream dstream)
  315. {
  316. ReInit(dstream, 1, 1, 4096);
  317. }
  318. /** Reinitialise. */
  319. public void ReInit(java.io.InputStream dstream, String encoding, int startline,
  320. int startcolumn) throws java.io.UnsupportedEncodingException
  321. {
  322. ReInit(dstream, encoding, startline, startcolumn, 4096);
  323. }
  324. /** Reinitialise. */
  325. public void ReInit(java.io.InputStream dstream, int startline,
  326. int startcolumn)
  327. {
  328. ReInit(dstream, startline, startcolumn, 4096);
  329. }
  330. /** Get token literal value. */
  331. public String GetImage()
  332. {
  333. if (bufpos >= tokenBegin)
  334. return new String(buffer, tokenBegin, bufpos - tokenBegin + 1);
  335. else
  336. return new String(buffer, tokenBegin, bufsize - tokenBegin) +
  337. new String(buffer, 0, bufpos + 1);
  338. }
  339. /** Get the suffix. */
  340. public char[] GetSuffix(int len)
  341. {
  342. char[] ret = new char[len];
  343. if ((bufpos + 1) >= len)
  344. System.arraycopy(buffer, bufpos - len + 1, ret, 0, len);
  345. else
  346. {
  347. System.arraycopy(buffer, bufsize - (len - bufpos - 1), ret, 0,
  348. len - bufpos - 1);
  349. System.arraycopy(buffer, 0, ret, len - bufpos - 1, bufpos + 1);
  350. }
  351. return ret;
  352. }
  353. /** Reset buffer when finished. */
  354. public void Done()
  355. {
  356. buffer = null;
  357. bufline = null;
  358. bufcolumn = null;
  359. }
  360. /**
  361. * Method to adjust line and column numbers for the start of a token.
  362. */
  363. public void adjustBeginLineColumn(int newLine, int newCol)
  364. {
  365. int start = tokenBegin;
  366. int len;
  367. if (bufpos >= tokenBegin)
  368. {
  369. len = bufpos - tokenBegin + inBuf + 1;
  370. }
  371. else
  372. {
  373. len = bufsize - tokenBegin + bufpos + 1 + inBuf;
  374. }
  375. int i = 0, j = 0, k = 0;
  376. int nextColDiff = 0, columnDiff = 0;
  377. while (i < len && bufline[j = start % bufsize] == bufline[k = ++start % bufsize])
  378. {
  379. bufline[j] = newLine;
  380. nextColDiff = columnDiff + bufcolumn[k] - bufcolumn[j];
  381. bufcolumn[j] = newCol + columnDiff;
  382. columnDiff = nextColDiff;
  383. i++;
  384. }
  385. if (i < len)
  386. {
  387. bufline[j] = newLine++;
  388. bufcolumn[j] = newCol + columnDiff;
  389. while (i++ < len)
  390. {
  391. if (bufline[j = start % bufsize] != bufline[++start % bufsize])
  392. bufline[j] = newLine++;
  393. else
  394. bufline[j] = newLine;
  395. }
  396. }
  397. line = bufline[j];
  398. column = bufcolumn[j];
  399. }
  400. }
  401. /* JavaCC - OriginalChecksum=4e35d8d856866a323d006976f12ef778 (do not edit this line) */