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