-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExpressionParser.java
More file actions
160 lines (140 loc) · 4.99 KB
/
ExpressionParser.java
File metadata and controls
160 lines (140 loc) · 4.99 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
package expression.exceptions;
import java.util.Map;
import java.util.Set;
import java.util.function.Supplier;
import expression.BothExpressions;
import expression.Max;
import expression.Min;
import expression.Const;
import expression.Variable;
public class ExpressionParser extends BaseParser implements Parser {
private static Set<Character> VARIABLE_NAMES = Set.of('x', 'y', 'z');
private final Supplier<BothExpressions> minMax = () -> minMax();
private final Supplier<BothExpressions> addSubtract = () -> addSubtract();
private final Supplier<BothExpressions> multiplyDivide = () -> multiplyDivide();
private final Supplier<BothExpressions> varConst = () -> varConst();
private final Map<Supplier<BothExpressions>, Supplier<BothExpressions>> nextFunc = Map.of(
minMax, addSubtract,
addSubtract, multiplyDivide,
multiplyDivide, varConst,
varConst, minMax);
public BothExpressions parse(final String s) {
setSource(s);
nextChar();
BothExpressions result = minMax();
if (eof()) {
return result;
} else {
throw new ParseException("Bracket Mismatch");
}
}
private BothExpressions minMax() {
Supplier<BothExpressions> lower = nextFunc.get(minMax);
BothExpressions first = lower.get();
while (!eof()) {
if (test("m")) {
if (test("in") && (Character.isWhitespace(getChar()) || getChar() == '(') || getChar() == '-') {
first = new Min(first, lower.get());
} else if (test("ax") && (Character.isWhitespace(getChar()) || getChar() == '(') || getChar() == '-') {
first = new Max(first, lower.get());
} else {
throw new ParseException("expected min or max, received m**");
}
} else {
break;
}
}
return first;
}
private BothExpressions addSubtract() {
Supplier<BothExpressions> lower = nextFunc.get(addSubtract);
BothExpressions first = lower.get();
while (!eof()) {
if (test('+')) {
first = new CheckedAdd(first, lower.get());
} else if (test('-')) {
first = new CheckedSubtract(first, lower.get());
} else {
break;
}
}
return first;
}
private BothExpressions multiplyDivide() {
Supplier<BothExpressions> lower = nextFunc.get(multiplyDivide);
BothExpressions first = lower.get();
while (!eof()) {
if (test('*')) {
first = new CheckedMultiply(first, lower.get());
} else if (test('/')) {
first = new CheckedDivide(first, lower.get());
} else {
break;
}
}
return first;
}
private BothExpressions varConst() {
if (eof()) {
throw new ParseException("Expected variable or const, found EOF");
}
if (isDigit()) {
return parseConst(1);
}
if (test('(')) {
BothExpressions result = nextFunc.get(varConst).get();
if (!test(')')) {
throw new ParseException("Excected: ), found:" + getChar());
}
return result;
}
if (test('l')) {
if (test('0') && (Character.isWhitespace(getChar()) || getChar() == '(')) {
return new LeadingZeroes(varConst());
} else {
throw new ParseException("exprected l0, got l*");
}
}
if (test('t')) {
if (test('0') && (Character.isWhitespace(getChar()) || getChar() == '(')) {
return new TailZeroes(varConst());
} else {
throw new ParseException("exprected d0, got t*");
}
}
if (test('-')) {
return parseMinus();
}
if (isValidName()) {
return parseVariable();
}
throw new ParseException("Unknown symbol");
}
private BothExpressions parseMinus() {
if (isDigit()) {
return parseConst(-1);
}
return new CheckedNegate(varConst());
}
private BothExpressions parseConst(int sign) {
int num = 0;
while (isDigit()) {
char ch = getChar();
if ((num > 0 && num * 10 + sign * (ch - '0') < 0)
|| (num < 0 && num * 10 + sign * (ch - '0') > 0)) {
throw new OverflowException("Overflow in parseConst method");
}
num = num * 10 + sign * (int) (ch - '0');
nextChar();
}
return new Const(num);
}
private BothExpressions parseVariable() {
final String name = Character.toString(getChar());
nextChar();
return new Variable(name);
}
private boolean isValidName() {
return VARIABLE_NAMES.contains(getChar());
}
}