Skip to content

Commit

Permalink
More parser improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
mikera committed Sep 29, 2024
1 parent 4f11e05 commit 2cf75b5
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ DECIMAL:

fragment
EPART:
[eE] (DIGITS | SIGNED_DIGITS);
[eE] (DIGITS | SIGNED_DIGITS) SYMBOL_FOLLOWING*;

ADDRESS:
'#' [0-9]+;
Expand Down
11 changes: 8 additions & 3 deletions convex-core/src/main/java/convex/core/data/prim/CVMDouble.java
Original file line number Diff line number Diff line change
Expand Up @@ -145,11 +145,16 @@ public double doubleValue() {
/**
* Parses a CVM Double value.
* @param s String to parse
* @return CVMDouble value
* @throws NumberFormatException If number format is invalid
* @return CVMDouble value, or null if not parseable as a double
*/
public static CVMDouble parse(String s) {
return create(Double.parseDouble(s));
try {
double d=Double.parseDouble(s);
return create(d);
} catch (NumberFormatException e) {
return null;
}

}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,9 @@ public void enterDoubleValue(DoubleValueContext ctx) {
@Override
public void exitDoubleValue(DoubleValueContext ctx) {
String s=ctx.getText();
push( CVMDouble.parse(s));
CVMDouble v=CVMDouble.parse(s);
if (v==null) throw new ParseException("Bad double format: "+s);
push(v);
}

@Override
Expand Down
10 changes: 7 additions & 3 deletions convex-core/src/test/java/convex/core/lang/ReaderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,12 @@ public void testSymbols() {
public void testSymbolPath() {
ACell form=Reader.read("foo/bar/baz");
assertEquals(Lists.of(Symbols.LOOKUP,Lists.of(Symbols.LOOKUP,Symbols.FOO,Symbols.BAR),Symbols.BAZ),form) ;

assertParseException(()->Reader.read("foo/12"));

// TODO: is this sane?
// assertParseException(()->Reader.read("foo/ bar"));

}

@Test
Expand Down Expand Up @@ -174,9 +180,7 @@ public void testNumbers() {
assertParseException(() -> {
Reader.read("2.0e0.1234");
});
// assertNull( Reader.read("[2.0e0.1234]"));
// TODO: do we want this?
//assertThrows(Error.class, () -> Reader.read("[2.0e0.1234]")); // Issue #70
assertParseException( () -> Reader.read("[2.0e0.1234]")); // Issue #70

// metadata ignored
assertEquals(Syntax.create(RT.cvm(3.23),Maps.of(Keywords.FOO, CVMBool.TRUE)), Reader.read("^:foo 3.23"));
Expand Down

0 comments on commit 2cf75b5

Please sign in to comment.