1
2 /***************************************************************************************
3 * Copyright (c) Jonas Bonér, Alexandre Vasseur. All rights reserved. *
4 * http://aspectwerkz.codehaus.org *
5 * ---------------------------------------------------------------------------------- *
6 * The software in this package is published under the terms of the LGPL license *
7 * a copy of which has been included with this distribution in the license.txt file. *
8 **************************************************************************************/
9 package org.codehaus.aspectwerkz.expression.ast;
10
11 /***
12 * This exception is thrown when parse errors are encountered. You can explicitly create objects of this exception type
13 * by calling the method generateParseException in the generated parser. You can modify this class to customize your
14 * error reporting mechanisms so long as you retain the public fields.
15 */
16 public class ParseException extends Exception {
17
18 /***
19 * This constructor is used by the method "generateParseException" in the generated parser. Calling this constructor
20 * generates a new object of this type with the fields "currentToken", "expectedTokenSequences", and "tokenImage"
21 * set. The boolean flag "specialConstructor" is also set to true to indicate that this constructor was used to
22 * create this object. This constructor calls its super class with the empty string to force the "toString" method
23 * of parent class "Throwable" to print the error message in the form: ParseException: <result of getMessage>
24 */
25 public ParseException(Token currentTokenVal, int[][] expectedTokenSequencesVal, String[] tokenImageVal) {
26 super("");
27 specialConstructor = true;
28 currentToken = currentTokenVal;
29 expectedTokenSequences = expectedTokenSequencesVal;
30 tokenImage = tokenImageVal;
31 }
32
33 /***
34 * The following constructors are for use by you for whatever purpose you can think of. Constructing the exception
35 * in this manner makes the exception behave in the normal way - i.e., as documented in the class "Throwable". The
36 * fields "errorToken", "expectedTokenSequences", and "tokenImage" do not contain relevant information. The JavaCC
37 * generated code does not use these constructors.
38 */
39
40 public ParseException() {
41 super();
42 specialConstructor = false;
43 }
44
45 public ParseException(String message) {
46 super(message);
47 specialConstructor = false;
48 }
49
50 /***
51 * This variable determines which constructor was used to create this object and thereby affects the semantics of
52 * the "getMessage" method (see below).
53 */
54 protected boolean specialConstructor;
55
56 /***
57 * This is the last token that has been consumed successfully. If this object has been created due to a parse error,
58 * the token followng this token will (therefore) be the first error token.
59 */
60 public Token currentToken;
61
62 /***
63 * Each entry in this array is an array of integers. Each array of integers represents a sequence of tokens (by
64 * their ordinal values) that is expected at this point of the parse.
65 */
66 public int[][] expectedTokenSequences;
67
68 /***
69 * This is a reference to the "tokenImage" array of the generated parser within which the parse error occurred. This
70 * array is defined in the generated ...Constants interface.
71 */
72 public String[] tokenImage;
73
74 /***
75 * This method has the standard behavior when this object has been created using the standard constructors.
76 * Otherwise, it uses "currentToken" and "expectedTokenSequences" to generate a parse error message and returns it.
77 * If this object has been created due to a parse error, and you do not catch it (it gets thrown from the parser),
78 * then this method is called during the printing of the final stack trace, and hence the correct error message gets
79 * displayed.
80 */
81 public String getMessage() {
82 if (!specialConstructor) {
83 return super.getMessage();
84 }
85 String expected = "";
86 int maxSize = 0;
87 for (int i = 0; i < expectedTokenSequences.length; i++) {
88 if (maxSize < expectedTokenSequences[i].length) {
89 maxSize = expectedTokenSequences[i].length;
90 }
91 for (int j = 0; j < expectedTokenSequences[i].length; j++) {
92 expected += tokenImage[expectedTokenSequences[i][j]] + " ";
93 }
94 if (expectedTokenSequences[i][expectedTokenSequences[i].length - 1] != 0) {
95 expected += "...";
96 }
97 expected += eol + " ";
98 }
99 String retval = "Encountered \"";
100 Token tok = currentToken.next;
101 for (int i = 0; i < maxSize; i++) {
102 if (i != 0)
103 retval += " ";
104 if (tok.kind == 0) {
105 retval += tokenImage[0];
106 break;
107 }
108 retval += add_escapes(tok.image);
109 tok = tok.next;
110 }
111 retval += "\" at line " + currentToken.next.beginLine + ", column " + currentToken.next.beginColumn;
112 retval += "." + eol;
113 if (expectedTokenSequences.length == 1) {
114 retval += "Was expecting:" + eol + " ";
115 } else {
116 retval += "Was expecting one of:" + eol + " ";
117 }
118 retval += expected;
119 return retval;
120 }
121
122 /***
123 * The end of line string for this machine.
124 */
125 protected String eol = System.getProperty("line.separator", "\n");
126
127 /***
128 * Used to convert raw characters to their escaped version when these raw version cannot be used as part of an ASCII
129 * string literal.
130 */
131 protected String add_escapes(String str) {
132 StringBuffer retval = new StringBuffer();
133 char ch;
134 for (int i = 0; i < str.length(); i++) {
135 switch (str.charAt(i)) {
136 case 0:
137 continue;
138 case '\b':
139 retval.append("//b");
140 continue;
141 case '\t':
142 retval.append("//t");
143 continue;
144 case '\n':
145 retval.append("//n");
146 continue;
147 case '\f':
148 retval.append("//f");
149 continue;
150 case '\r':
151 retval.append("//r");
152 continue;
153 case '\"':
154 retval.append("//\"");
155 continue;
156 case '\'':
157 retval.append("//\'");
158 continue;
159 case '//':
160 retval.append("////");
161 continue;
162 default:
163 if ((ch = str.charAt(i)) < 0x20 || ch > 0x7e) {
164 String s = "0000" + Integer.toString(ch, 16);
165 retval.append("//u" + s.substring(s.length() - 4, s.length()));
166 } else {
167 retval.append(ch);
168 }
169 continue;
170 }
171 }
172 return retval.toString();
173 }
174
175 }