我有
stringparse = mstring
<$> char '"'
<*> (many alphaNum <|> many space)
<*> char '"'
where mstring a b c = [a] ++ b ++ [c]
当我这样做的时候,
解析 stringparse """\"hello\"
我得到 正确的 "\"hello\""
当我这样做的时候,
解析 stringparse """\"\""
我得到正确的 "\"\""
但是当我这样做时,
解析 stringparse """\"\""
或 解析 stringparse """\"he llo\""
它不起作用。
我收到错误,
Left (line 1, column 2):
unexpected " "
expecting letter or digit or "\""
和
Left (line 1, column 4):
unexpected " "
expecting letter or digit or "\""
分别。
我不明白为什么代码不能正确解析空格。
最佳答案
因为你这样做many alphaNum <|> many space
。 many
接受 0 作为可接受的字符数,它总是成功。这与 *
的行为相同。在正则表达式中。
所以在 <|>
它永远不会失败并调用正确的一方。所以你是说“尝试尽可能多的 alphaNum
,然后采取 "
。
你想要的是
many (alphaNum <|> space)
换句话说,“尽可能多的 alphaNum
或 space
”。
关于Haskell 秒差距空间解析错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18777191/