/branches/cleka_acts_journalmode/Colossus/datatools/src/main/javacc/net/sf/colossus/parser/SimpleCharStream.java

# · Java · 507 lines · 385 code · 74 blank · 48 comment · 43 complexity · a4b9b44cb6704f12a3b611d61bcb4dd7 MD5 · raw file

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