As part of trying to write a JSON Parser, I'm working on parsing a JSON String value.
Given the following definition from Prof. Brent Yorgey's Haskell course:
-- A parser for a value of type a is a function which takes a String
-- represnting the input to be parsed, and succeeds or fails; if it
-- succeeds, it returns the parsed value along with the remainder of
-- the input.
newtype Parser a = Parser { runParser :: String -> Maybe (a, String) }
I'm running into trouble since, as I understand, my second parser, zeroOrMore notEndOfString
, simply consumes every token until reaching the end of the String.
data JValue = S String | ...
parseStringJValue :: Parser JValue
parseStringJValue = S <$> ((char '"') *> (zeroOrMore notEndOfString) <* (char '"'))
notEndOfString :: Parser Char
notEndOfString = Parser f
where
f [] = Nothing
f (x:xs) = Just (x, xs)
Using this applicative approach, please give me a hint towards modifying my above parseStringJValue
to actually work.
Note - I have a faint idea on how to solve it with Monads, but I'm not sure if if they are required.
notEndOfString
is the result of your parse, not the input to be parsed.zeroOrMore notEndOfStringOrQuote
. In parsec, for example, I would probably just writemany (noneOf "\"")
.