001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: /* Generated By:JavaCC: Do not edit this line. SimpleCharStream.java Version 3.0 */
043: package org.netbeans.modules.debugger.jpda.expr;
044:
045: /**
046: * An implementation of interface CharStream, where the stream is assumed to
047: * contain only ASCII characters (without unicode processing).
048: */
049:
050: class SimpleCharStream {
051: public static final boolean staticFlag = false;
052: int bufsize;
053: int available;
054: int tokenBegin;
055: public int bufpos = -1;
056: protected int bufline[];
057: protected int bufcolumn[];
058:
059: protected int column = 0;
060: protected int line = 1;
061:
062: protected boolean prevCharIsCR = false;
063: protected boolean prevCharIsLF = false;
064:
065: protected java.io.Reader inputStream;
066:
067: protected char[] buffer;
068: protected int maxNextCharInd = 0;
069: protected int inBuf = 0;
070:
071: protected void ExpandBuff(boolean wrapAround) {
072: char[] newbuffer = new char[bufsize + 2048];
073: int newbufline[] = new int[bufsize + 2048];
074: int newbufcolumn[] = new int[bufsize + 2048];
075:
076: if (wrapAround) {
077: System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize
078: - tokenBegin);
079: System.arraycopy(buffer, 0, newbuffer,
080: bufsize - tokenBegin, bufpos);
081: buffer = newbuffer;
082:
083: System.arraycopy(bufline, tokenBegin, newbufline, 0,
084: bufsize - tokenBegin);
085: System.arraycopy(bufline, 0, newbufline, bufsize
086: - tokenBegin, bufpos);
087: bufline = newbufline;
088:
089: System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0,
090: bufsize - tokenBegin);
091: System.arraycopy(bufcolumn, 0, newbufcolumn, bufsize
092: - tokenBegin, bufpos);
093: bufcolumn = newbufcolumn;
094:
095: maxNextCharInd = (bufpos += (bufsize - tokenBegin));
096: } else {
097: System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize
098: - tokenBegin);
099: buffer = newbuffer;
100:
101: System.arraycopy(bufline, tokenBegin, newbufline, 0,
102: bufsize - tokenBegin);
103: bufline = newbufline;
104:
105: System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0,
106: bufsize - tokenBegin);
107: bufcolumn = newbufcolumn;
108:
109: maxNextCharInd = (bufpos -= tokenBegin);
110: }
111:
112: bufsize += 2048;
113: available = bufsize;
114: tokenBegin = 0;
115: }
116:
117: protected void FillBuff() throws java.io.IOException {
118: if (maxNextCharInd == available) {
119: if (available == bufsize) {
120: if (tokenBegin > 2048) {
121: bufpos = maxNextCharInd = 0;
122: available = tokenBegin;
123: } else if (tokenBegin < 0)
124: bufpos = maxNextCharInd = 0;
125: else
126: ExpandBuff(false);
127: } else if (available > tokenBegin)
128: available = bufsize;
129: else if ((tokenBegin - available) < 2048)
130: ExpandBuff(true);
131: else
132: available = tokenBegin;
133: }
134:
135: int i;
136: try {
137: if ((i = inputStream.read(buffer, maxNextCharInd, available
138: - maxNextCharInd)) == -1) {
139: inputStream.close();
140: throw new java.io.IOException();
141: } else
142: maxNextCharInd += i;
143: return;
144: } catch (java.io.IOException e) {
145: --bufpos;
146: backup(0);
147: if (tokenBegin == -1)
148: tokenBegin = bufpos;
149: throw e;
150: }
151: }
152:
153: public char BeginToken() throws java.io.IOException {
154: tokenBegin = -1;
155: char c = readChar();
156: tokenBegin = bufpos;
157:
158: return c;
159: }
160:
161: protected void UpdateLineColumn(char c) {
162: column++;
163:
164: if (prevCharIsLF) {
165: prevCharIsLF = false;
166: line += (column = 1);
167: } else if (prevCharIsCR) {
168: prevCharIsCR = false;
169: if (c == '\n') {
170: prevCharIsLF = true;
171: } else
172: line += (column = 1);
173: }
174:
175: switch (c) {
176: case '\r':
177: prevCharIsCR = true;
178: break;
179: case '\n':
180: prevCharIsLF = true;
181: break;
182: case '\t':
183: column--;
184: column += (8 - (column & 07));
185: break;
186: default:
187: break;
188: }
189:
190: bufline[bufpos] = line;
191: bufcolumn[bufpos] = column;
192: }
193:
194: public char readChar() throws java.io.IOException {
195: if (inBuf > 0) {
196: --inBuf;
197:
198: if (++bufpos == bufsize)
199: bufpos = 0;
200:
201: return buffer[bufpos];
202: }
203:
204: if (++bufpos >= maxNextCharInd)
205: FillBuff();
206:
207: char c = buffer[bufpos];
208:
209: UpdateLineColumn(c);
210: return (c);
211: }
212:
213: /**
214: * @deprecated
215: * @see #getEndColumn
216: */
217:
218: public int getColumn() {
219: return bufcolumn[bufpos];
220: }
221:
222: /**
223: * @deprecated
224: * @see #getEndLine
225: */
226:
227: public int getLine() {
228: return bufline[bufpos];
229: }
230:
231: public int getEndColumn() {
232: return bufcolumn[bufpos];
233: }
234:
235: public int getEndLine() {
236: return bufline[bufpos];
237: }
238:
239: public int getBeginColumn() {
240: return bufcolumn[tokenBegin];
241: }
242:
243: public int getBeginLine() {
244: return bufline[tokenBegin];
245: }
246:
247: public void backup(int amount) {
248:
249: inBuf += amount;
250: if ((bufpos -= amount) < 0)
251: bufpos += bufsize;
252: }
253:
254: public SimpleCharStream(java.io.Reader dstream, int startline,
255: int startcolumn, int buffersize) {
256: inputStream = dstream;
257: line = startline;
258: column = startcolumn - 1;
259:
260: available = bufsize = buffersize;
261: buffer = new char[buffersize];
262: bufline = new int[buffersize];
263: bufcolumn = new int[buffersize];
264: }
265:
266: public SimpleCharStream(java.io.Reader dstream, int startline,
267: int startcolumn) {
268: this (dstream, startline, startcolumn, 4096);
269: }
270:
271: public SimpleCharStream(java.io.Reader dstream) {
272: this (dstream, 1, 1, 4096);
273: }
274:
275: public void ReInit(java.io.Reader dstream, int startline,
276: int startcolumn, int buffersize) {
277: inputStream = dstream;
278: line = startline;
279: column = startcolumn - 1;
280:
281: if (buffer == null || buffersize != buffer.length) {
282: available = bufsize = buffersize;
283: buffer = new char[buffersize];
284: bufline = new int[buffersize];
285: bufcolumn = new int[buffersize];
286: }
287: prevCharIsLF = prevCharIsCR = false;
288: tokenBegin = inBuf = maxNextCharInd = 0;
289: bufpos = -1;
290: }
291:
292: public void ReInit(java.io.Reader dstream, int startline,
293: int startcolumn) {
294: ReInit(dstream, startline, startcolumn, 4096);
295: }
296:
297: public void ReInit(java.io.Reader dstream) {
298: ReInit(dstream, 1, 1, 4096);
299: }
300:
301: public SimpleCharStream(java.io.InputStream dstream, int startline,
302: int startcolumn, int buffersize) {
303: this (new java.io.InputStreamReader(dstream), startline,
304: startcolumn, 4096);
305: }
306:
307: public SimpleCharStream(java.io.InputStream dstream, int startline,
308: int startcolumn) {
309: this (dstream, startline, startcolumn, 4096);
310: }
311:
312: public SimpleCharStream(java.io.InputStream dstream) {
313: this (dstream, 1, 1, 4096);
314: }
315:
316: public void ReInit(java.io.InputStream dstream, int startline,
317: int startcolumn, int buffersize) {
318: ReInit(new java.io.InputStreamReader(dstream), startline,
319: startcolumn, 4096);
320: }
321:
322: public void ReInit(java.io.InputStream dstream) {
323: ReInit(dstream, 1, 1, 4096);
324: }
325:
326: public void ReInit(java.io.InputStream dstream, int startline,
327: int startcolumn) {
328: ReInit(dstream, startline, startcolumn, 4096);
329: }
330:
331: public String GetImage() {
332: if (bufpos >= tokenBegin)
333: return new String(buffer, tokenBegin, bufpos - tokenBegin
334: + 1);
335: else
336: return new String(buffer, tokenBegin, bufsize - tokenBegin)
337: + new String(buffer, 0, bufpos + 1);
338: }
339:
340: public char[] GetSuffix(int len) {
341: char[] ret = new char[len];
342:
343: if ((bufpos + 1) >= len)
344: System.arraycopy(buffer, bufpos - len + 1, ret, 0, len);
345: else {
346: System.arraycopy(buffer, bufsize - (len - bufpos - 1), ret,
347: 0, len - bufpos - 1);
348: System.arraycopy(buffer, 0, ret, len - bufpos - 1,
349: bufpos + 1);
350: }
351:
352: return ret;
353: }
354:
355: public void Done() {
356: buffer = null;
357: bufline = null;
358: bufcolumn = null;
359: }
360:
361: /**
362: * Method to adjust line and column numbers for the start of a token.
363: */
364: public void adjustBeginLineColumn(int newLine, int newCol) {
365: int start = tokenBegin;
366: int len;
367:
368: if (bufpos >= tokenBegin) {
369: len = bufpos - tokenBegin + inBuf + 1;
370: } else {
371: len = bufsize - tokenBegin + bufpos + 1 + inBuf;
372: }
373:
374: int i = 0, j = 0, k = 0;
375: int nextColDiff = 0, columnDiff = 0;
376:
377: while (i < len
378: && bufline[j = start % bufsize] == bufline[k = ++start
379: % bufsize]) {
380: bufline[j] = newLine;
381: nextColDiff = columnDiff + bufcolumn[k] - bufcolumn[j];
382: bufcolumn[j] = newCol + columnDiff;
383: columnDiff = nextColDiff;
384: i++;
385: }
386:
387: if (i < len) {
388: bufline[j] = newLine++;
389: bufcolumn[j] = newCol + columnDiff;
390:
391: while (i++ < len) {
392: if (bufline[j = start % bufsize] != bufline[++start
393: % bufsize])
394: bufline[j] = newLine++;
395: else
396: bufline[j] = newLine;
397: }
398: }
399:
400: line = bufline[j];
401: column = bufcolumn[j];
402: }
403:
404: }
|