parsec
parsec copied to clipboard
parser never returns
This is a primitve csv parser which skips intermediate blank lines:
parser :: Stream s m Char => ParsecT s u m [[String]]
parser = line `sepEndBy` (some endOfLine) <* eof
where line = many (noneOf ",\n\r") `sepBy` char ','
and looks like it works:
\> parse parser "" "1,2,3\n\n\n4,5,6"
Right [["1","2","3"],["4","5","6"]]
but if i change some
to many
:
parser :: Stream s m Char => ParsecT s u m [[String]]
parser = line `sepEndBy` (many endOfLine) <* eof
where line = many (noneOf ",\n\r") `sepBy` char ','
it will never return:
\> parse parser "" "1,2,3\n\n\n4,5,6"
^CInterrupted.
This parser gets stuck at the end of input iterating endlessly, that's why it never returns. Follow the second version in your mind step by step and you will see why. Hint: many
includes zero occurences and succeeds even when there is nothing its argument can match.