Save This Page
Home » lucene-3.0.1-src » org.apache.lucene.queryParser.standard.parser » [javadoc | source]
    1   /* Generated By:JavaCC: Do not edit this line. JavaCharStream.java Version 4.1 */
    2   /* JavaCCOptions:STATIC=false,SUPPORT_CLASS_VISIBILITY_PUBLIC=true */
    3   package org.apache.lucene.queryParser.standard.parser;
    4   
    5   /**
    6    * An implementation of interface CharStream, where the stream is assumed to
    7    * contain only ASCII characters (with java-like unicode escape processing).
    8    */
    9   
   10   public
   11   class JavaCharStream
   12   {
   13     /** Whether parser is static. */
   14     public static final boolean staticFlag = false;
   15   
   16     static final int hexval(char c) throws java.io.IOException {
   17       switch(c)
   18       {
   19          case '0' :
   20             return 0;
   21          case '1' :
   22             return 1;
   23          case '2' :
   24             return 2;
   25          case '3' :
   26             return 3;
   27          case '4' :
   28             return 4;
   29          case '5' :
   30             return 5;
   31          case '6' :
   32             return 6;
   33          case '7' :
   34             return 7;
   35          case '8' :
   36             return 8;
   37          case '9' :
   38             return 9;
   39   
   40          case 'a' :
   41          case 'A' :
   42             return 10;
   43          case 'b' :
   44          case 'B' :
   45             return 11;
   46          case 'c' :
   47          case 'C' :
   48             return 12;
   49          case 'd' :
   50          case 'D' :
   51             return 13;
   52          case 'e' :
   53          case 'E' :
   54             return 14;
   55          case 'f' :
   56          case 'F' :
   57             return 15;
   58       }
   59   
   60       throw new java.io.IOException(); // Should never come here
   61     }
   62   
   63   /** Position in buffer. */
   64     public int bufpos = -1;
   65     int bufsize;
   66     int available;
   67     int tokenBegin;
   68     protected int bufline[];
   69     protected int bufcolumn[];
   70   
   71     protected int column = 0;
   72     protected int line = 1;
   73   
   74     protected boolean prevCharIsCR = false;
   75     protected boolean prevCharIsLF = false;
   76   
   77     protected java.io.Reader inputStream;
   78   
   79     protected char[] nextCharBuf;
   80     protected char[] buffer;
   81     protected int maxNextCharInd = 0;
   82     protected int nextCharInd = -1;
   83     protected int inBuf = 0;
   84     protected int tabSize = 8;
   85   
   86     protected void setTabSize(int i) { tabSize = i; }
   87     protected int getTabSize(int i) { return tabSize; }
   88   
   89     protected void ExpandBuff(boolean wrapAround)
   90     {
   91       char[] newbuffer = new char[bufsize + 2048];
   92       int newbufline[] = new int[bufsize + 2048];
   93       int newbufcolumn[] = new int[bufsize + 2048];
   94   
   95       try
   96       {
   97         if (wrapAround)
   98         {
   99           System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin);
  100           System.arraycopy(buffer, 0, newbuffer, bufsize - tokenBegin, bufpos);
  101           buffer = newbuffer;
  102   
  103           System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin);
  104           System.arraycopy(bufline, 0, newbufline, bufsize - tokenBegin, bufpos);
  105           bufline = newbufline;
  106   
  107           System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin);
  108           System.arraycopy(bufcolumn, 0, newbufcolumn, bufsize - tokenBegin, bufpos);
  109           bufcolumn = newbufcolumn;
  110   
  111           bufpos += (bufsize - tokenBegin);
  112       }
  113       else
  114       {
  115           System.arraycopy(buffer, tokenBegin, newbuffer, 0, bufsize - tokenBegin);
  116           buffer = newbuffer;
  117   
  118           System.arraycopy(bufline, tokenBegin, newbufline, 0, bufsize - tokenBegin);
  119           bufline = newbufline;
  120   
  121           System.arraycopy(bufcolumn, tokenBegin, newbufcolumn, 0, bufsize - tokenBegin);
  122           bufcolumn = newbufcolumn;
  123   
  124           bufpos -= tokenBegin;
  125         }
  126       }
  127       catch (Throwable t)
  128       {
  129         throw new Error(t.getMessage());
  130       }
  131   
  132       available = (bufsize += 2048);
  133       tokenBegin = 0;
  134     }
  135   
  136     protected void FillBuff() throws java.io.IOException
  137     {
  138       int i;
  139       if (maxNextCharInd == 4096)
  140         maxNextCharInd = nextCharInd = 0;
  141   
  142       try {
  143         if ((i = inputStream.read(nextCharBuf, maxNextCharInd,
  144                                             4096 - maxNextCharInd)) == -1)
  145         {
  146           inputStream.close();
  147           throw new java.io.IOException();
  148         }
  149         else
  150            maxNextCharInd += i;
  151         return;
  152       }
  153       catch(java.io.IOException e) {
  154         if (bufpos != 0)
  155         {
  156           --bufpos;
  157           backup(0);
  158         }
  159         else
  160         {
  161           bufline[bufpos] = line;
  162           bufcolumn[bufpos] = column;
  163         }
  164         throw e;
  165       }
  166     }
  167   
  168     protected char ReadByte() throws java.io.IOException
  169     {
  170       if (++nextCharInd >= maxNextCharInd)
  171         FillBuff();
  172   
  173       return nextCharBuf[nextCharInd];
  174     }
  175   
  176   /** @return starting character for token. */
  177     public char BeginToken() throws java.io.IOException
  178     {
  179       if (inBuf > 0)
  180       {
  181         --inBuf;
  182   
  183         if (++bufpos == bufsize)
  184           bufpos = 0;
  185   
  186         tokenBegin = bufpos;
  187         return buffer[bufpos];
  188       }
  189   
  190       tokenBegin = 0;
  191       bufpos = -1;
  192   
  193       return readChar();
  194     }
  195   
  196     protected void AdjustBuffSize()
  197     {
  198       if (available == bufsize)
  199       {
  200         if (tokenBegin > 2048)
  201         {
  202           bufpos = 0;
  203           available = tokenBegin;
  204         }
  205         else
  206           ExpandBuff(false);
  207       }
  208       else if (available > tokenBegin)
  209         available = bufsize;
  210       else if ((tokenBegin - available) < 2048)
  211         ExpandBuff(true);
  212       else
  213         available = tokenBegin;
  214     }
  215   
  216     protected void UpdateLineColumn(char c)
  217     {
  218       column++;
  219   
  220       if (prevCharIsLF)
  221       {
  222         prevCharIsLF = false;
  223         line += (column = 1);
  224       }
  225       else if (prevCharIsCR)
  226       {
  227         prevCharIsCR = false;
  228         if (c == '\n')
  229         {
  230           prevCharIsLF = true;
  231         }
  232         else
  233           line += (column = 1);
  234       }
  235   
  236       switch (c)
  237       {
  238         case '\r' :
  239           prevCharIsCR = true;
  240           break;
  241         case '\n' :
  242           prevCharIsLF = true;
  243           break;
  244         case '\t' :
  245           column--;
  246           column += (tabSize - (column % tabSize));
  247           break;
  248         default :
  249           break;
  250       }
  251   
  252       bufline[bufpos] = line;
  253       bufcolumn[bufpos] = column;
  254     }
  255   
  256   /** Read a character. */
  257     public char readChar() throws java.io.IOException
  258     {
  259       if (inBuf > 0)
  260       {
  261         --inBuf;
  262   
  263         if (++bufpos == bufsize)
  264           bufpos = 0;
  265   
  266         return buffer[bufpos];
  267       }
  268   
  269       char c;
  270   
  271       if (++bufpos == available)
  272         AdjustBuffSize();
  273   
  274       if ((buffer[bufpos] = c = ReadByte()) == '\\')
  275       {
  276         UpdateLineColumn(c);
  277   
  278         int backSlashCnt = 1;
  279   
  280         for (;;) // Read all the backslashes
  281         {
  282           if (++bufpos == available)
  283             AdjustBuffSize();
  284   
  285           try
  286           {
  287             if ((buffer[bufpos] = c = ReadByte()) != '\\')
  288             {
  289               UpdateLineColumn(c);
  290               // found a non-backslash char.
  291               if ((c == 'u') && ((backSlashCnt & 1) == 1))
  292               {
  293                 if (--bufpos < 0)
  294                   bufpos = bufsize - 1;
  295   
  296                 break;
  297               }
  298   
  299               backup(backSlashCnt);
  300               return '\\';
  301             }
  302           }
  303           catch(java.io.IOException e)
  304           {
  305   	  // We are returning one backslash so we should only backup (count-1)
  306             if (backSlashCnt > 1)
  307               backup(backSlashCnt-1);
  308   
  309             return '\\';
  310           }
  311   
  312           UpdateLineColumn(c);
  313           backSlashCnt++;
  314         }
  315   
  316         // Here, we have seen an odd number of backslash's followed by a 'u'
  317         try
  318         {
  319           while ((c = ReadByte()) == 'u')
  320             ++column;
  321   
  322           buffer[bufpos] = c = (char)(hexval(c) << 12 |
  323                                       hexval(ReadByte()) << 8 |
  324                                       hexval(ReadByte()) << 4 |
  325                                       hexval(ReadByte()));
  326   
  327           column += 4;
  328         }
  329         catch(java.io.IOException e)
  330         {
  331           throw new Error("Invalid escape character at line " + line +
  332                                            " column " + column + ".");
  333         }
  334   
  335         if (backSlashCnt == 1)
  336           return c;
  337         else
  338         {
  339           backup(backSlashCnt - 1);
  340           return '\\';
  341         }
  342       }
  343       else
  344       {
  345         UpdateLineColumn(c);
  346         return c;
  347       }
  348     }
  349   
  350     @Deprecated
  351     /**
  352      * @deprecated
  353      * @see #getEndColumn
  354      */
  355     public int getColumn() {
  356       return bufcolumn[bufpos];
  357     }
  358   
  359     @Deprecated
  360     /**
  361      * @deprecated
  362      * @see #getEndLine
  363      */
  364     public int getLine() {
  365       return bufline[bufpos];
  366     }
  367   
  368   /** Get end column. */
  369     public int getEndColumn() {
  370       return bufcolumn[bufpos];
  371     }
  372   
  373   /** Get end line. */
  374     public int getEndLine() {
  375       return bufline[bufpos];
  376     }
  377   
  378   /** @return column of token start */
  379     public int getBeginColumn() {
  380       return bufcolumn[tokenBegin];
  381     }
  382   
  383   /** @return line number of token start */
  384     public int getBeginLine() {
  385       return bufline[tokenBegin];
  386     }
  387   
  388   /** Retreat. */
  389     public void backup(int amount) {
  390   
  391       inBuf += amount;
  392       if ((bufpos -= amount) < 0)
  393         bufpos += bufsize;
  394     }
  395   
  396   /** Constructor. */
  397     public JavaCharStream(java.io.Reader dstream,
  398                    int startline, int startcolumn, int buffersize)
  399     {
  400       inputStream = dstream;
  401       line = startline;
  402       column = startcolumn - 1;
  403   
  404       available = bufsize = buffersize;
  405       buffer = new char[buffersize];
  406       bufline = new int[buffersize];
  407       bufcolumn = new int[buffersize];
  408       nextCharBuf = new char[4096];
  409     }
  410   
  411   /** Constructor. */
  412     public JavaCharStream(java.io.Reader dstream,
  413                                           int startline, int startcolumn)
  414     {
  415       this(dstream, startline, startcolumn, 4096);
  416     }
  417   
  418   /** Constructor. */
  419     public JavaCharStream(java.io.Reader dstream)
  420     {
  421       this(dstream, 1, 1, 4096);
  422     }
  423   /** Reinitialise. */
  424     public void ReInit(java.io.Reader dstream,
  425                    int startline, int startcolumn, int buffersize)
  426     {
  427       inputStream = dstream;
  428       line = startline;
  429       column = startcolumn - 1;
  430   
  431       if (buffer == null || buffersize != buffer.length)
  432       {
  433         available = bufsize = buffersize;
  434         buffer = new char[buffersize];
  435         bufline = new int[buffersize];
  436         bufcolumn = new int[buffersize];
  437         nextCharBuf = new char[4096];
  438       }
  439       prevCharIsLF = prevCharIsCR = false;
  440       tokenBegin = inBuf = maxNextCharInd = 0;
  441       nextCharInd = bufpos = -1;
  442     }
  443   
  444   /** Reinitialise. */
  445     public void ReInit(java.io.Reader dstream,
  446                                           int startline, int startcolumn)
  447     {
  448       ReInit(dstream, startline, startcolumn, 4096);
  449     }
  450   
  451   /** Reinitialise. */
  452     public void ReInit(java.io.Reader dstream)
  453     {
  454       ReInit(dstream, 1, 1, 4096);
  455     }
  456   /** Constructor. */
  457     public JavaCharStream(java.io.InputStream dstream, String encoding, int startline,
  458     int startcolumn, int buffersize) throws java.io.UnsupportedEncodingException
  459     {
  460       this(encoding == null ? new java.io.InputStreamReader(dstream) : new java.io.InputStreamReader(dstream, encoding), startline, startcolumn, buffersize);
  461     }
  462   
  463   /** Constructor. */
  464     public JavaCharStream(java.io.InputStream dstream, int startline,
  465     int startcolumn, int buffersize)
  466     {
  467       this(new java.io.InputStreamReader(dstream), startline, startcolumn, 4096);
  468     }
  469   
  470   /** Constructor. */
  471     public JavaCharStream(java.io.InputStream dstream, String encoding, int startline,
  472                           int startcolumn) throws java.io.UnsupportedEncodingException
  473     {
  474       this(dstream, encoding, startline, startcolumn, 4096);
  475     }
  476   
  477   /** Constructor. */
  478     public JavaCharStream(java.io.InputStream dstream, int startline,
  479                           int startcolumn)
  480     {
  481       this(dstream, startline, startcolumn, 4096);
  482     }
  483   
  484   /** Constructor. */
  485     public JavaCharStream(java.io.InputStream dstream, String encoding) throws java.io.UnsupportedEncodingException
  486     {
  487       this(dstream, encoding, 1, 1, 4096);
  488     }
  489   
  490   /** Constructor. */
  491     public JavaCharStream(java.io.InputStream dstream)
  492     {
  493       this(dstream, 1, 1, 4096);
  494     }
  495   
  496   /** Reinitialise. */
  497     public void ReInit(java.io.InputStream dstream, String encoding, int startline,
  498     int startcolumn, int buffersize) throws java.io.UnsupportedEncodingException
  499     {
  500       ReInit(encoding == null ? new java.io.InputStreamReader(dstream) : new java.io.InputStreamReader(dstream, encoding), startline, startcolumn, buffersize);
  501     }
  502   
  503   /** Reinitialise. */
  504     public void ReInit(java.io.InputStream dstream, int startline,
  505     int startcolumn, int buffersize)
  506     {
  507       ReInit(new java.io.InputStreamReader(dstream), startline, startcolumn, buffersize);
  508     }
  509   /** Reinitialise. */
  510     public void ReInit(java.io.InputStream dstream, String encoding, int startline,
  511                        int startcolumn) throws java.io.UnsupportedEncodingException
  512     {
  513       ReInit(dstream, encoding, startline, startcolumn, 4096);
  514     }
  515   /** Reinitialise. */
  516     public void ReInit(java.io.InputStream dstream, int startline,
  517                        int startcolumn)
  518     {
  519       ReInit(dstream, startline, startcolumn, 4096);
  520     }
  521   /** Reinitialise. */
  522     public void ReInit(java.io.InputStream dstream, String encoding) throws java.io.UnsupportedEncodingException
  523     {
  524       ReInit(dstream, encoding, 1, 1, 4096);
  525     }
  526   
  527   /** Reinitialise. */
  528     public void ReInit(java.io.InputStream dstream)
  529     {
  530       ReInit(dstream, 1, 1, 4096);
  531     }
  532   
  533     /** @return token image as String */
  534     public String GetImage()
  535     {
  536       if (bufpos >= tokenBegin)
  537         return new String(buffer, tokenBegin, bufpos - tokenBegin + 1);
  538       else
  539         return new String(buffer, tokenBegin, bufsize - tokenBegin) +
  540                                 new String(buffer, 0, bufpos + 1);
  541     }
  542   
  543     /** @return suffix */
  544     public char[] GetSuffix(int len)
  545     {
  546       char[] ret = new char[len];
  547   
  548       if ((bufpos + 1) >= len)
  549         System.arraycopy(buffer, bufpos - len + 1, ret, 0, len);
  550       else
  551       {
  552         System.arraycopy(buffer, bufsize - (len - bufpos - 1), ret, 0,
  553                                                           len - bufpos - 1);
  554         System.arraycopy(buffer, 0, ret, len - bufpos - 1, bufpos + 1);
  555       }
  556   
  557       return ret;
  558     }
  559   
  560     /** Set buffers back to null when finished. */
  561     public void Done()
  562     {
  563       nextCharBuf = null;
  564       buffer = null;
  565       bufline = null;
  566       bufcolumn = null;
  567     }
  568   
  569     /**
  570      * Method to adjust line and column numbers for the start of a token.
  571      */
  572     public void adjustBeginLineColumn(int newLine, int newCol)
  573     {
  574       int start = tokenBegin;
  575       int len;
  576   
  577       if (bufpos >= tokenBegin)
  578       {
  579         len = bufpos - tokenBegin + inBuf + 1;
  580       }
  581       else
  582       {
  583         len = bufsize - tokenBegin + bufpos + 1 + inBuf;
  584       }
  585   
  586       int i = 0, j = 0, k = 0;
  587       int nextColDiff = 0, columnDiff = 0;
  588   
  589       while (i < len && bufline[j = start % bufsize] == bufline[k = ++start % bufsize])
  590       {
  591         bufline[j] = newLine;
  592         nextColDiff = columnDiff + bufcolumn[k] - bufcolumn[j];
  593         bufcolumn[j] = newCol + columnDiff;
  594         columnDiff = nextColDiff;
  595         i++;
  596       }
  597   
  598       if (i < len)
  599       {
  600         bufline[j] = newLine++;
  601         bufcolumn[j] = newCol + columnDiff;
  602   
  603         while (i++ < len)
  604         {
  605           if (bufline[j = start % bufsize] != bufline[++start % bufsize])
  606             bufline[j] = newLine++;
  607           else
  608             bufline[j] = newLine;
  609         }
  610       }
  611   
  612       line = bufline[j];
  613       column = bufcolumn[j];
  614     }
  615   
  616   }
  617   /* JavaCC - OriginalChecksum=d665eff1df49d9f82f07f7dc863fcd22 (do not edit this line) */

Save This Page
Home » lucene-3.0.1-src » org.apache.lucene.queryParser.standard.parser » [javadoc | source]