View Javadoc
1   /*
2    * Copyright (c) 1999, 2011, Oracle and/or its affiliates. All rights reserved.
3    * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4    *
5    * This code is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU General Public License version 2 only, as
7    * published by the Free Software Foundation.  Oracle designates this
8    * particular file as subject to the "Classpath" exception as provided
9    * by Oracle in the LICENSE file that accompanied this code.
10   *
11   * This code is distributed in the hope that it will be useful, but WITHOUT
12   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13   * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14   * version 2 for more details (a copy is included in the LICENSE file that
15   * accompanied this code).
16   *
17   * You should have received a copy of the GNU General Public License version
18   * 2 along with this work; if not, write to the Free Software Foundation,
19   * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20   *
21   * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22   * or visit www.oracle.com if you need additional information or have any
23   * questions.
24   */
25  
26  /*
27   * This source code is provided to illustrate the usage of a given feature
28   * or technique and has been deliberately simplified. Additional steps
29   * required for a production-quality application, such as security checks,
30   * input validation and proper error handling, might not be present in
31   * this sample code.
32   */
33  
34  
35  /* Generated By:JavaCC: Do not edit this line. ParseException.java Version 0.7pre6 */
36  package com.sun.tools.example.debug.expr;
37  
38  /**
39   * This exception is thrown when parse errors are encountered.
40   * You can explicitly create objects of this exception type by
41   * calling the method generateParseException in the generated
42   * parser.
43   *
44   * You can modify this class to customize your error reporting
45   * mechanisms so long as you retain the public fields.
46   */
47  public class ParseException extends Exception {
48  
49    private static final long serialVersionUID = 7978489144303647901L;
50  
51    /**
52     * This constructor is used by the method "generateParseException"
53     * in the generated parser.  Calling this constructor generates
54     * a new object of this type with the fields "currentToken",
55     * "expectedTokenSequences", and "tokenImage" set.  The boolean
56     * flag "specialConstructor" is also set to true to indicate that
57     * this constructor was used to create this object.
58     * This constructor calls its super class with the empty string
59     * to force the "toString" method of parent class "Throwable" to
60     * print the error message in the form:
61     *     ParseException: <result of getMessage>
62     */
63    public ParseException(Token currentTokenVal,
64                          int[][] expectedTokenSequencesVal,
65                          String[] tokenImageVal
66                         )
67    {
68      super("");
69      specialConstructor = true;
70      currentToken = currentTokenVal;
71      expectedTokenSequences = expectedTokenSequencesVal;
72      tokenImage = tokenImageVal;
73    }
74  
75    /**
76     * The following constructors are for use by you for whatever
77     * purpose you can think of.  Constructing the exception in this
78     * manner makes the exception behave in the normal way - i.e., as
79     * documented in the class "Throwable".  The fields "errorToken",
80     * "expectedTokenSequences", and "tokenImage" do not contain
81     * relevant information.  The JavaCC generated code does not use
82     * these constructors.
83     */
84  
85    public ParseException() {
86      super();
87      specialConstructor = false;
88    }
89  
90    public ParseException(String message) {
91      super(message);
92      specialConstructor = false;
93    }
94  
95    /**
96     * This variable determines which constructor was used to create
97     * this object and thereby affects the semantics of the
98     * "getMessage" method (see below).
99     */
100   protected boolean specialConstructor;
101 
102   /**
103    * This is the last token that has been consumed successfully.  If
104    * this object has been created due to a parse error, the token
105    * followng this token will (therefore) be the first error token.
106    */
107   public Token currentToken;
108 
109   /**
110    * Each entry in this array is an array of integers.  Each array
111    * of integers represents a sequence of tokens (by their ordinal
112    * values) that is expected at this point of the parse.
113    */
114   public int[][] expectedTokenSequences;
115 
116   /**
117    * This is a reference to the "tokenImage" array of the generated
118    * parser within which the parse error occurred.  This array is
119    * defined in the generated ...Constants interface.
120    */
121   public String[] tokenImage;
122 
123   /**
124    * This method has the standard behavior when this object has been
125    * created using the standard constructors.  Otherwise, it uses
126    * "currentToken" and "expectedTokenSequences" to generate a parse
127    * error message and returns it.  If this object has been created
128    * due to a parse error, and you do not catch it (it gets thrown
129    * from the parser), then this method is called during the printing
130    * of the final stack trace, and hence the correct error message
131    * gets displayed.
132    */
133   @Override
134   public String getMessage() {
135     if (!specialConstructor) {
136       return super.getMessage();
137     }
138     String expected = "";
139     int maxSize = 0;
140     for (int[] expectedTokenSequence : expectedTokenSequences) {
141       if (maxSize < expectedTokenSequence.length) {
142         maxSize = expectedTokenSequence.length;
143       }
144       for (int j = 0; j < expectedTokenSequence.length; j++) {
145         expected += tokenImage[expectedTokenSequence[j]] + " ";
146       }
147       if (expectedTokenSequence[expectedTokenSequence.length - 1] != 0) {
148         expected += "...";
149       }
150       expected += eol + "    ";
151     }
152     String retval = "Encountered \"";
153     Token tok = currentToken.next;
154     for (int i = 0; i < maxSize; i++) {
155       if (i != 0) {
156          retval += " ";
157       }
158       if (tok.kind == 0) {
159         retval += tokenImage[0];
160         break;
161       }
162       retval += add_escapes(tok.image);
163       tok = tok.next;
164     }
165     retval += "\" at line " + currentToken.next.beginLine + ", column " + currentToken.next.beginColumn + "." + eol;
166     if (expectedTokenSequences.length == 1) {
167       retval += "Was expecting:" + eol + "    ";
168     } else {
169       retval += "Was expecting one of:" + eol + "    ";
170     }
171     retval += expected;
172     return retval;
173   }
174 
175   /**
176    * The end of line string for this machine.
177    */
178   protected String eol = System.getProperty("line.separator", "\n");
179 
180   /**
181    * Used to convert raw characters to their escaped version
182    * when these raw version cannot be used as part of an ASCII
183    * string literal.
184    */
185   protected String add_escapes(String str) {
186       StringBuffer retval = new StringBuffer();
187       char ch;
188       for (int i = 0; i < str.length(); i++) {
189         switch (str.charAt(i))
190         {
191            case 0 :
192               continue;
193            case '\b':
194               retval.append("\\b");
195               continue;
196            case '\t':
197               retval.append("\\t");
198               continue;
199            case '\n':
200               retval.append("\\n");
201               continue;
202            case '\f':
203               retval.append("\\f");
204               continue;
205            case '\r':
206               retval.append("\\r");
207               continue;
208            case '\"':
209               retval.append("\\\"");
210               continue;
211            case '\'':
212               retval.append("\\\'");
213               continue;
214            case '\\':
215               retval.append("\\\\");
216               continue;
217            default:
218               if ((ch = str.charAt(i)) < 0x20 || ch > 0x7e) {
219                  String s = "0000" + Integer.toString(ch, 16);
220                  retval.append("\\u" + s.substring(s.length() - 4, s.length()));
221               } else {
222                  retval.append(ch);
223               }
224               continue;
225         }
226       }
227       return retval.toString();
228    }
229 
230 }