HORIZON HASKELLDocslts/ghc-9.10.x248f8f02026-10-05Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · 248f8f0 · 2026-10-05

Modulepolyparse-1.13Haskell98

Text.ParserCombinators.HuttonMeijerWallace

This library of monadic parser combinators is based on the ones defined by Graham Hutton and Erik Meijer. It has been extended by Malcolm Wallace to use an abstract token type (no longer just a string) as input, and to incorporate state in the monad, useful for symbol tables, macros, and so on. Basic facilities for error reporting have also been added, and later extended by Graham Klyne to return the errors through an Either type, rather than just calling error.

  • 1 type
  • 23 values
  • Packagepolyparse-1.13
  • Exports24
  • LanguageHaskell98
  • LicenceLicenseRef-LGPL
  • SourceHuttonMeijerWallace.hs

The parser monad

1 declaration
newtypenewtype Parser s t e a
#

Constructors

  • P (s -> [Either e t] -> ParseResult s t e a)

    The parser type is parametrised on the types of the state s, the input tokens t, error-type e, and the result value a. The state and remaining input are threaded through the monad.

Instances6Monad, Functor, MonadFail, Applicative, Alternative, MonadPlus
  • Monad (Parser s t e)Defined in polyparse-1.13 · Text.ParserCombinators.HuttonMeijerWallace
  • Functor (Parser s t e)Defined in polyparse-1.13 · Text.ParserCombinators.HuttonMeijerWallace
  • MonadFail (Parser s t e)Defined in polyparse-1.13 · Text.ParserCombinators.HuttonMeijerWallace
  • Applicative (Parser s t e)Defined in polyparse-1.13 · Text.ParserCombinators.HuttonMeijerWallace
  • Alternative (Parser s t e)Defined in polyparse-1.13 · Text.ParserCombinators.HuttonMeijerWallace
  • MonadPlus (Parser s t e)Defined in polyparse-1.13 · Text.ParserCombinators.HuttonMeijerWallace

Primitive parser combinators

4 declarations
valueitem :: Parser s t e t
#

Deliver the first remaining token.

valuepapply
  1. :: Parser s t String a
  2. -> s
  3. -> [Either String t]
  4. -> [(a, s, [Either String t])]
#

Apply the parser to some real input, given an initial state value. If the parser fails, raise error to halt the program. (This is the original exported behaviour - to allow the caller to deal with the error differently, see papply'.)

valuepapply'
  1. :: Parser s t e a
  2. -> s
  3. -> [Either e t]
  4. -> Either e [(a, s, [Either e t])]
#

Apply the parser to some real input, given an initial state value. If the parser fails, return a diagnostic message to the caller.

Derived combinators

14 declarations
value(+++) :: Parser s t e a -> Parser s t e a -> Parser s t e a
#

A choice between parsers. Keep only the first success.

valuetok :: Eq t => t -> Parser s (p, t) e t
#

Deliver the first token if it equals the argument.

valuenottok :: Eq t => [t] -> Parser s (p, t) e t
#

Deliver the first token if it does not equal the argument.

valuemany :: Parser s t e a -> Parser s t e [a]
#

Deliver zero or more values of a.

valuesepby :: Parser s t e a -> Parser s t e b -> Parser s t e [a]
#

Deliver zero or more values of a separated by b's.

Error handling

1 declaration

State handling

3 declarations
valuestupd :: (s -> s) -> Parser s t e ()
#

Update the internal state.

valuestquery :: (s -> a) -> Parser s t e a
#

Query the internal state.

valuestget :: Parser s t e s
#

Deliver the entire internal state.

Re-parsing

1 declaration
valuereparse :: [Either e t] -> Parser s t e ()
#

This is useful for recursively expanding macros. When the user-parser recognises a macro use, it can lookup the macro expansion from the parse state, lex it, and then stuff the lexed expansion back down into the parser.