forked from darius/expr
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSyntaxException.java
More file actions
177 lines (155 loc) · 5.31 KB
/
Copy pathSyntaxException.java
File metadata and controls
177 lines (155 loc) · 5.31 KB
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// Syntax-error exception.
// Copyright 1996 by Darius Bacon; see the file COPYING.
package com.expr;
/**
* An exception indicating a problem in parsing an expression. It can
* produce a short, cryptic error message (with getMessage()) or a
* long, hopefully helpful one (with explain()).
*/
public class SyntaxException extends Exception {
/**
* An error code meaning the input string couldn't reach the end
* of the input; the beginning constituted a legal expression,
* but there was unparsable stuff left over.
*/
public static final int INCOMPLETE = 0;
/**
* An error code meaning the parser ran into a non-value token
* (like "/") at a point it was expecting a value (like "42" or
* "x^2").
*/
public static final int BAD_FACTOR = 1;
/**
* An error code meaning the parser hit the end of its input
* before it had parsed a full expression.
*/
public static final int PREMATURE_EOF = 2;
/**
* An error code meaning the parser hit an unexpected token at a
* point where it expected to see some particular other token.
*/
public static final int EXPECTED = 3;
/**
* An error code meaning the expression includes a variable not
* on the `allowed' list.
*/
public static final int UNKNOWN_VARIABLE = 4;
/**
* Make a new instance.
*
* @param complaint short error message
* @param parser the parser that hit this snag
* @param reason one of the error codes defined in this class
* @param expected if nonnull, the token the parser expected to
* see (in place of the erroneous token it did see)
*/
public SyntaxException(String complaint,
Parser parser,
int reason,
String expected) {
super(complaint);
this.reason = reason;
this.parser = parser;
this.scanner = parser.tokens;
this.expected = expected;
}
/**
* Give a long, hopefully helpful error message.
*
* @return the message
*/
public String explain() {
StringBuffer sb = new StringBuffer();
sb.append("I don't understand your formula ");
quotify(sb, scanner.getInput());
sb.append(".\n\n");
explainWhere(sb);
explainWhy(sb);
explainWhat(sb);
return sb.toString();
}
private Parser parser;
private Scanner scanner;
private int reason;
private String expected;
private void explainWhere(StringBuffer sb) {
if (scanner.isEmpty()) {
sb.append("It's empty!\n");
} else if (scanner.atStart()) {
sb.append("It starts with ");
quotify(sb, theToken());
if (isLegalToken())
sb.append(", which can never be the start of a formula.\n");
else
sb.append(", which is a meaningless symbol to me.\n");
} else {
sb.append("I got as far as ");
quotify(sb, asFarAs());
sb.append(" and then ");
if (scanner.atEnd()) {
sb.append("reached the end unexpectedly.\n");
} else {
sb.append("saw ");
quotify(sb, theToken());
if (isLegalToken())
sb.append(".\n");
else
sb.append(", which is a meaningless symbol to me.\n");
}
}
}
private void explainWhy(StringBuffer sb) {
switch (reason) {
case INCOMPLETE:
if (isLegalToken())
sb.append("The first part makes sense, but I don't see " +
"how the rest connects to it.\n");
break;
case BAD_FACTOR:
case PREMATURE_EOF:
sb.append("I expected a value");
if (!scanner.atStart()) sb.append(" to follow");
sb.append(", instead.\n");
break;
case EXPECTED:
sb.append("I expected ");
quotify(sb, expected);
sb.append(" at that point, instead.\n");
break;
case UNKNOWN_VARIABLE:
sb.append("That variable has no value.\n");
break;
default:
throw new Error("Can't happen");
}
}
private void explainWhat(StringBuffer sb) {
String fixedInput = tryToFix();
if (null != fixedInput) {
sb.append("An example of a formula I can parse is ");
quotify(sb, fixedInput);
sb.append(".\n");
}
}
private String tryToFix() {
return (parser.tryCorrections() ? scanner.toString() : null);
}
private void quotify(StringBuffer sb, String s) {
sb.append('"');
sb.append(s);
sb.append('"');
}
private String asFarAs() {
Token t = scanner.getCurrentToken();
int point = t.location - t.leadingWhitespace;
return scanner.getInput().substring(0, point);
}
private String theToken() {
return scanner.getCurrentToken().sval;
}
private boolean isLegalToken() {
Token t = scanner.getCurrentToken();
return t.ttype != Token.TT_EOF
&& t.ttype != Token.TT_ERROR;
}
}