Problem with incomplete input when using Attoparsec

后端 未结 3 517
梦毁少年i
梦毁少年i 2021-01-12 16:16

I am converting some functioning Haskell code that uses Parsec to instead use Attoparsec in the hope of getting better performance. I have made the changes and everything c

3条回答
  •  独厮守ぢ
    2021-01-12 17:08

    I've run into this problem before and my understanding is that it's caused by the way that <|> works in the definition of sepBy:

    sepBy1 :: Alternative f => f a -> f s -> f [a]
    sepBy1 p s = scan
        where scan = liftA2 (:) p ((s *> scan) <|> pure [])
    

    This will only move to pure [] once (s *> scan) has failed, which won't happen just because you're at the end of the input.

    My solution has been just to call feed with an empty ByteString on the Result returned by parse. This might be kind of a hack, but it also seems to be how attoparsec-iteratee deals with the issue:

    f k (EOF Nothing)  = finalChunk $ feed (k S.empty) S.empty
    

    As far as I can tell this is the only reason that attoparsec-iteratee works here and plain old parse doesn't.

提交回复
热议问题