001    /* Generated By:JavaCC: Do not edit this line. ParseException.java Version 3.0 */
002    /**
003     *
004     * Licensed to the Apache Software Foundation (ASF) under one or more
005     * contributor license agreements.  See the NOTICE file distributed with
006     * this work for additional information regarding copyright ownership.
007     * The ASF licenses this file to You under the Apache License, Version 2.0
008     * (the "License"); you may not use this file except in compliance with
009     * the License.  You may obtain a copy of the License at
010     *
011     * http://www.apache.org/licenses/LICENSE-2.0
012     *
013     * Unless required by applicable law or agreed to in writing, software
014     * distributed under the License is distributed on an "AS IS" BASIS,
015     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016     * See the License for the specific language governing permissions and
017     * limitations under the License.
018     */
019    
020    package org.granite.gravity.selector;
021    
022    /**
023     * This exception is thrown when parse errors are encountered.
024     * You can explicitly create objects of this exception type by
025     * calling the method generateParseException in the generated
026     * parser.
027     *
028     * You can modify this class to customize your error reporting
029     * mechanisms so long as you retain the public fields.
030     */
031    public class ParseException extends Exception {
032    
033        private static final long serialVersionUID = 1L;
034    
035    /**
036       * This constructor is used by the method "generateParseException"
037       * in the generated parser.  Calling this constructor generates
038       * a new object of this type with the fields "currentToken",
039       * "expectedTokenSequences", and "tokenImage" set.  The boolean
040       * flag "specialConstructor" is also set to true to indicate that
041       * this constructor was used to create this object.
042       * This constructor calls its super class with the empty string
043       * to force the "toString" method of parent class "Throwable" to
044       * print the error message in the form:
045       *     ParseException: <result of getMessage>
046       */
047      public ParseException(Token currentTokenVal,
048                            int[][] expectedTokenSequencesVal,
049                            String[] tokenImageVal
050                           )
051      {
052        super("");
053        specialConstructor = true;
054        currentToken = currentTokenVal;
055        expectedTokenSequences = expectedTokenSequencesVal;
056        tokenImage = tokenImageVal;
057      }
058    
059      /**
060       * The following constructors are for use by you for whatever
061       * purpose you can think of.  Constructing the exception in this
062       * manner makes the exception behave in the normal way - i.e., as
063       * documented in the class "Throwable".  The fields "errorToken",
064       * "expectedTokenSequences", and "tokenImage" do not contain
065       * relevant information.  The JavaCC generated code does not use
066       * these constructors.
067       */
068    
069      public ParseException() {
070        super();
071        specialConstructor = false;
072      }
073    
074      public ParseException(String message) {
075        super(message);
076        specialConstructor = false;
077      }
078    
079      /**
080       * This variable determines which constructor was used to create
081       * this object and thereby affects the semantics of the
082       * "getMessage" method (see below).
083       */
084      protected boolean specialConstructor;
085    
086      /**
087       * This is the last token that has been consumed successfully.  If
088       * this object has been created due to a parse error, the token
089       * followng this token will (therefore) be the first error token.
090       */
091      public Token currentToken;
092    
093      /**
094       * Each entry in this array is an array of integers.  Each array
095       * of integers represents a sequence of tokens (by their ordinal
096       * values) that is expected at this point of the parse.
097       */
098      public int[][] expectedTokenSequences;
099    
100      /**
101       * This is a reference to the "tokenImage" array of the generated
102       * parser within which the parse error occurred.  This array is
103       * defined in the generated ...Constants interface.
104       */
105      public String[] tokenImage;
106    
107      /**
108       * This method has the standard behavior when this object has been
109       * created using the standard constructors.  Otherwise, it uses
110       * "currentToken" and "expectedTokenSequences" to generate a parse
111       * error message and returns it.  If this object has been created
112       * due to a parse error, and you do not catch it (it gets thrown
113       * from the parser), then this method is called during the printing
114       * of the final stack trace, and hence the correct error message
115       * gets displayed.
116       */
117      @Override
118      public String getMessage() {
119        if (!specialConstructor) {
120          return super.getMessage();
121        }
122        StringBuffer expected = new StringBuffer();
123        int maxSize = 0;
124        for (int i = 0; i < expectedTokenSequences.length; i++) {
125          if (maxSize < expectedTokenSequences[i].length) {
126            maxSize = expectedTokenSequences[i].length;
127          }
128          for (int j = 0; j < expectedTokenSequences[i].length; j++) {
129            expected.append(tokenImage[expectedTokenSequences[i][j]]).append(" ");
130          }
131          if (expectedTokenSequences[i][expectedTokenSequences[i].length - 1] != 0) {
132            expected.append("...");
133          }
134          expected.append(eol).append("    ");
135        }
136        String retval = "Encountered \"";
137        Token tok = currentToken.next;
138        for (int i = 0; i < maxSize; i++) {
139          if (i != 0) retval += " ";
140          if (tok.kind == 0) {
141            retval += tokenImage[0];
142            break;
143          }
144          retval += add_escapes(tok.image);
145          tok = tok.next;
146        }
147        retval += "\" at line " + currentToken.next.beginLine + ", column " + currentToken.next.beginColumn;
148        retval += "." + eol;
149        if (expectedTokenSequences.length == 1) {
150          retval += "Was expecting:" + eol + "    ";
151        } else {
152          retval += "Was expecting one of:" + eol + "    ";
153        }
154        retval += expected.toString();
155        return retval;
156      }
157    
158      /**
159       * The end of line string for this machine.
160       */
161      protected String eol = System.getProperty("line.separator", "\n");
162    
163      /**
164       * Used to convert raw characters to their escaped version
165       * when these raw version cannot be used as part of an ASCII
166       * string literal.
167       */
168      protected String add_escapes(String str) {
169          StringBuffer retval = new StringBuffer();
170          char ch;
171          for (int i = 0; i < str.length(); i++) {
172            switch (str.charAt(i))
173            {
174               case 0 :
175                  continue;
176               case '\b':
177                  retval.append("\\b");
178                  continue;
179               case '\t':
180                  retval.append("\\t");
181                  continue;
182               case '\n':
183                  retval.append("\\n");
184                  continue;
185               case '\f':
186                  retval.append("\\f");
187                  continue;
188               case '\r':
189                  retval.append("\\r");
190                  continue;
191               case '\"':
192                  retval.append("\\\"");
193                  continue;
194               case '\'':
195                  retval.append("\\\'");
196                  continue;
197               case '\\':
198                  retval.append("\\\\");
199                  continue;
200               default:
201                  if ((ch = str.charAt(i)) < 0x20 || ch > 0x7e) {
202                     String s = "0000" + Integer.toString(ch, 16);
203                     retval.append("\\u" + s.substring(s.length() - 4, s.length()));
204                  } else {
205                     retval.append(ch);
206                  }
207                  continue;
208            }
209          }
210          return retval.toString();
211       }
212    
213    }