001/* Generated By:JavaCC: Do not edit this line. SimpleCharStream.java Version 5.0 */
002/* JavaCCOptions:STATIC=false,SUPPORT_CLASS_VISIBILITY_PUBLIC=true */
003/* Parser for matrices written in matlab format.
004
005 Copyright (c) 1998-2008 The Regents of the University of California.
006 All rights reserved.
007 Permission is hereby granted, without written agreement and without
008 license or royalty fees, to use, copy, modify, and distribute this
009 software and its documentation for any purpose, provided that the above
010 copyright notice and the following two paragraphs appear in all copies
011 of this software.
012
013 IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
014 FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
015 ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
016 THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
017 SUCH DAMAGE.
018
019 THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
020 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
021 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
022 PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
023 CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
024 ENHANCEMENTS, OR MODIFICATIONS.
025
026                                        PT_COPYRIGHT_VERSION_2
027                                        COPYRIGHTENDKEY
028 */
029package ptolemy.data.expr;
030
031/**
032 * An implementation of interface CharStream, where the stream is assumed to
033 * contain only ASCII characters (without unicode processing).
034 */
035
036public class SimpleCharStream {
037    /** Whether parser is static. */
038    public static final boolean staticFlag = false;
039    int bufsize;
040    int available;
041    int tokenBegin;
042    /** Position in buffer. */
043    public int bufpos = -1;
044    protected int bufline[];
045    protected int bufcolumn[];
046
047    protected int column = 0;
048    protected int line = 1;
049
050    protected boolean prevCharIsCR = false;
051    protected boolean prevCharIsLF = false;
052
053    protected java.io.Reader inputStream;
054
055    protected char[] buffer;
056    protected int maxNextCharInd = 0;
057    protected int inBuf = 0;
058    protected int tabSize = 8;
059
060    protected void setTabSize(int i) {
061        tabSize = i;
062    }
063
064    protected int getTabSize(int i) {
065        return tabSize;
066    }
067
068    protected void ExpandBuff(boolean wrapAround) {
069        char[] newbuffer = new char[bufsize + 2048];
070        int newbufline[] = new int[bufsize + 2048];
071        int newbufcolumn[] = new int[bufsize + 2048];
072
073        try {
074            if (wrapAround) {
075                System.arraycopy(buffer, tokenBegin, newbuffer, 0,
076                        bufsize - tokenBegin);
077                System.arraycopy(buffer, 0, newbuffer, bufsize - tokenBegin,
078                        bufpos);
079                buffer = newbuffer;
080
081                System.arraycopy(bufline, tokenBegin, newbufline, 0,
082                        bufsize - tokenBegin);
083                System.arraycopy(bufline, 0, newbufline, bufsize - tokenBegin,
084                        bufpos);
085                bufline = newbufline;
086
087                System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0,
088                        bufsize - tokenBegin);
089                System.arraycopy(bufcolumn, 0, newbufcolumn,
090                        bufsize - tokenBegin, bufpos);
091                bufcolumn = newbufcolumn;
092
093                maxNextCharInd = (bufpos += (bufsize - tokenBegin));
094            } else {
095                System.arraycopy(buffer, tokenBegin, newbuffer, 0,
096                        bufsize - tokenBegin);
097                buffer = newbuffer;
098
099                System.arraycopy(bufline, tokenBegin, newbufline, 0,
100                        bufsize - tokenBegin);
101                bufline = newbufline;
102
103                System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0,
104                        bufsize - tokenBegin);
105                bufcolumn = newbufcolumn;
106
107                maxNextCharInd = (bufpos -= tokenBegin);
108            }
109        } catch (Throwable t) {
110            throw new Error(t.getMessage());
111        }
112
113        bufsize += 2048;
114        available = bufsize;
115        tokenBegin = 0;
116    }
117
118    protected void FillBuff() throws java.io.IOException {
119        if (maxNextCharInd == available) {
120            if (available == bufsize) {
121                if (tokenBegin > 2048) {
122                    bufpos = maxNextCharInd = 0;
123                    available = tokenBegin;
124                } else if (tokenBegin < 0) {
125                    bufpos = maxNextCharInd = 0;
126                } else {
127                    ExpandBuff(false);
128                }
129            } else if (available > tokenBegin) {
130                available = bufsize;
131            } else if ((tokenBegin - available) < 2048) {
132                ExpandBuff(true);
133            } else {
134                available = tokenBegin;
135            }
136        }
137
138        int i;
139        try {
140            if ((i = inputStream.read(buffer, maxNextCharInd,
141                    available - maxNextCharInd)) == -1) {
142                inputStream.close();
143                throw new java.io.IOException();
144            } else {
145                maxNextCharInd += i;
146            }
147            return;
148        } catch (java.io.IOException e) {
149            --bufpos;
150            backup(0);
151            if (tokenBegin == -1) {
152                tokenBegin = bufpos;
153            }
154            throw e;
155        }
156    }
157
158    /** Start. */
159    public char BeginToken() throws java.io.IOException {
160        tokenBegin = -1;
161        char c = readChar();
162        tokenBegin = bufpos;
163
164        return c;
165    }
166
167    protected void UpdateLineColumn(char c) {
168        column++;
169
170        if (prevCharIsLF) {
171            prevCharIsLF = false;
172            line += (column = 1);
173        } else if (prevCharIsCR) {
174            prevCharIsCR = false;
175            if (c == '\n') {
176                prevCharIsLF = true;
177            } else {
178                line += (column = 1);
179            }
180        }
181
182        switch (c) {
183        case '\r':
184            prevCharIsCR = true;
185            break;
186        case '\n':
187            prevCharIsLF = true;
188            break;
189        case '\t':
190            column--;
191            column += (tabSize - (column % tabSize));
192            break;
193        default:
194            break;
195        }
196
197        bufline[bufpos] = line;
198        bufcolumn[bufpos] = column;
199    }
200
201    /** Read a character. */
202    public char readChar() throws java.io.IOException {
203        if (inBuf > 0) {
204            --inBuf;
205
206            if (++bufpos == bufsize) {
207                bufpos = 0;
208            }
209
210            return buffer[bufpos];
211        }
212
213        if (++bufpos >= maxNextCharInd) {
214            FillBuff();
215        }
216
217        char c = buffer[bufpos];
218
219        UpdateLineColumn(c);
220        return c;
221    }
222
223    @Deprecated
224    /**
225     * @deprecated
226     * @see #getEndColumn
227     */
228    public int getColumn() {
229        return bufcolumn[bufpos];
230    }
231
232    @Deprecated
233    /**
234     * @deprecated
235     * @see #getEndLine
236     */
237    public int getLine() {
238        return bufline[bufpos];
239    }
240
241    /** Get token end column number. */
242    public int getEndColumn() {
243        return bufcolumn[bufpos];
244    }
245
246    /** Get token end line number. */
247    public int getEndLine() {
248        return bufline[bufpos];
249    }
250
251    /** Get token beginning column number. */
252    public int getBeginColumn() {
253        return bufcolumn[tokenBegin];
254    }
255
256    /** Get token beginning line number. */
257    public int getBeginLine() {
258        return bufline[tokenBegin];
259    }
260
261    /** Backup a number of characters. */
262    public void backup(int amount) {
263
264        inBuf += amount;
265        if ((bufpos -= amount) < 0) {
266            bufpos += bufsize;
267        }
268    }
269
270    /** Constructor. */
271    public SimpleCharStream(java.io.Reader dstream, int startline,
272            int startcolumn, int buffersize) {
273        inputStream = dstream;
274        line = startline;
275        column = startcolumn - 1;
276
277        available = bufsize = buffersize;
278        buffer = new char[buffersize];
279        bufline = new int[buffersize];
280        bufcolumn = new int[buffersize];
281    }
282
283    /** Constructor. */
284    public SimpleCharStream(java.io.Reader dstream, int startline,
285            int startcolumn) {
286        this(dstream, startline, startcolumn, 4096);
287    }
288
289    /** Constructor. */
290    public SimpleCharStream(java.io.Reader dstream) {
291        this(dstream, 1, 1, 4096);
292    }
293
294    /** Reinitialise. */
295    public void ReInit(java.io.Reader dstream, int startline, int startcolumn,
296            int buffersize) {
297        inputStream = dstream;
298        line = startline;
299        column = startcolumn - 1;
300
301        if (buffer == null || buffersize != buffer.length) {
302            available = bufsize = buffersize;
303            buffer = new char[buffersize];
304            bufline = new int[buffersize];
305            bufcolumn = new int[buffersize];
306        }
307        prevCharIsLF = prevCharIsCR = false;
308        tokenBegin = inBuf = maxNextCharInd = 0;
309        bufpos = -1;
310    }
311
312    /** Reinitialise. */
313    public void ReInit(java.io.Reader dstream, int startline, int startcolumn) {
314        ReInit(dstream, startline, startcolumn, 4096);
315    }
316
317    /** Reinitialise. */
318    public void ReInit(java.io.Reader dstream) {
319        ReInit(dstream, 1, 1, 4096);
320    }
321
322    /** Constructor. */
323    public SimpleCharStream(java.io.InputStream dstream, String encoding,
324            int startline, int startcolumn, int buffersize)
325            throws java.io.UnsupportedEncodingException {
326        this(encoding == null ? new java.io.InputStreamReader(dstream)
327                : new java.io.InputStreamReader(dstream, encoding), startline,
328                startcolumn, buffersize);
329    }
330
331    /** Constructor. */
332    public SimpleCharStream(java.io.InputStream dstream, int startline,
333            int startcolumn, int buffersize) {
334        this(new java.io.InputStreamReader(dstream), startline, startcolumn,
335                buffersize);
336    }
337
338    /** Constructor. */
339    public SimpleCharStream(java.io.InputStream dstream, String encoding,
340            int startline, int startcolumn)
341            throws java.io.UnsupportedEncodingException {
342        this(dstream, encoding, startline, startcolumn, 4096);
343    }
344
345    /** Constructor. */
346    public SimpleCharStream(java.io.InputStream dstream, int startline,
347            int startcolumn) {
348        this(dstream, startline, startcolumn, 4096);
349    }
350
351    /** Constructor. */
352    public SimpleCharStream(java.io.InputStream dstream, String encoding)
353            throws java.io.UnsupportedEncodingException {
354        this(dstream, encoding, 1, 1, 4096);
355    }
356
357    /** Constructor. */
358    public SimpleCharStream(java.io.InputStream dstream) {
359        this(dstream, 1, 1, 4096);
360    }
361
362    /** Reinitialise. */
363    public void ReInit(java.io.InputStream dstream, String encoding,
364            int startline, int startcolumn, int buffersize)
365            throws java.io.UnsupportedEncodingException {
366        ReInit(encoding == null ? new java.io.InputStreamReader(dstream)
367                : new java.io.InputStreamReader(dstream, encoding), startline,
368                startcolumn, buffersize);
369    }
370
371    /** Reinitialise. */
372    public void ReInit(java.io.InputStream dstream, int startline,
373            int startcolumn, int buffersize) {
374        ReInit(new java.io.InputStreamReader(dstream), startline, startcolumn,
375                buffersize);
376    }
377
378    /** Reinitialise. */
379    public void ReInit(java.io.InputStream dstream, String encoding)
380            throws java.io.UnsupportedEncodingException {
381        ReInit(dstream, encoding, 1, 1, 4096);
382    }
383
384    /** Reinitialise. */
385    public void ReInit(java.io.InputStream dstream) {
386        ReInit(dstream, 1, 1, 4096);
387    }
388
389    /** Reinitialise. */
390    public void ReInit(java.io.InputStream dstream, String encoding,
391            int startline, int startcolumn)
392            throws java.io.UnsupportedEncodingException {
393        ReInit(dstream, encoding, startline, startcolumn, 4096);
394    }
395
396    /** Reinitialise. */
397    public void ReInit(java.io.InputStream dstream, int startline,
398            int startcolumn) {
399        ReInit(dstream, startline, startcolumn, 4096);
400    }
401
402    /** Get token literal value. */
403    public String GetImage() {
404        if (bufpos >= tokenBegin) {
405            return new String(buffer, tokenBegin, bufpos - tokenBegin + 1);
406        } else {
407            return new String(buffer, tokenBegin, bufsize - tokenBegin)
408                    + new String(buffer, 0, bufpos + 1);
409        }
410    }
411
412    /** Get the suffix. */
413    public char[] GetSuffix(int len) {
414        char[] ret = new char[len];
415
416        if ((bufpos + 1) >= len) {
417            System.arraycopy(buffer, bufpos - len + 1, ret, 0, len);
418        } else {
419            System.arraycopy(buffer, bufsize - (len - bufpos - 1), ret, 0,
420                    len - bufpos - 1);
421            System.arraycopy(buffer, 0, ret, len - bufpos - 1, bufpos + 1);
422        }
423
424        return ret;
425    }
426
427    /** Reset buffer when finished. */
428    public void Done() {
429        buffer = null;
430        bufline = null;
431        bufcolumn = null;
432    }
433
434    /**
435     * Method to adjust line and column numbers for the start of a token.
436     */
437    public void adjustBeginLineColumn(int newLine, int newCol) {
438        int start = tokenBegin;
439        int len;
440
441        if (bufpos >= tokenBegin) {
442            len = bufpos - tokenBegin + inBuf + 1;
443        } else {
444            len = bufsize - tokenBegin + bufpos + 1 + inBuf;
445        }
446
447        int i = 0, j = 0, k = 0;
448        int nextColDiff = 0, columnDiff = 0;
449
450        while (i < len && bufline[j = start % bufsize] == bufline[k = ++start
451                % bufsize]) {
452            bufline[j] = newLine;
453            nextColDiff = columnDiff + bufcolumn[k] - bufcolumn[j];
454            bufcolumn[j] = newCol + columnDiff;
455            columnDiff = nextColDiff;
456            i++;
457        }
458
459        if (i < len) {
460            bufline[j] = newLine++;
461            bufcolumn[j] = newCol + columnDiff;
462
463            while (i++ < len) {
464                if (bufline[j = start % bufsize] != bufline[++start
465                        % bufsize]) {
466                    bufline[j] = newLine++;
467                } else {
468                    bufline[j] = newLine;
469                }
470            }
471        }
472
473        line = bufline[j];
474        column = bufcolumn[j];
475    }
476
477}
478/* JavaCC - OriginalChecksum=c04669e88d4cb9b057b9a97082be2f22 (do not edit this line) */