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. ParseException.java Version 4.1 */
    2   /* JavaCCOptions:KEEP_LINE_COL=null */
    3   package org.apache.lucene.queryParser.standard.parser;
    4   
    5   /**
    6    * Licensed to the Apache Software Foundation (ASF) under one or more
    7    * contributor license agreements.  See the NOTICE file distributed with
    8    * this work for additional information regarding copyright ownership.
    9    * The ASF licenses this file to You under the Apache License, Version 2.0
   10    * (the "License"); you may not use this file except in compliance with
   11    * the License.  You may obtain a copy of the License at
   12    *
   13    *     http://www.apache.org/licenses/LICENSE-2.0
   14    *
   15    * Unless required by applicable law or agreed to in writing, software
   16    * distributed under the License is distributed on an "AS IS" BASIS,
   17    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   18    * See the License for the specific language governing permissions and
   19    * limitations under the License.
   20    */
   21   import org.apache.lucene.messages.Message;
   22   import org.apache.lucene.messages.MessageImpl;
   23   import org.apache.lucene.queryParser.core.QueryNodeParseException;
   24   import org.apache.lucene.queryParser.core.messages.QueryParserMessages;
   25   
   26   /**
   27    * This exception is thrown when parse errors are encountered. You can
   28    * explicitly create objects of this exception type by calling the method
   29    * generateParseException in the generated parser.
   30    * 
   31    * You can modify this class to customize your error reporting mechanisms so
   32    * long as you retain the public fields.
   33    */
   34   public class ParseException extends QueryNodeParseException {
   35   
   36     /**
   37      * The version identifier for this Serializable class. Increment only if the
   38      * <i>serialized</i> form of the class changes.
   39      */
   40     private static final long serialVersionUID = 1L;
   41   
   42     /**
   43      * This constructor is used by the method "generateParseException" in the
   44      * generated parser. Calling this constructor generates a new object of this
   45      * type with the fields "currentToken", "expectedTokenSequences", and
   46      * "tokenImage" set.
   47      */
   48     public ParseException(Token currentTokenVal,
   49         int[][] expectedTokenSequencesVal, String[] tokenImageVal) {
   50       super(new MessageImpl(QueryParserMessages.INVALID_SYNTAX, initialise(
   51           currentTokenVal, expectedTokenSequencesVal, tokenImageVal)));
   52       this.currentToken = currentTokenVal;
   53       this.expectedTokenSequences = expectedTokenSequencesVal;
   54       this.tokenImage = tokenImageVal;
   55     }
   56   
   57     public ParseException(Message message) {
   58       super(message);
   59     }
   60   
   61     public ParseException() {
   62       super(new MessageImpl(QueryParserMessages.INVALID_SYNTAX, "Error"));
   63     }
   64   
   65     /**
   66      * This is the last token that has been consumed successfully. If this object
   67      * has been created due to a parse error, the token followng this token will
   68      * (therefore) be the first error token.
   69      */
   70     @SuppressWarnings("unused")
   71     private Token currentToken;
   72   
   73     /**
   74      * Each entry in this array is an array of integers. Each array of integers
   75      * represents a sequence of tokens (by their ordinal values) that is expected
   76      * at this point of the parse.
   77      */
   78     @SuppressWarnings("unused")
   79     private int[][] expectedTokenSequences;
   80   
   81     /**
   82      * This is a reference to the "tokenImage" array of the generated parser
   83      * within which the parse error occurred. This array is defined in the
   84      * generated ...Constants interface.
   85      */
   86     @SuppressWarnings("unused")
   87     private String[] tokenImage;
   88   
   89     /**
   90      * It uses "currentToken" and "expectedTokenSequences" to generate a parse
   91      * error message and returns it. If this object has been created due to a
   92      * parse error, and you do not catch it (it gets thrown from the parser) the
   93      * correct error message gets displayed.
   94      */
   95     private static String initialise(Token currentToken,
   96         int[][] expectedTokenSequences, String[] tokenImage) {
   97       String eol = System.getProperty("line.separator", "\n");
   98       StringBuffer expected = new StringBuffer();
   99       int maxSize = 0;
  100       for (int i = 0; i < expectedTokenSequences.length; i++) {
  101         if (maxSize < expectedTokenSequences[i].length) {
  102           maxSize = expectedTokenSequences[i].length;
  103         }
  104         for (int j = 0; j < expectedTokenSequences[i].length; j++) {
  105           expected.append(tokenImage[expectedTokenSequences[i][j]]).append(' ');
  106         }
  107         if (expectedTokenSequences[i][expectedTokenSequences[i].length - 1] != 0) {
  108           expected.append("...");
  109         }
  110         expected.append(eol).append("    ");
  111       }
  112       String retval = "Encountered \"";
  113       Token tok = currentToken.next;
  114       for (int i = 0; i < maxSize; i++) {
  115         if (i != 0)
  116           retval += " ";
  117         if (tok.kind == 0) {
  118           retval += tokenImage[0];
  119           break;
  120         }
  121         retval += " " + tokenImage[tok.kind];
  122         retval += " \"";
  123         retval += add_escapes(tok.image);
  124         retval += " \"";
  125         tok = tok.next;
  126       }
  127       retval += "\" at line " + currentToken.next.beginLine + ", column "
  128           + currentToken.next.beginColumn;
  129       retval += "." + eol;
  130       if (expectedTokenSequences.length == 1) {
  131         retval += "Was expecting:" + eol + "    ";
  132       } else {
  133         retval += "Was expecting one of:" + eol + "    ";
  134       }
  135       retval += expected.toString();
  136       return retval;
  137     }
  138   
  139     /**
  140      * The end of line string for this machine.
  141      */
  142     @SuppressWarnings("unused")
  143     private String eol = System.getProperty("line.separator", "\n");
  144   
  145     /**
  146      * Used to convert raw characters to their escaped version when these raw
  147      * version cannot be used as part of an ASCII string literal.
  148      */
  149     static private String add_escapes(String str) {
  150       StringBuffer retval = new StringBuffer();
  151       char ch;
  152       for (int i = 0; i < str.length(); i++) {
  153         switch (str.charAt(i)) {
  154         case 0:
  155           continue;
  156         case '\b':
  157           retval.append("\\b");
  158           continue;
  159         case '\t':
  160           retval.append("\\t");
  161           continue;
  162         case '\n':
  163           retval.append("\\n");
  164           continue;
  165         case '\f':
  166           retval.append("\\f");
  167           continue;
  168         case '\r':
  169           retval.append("\\r");
  170           continue;
  171         case '\"':
  172           retval.append("\\\"");
  173           continue;
  174         case '\'':
  175           retval.append("\\\'");
  176           continue;
  177         case '\\':
  178           retval.append("\\\\");
  179           continue;
  180         default:
  181           if ((ch = str.charAt(i)) < 0x20 || ch > 0x7e) {
  182             String s = "0000" + Integer.toString(ch, 16);
  183             retval.append("\\u" + s.substring(s.length() - 4, s.length()));
  184           } else {
  185             retval.append(ch);
  186           }
  187           continue;
  188         }
  189       }
  190       return retval.toString();
  191     }
  192   
  193   }
  194   /*
  195    * JavaCC - StandardChecksum=c04ac45b94787832e67e6d1b49d8774c (do not edit this
  196    * line)
  197    */

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