001: /* Generated By:JavaCC: Do not edit this line. SimpleCharStream.java Version 4.0 */
002: package jaxx.css;
003:
004: /**
005: * An implementation of interface CharStream, where the stream is assumed to
006: * contain only ASCII characters (without unicode processing).
007: */
008:
009: public class SimpleCharStream {
010: public static final boolean staticFlag = false;
011: int bufsize;
012: int available;
013: int tokenBegin;
014: public int bufpos = -1;
015: protected int bufline[];
016: protected int bufcolumn[];
017:
018: protected int column = 0;
019: protected int line = 1;
020:
021: protected boolean prevCharIsCR = false;
022: protected boolean prevCharIsLF = false;
023:
024: protected java.io.Reader inputStream;
025:
026: protected char[] buffer;
027: protected int maxNextCharInd = 0;
028: protected int inBuf = 0;
029: protected int tabSize = 8;
030:
031: protected void setTabSize(int i) {
032: tabSize = i;
033: }
034:
035: protected int getTabSize(int i) {
036: return tabSize;
037: }
038:
039: protected void ExpandBuff(boolean wrapAround) {
040: char[] newbuffer = new char[bufsize + 2048];
041: int newbufline[] = new int[bufsize + 2048];
042: int newbufcolumn[] = new int[bufsize + 2048];
043:
044: try {
045: if (wrapAround) {
046: System.arraycopy(buffer, tokenBegin, newbuffer, 0,
047: bufsize - tokenBegin);
048: System.arraycopy(buffer, 0, newbuffer, bufsize
049: - tokenBegin, bufpos);
050: buffer = newbuffer;
051:
052: System.arraycopy(bufline, tokenBegin, newbufline, 0,
053: bufsize - tokenBegin);
054: System.arraycopy(bufline, 0, newbufline, bufsize
055: - tokenBegin, bufpos);
056: bufline = newbufline;
057:
058: System.arraycopy(bufcolumn, tokenBegin, newbufcolumn,
059: 0, bufsize - tokenBegin);
060: System.arraycopy(bufcolumn, 0, newbufcolumn, bufsize
061: - tokenBegin, bufpos);
062: bufcolumn = newbufcolumn;
063:
064: maxNextCharInd = (bufpos += (bufsize - tokenBegin));
065: } else {
066: System.arraycopy(buffer, tokenBegin, newbuffer, 0,
067: bufsize - tokenBegin);
068: buffer = newbuffer;
069:
070: System.arraycopy(bufline, tokenBegin, newbufline, 0,
071: bufsize - tokenBegin);
072: bufline = newbufline;
073:
074: System.arraycopy(bufcolumn, tokenBegin, newbufcolumn,
075: 0, bufsize - tokenBegin);
076: bufcolumn = newbufcolumn;
077:
078: maxNextCharInd = (bufpos -= tokenBegin);
079: }
080: } catch (Throwable t) {
081: throw new Error(t.getMessage());
082: }
083:
084: bufsize += 2048;
085: available = bufsize;
086: tokenBegin = 0;
087: }
088:
089: protected void FillBuff() throws java.io.IOException {
090: if (maxNextCharInd == available) {
091: if (available == bufsize) {
092: if (tokenBegin > 2048) {
093: bufpos = maxNextCharInd = 0;
094: available = tokenBegin;
095: } else if (tokenBegin < 0)
096: bufpos = maxNextCharInd = 0;
097: else
098: ExpandBuff(false);
099: } else if (available > tokenBegin)
100: available = bufsize;
101: else if ((tokenBegin - available) < 2048)
102: ExpandBuff(true);
103: else
104: available = tokenBegin;
105: }
106:
107: int i;
108: try {
109: if ((i = inputStream.read(buffer, maxNextCharInd, available
110: - maxNextCharInd)) == -1) {
111: inputStream.close();
112: throw new java.io.IOException();
113: } else
114: maxNextCharInd += i;
115: return;
116: } catch (java.io.IOException e) {
117: --bufpos;
118: backup(0);
119: if (tokenBegin == -1)
120: tokenBegin = bufpos;
121: throw e;
122: }
123: }
124:
125: public char BeginToken() throws java.io.IOException {
126: tokenBegin = -1;
127: char c = readChar();
128: tokenBegin = bufpos;
129:
130: return c;
131: }
132:
133: protected void UpdateLineColumn(char c) {
134: column++;
135:
136: if (prevCharIsLF) {
137: prevCharIsLF = false;
138: line += (column = 1);
139: } else if (prevCharIsCR) {
140: prevCharIsCR = false;
141: if (c == '\n') {
142: prevCharIsLF = true;
143: } else
144: line += (column = 1);
145: }
146:
147: switch (c) {
148: case '\r':
149: prevCharIsCR = true;
150: break;
151: case '\n':
152: prevCharIsLF = true;
153: break;
154: case '\t':
155: column--;
156: column += (tabSize - (column % tabSize));
157: break;
158: default:
159: break;
160: }
161:
162: bufline[bufpos] = line;
163: bufcolumn[bufpos] = column;
164: }
165:
166: public char readChar() throws java.io.IOException {
167: if (inBuf > 0) {
168: --inBuf;
169:
170: if (++bufpos == bufsize)
171: bufpos = 0;
172:
173: return buffer[bufpos];
174: }
175:
176: if (++bufpos >= maxNextCharInd)
177: FillBuff();
178:
179: char c = buffer[bufpos];
180:
181: UpdateLineColumn(c);
182: return (c);
183: }
184:
185: /**
186: * @deprecated
187: * @see #getEndColumn
188: */
189:
190: public int getColumn() {
191: return bufcolumn[bufpos];
192: }
193:
194: /**
195: * @deprecated
196: * @see #getEndLine
197: */
198:
199: public int getLine() {
200: return bufline[bufpos];
201: }
202:
203: public int getEndColumn() {
204: return bufcolumn[bufpos];
205: }
206:
207: public int getEndLine() {
208: return bufline[bufpos];
209: }
210:
211: public int getBeginColumn() {
212: return bufcolumn[tokenBegin];
213: }
214:
215: public int getBeginLine() {
216: return bufline[tokenBegin];
217: }
218:
219: public void backup(int amount) {
220:
221: inBuf += amount;
222: if ((bufpos -= amount) < 0)
223: bufpos += bufsize;
224: }
225:
226: public SimpleCharStream(java.io.Reader dstream, int startline,
227: int startcolumn, int buffersize) {
228: inputStream = dstream;
229: line = startline;
230: column = startcolumn - 1;
231:
232: available = bufsize = buffersize;
233: buffer = new char[buffersize];
234: bufline = new int[buffersize];
235: bufcolumn = new int[buffersize];
236: }
237:
238: public SimpleCharStream(java.io.Reader dstream, int startline,
239: int startcolumn) {
240: this (dstream, startline, startcolumn, 4096);
241: }
242:
243: public SimpleCharStream(java.io.Reader dstream) {
244: this (dstream, 1, 1, 4096);
245: }
246:
247: public void ReInit(java.io.Reader dstream, int startline,
248: int startcolumn, int buffersize) {
249: inputStream = dstream;
250: line = startline;
251: column = startcolumn - 1;
252:
253: if (buffer == null || buffersize != buffer.length) {
254: available = bufsize = buffersize;
255: buffer = new char[buffersize];
256: bufline = new int[buffersize];
257: bufcolumn = new int[buffersize];
258: }
259: prevCharIsLF = prevCharIsCR = false;
260: tokenBegin = inBuf = maxNextCharInd = 0;
261: bufpos = -1;
262: }
263:
264: public void ReInit(java.io.Reader dstream, int startline,
265: int startcolumn) {
266: ReInit(dstream, startline, startcolumn, 4096);
267: }
268:
269: public void ReInit(java.io.Reader dstream) {
270: ReInit(dstream, 1, 1, 4096);
271: }
272:
273: public SimpleCharStream(java.io.InputStream dstream,
274: String encoding, int startline, int startcolumn,
275: int buffersize) throws java.io.UnsupportedEncodingException {
276: this (encoding == null ? new java.io.InputStreamReader(dstream)
277: : new java.io.InputStreamReader(dstream, encoding),
278: startline, startcolumn, buffersize);
279: }
280:
281: public SimpleCharStream(java.io.InputStream dstream, int startline,
282: int startcolumn, int buffersize) {
283: this (new java.io.InputStreamReader(dstream), startline,
284: startcolumn, buffersize);
285: }
286:
287: public SimpleCharStream(java.io.InputStream dstream,
288: String encoding, int startline, int startcolumn)
289: throws java.io.UnsupportedEncodingException {
290: this (dstream, encoding, startline, startcolumn, 4096);
291: }
292:
293: public SimpleCharStream(java.io.InputStream dstream, int startline,
294: int startcolumn) {
295: this (dstream, startline, startcolumn, 4096);
296: }
297:
298: public SimpleCharStream(java.io.InputStream dstream, String encoding)
299: throws java.io.UnsupportedEncodingException {
300: this (dstream, encoding, 1, 1, 4096);
301: }
302:
303: public SimpleCharStream(java.io.InputStream dstream) {
304: this (dstream, 1, 1, 4096);
305: }
306:
307: public void ReInit(java.io.InputStream dstream, String encoding,
308: int startline, int startcolumn, int buffersize)
309: throws java.io.UnsupportedEncodingException {
310: ReInit(
311: encoding == null ? new java.io.InputStreamReader(
312: dstream) : new java.io.InputStreamReader(
313: dstream, encoding), startline, startcolumn,
314: buffersize);
315: }
316:
317: public void ReInit(java.io.InputStream dstream, int startline,
318: int startcolumn, int buffersize) {
319: ReInit(new java.io.InputStreamReader(dstream), startline,
320: startcolumn, buffersize);
321: }
322:
323: public void ReInit(java.io.InputStream dstream, String encoding)
324: throws java.io.UnsupportedEncodingException {
325: ReInit(dstream, encoding, 1, 1, 4096);
326: }
327:
328: public void ReInit(java.io.InputStream dstream) {
329: ReInit(dstream, 1, 1, 4096);
330: }
331:
332: public void ReInit(java.io.InputStream dstream, String encoding,
333: int startline, int startcolumn)
334: throws java.io.UnsupportedEncodingException {
335: ReInit(dstream, encoding, startline, startcolumn, 4096);
336: }
337:
338: public void ReInit(java.io.InputStream dstream, int startline,
339: int startcolumn) {
340: ReInit(dstream, startline, startcolumn, 4096);
341: }
342:
343: public String GetImage() {
344: if (bufpos >= tokenBegin)
345: return new String(buffer, tokenBegin, bufpos - tokenBegin
346: + 1);
347: else
348: return new String(buffer, tokenBegin, bufsize - tokenBegin)
349: + new String(buffer, 0, bufpos + 1);
350: }
351:
352: public char[] GetSuffix(int len) {
353: char[] ret = new char[len];
354:
355: if ((bufpos + 1) >= len)
356: System.arraycopy(buffer, bufpos - len + 1, ret, 0, len);
357: else {
358: System.arraycopy(buffer, bufsize - (len - bufpos - 1), ret,
359: 0, len - bufpos - 1);
360: System.arraycopy(buffer, 0, ret, len - bufpos - 1,
361: bufpos + 1);
362: }
363:
364: return ret;
365: }
366:
367: public void Done() {
368: buffer = null;
369: bufline = null;
370: bufcolumn = null;
371: }
372:
373: /**
374: * Method to adjust line and column numbers for the start of a token.
375: */
376: public void adjustBeginLineColumn(int newLine, int newCol) {
377: int start = tokenBegin;
378: int len;
379:
380: if (bufpos >= tokenBegin) {
381: len = bufpos - tokenBegin + inBuf + 1;
382: } else {
383: len = bufsize - tokenBegin + bufpos + 1 + inBuf;
384: }
385:
386: int i = 0, j = 0, k = 0;
387: int nextColDiff = 0, columnDiff = 0;
388:
389: while (i < len
390: && bufline[j = start % bufsize] == bufline[k = ++start
391: % bufsize]) {
392: bufline[j] = newLine;
393: nextColDiff = columnDiff + bufcolumn[k] - bufcolumn[j];
394: bufcolumn[j] = newCol + columnDiff;
395: columnDiff = nextColDiff;
396: i++;
397: }
398:
399: if (i < len) {
400: bufline[j] = newLine++;
401: bufcolumn[j] = newCol + columnDiff;
402:
403: while (i++ < len) {
404: if (bufline[j = start % bufsize] != bufline[++start
405: % bufsize])
406: bufline[j] = newLine++;
407: else
408: bufline[j] = newLine;
409: }
410: }
411:
412: line = bufline[j];
413: column = bufcolumn[j];
414: }
415:
416: }
|