-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBaseParser.java
More file actions
85 lines (69 loc) · 1.66 KB
/
BaseParser.java
File metadata and controls
85 lines (69 loc) · 1.66 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
package expression.exceptions;
public class BaseParser {
private static final char EOF = '\0';
private String source;
private char ch;
private int lenSource;
private int pos;
protected BaseParser() {
setSource("");
}
protected BaseParser(String str) {
setSource(str);
}
protected void nextChar() {
if (hasNext()) {
ch = source.charAt(pos++);
} else {
ch = EOF;
}
}
protected boolean test(char expected) {
if (ch == EOF) {
return false;
}
if (ch == expected) {
nextChar();
return true;
}
return false;
}
protected boolean test(String expected) {
for(char c: expected.toCharArray()) {
if (!test(c)) {
return false;
}
}
return true;
}
protected boolean eof() {
whitespace();
return ch == EOF;
}
private boolean between(final char from, final char to) {
return from <= ch && ch <= to;
}
protected boolean isDigit() {
return between('0', '9');
}
protected boolean isLetter() {
return between('a', 'z') || between('A', 'Z');
}
protected void whitespace() {
if (Character.isWhitespace(ch)) {
nextChar();
whitespace();
}
}
private boolean hasNext() {
return pos < lenSource;
}
protected void setSource(String str) {
this.source = str;
this.pos = 0;
this.lenSource = str.length();
}
protected char getChar() {
return ch;
}
}