1
2 package org.apache.velocity.runtime.parser;
3
4
5
6
7
8
9
10
11
12
13
14
15
16 public class ParseException extends Exception {
17
18 private static final long serialVersionUID = -309603325673449381L;
19
20
21
22
23
24
25
26
27
28
29
30
31
32 public ParseException(Token currentTokenVal,
33 int[][] expectedTokenSequencesVal,
34 String[] tokenImageVal
35 )
36 {
37 super("");
38 specialConstructor = true;
39 currentToken = currentTokenVal;
40 expectedTokenSequences = expectedTokenSequencesVal;
41 tokenImage = tokenImageVal;
42 }
43
44
45
46
47
48
49
50
51
52
53
54 public ParseException() {
55 super();
56 specialConstructor = false;
57 }
58
59 public ParseException(String message) {
60 super(message);
61 specialConstructor = false;
62 }
63
64
65
66
67
68
69 protected boolean specialConstructor;
70
71
72
73
74
75
76 public Token currentToken;
77
78
79
80
81
82
83 public int[][] expectedTokenSequences;
84
85
86
87
88
89
90 public String[] tokenImage;
91
92
93
94
95
96
97
98
99
100
101
102 public String getMessage() {
103 if (!specialConstructor) {
104 return super.getMessage();
105 }
106 String expected = "";
107 int maxSize = 0;
108 for (int i = 0; i < expectedTokenSequences.length; i++) {
109 if (maxSize < expectedTokenSequences[i].length) {
110 maxSize = expectedTokenSequences[i].length;
111 }
112 for (int j = 0; j < expectedTokenSequences[i].length; j++) {
113 expected += tokenImage[expectedTokenSequences[i][j]] + " ";
114 }
115 if (expectedTokenSequences[i][expectedTokenSequences[i].length - 1] != 0) {
116 expected += "...";
117 }
118 expected += eol + " ";
119 }
120 String retval = "Encountered \"";
121 Token tok = currentToken.next;
122 for (int i = 0; i < maxSize; i++) {
123 if (i != 0) retval += " ";
124 if (tok.kind == 0) {
125 retval += tokenImage[0];
126 break;
127 }
128 retval += add_escapes(tok.image);
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;
139 return retval;
140 }
141
142
143
144
145 protected String eol = System.getProperty("line.separator", "\n");
146
147
148
149
150
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 }