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