KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > tools > example > debug > expr > ParseException


1 /*
2  * @(#)ParseException.java 1.8 05/11/17
3  *
4  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 /* Generated By:JavaCC: Do not edit this line. ParseException.java Version 0.7pre6 */
9 package com.sun.tools.example.debug.expr;
10
11 /**
12  * This exception is thrown when parse errors are encountered.
13  * You can explicitly create objects of this exception type by
14  * calling the method generateParseException in the generated
15  * parser.
16  *
17  * You can modify this class to customize your error reporting
18  * mechanisms so long as you retain the public fields.
19  */

20 public class ParseException extends Exception JavaDoc {
21
22   /**
23    * This constructor is used by the method "generateParseException"
24    * in the generated parser. Calling this constructor generates
25    * a new object of this type with the fields "currentToken",
26    * "expectedTokenSequences", and "tokenImage" set. The boolean
27    * flag "specialConstructor" is also set to true to indicate that
28    * this constructor was used to create this object.
29    * This constructor calls its super class with the empty string
30    * to force the "toString" method of parent class "Throwable" to
31    * print the error message in the form:
32    * ParseException: <result of getMessage>
33    */

34   public ParseException(Token currentTokenVal,
35                         int[][] expectedTokenSequencesVal,
36                         String JavaDoc[] tokenImageVal
37                        )
38   {
39     super("");
40     specialConstructor = true;
41     currentToken = currentTokenVal;
42     expectedTokenSequences = expectedTokenSequencesVal;
43     tokenImage = tokenImageVal;
44   }
45
46   /**
47    * The following constructors are for use by you for whatever
48    * purpose you can think of. Constructing the exception in this
49    * manner makes the exception behave in the normal way - i.e., as
50    * documented in the class "Throwable". The fields "errorToken",
51    * "expectedTokenSequences", and "tokenImage" do not contain
52    * relevant information. The JavaCC generated code does not use
53    * these constructors.
54    */

55
56   public ParseException() {
57     super();
58     specialConstructor = false;
59   }
60
61   public ParseException(String JavaDoc message) {
62     super(message);
63     specialConstructor = false;
64   }
65
66   /**
67    * This variable determines which constructor was used to create
68    * this object and thereby affects the semantics of the
69    * "getMessage" method (see below).
70    */

71   protected boolean specialConstructor;
72
73   /**
74    * This is the last token that has been consumed successfully. If
75    * this object has been created due to a parse error, the token
76    * followng this token will (therefore) be the first error token.
77    */

78   public Token currentToken;
79
80   /**
81    * Each entry in this array is an array of integers. Each array
82    * of integers represents a sequence of tokens (by their ordinal
83    * values) that is expected at this point of the parse.
84    */

85   public int[][] expectedTokenSequences;
86
87   /**
88    * This is a reference to the "tokenImage" array of the generated
89    * parser within which the parse error occurred. This array is
90    * defined in the generated ...Constants interface.
91    */

92   public String JavaDoc[] tokenImage;
93
94   /**
95    * This method has the standard behavior when this object has been
96    * created using the standard constructors. Otherwise, it uses
97    * "currentToken" and "expectedTokenSequences" to generate a parse
98    * error message and returns it. If this object has been created
99    * due to a parse error, and you do not catch it (it gets thrown
100    * from the parser), then this method is called during the printing
101    * of the final stack trace, and hence the correct error message
102    * gets displayed.
103    */

104   public String JavaDoc getMessage() {
105     if (!specialConstructor) {
106       return super.getMessage();
107     }
108     String JavaDoc expected = "";
109     int maxSize = 0;
110     for (int i = 0; i < expectedTokenSequences.length; i++) {
111       if (maxSize < expectedTokenSequences[i].length) {
112         maxSize = expectedTokenSequences[i].length;
113       }
114       for (int j = 0; j < expectedTokenSequences[i].length; j++) {
115         expected += tokenImage[expectedTokenSequences[i][j]] + " ";
116       }
117       if (expectedTokenSequences[i][expectedTokenSequences[i].length - 1] != 0) {
118         expected += "...";
119       }
120       expected += eol + " ";
121     }
122     String JavaDoc retval = "Encountered \"";
123     Token tok = currentToken.next;
124     for (int i = 0; i < maxSize; i++) {
125       if (i != 0) retval += " ";
126       if (tok.kind == 0) {
127         retval += tokenImage[0];
128         break;
129       }
130       retval += add_escapes(tok.image);
131       tok = tok.next;
132     }
133     retval += "\" at line " + currentToken.next.beginLine + ", column " + currentToken.next.beginColumn + "." + eol;
134     if (expectedTokenSequences.length == 1) {
135       retval += "Was expecting:" + eol + " ";
136     } else {
137       retval += "Was expecting one of:" + eol + " ";
138     }
139     retval += expected;
140     return retval;
141   }
142
143   /**
144    * The end of line string for this machine.
145    */

146   protected String JavaDoc eol = System.getProperty("line.separator", "\n");
147  
148   /**
149    * Used to convert raw characters to their escaped version
150    * when these raw version cannot be used as part of an ASCII
151    * string literal.
152    */

153   protected String JavaDoc add_escapes(String JavaDoc str) {
154       StringBuffer JavaDoc retval = new StringBuffer JavaDoc();
155       char ch;
156       for (int i = 0; i < str.length(); i++) {
157         switch (str.charAt(i))
158         {
159            case 0 :
160               continue;
161            case '\b':
162               retval.append("\\b");
163               continue;
164            case '\t':
165               retval.append("\\t");
166               continue;
167            case '\n':
168               retval.append("\\n");
169               continue;
170            case '\f':
171               retval.append("\\f");
172               continue;
173            case '\r':
174               retval.append("\\r");
175               continue;
176            case '\"':
177               retval.append("\\\"");
178               continue;
179            case '\'':
180               retval.append("\\\'");
181               continue;
182            case '\\':
183               retval.append("\\\\");
184               continue;
185            default:
186               if ((ch = str.charAt(i)) < 0x20 || ch > 0x7e) {
187                  String JavaDoc s = "0000" + Integer.toString(ch, 16);
188                  retval.append("\\u" + s.substring(s.length() - 4, s.length()));
189               } else {
190                  retval.append(ch);
191               }
192               continue;
193         }
194       }
195       return retval.toString();
196    }
197
198 }
199
Popular Tags