秒差距选项可能总是消耗输入
Parsec optionMaybe always consuming input
我正在尝试调试为什么尽管 optionMaybe
与 try
相结合,parsec 仍在消耗输入。基本上在下面的代码中,为什么第二个 运行 没有返回 Right "abc"
(这是我所期望的)?
给出
myParser = do
optionMaybe $ try (between (char '/') (char '/') $ many1 (noneOf "/")
many1 (noneOf "/")
我明白了
> parse myParser "" "/abc/def"
Right "def"
> parse myParser "" "/abc"
Left (line 1, column 5):
unexpected end of input
expecting "/"
由于 "/abc"
中缺少关闭 '/'
,整个 between (char '/') (char '/') $ many1 (noneOf "/")
失败,因此 optionMaybe $ try $ between ...
不消耗任何东西(并导致 Nothing
).您剩下所有 "/abc"
输入,这当然与剩余的解析器 many1 (noneOf "/")
.
不匹配
我正在尝试调试为什么尽管 optionMaybe
与 try
相结合,parsec 仍在消耗输入。基本上在下面的代码中,为什么第二个 运行 没有返回 Right "abc"
(这是我所期望的)?
给出
myParser = do
optionMaybe $ try (between (char '/') (char '/') $ many1 (noneOf "/")
many1 (noneOf "/")
我明白了
> parse myParser "" "/abc/def"
Right "def"
> parse myParser "" "/abc"
Left (line 1, column 5):
unexpected end of input
expecting "/"
由于 "/abc"
中缺少关闭 '/'
,整个 between (char '/') (char '/') $ many1 (noneOf "/")
失败,因此 optionMaybe $ try $ between ...
不消耗任何东西(并导致 Nothing
).您剩下所有 "/abc"
输入,这当然与剩余的解析器 many1 (noneOf "/")
.