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