I have
stringparse = mstring
<$> char '"'
<*> (many alphaNum <|> many space)
<*> char '"'
where mstring a b c = [a] ++ b ++ [c]
When I do,
parse stringparse "" "\"hello\"
I get Right "\"hello\""
When I do,
parse stringparse "" "\"\""
I get Right "\"\""
But when I do,
parse stringparse "" "\" \""
or parse stringparse "" "\"he llo\""
it does not work.
I get errors,
Left (line 1, column 2):
unexpected " "
expecting letter or digit or "\""
and
Left (line 1, column 4):
unexpected " "
expecting letter or digit or "\""
respectively.
I don't understand why the code does not parse spaces properly.
It's because you're doing this many alphaNum <|> many space
. many
accepts 0 as an acceptable amount of characters, it always succeeds. This is the same behavior as *
in regexs.
So in a <|>
it's never going to fail and call the right side. So you're saying "try as many alphaNum
as possible then take a "
.
What you want is
many (alphaNum <|> space)
In other words, "as many alphaNum
s or space
s as possible".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With