How to adapt the many' to return Left in attoparsec of Haskel

49 Views Asked by At

For example

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 []

How can I make the second one to return Left with lost ), and I also want to parse the case with empty string with answer Right []

1

There are 1 best solutions below

0
Li-yao Xia On BEST ANSWER

From the doc on parseOnly

To force a parser to consume all of its input, use something like this:

parseOnly (myParser <* endOfInput)

ParseIni.parseTests applies the parser char '(' *> return 1 <* char ')' zero or more times. It always succeeds, as it can at least apply it zero times. Thus you need to ensure it consumed everything by composing it with endOfInput.

parseOnly (ParseIni.parseTests <* endOfInput) "("