Hello!
As an exercise I was trying to implement my own monadic parser combinators library and to test it I wanted to implement a very simple Json parser.
My simplified Json AST and Parser are represented by the following types:
data JsonValue
= JsonNull
| JsonBool Boolean
| JsonNumber Int
| JsonString String
| JsonArray (Array JsonValue)
| JsonObject (Array (Tuple String JsonValue))
newtype Parser a
= Parser (String -> Maybe (Tuple a String))
Everything was going well until I tried to implement the jsonArray Parser. Following the advise on this link https://github.com/Thimoteus/SandScript/wiki/2.-Parsing-recursively I made my Parser Lazy
instance lazyParser :: Lazy (Parser a) where
defer f = Parser $ \input -> p input
where
(Parser p) = f unit
used the function fix
when parsing my jsonValue
jsonValue :: Parser JsonValue
jsonValue =
fix
$ \p ->
(jsonNull <|> jsonBool <|> jsonNumber <|> jsonString <|> jsonArray p)
And also made the jsonArray parser take another parser
jsonArray :: Parser JsonValue -> Parser JsonValue
jsonArray p = JsonArray <$> wrappedP open (sepByP p wsAndComma) close
where
open = (charP '[') *> (manyP wsP)
close = (manyP wsP) <* (charP ']')
wsAndComma = (manyP wsP) *> (charP ',') <* (manyP wsP)
However, I am still getting a runtime error that I have no clue how to solve
/.psci_modules/node_modules/Parser/index.js:71
return Control_Bind.bind(Data_Maybe.bindMaybe)(v(input))(function (v1) {
^
TypeError: v is not a function
This error only happens if I pass a jsonValue
parser to jsonArray
Parser. I tried passing the other parsers to jsonArray
and it works, so I believe the problem may be in the way I am implementing Lazy or the way I am using the fix
function.
If you have any idea of what may be happening here. Please help me!
Thanks,
Andres!