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. ParseException.java Version 3.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
041package org.granite.gravity.selector;
042
043/**
044 * This exception is thrown when parse errors are encountered.
045 * You can explicitly create objects of this exception type by
046 * calling the method generateParseException in the generated
047 * parser.
048 *
049 * You can modify this class to customize your error reporting
050 * mechanisms so long as you retain the public fields.
051 */
052public class ParseException extends Exception {
053
054    private static final long serialVersionUID = 1L;
055
056/**
057   * This constructor is used by the method "generateParseException"
058   * in the generated parser.  Calling this constructor generates
059   * a new object of this type with the fields "currentToken",
060   * "expectedTokenSequences", and "tokenImage" set.  The boolean
061   * flag "specialConstructor" is also set to true to indicate that
062   * this constructor was used to create this object.
063   * This constructor calls its super class with the empty string
064   * to force the "toString" method of parent class "Throwable" to
065   * print the error message in the form:
066   *     ParseException: <result of getMessage>
067   */
068  public ParseException(Token currentTokenVal,
069                        int[][] expectedTokenSequencesVal,
070                        String[] tokenImageVal
071                       )
072  {
073    super("");
074    specialConstructor = true;
075    currentToken = currentTokenVal;
076    expectedTokenSequences = expectedTokenSequencesVal;
077    tokenImage = tokenImageVal;
078  }
079
080  /**
081   * The following constructors are for use by you for whatever
082   * purpose you can think of.  Constructing the exception in this
083   * manner makes the exception behave in the normal way - i.e., as
084   * documented in the class "Throwable".  The fields "errorToken",
085   * "expectedTokenSequences", and "tokenImage" do not contain
086   * relevant information.  The JavaCC generated code does not use
087   * these constructors.
088   */
089
090  public ParseException() {
091    super();
092    specialConstructor = false;
093  }
094
095  public ParseException(String message) {
096    super(message);
097    specialConstructor = false;
098  }
099
100  /**
101   * This variable determines which constructor was used to create
102   * this object and thereby affects the semantics of the
103   * "getMessage" method (see below).
104   */
105  protected boolean specialConstructor;
106
107  /**
108   * This is the last token that has been consumed successfully.  If
109   * this object has been created due to a parse error, the token
110   * followng this token will (therefore) be the first error token.
111   */
112  public Token currentToken;
113
114  /**
115   * Each entry in this array is an array of integers.  Each array
116   * of integers represents a sequence of tokens (by their ordinal
117   * values) that is expected at this point of the parse.
118   */
119  public int[][] expectedTokenSequences;
120
121  /**
122   * This is a reference to the "tokenImage" array of the generated
123   * parser within which the parse error occurred.  This array is
124   * defined in the generated ...Constants interface.
125   */
126  public String[] tokenImage;
127
128  /**
129   * This method has the standard behavior when this object has been
130   * created using the standard constructors.  Otherwise, it uses
131   * "currentToken" and "expectedTokenSequences" to generate a parse
132   * error message and returns it.  If this object has been created
133   * due to a parse error, and you do not catch it (it gets thrown
134   * from the parser), then this method is called during the printing
135   * of the final stack trace, and hence the correct error message
136   * gets displayed.
137   */
138  @Override
139  public String getMessage() {
140    if (!specialConstructor) {
141      return super.getMessage();
142    }
143    StringBuffer expected = new StringBuffer();
144    int maxSize = 0;
145    for (int i = 0; i < expectedTokenSequences.length; i++) {
146      if (maxSize < expectedTokenSequences[i].length) {
147        maxSize = expectedTokenSequences[i].length;
148      }
149      for (int j = 0; j < expectedTokenSequences[i].length; j++) {
150        expected.append(tokenImage[expectedTokenSequences[i][j]]).append(" ");
151      }
152      if (expectedTokenSequences[i][expectedTokenSequences[i].length - 1] != 0) {
153        expected.append("...");
154      }
155      expected.append(eol).append("    ");
156    }
157    String retval = "Encountered \"";
158    Token tok = currentToken.next;
159    for (int i = 0; i < maxSize; i++) {
160      if (i != 0) retval += " ";
161      if (tok.kind == 0) {
162        retval += tokenImage[0];
163        break;
164      }
165      retval += add_escapes(tok.image);
166      tok = tok.next;
167    }
168    retval += "\" at line " + currentToken.next.beginLine + ", column " + currentToken.next.beginColumn;
169    retval += "." + eol;
170    if (expectedTokenSequences.length == 1) {
171      retval += "Was expecting:" + eol + "    ";
172    } else {
173      retval += "Was expecting one of:" + eol + "    ";
174    }
175    retval += expected.toString();
176    return retval;
177  }
178
179  /**
180   * The end of line string for this machine.
181   */
182  protected String eol = System.getProperty("line.separator", "\n");
183
184  /**
185   * Used to convert raw characters to their escaped version
186   * when these raw version cannot be used as part of an ASCII
187   * string literal.
188   */
189  protected String add_escapes(String str) {
190      StringBuffer retval = new StringBuffer();
191      char ch;
192      for (int i = 0; i < str.length(); i++) {
193        switch (str.charAt(i))
194        {
195           case 0 :
196              continue;
197           case '\b':
198              retval.append("\\b");
199              continue;
200           case '\t':
201              retval.append("\\t");
202              continue;
203           case '\n':
204              retval.append("\\n");
205              continue;
206           case '\f':
207              retval.append("\\f");
208              continue;
209           case '\r':
210              retval.append("\\r");
211              continue;
212           case '\"':
213              retval.append("\\\"");
214              continue;
215           case '\'':
216              retval.append("\\\'");
217              continue;
218           case '\\':
219              retval.append("\\\\");
220              continue;
221           default:
222              if ((ch = str.charAt(i)) < 0x20 || ch > 0x7e) {
223                 String s = "0000" + Integer.toString(ch, 16);
224                 retval.append("\\u" + s.substring(s.length() - 4, s.length()));
225              } else {
226                 retval.append(ch);
227              }
228              continue;
229        }
230      }
231      return retval.toString();
232   }
233
234}