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