1. /* Generated By:JavaCC: Do not edit this line. SimpleCharStream.java Version 2.1 */
  2. package org.apache.commons.el.parser;
  3. /**
  4. * An implementation of interface CharStream, where the stream is assumed to
  5. * contain only ASCII characters (without unicode processing).
  6. */
  7. public final class SimpleCharStream
  8. {
  9. public static final boolean staticFlag = false;
  10. int bufsize;
  11. int available;
  12. int tokenBegin;
  13. public int bufpos = -1;
  14. private int bufline[];
  15. private int bufcolumn[];
  16. private int column = 0;
  17. private int line = 1;
  18. private boolean prevCharIsCR = false;
  19. private boolean prevCharIsLF = false;
  20. private java.io.Reader inputStream;
  21. private char[] buffer;
  22. private int maxNextCharInd = 0;
  23. private int inBuf = 0;
  24. private final void ExpandBuff(boolean wrapAround)
  25. {
  26. char[] newbuffer = new char[bufsize + 2048];
  27. int newbufline[] = new int[bufsize + 2048];
  28. int newbufcolumn[] = new int[bufsize + 2048];
  29. try
  30. {
  31. if (wrapAround)
  32. {
  33. System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin);
  34. System.arraycopy(buffer, 0, newbuffer,
  35. bufsize - tokenBegin, bufpos);
  36. buffer = newbuffer;
  37. System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin);
  38. System.arraycopy(bufline, 0, newbufline, bufsize - tokenBegin, bufpos);
  39. bufline = newbufline;
  40. System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin);
  41. System.arraycopy(bufcolumn, 0, newbufcolumn, bufsize - tokenBegin, bufpos);
  42. bufcolumn = newbufcolumn;
  43. maxNextCharInd = (bufpos += (bufsize - tokenBegin));
  44. }
  45. else
  46. {
  47. System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin);
  48. buffer = newbuffer;
  49. System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin);
  50. bufline = newbufline;
  51. System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin);
  52. bufcolumn = newbufcolumn;
  53. maxNextCharInd = (bufpos -= tokenBegin);
  54. }
  55. }
  56. catch (Throwable t)
  57. {
  58. throw new Error(t.getMessage());
  59. }
  60. bufsize += 2048;
  61. available = bufsize;
  62. tokenBegin = 0;
  63. }
  64. private final void FillBuff() throws java.io.IOException
  65. {
  66. if (maxNextCharInd == available)
  67. {
  68. if (available == bufsize)
  69. {
  70. if (tokenBegin > 2048)
  71. {
  72. bufpos = maxNextCharInd = 0;
  73. available = tokenBegin;
  74. }
  75. else if (tokenBegin < 0)
  76. bufpos = maxNextCharInd = 0;
  77. else
  78. ExpandBuff(false);
  79. }
  80. else if (available > tokenBegin)
  81. available = bufsize;
  82. else if ((tokenBegin - available) < 2048)
  83. ExpandBuff(true);
  84. else
  85. available = tokenBegin;
  86. }
  87. int i;
  88. try {
  89. if ((i = inputStream.read(buffer, maxNextCharInd,
  90. available - maxNextCharInd)) == -1)
  91. {
  92. inputStream.close();
  93. throw new java.io.IOException();
  94. }
  95. else
  96. maxNextCharInd += i;
  97. return;
  98. }
  99. catch(java.io.IOException e) {
  100. --bufpos;
  101. backup(0);
  102. if (tokenBegin == -1)
  103. tokenBegin = bufpos;
  104. throw e;
  105. }
  106. }
  107. public final char BeginToken() throws java.io.IOException
  108. {
  109. tokenBegin = -1;
  110. char c = readChar();
  111. tokenBegin = bufpos;
  112. return c;
  113. }
  114. private final void UpdateLineColumn(char c)
  115. {
  116. column++;
  117. if (prevCharIsLF)
  118. {
  119. prevCharIsLF = false;
  120. line += (column = 1);
  121. }
  122. else if (prevCharIsCR)
  123. {
  124. prevCharIsCR = false;
  125. if (c == '\n')
  126. {
  127. prevCharIsLF = true;
  128. }
  129. else
  130. line += (column = 1);
  131. }
  132. switch (c)
  133. {
  134. case '\r' :
  135. prevCharIsCR = true;
  136. break;
  137. case '\n' :
  138. prevCharIsLF = true;
  139. break;
  140. case '\t' :
  141. column--;
  142. column += (8 - (column & 07));
  143. break;
  144. default :
  145. break;
  146. }
  147. bufline[bufpos] = line;
  148. bufcolumn[bufpos] = column;
  149. }
  150. public final char readChar() throws java.io.IOException
  151. {
  152. if (inBuf > 0)
  153. {
  154. --inBuf;
  155. if (++bufpos == bufsize)
  156. bufpos = 0;
  157. return buffer[bufpos];
  158. }
  159. if (++bufpos >= maxNextCharInd)
  160. FillBuff();
  161. char c = buffer[bufpos];
  162. UpdateLineColumn(c);
  163. return (c);
  164. }
  165. /**
  166. * @deprecated
  167. * @see #getEndColumn
  168. */
  169. public final int getColumn() {
  170. return bufcolumn[bufpos];
  171. }
  172. /**
  173. * @deprecated
  174. * @see #getEndLine
  175. */
  176. public final int getLine() {
  177. return bufline[bufpos];
  178. }
  179. public final int getEndColumn() {
  180. return bufcolumn[bufpos];
  181. }
  182. public final int getEndLine() {
  183. return bufline[bufpos];
  184. }
  185. public final int getBeginColumn() {
  186. return bufcolumn[tokenBegin];
  187. }
  188. public final int getBeginLine() {
  189. return bufline[tokenBegin];
  190. }
  191. public final void backup(int amount) {
  192. inBuf += amount;
  193. if ((bufpos -= amount) < 0)
  194. bufpos += bufsize;
  195. }
  196. public SimpleCharStream(java.io.Reader dstream, int startline,
  197. int startcolumn, int buffersize)
  198. {
  199. inputStream = dstream;
  200. line = startline;
  201. column = startcolumn - 1;
  202. available = bufsize = buffersize;
  203. buffer = new char[buffersize];
  204. bufline = new int[buffersize];
  205. bufcolumn = new int[buffersize];
  206. }
  207. public SimpleCharStream(java.io.Reader dstream, int startline,
  208. int startcolumn)
  209. {
  210. this(dstream, startline, startcolumn, 4096);
  211. }
  212. public SimpleCharStream(java.io.Reader dstream)
  213. {
  214. this(dstream, 1, 1, 4096);
  215. }
  216. public void ReInit(java.io.Reader dstream, int startline,
  217. int startcolumn, int buffersize)
  218. {
  219. inputStream = dstream;
  220. line = startline;
  221. column = startcolumn - 1;
  222. if (buffer == null || buffersize != buffer.length)
  223. {
  224. available = bufsize = buffersize;
  225. buffer = new char[buffersize];
  226. bufline = new int[buffersize];
  227. bufcolumn = new int[buffersize];
  228. }
  229. prevCharIsLF = prevCharIsCR = false;
  230. tokenBegin = inBuf = maxNextCharInd = 0;
  231. bufpos = -1;
  232. }
  233. public void ReInit(java.io.Reader dstream, int startline,
  234. int startcolumn)
  235. {
  236. ReInit(dstream, startline, startcolumn, 4096);
  237. }
  238. public void ReInit(java.io.Reader dstream)
  239. {
  240. ReInit(dstream, 1, 1, 4096);
  241. }
  242. public SimpleCharStream(java.io.InputStream dstream, int startline,
  243. int startcolumn, int buffersize)
  244. {
  245. this(new java.io.InputStreamReader(dstream), startline, startcolumn, 4096);
  246. }
  247. public SimpleCharStream(java.io.InputStream dstream, int startline,
  248. int startcolumn)
  249. {
  250. this(dstream, startline, startcolumn, 4096);
  251. }
  252. public SimpleCharStream(java.io.InputStream dstream)
  253. {
  254. this(dstream, 1, 1, 4096);
  255. }
  256. public void ReInit(java.io.InputStream dstream, int startline,
  257. int startcolumn, int buffersize)
  258. {
  259. ReInit(new java.io.InputStreamReader(dstream), startline, startcolumn, 4096);
  260. }
  261. public void ReInit(java.io.InputStream dstream)
  262. {
  263. ReInit(dstream, 1, 1, 4096);
  264. }
  265. public void ReInit(java.io.InputStream dstream, int startline,
  266. int startcolumn)
  267. {
  268. ReInit(dstream, startline, startcolumn, 4096);
  269. }
  270. public final String GetImage()
  271. {
  272. if (bufpos >= tokenBegin)
  273. return new String(buffer, tokenBegin, bufpos - tokenBegin + 1);
  274. else
  275. return new String(buffer, tokenBegin, bufsize - tokenBegin) +
  276. new String(buffer, 0, bufpos + 1);
  277. }
  278. public final char[] GetSuffix(int len)
  279. {
  280. char[] ret = new char[len];
  281. if ((bufpos + 1) >= len)
  282. System.arraycopy(buffer, bufpos - len + 1, ret, 0, len);
  283. else
  284. {
  285. System.arraycopy(buffer, bufsize - (len - bufpos - 1), ret, 0,
  286. len - bufpos - 1);
  287. System.arraycopy(buffer, 0, ret, len - bufpos - 1, bufpos + 1);
  288. }
  289. return ret;
  290. }
  291. public void Done()
  292. {
  293. buffer = null;
  294. bufline = null;
  295. bufcolumn = null;
  296. }
  297. /**
  298. * Method to adjust line and column numbers for the start of a token.<BR>
  299. */
  300. public void adjustBeginLineColumn(int newLine, int newCol)
  301. {
  302. int start = tokenBegin;
  303. int len;
  304. if (bufpos >= tokenBegin)
  305. {
  306. len = bufpos - tokenBegin + inBuf + 1;
  307. }
  308. else
  309. {
  310. len = bufsize - tokenBegin + bufpos + 1 + inBuf;
  311. }
  312. int i = 0, j = 0, k = 0;
  313. int nextColDiff = 0, columnDiff = 0;
  314. while (i < len &&
  315. bufline[j = start % bufsize] == bufline[k = ++start % bufsize])
  316. {
  317. bufline[j] = newLine;
  318. nextColDiff = columnDiff + bufcolumn[k] - bufcolumn[j];
  319. bufcolumn[j] = newCol + columnDiff;
  320. columnDiff = nextColDiff;
  321. i++;
  322. }
  323. if (i < len)
  324. {
  325. bufline[j] = newLine++;
  326. bufcolumn[j] = newCol + columnDiff;
  327. while (i++ < len)
  328. {
  329. if (bufline[j = start % bufsize] != bufline[++start % bufsize])
  330. bufline[j] = newLine++;
  331. else
  332. bufline[j] = newLine;
  333. }
  334. }
  335. line = bufline[j];
  336. column = bufcolumn[j];
  337. }
  338. }