Skip to content

Commit e169e56

Browse files
fix: Fix EOF issues in Parser
1 parent 5c560b4 commit e169e56

File tree

5 files changed

+42
-16
lines changed

5 files changed

+42
-16
lines changed

src/main/java/net/marcellperger/mathexpr/interactive/Shell.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,9 @@
55
import net.marcellperger.mathexpr.parser.Parser;
66
import net.marcellperger.mathexpr.util.Input;
77
import net.marcellperger.mathexpr.util.MathUtil;
8-
import org.jetbrains.annotations.NotNull;
98
import org.jetbrains.annotations.Nullable;
109

1110
import java.io.PrintStream;
12-
import java.util.Arrays;
13-
14-
import static net.marcellperger.mathexpr.util.Input.input;
1511

1612
public class Shell {
1713
Input in;
@@ -27,7 +23,6 @@ public static void main(String[] args) {
2723
}
2824

2925
// TODO run() until exit better - parse exit command/Ctrl+C
30-
// TODO fails badly with unchecked exception on parsing `12 + (`
3126

3227
public void run() {
3328
//noinspection InfiniteLoopStatement
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package net.marcellperger.mathexpr.parser;
2+
3+
public class ExprParseEofException extends ExprParseException {
4+
public ExprParseEofException() {
5+
}
6+
7+
public ExprParseEofException(String message) {
8+
super(message);
9+
}
10+
11+
public ExprParseEofException(String message, Throwable cause) {
12+
super(message, cause);
13+
}
14+
15+
public ExprParseEofException(Throwable cause) {
16+
super(cause);
17+
}
18+
}

src/main/java/net/marcellperger/mathexpr/parser/ExprParseException.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package net.marcellperger.mathexpr.parser;
22

3-
4-
@SuppressWarnings("unused") // I want all the constructors
53
public class ExprParseException extends Exception {
64
public ExprParseException() {
75
super();

src/main/java/net/marcellperger/mathexpr/parser/Parser.java

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public MathSymbol parseExpr() throws ExprParseException {
5353

5454
public @NotNull MathSymbol parseParensOrLiteral() throws ExprParseException {
5555
discardWhitespace();
56-
return peek() == '(' ? parseParens() : parseDoubleLiteral();
56+
return peekExpect() == '(' ? parseParens() : parseDoubleLiteral();
5757
}
5858

5959
public MathSymbol parseParens() throws ExprParseException {
@@ -115,21 +115,35 @@ protected CharSequence strFromHere() {
115115
return CharBuffer.wrap(src, idx, src.length());
116116
}
117117

118-
boolean notEof() {
118+
public boolean notEof() {
119119
return idx < src.length();
120120
}
121-
boolean isEof() {
121+
public boolean isEof() {
122122
return idx >= src.length();
123123
}
124124

125-
char peek() {
125+
protected char peekAssert() {
126126
return src.charAt(idx);
127127
}
128-
char advance() {
128+
protected char peekExpect() throws ExprParseEofException {
129+
if(isEof()) throw new ExprParseEofException("Unexpected end of input");
130+
return src.charAt(idx);
131+
}
132+
protected char advanceAssert() {
133+
return src.charAt(idx++);
134+
}
135+
protected char advanceExpect() throws ExprParseEofException {
136+
if(isEof()) throw new ExprParseEofException("Unexpected end of input");
129137
return src.charAt(idx++);
130138
}
131-
boolean advanceIf(@NotNull Function<Character, Boolean> predicate) {
132-
boolean doAdvance = predicate.apply(peek());
139+
@SuppressWarnings("unused")
140+
protected boolean advanceIf(@NotNull Function<Character, Boolean> predicate) throws ExprParseEofException {
141+
boolean doAdvance = predicate.apply(peekExpect());
142+
if(doAdvance) ++idx;
143+
return doAdvance;
144+
}
145+
protected boolean advanceIfAssert(@NotNull Function<Character, Boolean> predicate) {
146+
boolean doAdvance = predicate.apply(peekAssert());
133147
if(doAdvance) ++idx;
134148
return doAdvance;
135149
}
@@ -140,7 +154,7 @@ boolean advanceIf(@NotNull Function<Character, Boolean> predicate) {
140154
*/
141155
protected int advanceWhile(@NotNull Function<Character, Boolean> predicate) {
142156
int n = 0;
143-
while (notEof() && advanceIf(predicate)) ++n;
157+
while (notEof() && advanceIfAssert(predicate)) ++n;
144158
return n;
145159
}
146160

@@ -153,7 +167,7 @@ protected void discardWhitespace() {
153167
}
154168

155169
protected void advanceExpectNext(char expected) throws ExprParseException {
156-
char actual = advance();
170+
char actual = advanceExpect();
157171
if(actual != expected) throw new ExprParseException("Expected '%c', got '%c'".formatted(expected, actual));
158172
}
159173
protected void advanceExpectNext_ignoreWs(char expected) throws ExprParseException {

src/main/java/net/marcellperger/mathexpr/util/MathUtil.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
public class MathUtil {
66
protected MathUtil() {}
77

8+
// TODO this doesn't work: roundToSigFigs(1.22222 + 0.3, 10) == 1.5222200000000001
89
public static double roundToSigFigs(double value, @Range(from=1, to=Integer.MAX_VALUE) int sigFigs) {
910
int mostSignificantDigit = (int)Math.floor(Math.log10(value));
1011
int roundToDigit = mostSignificantDigit - sigFigs + 1;

0 commit comments

Comments
 (0)