Skip to content

Commit 23c8da5

Browse files
refactor: Start using Result in Parser (parseDoubleLiteral_result) [WIP]
1 parent 2d78831 commit 23c8da5

1 file changed

Lines changed: 17 additions & 0 deletions

File tree

  • src/main/java/net/marcellperger/mathexpr/parser

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import net.marcellperger.mathexpr.*;
44
import net.marcellperger.mathexpr.util.Pair;
55
import net.marcellperger.mathexpr.util.Util;
6+
import net.marcellperger.mathexpr.util.rs.Result;
67
import org.jetbrains.annotations.NotNull;
78
import org.jetbrains.annotations.Nullable;
89
import org.jetbrains.annotations.Range;
@@ -62,6 +63,22 @@ public MathSymbol parseExpr() throws ExprParseException {
6263
}
6364
return new BasicDoubleSymbol(value);
6465
}
66+
public @NotNull Result<MathSymbol, Throwable> parseDoubleLiteral_result() {
67+
discardWhitespace();
68+
Matcher m = DOUBLE_RE.matcher(strFromHere());
69+
if (!m.lookingAt()) return Result.fromExc(new ExprParseException("Invalid number"));
70+
String s = m.group();
71+
idx += s.length();
72+
double value;
73+
try {
74+
value = Double.parseDouble(s);
75+
} catch (NumberFormatException exc) {
76+
// Technically this should never happen - assuming I've got that regex right
77+
throw new AssertionError(
78+
"There is a problem with the regex, this should've been rejected earlier", exc);
79+
}
80+
return Result.newOk(new BasicDoubleSymbol(value));
81+
}
6582

6683
public @Nullable MathSymbol parseParensOrLiteral() throws ExprParseException {
6784
discardWhitespace();

0 commit comments

Comments
 (0)