Megaparsec tutorial
Megaparsec tutorial ๊ด๋ จ
Info
This is the Megaparsec tutorial which originally was written as a chapter for the Intermediate Haskell book. Due to lack of progress with the book in the last year, other authors agreed to let me publish the text as a standalone tutorial so that people can benefit at least from this part of our work.
Table of Contents
- 01. ParsecT and Parsec monads
- 02. Character and binary streams
- 03. Monadic and applicative syntax
- 04. Forcing consumption of input with eof
- 05. Working with alternatives
- 06. Controlling backtracking with try
- 07. Debugging parsers
- 08. Labeling and hiding things
- 09. Running a parser
- 10. The MonadParsec type class
- 11. Lexing
- 12. notFollowedBy and lookAhead
- 13. Parsing expressions
- 14. Indentation-sensitive parsing
- 15. Writing efficient parsers
- 16. Parse errors
- 17. Testing Megaparsec parsers
- 18. Working with custom input streams
The toy parser combinators developed in chapter โAn Example: Writing Your Own Parser Combinatorsโ are not suitable for real-world use, so letโs continue by taking a look at the libraries in the Haskell ecosystem that solve the same problem, and note various trade-offs they make:
parsec
has been the โdefaultโ parsing library in Haskell for a long time. The library is said to be focused on quality of error messages. It however does not have good test coverage and is currently in maintenance mode.attoparsec
is a robust, fast parsing library with focus on performance. It is the only library from this list that has full support for incremental parsing. Its downsides are poor quality of error messages, inability to be used as a monad transformer, and limited set of types that can be used as input stream.trifecta
features good error messages but is under-documented and hard to figure out. It can parseString
andByteString
out-of-the-box, but notText
.megaparsec
is a fork ofparsec
that has been actively developed in the last few years. The current version tries to find a nice balance between speed, flexibility, and quality of parse errors. As an unofficial successor ofparsec
, it stays conventional and immediately familiar for users who have used that library or who have readparsec
tutorials.
It would be impractical to try to cover all these libraries, and so we will focus on megaparsec
. More precisely, we are going to cover the version 9, which by the time this book is published will probably have replaced the older versions almost everywhere.