You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Generic_CharStream.java 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. /* Generated By:JavaCC: Do not edit this line. Generic_CharStream.java Version 0.7pre6 */
  2. package com.vaadin.sass.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 Generic_CharStream implements CharStream
  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 reader;
  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 = reader.read(buffer, maxNextCharInd,
  90. available - maxNextCharInd)) == -1)
  91. {
  92. reader.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. return (char)((char)0xff & buffer[(bufpos == bufsize - 1) ? (bufpos = 0) : ++bufpos]);
  156. }
  157. if (++bufpos >= maxNextCharInd)
  158. FillBuff();
  159. char c = (char)((char)0xff & buffer[bufpos]);
  160. UpdateLineColumn(c);
  161. return (c);
  162. }
  163. /**
  164. * @deprecated
  165. * @see #getEndColumn
  166. */
  167. public final int getColumn() {
  168. return bufcolumn[bufpos];
  169. }
  170. /**
  171. * @deprecated
  172. * @see #getEndLine
  173. */
  174. public final int getLine() {
  175. return bufline[bufpos];
  176. }
  177. public final int getEndColumn() {
  178. return bufcolumn[bufpos];
  179. }
  180. public final int getEndLine() {
  181. return bufline[bufpos];
  182. }
  183. public final int getBeginColumn() {
  184. return bufcolumn[tokenBegin];
  185. }
  186. public final int getBeginLine() {
  187. return bufline[tokenBegin];
  188. }
  189. public final void backup(int amount) {
  190. inBuf += amount;
  191. if ((bufpos -= amount) < 0)
  192. bufpos += bufsize;
  193. }
  194. public Generic_CharStream(java.io.Reader dstream, int startline,
  195. int startcolumn, int buffersize)
  196. {
  197. reader = dstream;
  198. line = startline;
  199. column = startcolumn - 1;
  200. available = bufsize = buffersize;
  201. buffer = new char[buffersize];
  202. bufline = new int[buffersize];
  203. bufcolumn = new int[buffersize];
  204. }
  205. public Generic_CharStream(java.io.Reader dstream, int startline,
  206. int startcolumn)
  207. {
  208. this(dstream, startline, startcolumn, 4096);
  209. }
  210. public void ReInit(java.io.Reader dstream, int startline,
  211. int startcolumn, int buffersize)
  212. {
  213. reader = dstream;
  214. line = startline;
  215. column = startcolumn - 1;
  216. if (buffer == null || buffersize != buffer.length)
  217. {
  218. available = bufsize = buffersize;
  219. buffer = new char[buffersize];
  220. bufline = new int[buffersize];
  221. bufcolumn = new int[buffersize];
  222. }
  223. prevCharIsLF = prevCharIsCR = false;
  224. tokenBegin = inBuf = maxNextCharInd = 0;
  225. bufpos = -1;
  226. }
  227. public void ReInit(java.io.Reader dstream, int startline,
  228. int startcolumn)
  229. {
  230. ReInit(dstream, startline, startcolumn, 4096);
  231. }
  232. public final String GetImage()
  233. {
  234. if (bufpos >= tokenBegin)
  235. return new String(buffer, tokenBegin, bufpos - tokenBegin + 1);
  236. else
  237. return new String(buffer, tokenBegin, bufsize - tokenBegin) +
  238. new String(buffer, 0, bufpos + 1);
  239. }
  240. public final char[] GetSuffix(int len)
  241. {
  242. char[] ret = new char[len];
  243. if ((bufpos + 1) >= len)
  244. System.arraycopy(buffer, bufpos - len + 1, ret, 0, len);
  245. else
  246. {
  247. System.arraycopy(buffer, bufsize - (len - bufpos - 1), ret, 0,
  248. len - bufpos - 1);
  249. System.arraycopy(buffer, 0, ret, len - bufpos - 1, bufpos + 1);
  250. }
  251. return ret;
  252. }
  253. public void Done()
  254. {
  255. buffer = null;
  256. bufline = null;
  257. bufcolumn = null;
  258. }
  259. /**
  260. * Method to adjust line and column numbers for the start of a token.<BR>
  261. */
  262. public void adjustBeginLineColumn(int newLine, int newCol)
  263. {
  264. int start = tokenBegin;
  265. int len;
  266. if (bufpos >= tokenBegin)
  267. {
  268. len = bufpos - tokenBegin + inBuf + 1;
  269. }
  270. else
  271. {
  272. len = bufsize - tokenBegin + bufpos + 1 + inBuf;
  273. }
  274. int i = 0, j = 0, k = 0;
  275. int nextColDiff = 0, columnDiff = 0;
  276. while (i < len &&
  277. bufline[j = start % bufsize] == bufline[k = ++start % bufsize])
  278. {
  279. bufline[j] = newLine;
  280. nextColDiff = columnDiff + bufcolumn[k] - bufcolumn[j];
  281. bufcolumn[j] = newCol + columnDiff;
  282. columnDiff = nextColDiff;
  283. i++;
  284. }
  285. if (i < len)
  286. {
  287. bufline[j] = newLine++;
  288. bufcolumn[j] = newCol + columnDiff;
  289. while (i++ < len)
  290. {
  291. if (bufline[j = start % bufsize] != bufline[++start % bufsize])
  292. bufline[j] = newLine++;
  293. else
  294. bufline[j] = newLine;
  295. }
  296. }
  297. line = bufline[j];
  298. column = bufcolumn[j];
  299. }
  300. }