我试图了解many
Haskell 的 Megaparsec 库中的行为。我本以为many
当输入解析器失败时会返回一个空列表,但事实似乎并非如此。
特别是,我对以下最少的代码感到困惑:
import Text.Megaparsec (Parsec, many, parseTest)
import Text.Megaparsec.Char (string)
import Data.Void
test :: Parsec Void String [String] -> String -> IO ()
test = parseTest
main = do
test (many (string "a" >> string "b")) "ac" -- error
test (many (string "a" <> string "b")) "ac" -- error
test (many (string "ab")) "ac" -- []
其输出如下:
1:2:
|
1 | ac
| ^
unexpected 'c'
expecting 'b'
1:2:
|
1 | ac
| ^
unexpected 'c'
expecting 'b'
[]
我不明白为什么前两个测试失败而第三个测试失败。