Skip to content
This repository was archived by the owner on Feb 29, 2024. It is now read-only.

Commit 2b48ad4

Browse files
Don't require zero before dot (#49)
1 parent db5eab9 commit 2b48ad4

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

modules/mql/src/main/java/net/hollowcube/mql/parser/MqlParser.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
import java.util.ArrayList;
88
import java.util.List;
99

10+
import static net.hollowcube.mql.parser.MqlToken.Type.NUMBER;
11+
1012
public class MqlParser {
1113
private final MqlLexer lexer;
1214

@@ -89,6 +91,12 @@ public MqlParser(@NotNull String source) {
8991
if (token == null) throw new MqlParseError("unexpected end of input");
9092

9193
return switch (token.type()) {
94+
case DOT -> {
95+
var next = lexer.next();
96+
if (next == null) throw new MqlParseError("unexpected end of input");
97+
if (next.type() != NUMBER) throw new MqlParseError("expected number after .");
98+
yield new MqlNumberExpr(Double.parseDouble("0." + lexer.span(next)));
99+
}
92100
case NUMBER -> new MqlNumberExpr(Double.parseDouble(lexer.span(token)));
93101
case IDENT -> new MqlIdentExpr(lexer.span(token));
94102
case MINUS -> {

modules/mql/src/test/java/net/hollowcube/mql/TestMql.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,21 @@ public void basicQueryCall() {
3838
assertEquals(1, ((MqlNumberValue) result).value());
3939
}
4040

41+
@Test
42+
public void testDot() {
43+
var source = ".1 + 10 + 10 + .3";
44+
45+
var expr = new MqlParser(source).parse();
46+
var scopeImpl = new MqlScopeImpl();
47+
var vars = new MqlScopeImpl.Mutable();
48+
49+
var scope = new MqlScriptScope(scopeImpl, vars, scopeImpl);
50+
var result = expr.evaluate(scope);
51+
52+
assertTrue(result instanceof MqlNumberValue);
53+
assertEquals(20.4, ((MqlNumberValue) result).value(), 0.0001);
54+
}
55+
4156
@Test
4257
public void testAdd() {
4358
var source = "10 + 10 + 10";

0 commit comments

Comments
 (0)