PageRenderTime 54ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/grails-web-common/src/main/groovy/org/grails/web/json/parser/SimpleCharStream.java

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