Parsec Documentation

repository·master·Indexed 21 days ago

https://github.com/haskell/parsec

An industrial-strength monadic parser combinator library for Haskell designed for simplicity, safety, and speed. It provides extensive libraries and high-quality error messages for parsing text.

Tokens
378
Snippets
2
Records
2
Agent score
25%

What's inside Parsec

  1. Install Parsec via cabal

    master

    To use Parsec in your Haskell project, install it using the cabal package manager. This requires a working installation of Haskell (such as the Haskell Platform) which includes cabal and ghci.

    cabal install parsec
  2. Quickstart: Create a simple parser in GHCI

    master

    You can test Parsec interactively using ghci. To start, load the Text.Parsec module using the :m + command.

    In the example below, we define a recursive parser parenSet that matches balanced parentheses and use the parse function to run it against input strings.

    • Right () indicates a successful parse.
    • Left (line, column): error_message indicates a parse failure with location details.
    Prelude> :m +Text.Parsec
    Prelude Text.Parsec> let parenSet = char '(' >> many parenSet >> char ')' :: Parsec String () Char
    Prelude Text.Parsec> let parens = (many parenSet >> eof) <|> eof
    Prelude Text.Parsec> parse parens "" "()"
    Right ()
    Prelude Text.Parsec> parse parens "" "()(())"
    Right ()
    Prelude Text.Parsec> parse parens "" "("
    Left (line 1, column 2):
    unexpected end of input
    expecting "(" or ")"