Home | History | Annotate | Download | only in Mini
      1 /*
      2  * Licensed to the Apache Software Foundation (ASF) under one or more
      3  * contributor license agreements.  See the NOTICE file distributed with
      4  * this work for additional information regarding copyright ownership.
      5  * The ASF licenses this file to You under the Apache License, Version 2.0
      6  * (the "License"); you may not use this file except in compliance with
      7  * the License.  You may obtain a copy of the License at
      8  *
      9  *      http://www.apache.org/licenses/LICENSE-2.0
     10  *
     11  *  Unless required by applicable law or agreed to in writing, software
     12  *  distributed under the License is distributed on an "AS IS" BASIS,
     13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14  *  See the License for the specific language governing permissions and
     15  *  limitations under the License.
     16  *
     17  */
     18 /* Generated By:JavaCC: Do not edit this line. ASCII_CharStream.java Version 0.7pre6 */
     19 package Mini;
     20 
     21 /**
     22  * An implementation of interface CharStream, where the stream is assumed to
     23  * contain only ASCII characters (without unicode processing).
     24  */
     25 
     26 public final class ASCII_CharStream
     27 {
     28   public static final boolean staticFlag = true;
     29   static int bufsize;
     30   static int available;
     31   static int tokenBegin;
     32   static public int bufpos = -1;
     33   static private int bufline[];
     34   static private int bufcolumn[];
     35 
     36   static private int column = 0;
     37   static private int line = 1;
     38 
     39   static private boolean prevCharIsCR = false;
     40   static private boolean prevCharIsLF = false;
     41 
     42   static private java.io.Reader inputStream;
     43 
     44   static private char[] buffer;
     45   static private int maxNextCharInd = 0;
     46   static private int inBuf = 0;
     47 
     48   static private void ExpandBuff(boolean wrapAround)
     49   {
     50      char[] newbuffer = new char[bufsize + 2048];
     51      int newbufline[] = new int[bufsize + 2048];
     52      int newbufcolumn[] = new int[bufsize + 2048];
     53 
     54      try
     55      {
     56         if (wrapAround)
     57         {
     58            System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin);
     59            System.arraycopy(buffer, 0, newbuffer,
     60                                              bufsize - tokenBegin, bufpos);
     61            buffer = newbuffer;
     62 
     63            System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin);
     64            System.arraycopy(bufline, 0, newbufline, bufsize - tokenBegin, bufpos);
     65            bufline = newbufline;
     66 
     67            System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin);
     68            System.arraycopy(bufcolumn, 0, newbufcolumn, bufsize - tokenBegin, bufpos);
     69            bufcolumn = newbufcolumn;
     70 
     71            maxNextCharInd = (bufpos += (bufsize - tokenBegin));
     72         }
     73         else
     74         {
     75            System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin);
     76            buffer = newbuffer;
     77 
     78            System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin);
     79            bufline = newbufline;
     80 
     81            System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin);
     82            bufcolumn = newbufcolumn;
     83 
     84            maxNextCharInd = (bufpos -= tokenBegin);
     85         }
     86      }
     87      catch (Throwable t)
     88      {
     89         throw new Error(t.getMessage());
     90      }
     91 
     92 
     93      bufsize += 2048;
     94      available = bufsize;
     95      tokenBegin = 0;
     96   }
     97 
     98   static private void FillBuff() throws java.io.IOException
     99   {
    100      if (maxNextCharInd == available)
    101      {
    102         if (available == bufsize)
    103         {
    104            if (tokenBegin > 2048)
    105            {
    106               bufpos = maxNextCharInd = 0;
    107               available = tokenBegin;
    108            }
    109            else if (tokenBegin < 0) {
    110             bufpos = maxNextCharInd = 0;
    111         } else {
    112             ExpandBuff(false);
    113         }
    114         }
    115         else if (available > tokenBegin) {
    116             available = bufsize;
    117         } else if ((tokenBegin - available) < 2048) {
    118             ExpandBuff(true);
    119         } else {
    120             available = tokenBegin;
    121         }
    122      }
    123 
    124      int i;
    125      try {
    126         if ((i = inputStream.read(buffer, maxNextCharInd,
    127                                     available - maxNextCharInd)) == -1)
    128         {
    129            inputStream.close();
    130            throw new java.io.IOException();
    131         } else {
    132             maxNextCharInd += i;
    133         }
    134         return;
    135      }
    136      catch(java.io.IOException e) {
    137         --bufpos;
    138         backup(0);
    139         if (tokenBegin == -1) {
    140             tokenBegin = bufpos;
    141         }
    142         throw e;
    143      }
    144   }
    145 
    146   static public final char BeginToken() throws java.io.IOException
    147   {
    148      tokenBegin = -1;
    149      char c = readChar();
    150      tokenBegin = bufpos;
    151 
    152      return c;
    153   }
    154 
    155   static private void UpdateLineColumn(char c)
    156   {
    157      column++;
    158 
    159      if (prevCharIsLF)
    160      {
    161         prevCharIsLF = false;
    162         line += (column = 1);
    163      }
    164      else if (prevCharIsCR)
    165      {
    166         prevCharIsCR = false;
    167         if (c == '\n')
    168         {
    169            prevCharIsLF = true;
    170         } else {
    171             line += (column = 1);
    172         }
    173      }
    174 
    175      switch (c)
    176      {
    177         case '\r' :
    178            prevCharIsCR = true;
    179            break;
    180         case '\n' :
    181            prevCharIsLF = true;
    182            break;
    183         case '\t' :
    184            column--;
    185            column += (8 - (column & 07));
    186            break;
    187         default :
    188            break;
    189      }
    190 
    191      bufline[bufpos] = line;
    192      bufcolumn[bufpos] = column;
    193   }
    194 
    195   static public final char readChar() throws java.io.IOException
    196   {
    197      if (inBuf > 0)
    198      {
    199         --inBuf;
    200         return (char)((char)0xff & buffer[(bufpos == bufsize - 1) ? (bufpos = 0) : ++bufpos]);
    201      }
    202 
    203      if (++bufpos >= maxNextCharInd) {
    204         FillBuff();
    205     }
    206 
    207      char c = (char)((char)0xff & buffer[bufpos]);
    208 
    209      UpdateLineColumn(c);
    210      return (c);
    211   }
    212 
    213   static public final int getEndColumn() {
    214      return bufcolumn[bufpos];
    215   }
    216 
    217   static public final int getEndLine() {
    218      return bufline[bufpos];
    219   }
    220 
    221   static public final int getBeginColumn() {
    222      return bufcolumn[tokenBegin];
    223   }
    224 
    225   static public final int getBeginLine() {
    226      return bufline[tokenBegin];
    227   }
    228 
    229   static public final void backup(int amount) {
    230 
    231     inBuf += amount;
    232     if ((bufpos -= amount) < 0) {
    233         bufpos += bufsize;
    234     }
    235   }
    236 
    237   public ASCII_CharStream(java.io.Reader dstream, int startline,
    238   int startcolumn, int buffersize)
    239   {
    240     if (inputStream != null) {
    241         throw new Error("\n   ERROR: Second call to the constructor of a static ASCII_CharStream.  You must\n" +
    242            "       either use ReInit() or set the JavaCC option STATIC to false\n" +
    243            "       during the generation of this class.");
    244     }
    245     inputStream = dstream;
    246     line = startline;
    247     column = startcolumn - 1;
    248 
    249     available = bufsize = buffersize;
    250     buffer = new char[buffersize];
    251     bufline = new int[buffersize];
    252     bufcolumn = new int[buffersize];
    253   }
    254 
    255   public ASCII_CharStream(java.io.Reader dstream, int startline,
    256                                                            int startcolumn)
    257   {
    258      this(dstream, startline, startcolumn, 4096);
    259   }
    260   static public void ReInit(java.io.Reader dstream, int startline,
    261   int startcolumn, int buffersize)
    262   {
    263     inputStream = dstream;
    264     line = startline;
    265     column = startcolumn - 1;
    266 
    267     if (buffer == null || buffersize != buffer.length)
    268     {
    269       available = bufsize = buffersize;
    270       buffer = new char[buffersize];
    271       bufline = new int[buffersize];
    272       bufcolumn = new int[buffersize];
    273     }
    274     prevCharIsLF = prevCharIsCR = false;
    275     tokenBegin = inBuf = maxNextCharInd = 0;
    276     bufpos = -1;
    277   }
    278 
    279   static public void ReInit(java.io.Reader dstream, int startline,
    280                                                            int startcolumn)
    281   {
    282      ReInit(dstream, startline, startcolumn, 4096);
    283   }
    284   public ASCII_CharStream(java.io.InputStream dstream, int startline,
    285   int startcolumn, int buffersize)
    286   {
    287      this(new java.io.InputStreamReader(dstream), startline, startcolumn, 4096);
    288   }
    289 
    290   public ASCII_CharStream(java.io.InputStream dstream, int startline,
    291                                                            int startcolumn)
    292   {
    293      this(dstream, startline, startcolumn, 4096);
    294   }
    295 
    296   static public void ReInit(java.io.InputStream dstream, int startline,
    297   int startcolumn, int buffersize)
    298   {
    299      ReInit(new java.io.InputStreamReader(dstream), startline, startcolumn, 4096);
    300   }
    301   static public void ReInit(java.io.InputStream dstream, int startline,
    302                                                            int startcolumn)
    303   {
    304      ReInit(dstream, startline, startcolumn, 4096);
    305   }
    306   static public final String GetImage()
    307   {
    308      if (bufpos >= tokenBegin) {
    309         return new String(buffer, tokenBegin, bufpos - tokenBegin + 1);
    310     } else {
    311         return new String(buffer, tokenBegin, bufsize - tokenBegin) +
    312                               new String(buffer, 0, bufpos + 1);
    313     }
    314   }
    315 
    316   static public final char[] GetSuffix(int len)
    317   {
    318      char[] ret = new char[len];
    319 
    320      if ((bufpos + 1) >= len) {
    321         System.arraycopy(buffer, bufpos - len + 1, ret, 0, len);
    322     } else
    323      {
    324         System.arraycopy(buffer, bufsize - (len - bufpos - 1), ret, 0,
    325                                                           len - bufpos - 1);
    326         System.arraycopy(buffer, 0, ret, len - bufpos - 1, bufpos + 1);
    327      }
    328 
    329      return ret;
    330   }
    331 
    332   static public void Done()
    333   {
    334      buffer = null;
    335      bufline = null;
    336      bufcolumn = null;
    337   }
    338 
    339   /**
    340    * Method to adjust line and column numbers for the start of a token.<BR>
    341    */
    342   static public void adjustBeginLineColumn(int newLine, int newCol)
    343   {
    344      int start = tokenBegin;
    345      int len;
    346 
    347      if (bufpos >= tokenBegin)
    348      {
    349         len = bufpos - tokenBegin + inBuf + 1;
    350      }
    351      else
    352      {
    353         len = bufsize - tokenBegin + bufpos + 1 + inBuf;
    354      }
    355 
    356      int i = 0, j = 0, k = 0;
    357      int nextColDiff = 0, columnDiff = 0;
    358 
    359      while (i < len &&
    360             bufline[j = start % bufsize] == bufline[k = ++start % bufsize])
    361      {
    362         bufline[j] = newLine;
    363         nextColDiff = columnDiff + bufcolumn[k] - bufcolumn[j];
    364         bufcolumn[j] = newCol + columnDiff;
    365         columnDiff = nextColDiff;
    366         i++;
    367      }
    368 
    369      if (i < len)
    370      {
    371         bufline[j] = newLine++;
    372         bufcolumn[j] = newCol + columnDiff;
    373 
    374         while (i++ < len)
    375         {
    376            if (bufline[j = start % bufsize] != bufline[++start % bufsize]) {
    377             bufline[j] = newLine++;
    378         } else {
    379             bufline[j] = newLine;
    380         }
    381         }
    382      }
    383 
    384      line = bufline[j];
    385      column = bufcolumn[j];
    386   }
    387 
    388 }
    389