|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| |
|
8 |
| |
|
9 |
| |
|
10 |
| |
|
11 |
| |
|
12 |
| |
|
13 |
| |
|
14 |
| |
|
15 |
| |
|
16 |
| |
|
17 |
| |
|
18 |
| |
|
19 |
| |
|
20 |
| |
|
21 |
| |
|
22 |
| |
|
23 |
| |
|
24 |
| |
|
25 |
| |
|
26 |
| |
|
27 |
| |
|
28 |
| |
|
29 |
| package koala.dynamicjava.parser.wrapper; |
|
30 |
| |
|
31 |
| |
|
32 |
| import java.io.File; |
|
33 |
| |
|
34 |
| import edu.rice.cs.plt.text.TextUtil; |
|
35 |
| import koala.dynamicjava.parser.impl.ParseException; |
|
36 |
| import koala.dynamicjava.parser.impl.Token; |
|
37 |
| import koala.dynamicjava.tree.SourceInfo; |
|
38 |
| |
|
39 |
| |
|
40 |
| |
|
41 |
| |
|
42 |
| |
|
43 |
| |
|
44 |
| |
|
45 |
| |
|
46 |
| |
|
47 |
| public class ParseError extends Error implements SourceInfo.Wrapper { |
|
48 |
| private SourceInfo _si; |
|
49 |
| |
|
50 |
| |
|
51 |
| |
|
52 |
| |
|
53 |
| |
|
54 |
| |
|
55 |
| |
|
56 |
0
| public ParseError(String s, SourceInfo si) {
|
|
57 |
0
| super(s);
|
|
58 |
0
| _si = si;
|
|
59 |
| } |
|
60 |
| |
|
61 |
| |
|
62 |
| |
|
63 |
| |
|
64 |
| |
|
65 |
| |
|
66 |
0
| public ParseError(ParseException e, File f) {
|
|
67 |
0
| super(parseExceptionMessage(e), e);
|
|
68 |
0
| _si = parseExceptionLocation(e, f);
|
|
69 |
| } |
|
70 |
| |
|
71 |
0
| public ParseError(Throwable t, SourceInfo si) {
|
|
72 |
0
| super(t.getMessage(), t);
|
|
73 |
0
| _si = si;
|
|
74 |
| } |
|
75 |
| |
|
76 |
0
| public SourceInfo getSourceInfo() { return _si; }
|
|
77 |
| |
|
78 |
| |
|
79 |
0
| private static String parseExceptionMessage(ParseException e) {
|
|
80 |
0
| if (e.expectedTokenSequences == null) { return e.getMessage(); }
|
|
81 |
| else { |
|
82 |
0
| int maxSize = 0;
|
|
83 |
0
| for (int i = 0; i < e.expectedTokenSequences.length; i++) {
|
|
84 |
0
| if (maxSize < e.expectedTokenSequences[i].length) {
|
|
85 |
0
| maxSize = e.expectedTokenSequences[i].length;
|
|
86 |
| } |
|
87 |
| } |
|
88 |
0
| String retval = "Syntax Error: \"";
|
|
89 |
0
| Token tok = e.currentToken.next;
|
|
90 |
| |
|
91 |
0
| for (int i = 0; i < maxSize; i++) {
|
|
92 |
0
| if (i != 0) retval += " ";
|
|
93 |
0
| if (tok.kind == 0) {
|
|
94 |
0
| retval += e.tokenImage[0];
|
|
95 |
0
| break;
|
|
96 |
| } |
|
97 |
0
| retval += TextUtil.javaEscape(tok.image);
|
|
98 |
0
| tok = tok.next;
|
|
99 |
| } |
|
100 |
0
| retval += "\"";
|
|
101 |
0
| return retval;
|
|
102 |
| } |
|
103 |
| } |
|
104 |
| |
|
105 |
0
| private static SourceInfo parseExceptionLocation(ParseException e, File f) {
|
|
106 |
0
| Token t = e.currentToken;
|
|
107 |
0
| if (t == null) { return SourceInfo.point(f, 0, 0); }
|
|
108 |
| else { |
|
109 |
0
| if (t.next != null) { t = t.next; }
|
|
110 |
0
| return SourceInfo.range(f, t.beginLine, t.beginColumn, t.endLine, t.endColumn);
|
|
111 |
| } |
|
112 |
| } |
|
113 |
| |
|
114 |
| } |