如何使 many' 适应 Haskel 的 attoparsec 中的 return Left
How to adapt the many' to return Left in attoparsec of Haskel
例如
parseTest :: Parser Int
parseTest = char '(' *> return 1 <* char ')'
parseTests :: Parser [Int]
parseTests = many' $ char '(' *> return 1 <* char ')'
parseOnly ParseIni.parseTest "(" -- returns Left with error
parseOnly ParseIni.parseTests "(" -- returns Right with []
如何使第二个 return 丢失 )
,并且我还想用答案 Right []
解析空字符串的情况
To force a parser to consume all of its input, use something like this:
parseOnly (myParser <* endOfInput)
ParseIni.parseTests
应用解析器 char '(' *> return 1 <* char ')'
零次或多次。它总是成功,因为它至少可以应用它零次。因此,您需要通过 endOfInput
.
组合来确保它消耗了所有内容
parseOnly (ParseIni.parseTests <* endOfInput) "("
例如
parseTest :: Parser Int
parseTest = char '(' *> return 1 <* char ')'
parseTests :: Parser [Int]
parseTests = many' $ char '(' *> return 1 <* char ')'
parseOnly ParseIni.parseTest "(" -- returns Left with error
parseOnly ParseIni.parseTests "(" -- returns Right with []
如何使第二个 return 丢失 )
,并且我还想用答案 Right []
To force a parser to consume all of its input, use something like this:
parseOnly (myParser <* endOfInput)
ParseIni.parseTests
应用解析器 char '(' *> return 1 <* char ')'
零次或多次。它总是成功,因为它至少可以应用它零次。因此,您需要通过 endOfInput
.
parseOnly (ParseIni.parseTests <* endOfInput) "("