parsec icon indicating copy to clipboard operation
parsec copied to clipboard

parser never returns

Open behzadnouri opened this issue 7 years ago • 1 comments

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.

behzadnouri avatar Sep 24 '16 23:09 behzadnouri

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.

mrkkrp avatar Sep 24 '16 23:09 mrkkrp