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