我目前正在开发一个需要重叠的后备解析器的项目。例如:
many1Till :: Show b => Parser a -> Parser b -> Parser [a]
many1Till parser delim = do notFollowedBy delim
car <- parser
cdr <- manyTill parser delim
return $ car : cdr
text = many1 anyChar
parser = many1Till text (lookAhead $ try $ string "hi")
pread input = case parse parser "main" input of
Left err -> "ERROR: " ++ show err
Right val -> show val
main :: IO ()
main = do
args <- getArgs
putStrLn (pread (args !! 0))
这里,当我运行时,文本会消耗所有输入,包括末尾的分隔符"hi"
./text "hellohi"
而非仅返回"hello"
。有什么方法可以让many1Till
更喜欢&#39;大量划分,还是应该在文本的每一点检查它是否与delim匹配?