Skip to content

Commit

Permalink
fix: add negative numbers to json parser and allow float parser to pa…
Browse files Browse the repository at this point in the history
…rse integers
  • Loading branch information
aboeglin committed Apr 18, 2024
1 parent bb66957 commit 82c427b
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 24 deletions.
2 changes: 1 addition & 1 deletion madlib.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ cabal-version: 2.0
-- see: https://github.com/sol/hpack

name: madlib
version: 0.23.8
version: 0.23.9
description: Please see the README on GitHub at <https://github.com/madlib-lang/madlib#readme>
homepage: https://github.com/madlib-lang/madlib#readme
bug-reports: https://github.com/madlib-lang/madlib/issues
Expand Down
2 changes: 1 addition & 1 deletion package.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: madlib
version: 0.23.8
version: 0.23.9
github: "madlib-lang/madlib"
license: BSD3
author: "Arnaud Boeglin, Brekk Bockrath"
Expand Down
2 changes: 1 addition & 1 deletion pkg/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@madlib-lang/madlib",
"version": "0.23.8",
"version": "0.23.9",
"main": "./src/run.js",
"bin": {
"madlib": "src/run.js"
Expand Down
56 changes: 38 additions & 18 deletions prelude/__internal__/Json/Parse.mad
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
import List from "List"
import { Just, Nothing } from "Maybe"
import {} from "Integer"
import {} from "Float"
import Float from "Float"
import Parse from "Parse"
import { char, choice, digit, many, notChar, runParser, sepBy, some, spaces, symbol } from "Parse"
import String from "String"
Expand Down Expand Up @@ -51,30 +51,47 @@ jsonString = do {


jsonInteger :: Parse.Parser Value
jsonInteger = pipe(
some,
chain(
pipe(
String.fromList,
scan,
where {
Just(i) =>
of(JsonInteger(i))

Nothing =>
Parse.fail
}
)
)
)(digit)
jsonInteger = do {
negSignChar <- map(Just, char('-')) <|> of(Nothing)
digitChars <- Parse.some(digit)

allChars = where(negSignChar) {
Just(s) =>
[s, ...digitChars]

_ =>
digitChars
}

return pipe(
String.fromList,
scan,
where {
Just(i) =>
of(JsonInteger(i))

Nothing =>
Parse.fail
},
)(allChars)
}


jsonFloat :: Parse.Parser Value
jsonFloat = do {
negSignChar <- map(Just, char('-')) <|> of(Nothing)
beforeDot <- some(digit)
dot <- char('.')
afterDot <- some(digit)

start = where(negSignChar) {
Just(s) =>
[s, ...beforeDot]

_ =>
beforeDot
}

return pipe(
List.concat($, afterDot),
String.fromList,
Expand All @@ -86,7 +103,7 @@ jsonFloat = do {
Nothing =>
Parse.fail
}
)(List.append(dot, beforeDot))
)(List.append(dot, start))
}


Expand Down Expand Up @@ -337,6 +354,9 @@ export float = Parser(
JsonFloat(f) =>
Right(f)

JsonInteger(i) =>
Right(Float.fromInteger(i))

_ =>
Left("Error parsing float")
}
Expand Down
11 changes: 8 additions & 3 deletions prelude/__internal__/Json/Parse.spec.mad
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { assertEquals, test } from "Test"

import { Left, Right } from "Either"
import { list, parse, string, float } from "./Parse"
import { integer, list, parse, string, float } from "./Parse"



Expand All @@ -20,5 +20,10 @@ test("list - error", () => assertEquals(parse(list(string), `1`), Left("Error p

test("float - good", () => assertEquals(parse(float, `1.3`), Right(1.3)))

// TODO: revisit that behavior and don't make the '.' mandatory
test("float - fail for integers", () => assertEquals(parse(float, `1`), Left("Error parsing float")))
test("integer - negative", () => assertEquals(parse(integer, `-1`), Right(-1)))

test("float - negative", () => assertEquals(parse(float, `-1.0`), Right(-1_f)))

test("float - without decimal", () => assertEquals(parse(float, `1`), Right(1)))

test("float - negative without decimal", () => assertEquals(parse(float, `-1`), Right(-1)))

0 comments on commit 82c427b

Please sign in to comment.