haskell / attoparsec

A fast Haskell library for parsing ByteStrings
http://hackage.haskell.org/package/attoparsec
Other
512 stars 93 forks source link

Consider adding a version of `parseWith` with a more informative type #106

Open treeowl opened 9 years ago

treeowl commented 9 years ago

parseWith runs a parser and returns a Result. This Result will always be either Done or Fail, but the type leaves open the possibility of Partial. This means that library users must add "can't happen" errors for Partial, and some may get confused and think they need to do something in that case. I think it might make sense to add types IFinalResult and FinalResult or similar to represent a result known not to be partial. Then we could add a function to produce that:

parseWith' :: Monad m =>
             (m B.ByteString)
          -> I.Parser a
          -> B.ByteString
          -> m (FinalResult a)
parseWith' refill p s = step $ parse p s
  where
        step (T.Partial k) = (step . k) =<< refill
        step (T.Done i r) = return (Done' i r)
        step (T.Fail i contexts err) = return (Fail' i contexts err)

Yes, this will probably allocate an extra Done' constructor, at least sometimes, but I still think it's worth having.

chris-martin commented 1 year ago

I've just released a new package which adds some of the types that Attoparsec is missing, including FinalResult: https://hackage.haskell.org/package/attoparsec-run

Would be happy to submit a PR to Attoparsec and deprecate my package, if there were any indication that changes would be accepted. Since I haven't been able to get simple metadata corrections released here, I figured I should just go ahead and release on my own for now.