KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > aspectwerkz > expression > ast > ParseException


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc.aspectwerkz.expression.ast;
5
6 /**
7  * This exception is thrown when parse errors are encountered. You can explicitly create objects of this exception type
8  * by calling the method generateParseException in the generated parser. You can modify this class to customize your
9  * error reporting mechanisms so long as you retain the public fields.
10  */

11 public class ParseException extends Exception JavaDoc {
12
13   /**
14    * This constructor is used by the method "generateParseException" in the generated parser. Calling this constructor
15    * generates a new object of this type with the fields "currentToken", "expectedTokenSequences", and "tokenImage"
16    * set. The boolean flag "specialConstructor" is also set to true to indicate that this constructor was used to
17    * create this object. This constructor calls its super class with the empty string to force the "toString" method
18    * of parent class "Throwable" to print the error message in the form: ParseException: <result of getMessage>
19    */

20   public ParseException(Token currentTokenVal, int[][] expectedTokenSequencesVal, String JavaDoc[] tokenImageVal) {
21     super("");
22     specialConstructor = true;
23     currentToken = currentTokenVal;
24     expectedTokenSequences = expectedTokenSequencesVal;
25     tokenImage = tokenImageVal;
26   }
27
28   /**
29    * The following constructors are for use by you for whatever purpose you can think of. Constructing the exception
30    * in this manner makes the exception behave in the normal way - i.e., as documented in the class "Throwable". The
31    * fields "errorToken", "expectedTokenSequences", and "tokenImage" do not contain relevant information. The JavaCC
32    * generated code does not use these constructors.
33    */

34
35   public ParseException() {
36     super();
37     specialConstructor = false;
38   }
39
40   public ParseException(String JavaDoc message) {
41     super(message);
42     specialConstructor = false;
43   }
44
45   /**
46    * This variable determines which constructor was used to create this object and thereby affects the semantics of
47    * the "getMessage" method (see below).
48    */

49   protected boolean specialConstructor;
50
51   /**
52    * This is the last token that has been consumed successfully. If this object has been created due to a parse error,
53    * the token followng this token will (therefore) be the first error token.
54    */

55   public Token currentToken;
56
57   /**
58    * Each entry in this array is an array of integers. Each array of integers represents a sequence of tokens (by
59    * their ordinal values) that is expected at this point of the parse.
60    */

61   public int[][] expectedTokenSequences;
62
63   /**
64    * This is a reference to the "tokenImage" array of the generated parser within which the parse error occurred. This
65    * array is defined in the generated ...Constants interface.
66    */

67   public String JavaDoc[] tokenImage;
68
69   /**
70    * This method has the standard behavior when this object has been created using the standard constructors.
71    * Otherwise, it uses "currentToken" and "expectedTokenSequences" to generate a parse error message and returns it.
72    * If this object has been created due to a parse error, and you do not catch it (it gets thrown from the parser),
73    * then this method is called during the printing of the final stack trace, and hence the correct error message gets
74    * displayed.
75    */

76   public String JavaDoc getMessage() {
77     if (!specialConstructor) {
78       return super.getMessage();
79     }
80     String JavaDoc expected = "";
81     int maxSize = 0;
82     for (int i = 0; i < expectedTokenSequences.length; i++) {
83       if (maxSize < expectedTokenSequences[i].length) {
84         maxSize = expectedTokenSequences[i].length;
85       }
86       for (int j = 0; j < expectedTokenSequences[i].length; j++) {
87         expected += tokenImage[expectedTokenSequences[i][j]] + " ";
88       }
89       if (expectedTokenSequences[i][expectedTokenSequences[i].length - 1] != 0) {
90         expected += "...";
91       }
92       expected += eol + " ";
93     }
94     String JavaDoc retval = "Encountered \"";
95     Token tok = currentToken.next;
96     for (int i = 0; i < maxSize; i++) {
97       if (i != 0) {
98         retval += " ";
99       }
100       if (tok.kind == 0) {
101         retval += tokenImage[0];
102         break;
103       }
104       retval += add_escapes(tok.image);
105       tok = tok.next;
106     }
107     retval += "\" at line " + currentToken.next.beginLine + ", column " + currentToken.next.beginColumn;
108     retval += "." + eol;
109     if (expectedTokenSequences.length == 1) {
110       retval += "Was expecting:" + eol + " ";
111     } else {
112       retval += "Was expecting one of:" + eol + " ";
113     }
114     retval += expected;
115     return retval;
116   }
117
118   /**
119    * The end of line string for this machine.
120    */

121   protected String JavaDoc eol = System.getProperty("line.separator", "\n");
122
123   /**
124    * Used to convert raw characters to their escaped version when these raw version cannot be used as part of an ASCII
125    * string literal.
126    */

127   protected String JavaDoc add_escapes(String JavaDoc str) {
128     StringBuffer JavaDoc retval = new StringBuffer JavaDoc();
129     char ch;
130     for (int i = 0; i < str.length(); i++) {
131       switch (str.charAt(i)) {
132         case 0:
133           continue;
134         case '\b':
135           retval.append("\\b");
136           continue;
137         case '\t':
138           retval.append("\\t");
139           continue;
140         case '\n':
141           retval.append("\\n");
142           continue;
143         case '\f':
144           retval.append("\\f");
145           continue;
146         case '\r':
147           retval.append("\\r");
148           continue;
149         case '\"':
150           retval.append("\\\"");
151           continue;
152         case '\'':
153           retval.append("\\\'");
154           continue;
155         case '\\':
156           retval.append("\\\\");
157           continue;
158         default:
159           if ((ch = str.charAt(i)) < 0x20 || ch > 0x7e) {
160             String JavaDoc s = "0000" + Integer.toString(ch, 16);
161             retval.append("\\u" + s.substring(s.length() - 4, s.length()));
162           } else {
163             retval.append(ch);
164           }
165           continue;
166       }
167     }
168     return retval.toString();
169   }
170
171 }
Popular Tags