[Haskell-cafe] [ANN] faster-megaparsec

Olaf Klinke olf at aatal-apotheke.de
Tue Nov 8 20:31:47 UTC 2022


Dear Haskellers,

megaparsec [1] is a parsing library with excellent error
reporting. Being able to emit decent parse error messages is traded off
against speed, however. Wouldn't it be nice to have a faster parser for
your daily parsing needs, but obtain megaparsec's informative error
messages for the rare cases when things go wrong? 

Thanks to megaparsec's parser type class, MonadParsec, you can have
both: I wrote a MonadParsec instance for StateT s Maybe (s being the
input stream type such as Text or ByteString) which does not do all the
bookkeeping ParsecT needs to do. If you manage to write your parser
only using the type class primitives, then you can specialize your
parser to StateT s Maybe. Only if that returns Nothing specialize again
to ParsecT Void s and parse the input again, thus obtaining a decent
error message. The package faster-megaparsec [1,2] provides a
convenient function to do just that. 

Of course StateT s Maybe can not provide all the features ParsecT has:
* StateT s Maybe is always backtracking.
* It does not know the offset in the stream.
* It must hold the entire input stream in memory for a second pass, 
  so no garbage collecting the consumed input while parsing.
So if your parsing relies on non-backtracking choice, stream offset or
garbage-collecting input while parsing, you can not use faster-
megaparsec as a drop-in replacement. 

Happy parsing!
Olaf

[1] https://hackage.haskell.org/package/megaparsec
[2] https://hackage.haskell.org/package/faster-megaparsec 
[3] https://hub.darcs.net/olf/faster-megaparsec



More information about the Haskell-Cafe mailing list