1
2
3 package org.apache.maven.surefire.group.parse;
4
5
6
7
8
9
10
11
12
13
14 public class ParseException extends Exception {
15
16
17
18
19
20
21 private static final long serialVersionUID = 1L;
22
23
24
25
26 protected static String EOL = System.getProperty("line.separator", "\n");
27
28
29
30
31
32
33
34 public ParseException(Token currentTokenVal,
35 int[][] expectedTokenSequencesVal,
36 String[] tokenImageVal
37 )
38 {
39 super(initialise(currentTokenVal, expectedTokenSequencesVal, tokenImageVal));
40 currentToken = currentTokenVal;
41 expectedTokenSequences = expectedTokenSequencesVal;
42 tokenImage = tokenImageVal;
43 }
44
45
46
47
48
49
50
51
52
53
54
55 public ParseException() {
56 super();
57 }
58
59
60 public ParseException(String message) {
61 super(message);
62 }
63
64
65
66
67
68
69
70 public Token currentToken;
71
72
73
74
75
76
77 public int[][] expectedTokenSequences;
78
79
80
81
82
83
84 public String[] tokenImage;
85
86
87
88
89
90
91
92
93 private static String initialise(Token currentToken,
94 int[][] expectedTokenSequences,
95 String[] tokenImage) {
96
97 StringBuilder expected = new StringBuilder();
98 int maxSize = 0;
99 for (int i = 0; i < expectedTokenSequences.length; i++) {
100 if (maxSize < expectedTokenSequences[i].length) {
101 maxSize = expectedTokenSequences[i].length;
102 }
103 for (int j = 0; j < expectedTokenSequences[i].length; j++) {
104 expected.append(tokenImage[expectedTokenSequences[i][j]]).append(' ');
105 }
106 if (expectedTokenSequences[i][expectedTokenSequences[i].length - 1] != 0) {
107 expected.append("...");
108 }
109 expected.append(EOL).append(" ");
110 }
111 String retval = "Encountered \"";
112 Token tok = currentToken.next;
113 for (int i = 0; i < maxSize; i++) {
114 if (i != 0) retval += " ";
115 if (tok.kind == 0) {
116 retval += tokenImage[0];
117 break;
118 }
119 retval += " " + tokenImage[tok.kind];
120 retval += " \"";
121 retval += add_escapes(tok.image);
122 retval += " \"";
123 tok = tok.next;
124 }
125 if (currentToken.next != null) {
126 retval += "\" at line " + currentToken.next.beginLine + ", column " + currentToken.next.beginColumn;
127 }
128 retval += "." + EOL;
129
130
131 if (expectedTokenSequences.length == 0) {
132
133 } else {
134 if (expectedTokenSequences.length == 1) {
135 retval += "Was expecting:" + EOL + " ";
136 } else {
137 retval += "Was expecting one of:" + EOL + " ";
138 }
139 retval += expected.toString();
140 }
141
142 return retval;
143 }
144
145
146
147
148
149
150
151 static String add_escapes(String str) {
152 StringBuilder retval = new StringBuilder();
153 char ch;
154 for (int i = 0; i < str.length(); i++) {
155 switch (str.charAt(i))
156 {
157 case '\b':
158 retval.append("\\b");
159 continue;
160 case '\t':
161 retval.append("\\t");
162 continue;
163 case '\n':
164 retval.append("\\n");
165 continue;
166 case '\f':
167 retval.append("\\f");
168 continue;
169 case '\r':
170 retval.append("\\r");
171 continue;
172 case '\"':
173 retval.append("\\\"");
174 continue;
175 case '\'':
176 retval.append("\\\'");
177 continue;
178 case '\\':
179 retval.append("\\\\");
180 continue;
181 default:
182 if ((ch = str.charAt(i)) < 0x20 || ch > 0x7e) {
183 String s = "0000" + Integer.toString(ch, 16);
184 retval.append("\\u" + s.substring(s.length() - 4, s.length()));
185 } else {
186 retval.append(ch);
187 }
188 continue;
189 }
190 }
191 return retval.toString();
192 }
193
194 }
195