-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathBaseParser.java
More file actions
59 lines (49 loc) · 1.38 KB
/
BaseParser.java
File metadata and controls
59 lines (49 loc) · 1.38 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
package expression.exceptions;
import expression.ExpressionException;
public abstract class BaseParser implements Parser {
protected ExpressionSource source;
protected char ch;
protected void setSource(ExpressionSource source) {
this.source = source;
}
protected void nextChar() {
ch = source.hasNext() ? source.next() : '\0';
}
protected boolean test(char expected) {
if (ch == expected) {
nextChar();
return true;
}
return false;
}
protected void expect(final char c) throws ParserException {
if (ch != c) {
throw error("Expected '" + c + "'', got '" + ch + "'");
}
nextChar();
}
protected boolean expect(final String value) {
source.savePos();
try {
for (char c : value.toCharArray()) {
expect(c);
}
} catch (ParserException e) {
source.restorePos();
nextChar();
return false;
}
return true;
}
protected ParserException error(final String message) {
return source.error(message);
}
protected boolean between(final char from, final char to) {
return from <= ch && ch <= to;
}
protected void skipWhitespace() {
while (Character.isWhitespace(ch)) {
nextChar();
}
}
}