SimpleCharStream.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. /* Generated By:JavaCC: Do not edit this line. SimpleCharStream.java Version 4.0 */
  2. /**
  3. * Licensed to the Apache Software Foundation (ASF) under one
  4. * or more contributor license agreements. See the NOTICE file
  5. * distributed with this work for additional information
  6. * regarding copyright ownership. The ASF licenses this file
  7. * to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance
  9. * with the License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. */
  19. package com.yahoo.jute.compiler.generated;
  20. /**
  21. * An implementation of interface CharStream, where the stream is assumed to
  22. * contain only ASCII characters (without unicode processing).
  23. */
  24. public class SimpleCharStream
  25. {
  26. public static final boolean staticFlag = false;
  27. int bufsize;
  28. int available;
  29. int tokenBegin;
  30. public int bufpos = -1;
  31. protected int bufline[];
  32. protected int bufcolumn[];
  33. protected int column = 0;
  34. protected int line = 1;
  35. protected boolean prevCharIsCR = false;
  36. protected boolean prevCharIsLF = false;
  37. protected java.io.Reader inputStream;
  38. protected char[] buffer;
  39. protected int maxNextCharInd = 0;
  40. protected int inBuf = 0;
  41. protected int tabSize = 8;
  42. protected void setTabSize(int i) { tabSize = i; }
  43. protected int getTabSize(int i) { return tabSize; }
  44. protected void ExpandBuff(boolean wrapAround)
  45. {
  46. char[] newbuffer = new char[bufsize + 2048];
  47. int newbufline[] = new int[bufsize + 2048];
  48. int newbufcolumn[] = new int[bufsize + 2048];
  49. try
  50. {
  51. if (wrapAround)
  52. {
  53. System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin);
  54. System.arraycopy(buffer, 0, newbuffer,
  55. bufsize - tokenBegin, bufpos);
  56. buffer = newbuffer;
  57. System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin);
  58. System.arraycopy(bufline, 0, newbufline, bufsize - tokenBegin, bufpos);
  59. bufline = newbufline;
  60. System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin);
  61. System.arraycopy(bufcolumn, 0, newbufcolumn, bufsize - tokenBegin, bufpos);
  62. bufcolumn = newbufcolumn;
  63. maxNextCharInd = (bufpos += (bufsize - tokenBegin));
  64. }
  65. else
  66. {
  67. System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin);
  68. buffer = newbuffer;
  69. System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin);
  70. bufline = newbufline;
  71. System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin);
  72. bufcolumn = newbufcolumn;
  73. maxNextCharInd = (bufpos -= tokenBegin);
  74. }
  75. }
  76. catch (Throwable t)
  77. {
  78. throw new Error(t.getMessage());
  79. }
  80. bufsize += 2048;
  81. available = bufsize;
  82. tokenBegin = 0;
  83. }
  84. protected void FillBuff() throws java.io.IOException
  85. {
  86. if (maxNextCharInd == available)
  87. {
  88. if (available == bufsize)
  89. {
  90. if (tokenBegin > 2048)
  91. {
  92. bufpos = maxNextCharInd = 0;
  93. available = tokenBegin;
  94. }
  95. else if (tokenBegin < 0)
  96. bufpos = maxNextCharInd = 0;
  97. else
  98. ExpandBuff(false);
  99. }
  100. else if (available > tokenBegin)
  101. available = bufsize;
  102. else if ((tokenBegin - available) < 2048)
  103. ExpandBuff(true);
  104. else
  105. available = tokenBegin;
  106. }
  107. int i;
  108. try {
  109. if ((i = inputStream.read(buffer, maxNextCharInd,
  110. available - maxNextCharInd)) == -1)
  111. {
  112. inputStream.close();
  113. throw new java.io.IOException();
  114. }
  115. else
  116. maxNextCharInd += i;
  117. return;
  118. }
  119. catch(java.io.IOException e) {
  120. --bufpos;
  121. backup(0);
  122. if (tokenBegin == -1)
  123. tokenBegin = bufpos;
  124. throw e;
  125. }
  126. }
  127. public char BeginToken() throws java.io.IOException
  128. {
  129. tokenBegin = -1;
  130. char c = readChar();
  131. tokenBegin = bufpos;
  132. return c;
  133. }
  134. protected void UpdateLineColumn(char c)
  135. {
  136. column++;
  137. if (prevCharIsLF)
  138. {
  139. prevCharIsLF = false;
  140. line += (column = 1);
  141. }
  142. else if (prevCharIsCR)
  143. {
  144. prevCharIsCR = false;
  145. if (c == '\n')
  146. {
  147. prevCharIsLF = true;
  148. }
  149. else
  150. line += (column = 1);
  151. }
  152. switch (c)
  153. {
  154. case '\r' :
  155. prevCharIsCR = true;
  156. break;
  157. case '\n' :
  158. prevCharIsLF = true;
  159. break;
  160. case '\t' :
  161. column--;
  162. column += (tabSize - (column % tabSize));
  163. break;
  164. default :
  165. break;
  166. }
  167. bufline[bufpos] = line;
  168. bufcolumn[bufpos] = column;
  169. }
  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. public int getColumn() {
  190. return bufcolumn[bufpos];
  191. }
  192. /**
  193. * @deprecated
  194. * @see #getEndLine
  195. */
  196. public int getLine() {
  197. return bufline[bufpos];
  198. }
  199. public int getEndColumn() {
  200. return bufcolumn[bufpos];
  201. }
  202. public int getEndLine() {
  203. return bufline[bufpos];
  204. }
  205. public int getBeginColumn() {
  206. return bufcolumn[tokenBegin];
  207. }
  208. public int getBeginLine() {
  209. return bufline[tokenBegin];
  210. }
  211. public void backup(int amount) {
  212. inBuf += amount;
  213. if ((bufpos -= amount) < 0)
  214. bufpos += bufsize;
  215. }
  216. public SimpleCharStream(java.io.Reader dstream, int startline,
  217. int startcolumn, int buffersize)
  218. {
  219. inputStream = dstream;
  220. line = startline;
  221. column = startcolumn - 1;
  222. available = bufsize = buffersize;
  223. buffer = new char[buffersize];
  224. bufline = new int[buffersize];
  225. bufcolumn = new int[buffersize];
  226. }
  227. public SimpleCharStream(java.io.Reader dstream, int startline,
  228. int startcolumn)
  229. {
  230. this(dstream, startline, startcolumn, 4096);
  231. }
  232. public SimpleCharStream(java.io.Reader dstream)
  233. {
  234. this(dstream, 1, 1, 4096);
  235. }
  236. public void ReInit(java.io.Reader dstream, int startline,
  237. int startcolumn, int buffersize)
  238. {
  239. inputStream = dstream;
  240. line = startline;
  241. column = startcolumn - 1;
  242. if (buffer == null || buffersize != buffer.length)
  243. {
  244. available = bufsize = buffersize;
  245. buffer = new char[buffersize];
  246. bufline = new int[buffersize];
  247. bufcolumn = new int[buffersize];
  248. }
  249. prevCharIsLF = prevCharIsCR = false;
  250. tokenBegin = inBuf = maxNextCharInd = 0;
  251. bufpos = -1;
  252. }
  253. public void ReInit(java.io.Reader dstream, int startline,
  254. int startcolumn)
  255. {
  256. ReInit(dstream, startline, startcolumn, 4096);
  257. }
  258. public void ReInit(java.io.Reader dstream)
  259. {
  260. ReInit(dstream, 1, 1, 4096);
  261. }
  262. public SimpleCharStream(java.io.InputStream dstream, String encoding, int startline,
  263. int startcolumn, int buffersize) throws java.io.UnsupportedEncodingException
  264. {
  265. this(encoding == null ? new java.io.InputStreamReader(dstream) : new java.io.InputStreamReader(dstream, encoding), startline, startcolumn, buffersize);
  266. }
  267. public SimpleCharStream(java.io.InputStream dstream, int startline,
  268. int startcolumn, int buffersize)
  269. {
  270. this(new java.io.InputStreamReader(dstream), startline, startcolumn, buffersize);
  271. }
  272. public SimpleCharStream(java.io.InputStream dstream, String encoding, int startline,
  273. int startcolumn) throws java.io.UnsupportedEncodingException
  274. {
  275. this(dstream, encoding, startline, startcolumn, 4096);
  276. }
  277. public SimpleCharStream(java.io.InputStream dstream, int startline,
  278. int startcolumn)
  279. {
  280. this(dstream, startline, startcolumn, 4096);
  281. }
  282. public SimpleCharStream(java.io.InputStream dstream, String encoding) throws java.io.UnsupportedEncodingException
  283. {
  284. this(dstream, encoding, 1, 1, 4096);
  285. }
  286. public SimpleCharStream(java.io.InputStream dstream)
  287. {
  288. this(dstream, 1, 1, 4096);
  289. }
  290. public void ReInit(java.io.InputStream dstream, String encoding, int startline,
  291. int startcolumn, int buffersize) throws java.io.UnsupportedEncodingException
  292. {
  293. ReInit(encoding == null ? new java.io.InputStreamReader(dstream) : new java.io.InputStreamReader(dstream, encoding), startline, startcolumn, buffersize);
  294. }
  295. public void ReInit(java.io.InputStream dstream, int startline,
  296. int startcolumn, int buffersize)
  297. {
  298. ReInit(new java.io.InputStreamReader(dstream), startline, startcolumn, buffersize);
  299. }
  300. public void ReInit(java.io.InputStream dstream, String encoding) throws java.io.UnsupportedEncodingException
  301. {
  302. ReInit(dstream, encoding, 1, 1, 4096);
  303. }
  304. public void ReInit(java.io.InputStream dstream)
  305. {
  306. ReInit(dstream, 1, 1, 4096);
  307. }
  308. public void ReInit(java.io.InputStream dstream, String encoding, int startline,
  309. int startcolumn) throws java.io.UnsupportedEncodingException
  310. {
  311. ReInit(dstream, encoding, startline, startcolumn, 4096);
  312. }
  313. public void ReInit(java.io.InputStream dstream, int startline,
  314. int startcolumn)
  315. {
  316. ReInit(dstream, startline, startcolumn, 4096);
  317. }
  318. public String GetImage()
  319. {
  320. if (bufpos >= tokenBegin)
  321. return new String(buffer, tokenBegin, bufpos - tokenBegin + 1);
  322. else
  323. return new String(buffer, tokenBegin, bufsize - tokenBegin) +
  324. new String(buffer, 0, bufpos + 1);
  325. }
  326. public char[] GetSuffix(int len)
  327. {
  328. char[] ret = new char[len];
  329. if ((bufpos + 1) >= len)
  330. System.arraycopy(buffer, bufpos - len + 1, ret, 0, len);
  331. else
  332. {
  333. System.arraycopy(buffer, bufsize - (len - bufpos - 1), ret, 0,
  334. len - bufpos - 1);
  335. System.arraycopy(buffer, 0, ret, len - bufpos - 1, bufpos + 1);
  336. }
  337. return ret;
  338. }
  339. public void Done()
  340. {
  341. buffer = null;
  342. bufline = null;
  343. bufcolumn = null;
  344. }
  345. /**
  346. * Method to adjust line and column numbers for the start of a token.
  347. */
  348. public void adjustBeginLineColumn(int newLine, int newCol)
  349. {
  350. int start = tokenBegin;
  351. int len;
  352. if (bufpos >= tokenBegin)
  353. {
  354. len = bufpos - tokenBegin + inBuf + 1;
  355. }
  356. else
  357. {
  358. len = bufsize - tokenBegin + bufpos + 1 + inBuf;
  359. }
  360. int i = 0, j = 0, k = 0;
  361. int nextColDiff = 0, columnDiff = 0;
  362. while (i < len &&
  363. bufline[j = start % bufsize] == bufline[k = ++start % bufsize])
  364. {
  365. bufline[j] = newLine;
  366. nextColDiff = columnDiff + bufcolumn[k] - bufcolumn[j];
  367. bufcolumn[j] = newCol + columnDiff;
  368. columnDiff = nextColDiff;
  369. i++;
  370. }
  371. if (i < len)
  372. {
  373. bufline[j] = newLine++;
  374. bufcolumn[j] = newCol + columnDiff;
  375. while (i++ < len)
  376. {
  377. if (bufline[j = start % bufsize] != bufline[++start % bufsize])
  378. bufline[j] = newLine++;
  379. else
  380. bufline[j] = newLine;
  381. }
  382. }
  383. line = bufline[j];
  384. column = bufcolumn[j];
  385. }
  386. }