001    /**
002     *   GRANITE DATA SERVICES
003     *   Copyright (C) 2006-2013 GRANITE DATA SERVICES S.A.S.
004     *
005     *   This file is part of the Granite Data Services Platform.
006     *
007     *   Granite Data Services is free software; you can redistribute it and/or
008     *   modify it under the terms of the GNU Lesser General Public
009     *   License as published by the Free Software Foundation; either
010     *   version 2.1 of the License, or (at your option) any later version.
011     *
012     *   Granite Data Services is distributed in the hope that it will be useful,
013     *   but WITHOUT ANY WARRANTY; without even the implied warranty of
014     *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
015     *   General Public License for more details.
016     *
017     *   You should have received a copy of the GNU Lesser General Public
018     *   License along with this library; if not, write to the Free Software
019     *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
020     *   USA, or see <http://www.gnu.org/licenses/>.
021     */
022    /* Generated By:JavaCC: Do not edit this line. SimpleCharStream.java Version 4.0 */
023    /**
024     *
025     * Licensed to the Apache Software Foundation (ASF) under one or more
026     * contributor license agreements.  See the NOTICE file distributed with
027     * this work for additional information regarding copyright ownership.
028     * The ASF licenses this file to You under the Apache License, Version 2.0
029     * (the "License"); you may not use this file except in compliance with
030     * the License.  You may obtain a copy of the License at
031     *
032     * http://www.apache.org/licenses/LICENSE-2.0
033     *
034     * Unless required by applicable law or agreed to in writing, software
035     * distributed under the License is distributed on an "AS IS" BASIS,
036     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
037     * See the License for the specific language governing permissions and
038     * limitations under the License.
039     */
040    
041    package org.granite.gravity.selector;
042    
043    /**
044     * An implementation of interface CharStream, where the stream is assumed to
045     * contain only ASCII characters (without unicode processing).
046     */
047    
048    public class SimpleCharStream
049    {
050      public static final boolean staticFlag = false;
051      int bufsize;
052      int available;
053      int tokenBegin;
054      public int bufpos = -1;
055      protected int bufline[];
056      protected int bufcolumn[];
057    
058      protected int column = 0;
059      protected int line = 1;
060    
061      protected boolean prevCharIsCR = false;
062      protected boolean prevCharIsLF = false;
063    
064      protected java.io.Reader inputStream;
065    
066      protected char[] buffer;
067      protected int maxNextCharInd = 0;
068      protected int inBuf = 0;
069      protected int tabSize = 8;
070    
071      protected void setTabSize(int i) { tabSize = i; }
072      protected int getTabSize(int i) { return tabSize; }
073    
074    
075      protected void ExpandBuff(boolean wrapAround)
076      {
077         char[] newbuffer = new char[bufsize + 2048];
078         int newbufline[] = new int[bufsize + 2048];
079         int newbufcolumn[] = new int[bufsize + 2048];
080    
081         try
082         {
083            if (wrapAround)
084            {
085               System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin);
086               System.arraycopy(buffer, 0, newbuffer,
087                                                 bufsize - tokenBegin, bufpos);
088               buffer = newbuffer;
089    
090               System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin);
091               System.arraycopy(bufline, 0, newbufline, bufsize - tokenBegin, bufpos);
092               bufline = newbufline;
093    
094               System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin);
095               System.arraycopy(bufcolumn, 0, newbufcolumn, bufsize - tokenBegin, bufpos);
096               bufcolumn = newbufcolumn;
097    
098               maxNextCharInd = (bufpos += (bufsize - tokenBegin));
099            }
100            else
101            {
102               System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin);
103               buffer = newbuffer;
104    
105               System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin);
106               bufline = newbufline;
107    
108               System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin);
109               bufcolumn = newbufcolumn;
110    
111               maxNextCharInd = (bufpos -= tokenBegin);
112            }
113         }
114         catch (Throwable t)
115         {
116            throw new Error(t.getMessage());
117         }
118    
119    
120         bufsize += 2048;
121         available = bufsize;
122         tokenBegin = 0;
123      }
124    
125      protected void FillBuff() throws java.io.IOException
126      {
127         if (maxNextCharInd == available)
128         {
129            if (available == bufsize)
130            {
131               if (tokenBegin > 2048)
132               {
133                  bufpos = maxNextCharInd = 0;
134                  available = tokenBegin;
135               }
136               else if (tokenBegin < 0)
137                  bufpos = maxNextCharInd = 0;
138               else
139                  ExpandBuff(false);
140            }
141            else if (available > tokenBegin)
142               available = bufsize;
143            else if ((tokenBegin - available) < 2048)
144               ExpandBuff(true);
145            else
146               available = tokenBegin;
147         }
148    
149         int i;
150         try {
151            if ((i = inputStream.read(buffer, maxNextCharInd,
152                                        available - maxNextCharInd)) == -1)
153            {
154               inputStream.close();
155               throw new java.io.IOException();
156            }
157            maxNextCharInd += i;
158            return;
159         }
160         catch(java.io.IOException e) {
161            --bufpos;
162            backup(0);
163            if (tokenBegin == -1)
164               tokenBegin = bufpos;
165            throw e;
166         }
167      }
168    
169      public char BeginToken() throws java.io.IOException
170      {
171         tokenBegin = -1;
172         char c = readChar();
173         tokenBegin = bufpos;
174    
175         return c;
176      }
177    
178      protected void UpdateLineColumn(char c)
179      {
180         column++;
181    
182         if (prevCharIsLF)
183         {
184            prevCharIsLF = false;
185            line += (column = 1);
186         }
187         else if (prevCharIsCR)
188         {
189            prevCharIsCR = false;
190            if (c == '\n')
191            {
192               prevCharIsLF = true;
193            }
194            else
195               line += (column = 1);
196         }
197    
198         switch (c)
199         {
200            case '\r' :
201               prevCharIsCR = true;
202               break;
203            case '\n' :
204               prevCharIsLF = true;
205               break;
206            case '\t' :
207               column--;
208               column += (tabSize - (column % tabSize));
209               break;
210            default :
211               break;
212         }
213    
214         bufline[bufpos] = line;
215         bufcolumn[bufpos] = column;
216      }
217    
218      public char readChar() throws java.io.IOException
219      {
220         if (inBuf > 0)
221         {
222            --inBuf;
223    
224            if (++bufpos == bufsize)
225               bufpos = 0;
226    
227            return buffer[bufpos];
228         }
229    
230         if (++bufpos >= maxNextCharInd)
231            FillBuff();
232    
233         char c = buffer[bufpos];
234    
235         UpdateLineColumn(c);
236         return (c);
237      }
238    
239      /**
240       * @deprecated
241       * @see #getEndColumn
242       */
243    
244      @SuppressWarnings("dep-ann")
245    public int getColumn() {
246         return bufcolumn[bufpos];
247      }
248    
249      /**
250       * @deprecated
251       * @see #getEndLine
252       */
253    
254      @SuppressWarnings("dep-ann")
255    public int getLine() {
256         return bufline[bufpos];
257      }
258    
259      public int getEndColumn() {
260         return bufcolumn[bufpos];
261      }
262    
263      public int getEndLine() {
264         return bufline[bufpos];
265      }
266    
267      public int getBeginColumn() {
268         return bufcolumn[tokenBegin];
269      }
270    
271      public int getBeginLine() {
272         return bufline[tokenBegin];
273      }
274    
275      public void backup(int amount) {
276    
277        inBuf += amount;
278        if ((bufpos -= amount) < 0)
279           bufpos += bufsize;
280      }
281    
282      public SimpleCharStream(java.io.Reader dstream, int startline,
283      int startcolumn, int buffersize)
284      {
285        inputStream = dstream;
286        line = startline;
287        column = startcolumn - 1;
288    
289        available = bufsize = buffersize;
290        buffer = new char[buffersize];
291        bufline = new int[buffersize];
292        bufcolumn = new int[buffersize];
293      }
294    
295      public SimpleCharStream(java.io.Reader dstream, int startline,
296                              int startcolumn)
297      {
298         this(dstream, startline, startcolumn, 4096);
299      }
300    
301      public SimpleCharStream(java.io.Reader dstream)
302      {
303         this(dstream, 1, 1, 4096);
304      }
305      public void ReInit(java.io.Reader dstream, int startline,
306      int startcolumn, int buffersize)
307      {
308        inputStream = dstream;
309        line = startline;
310        column = startcolumn - 1;
311    
312        if (buffer == null || buffersize != buffer.length)
313        {
314          available = bufsize = buffersize;
315          buffer = new char[buffersize];
316          bufline = new int[buffersize];
317          bufcolumn = new int[buffersize];
318        }
319        prevCharIsLF = prevCharIsCR = false;
320        tokenBegin = inBuf = maxNextCharInd = 0;
321        bufpos = -1;
322      }
323    
324      public void ReInit(java.io.Reader dstream, int startline,
325                         int startcolumn)
326      {
327         ReInit(dstream, startline, startcolumn, 4096);
328      }
329    
330      public void ReInit(java.io.Reader dstream)
331      {
332         ReInit(dstream, 1, 1, 4096);
333      }
334      public SimpleCharStream(java.io.InputStream dstream, String encoding, int startline,
335      int startcolumn, int buffersize) throws java.io.UnsupportedEncodingException
336      {
337         this(encoding == null ? new java.io.InputStreamReader(dstream) : new java.io.InputStreamReader(dstream, encoding), startline, startcolumn, buffersize);
338      }
339    
340      public SimpleCharStream(java.io.InputStream dstream, int startline,
341      int startcolumn, int buffersize)
342      {
343         this(new java.io.InputStreamReader(dstream), startline, startcolumn, buffersize);
344      }
345    
346      public SimpleCharStream(java.io.InputStream dstream, String encoding, int startline,
347                              int startcolumn) throws java.io.UnsupportedEncodingException
348      {
349         this(dstream, encoding, startline, startcolumn, 4096);
350      }
351    
352      public SimpleCharStream(java.io.InputStream dstream, int startline,
353                              int startcolumn)
354      {
355         this(dstream, startline, startcolumn, 4096);
356      }
357    
358      public SimpleCharStream(java.io.InputStream dstream, String encoding) throws java.io.UnsupportedEncodingException
359      {
360         this(dstream, encoding, 1, 1, 4096);
361      }
362    
363      public SimpleCharStream(java.io.InputStream dstream)
364      {
365         this(dstream, 1, 1, 4096);
366      }
367    
368      public void ReInit(java.io.InputStream dstream, String encoding, int startline,
369                              int startcolumn, int buffersize) throws java.io.UnsupportedEncodingException
370      {
371         ReInit(encoding == null ? new java.io.InputStreamReader(dstream) : new java.io.InputStreamReader(dstream, encoding), startline, startcolumn, buffersize);
372      }
373    
374      public void ReInit(java.io.InputStream dstream, int startline,
375                              int startcolumn, int buffersize)
376      {
377         ReInit(new java.io.InputStreamReader(dstream), startline, startcolumn, buffersize);
378      }
379    
380      public void ReInit(java.io.InputStream dstream, String encoding) throws java.io.UnsupportedEncodingException
381      {
382         ReInit(dstream, encoding, 1, 1, 4096);
383      }
384    
385      public void ReInit(java.io.InputStream dstream)
386      {
387         ReInit(dstream, 1, 1, 4096);
388      }
389      public void ReInit(java.io.InputStream dstream, String encoding, int startline,
390                         int startcolumn) throws java.io.UnsupportedEncodingException
391      {
392         ReInit(dstream, encoding, startline, startcolumn, 4096);
393      }
394      public void ReInit(java.io.InputStream dstream, int startline,
395                         int startcolumn)
396      {
397         ReInit(dstream, startline, startcolumn, 4096);
398      }
399      public String GetImage()
400      {
401         if (bufpos >= tokenBegin)
402            return new String(buffer, tokenBegin, bufpos - tokenBegin + 1);
403         return new String(buffer, tokenBegin, bufsize - tokenBegin) +
404                                  new String(buffer, 0, bufpos + 1);
405      }
406    
407      public char[] GetSuffix(int len)
408      {
409         char[] ret = new char[len];
410    
411         if ((bufpos + 1) >= len)
412            System.arraycopy(buffer, bufpos - len + 1, ret, 0, len);
413         else
414         {
415            System.arraycopy(buffer, bufsize - (len - bufpos - 1), ret, 0,
416                                                              len - bufpos - 1);
417            System.arraycopy(buffer, 0, ret, len - bufpos - 1, bufpos + 1);
418         }
419    
420         return ret;
421      }
422    
423      public void Done()
424      {
425         buffer = null;
426         bufline = null;
427         bufcolumn = null;
428      }
429    
430      /**
431       * Method to adjust line and column numbers for the start of a token.
432       */
433      public void adjustBeginLineColumn(int newLine, int newCol)
434      {
435         int start = tokenBegin;
436         int len;
437    
438         if (bufpos >= tokenBegin)
439         {
440            len = bufpos - tokenBegin + inBuf + 1;
441         }
442         else
443         {
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 &&
451                bufline[j = start % bufsize] == bufline[k = ++start % bufsize])
452         {
453            bufline[j] = newLine;
454            nextColDiff = columnDiff + bufcolumn[k] - bufcolumn[j];
455            bufcolumn[j] = newCol + columnDiff;
456            columnDiff = nextColDiff;
457            i++;
458         }
459    
460         if (i < len)
461         {
462            bufline[j] = newLine++;
463            bufcolumn[j] = newCol + columnDiff;
464    
465            while (i++ < len)
466            {
467               if (bufline[j = start % bufsize] != bufline[++start % bufsize])
468                  bufline[j] = newLine++;
469               else
470                  bufline[j] = newLine;
471            }
472         }
473    
474         line = bufline[j];
475         column = bufcolumn[j];
476      }
477    
478    }