HORIZON HASKELLDocslts/ghc-9.10.xc74966e2026-09-27Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · c74966e · 2026-09-27

Modulemegaparsec-9.7.0Haskell2010

Text.Megaparsec.Debug

Debugging helpers.

  • 1 class
  • 1 value
  • Packagemegaparsec-9.7.0
  • Exports2
  • LanguageHaskell2010
  • LicenceBSD-2-Clause
  • SourceDebug.hs
classclass MonadParsec e s m => MonadParsecDbg e s (m :: Type -> Type) where
#

Type class describing parser monads that can trace during evaluation.

Methods

  • dbg :: Show a => String -> m a -> m a

    dbg label p parser works exactly like p, but when it's evaluated it prints information useful for debugging. The label is only used to refer to this parser in the debugging output. This combinator uses the trace function from Debug.Trace under the hood.

    Typical usage is to wrap every sub-parser in misbehaving parser with dbg assigning meaningful labels. Then give it a shot and go through the print-out. As of current version, this combinator prints all available information except for hints, which are probably only interesting to the maintainer of Megaparsec itself and may be quite verbose to output in general. Let me know if you would like to be able to see hints in the debugging output.

    The output itself is pretty self-explanatory, although the following abbreviations should be clarified (they are derived from the low-level source code):

    • COK—“consumed OK”. The parser consumed input and succeeded.

    • CERR—“consumed error”. The parser consumed input and failed.

    • EOK—“empty OK”. The parser succeeded without consuming input.

    • EERR—“empty error”. The parser failed without consuming input.

    Note: up until the version 9.3.0 this was a non-polymorphic function that worked only in ParsecT. It was first introduced in the version 7.0.0.

Instances9MonadParsecDbg, …
  • MonadParsecDbg e s m => MonadParsecDbg e s (IdentityT m)Defined in megaparsec-9.7.0 · Text.Megaparsec.Debug
  • MonadParsecDbg e s m => MonadParsecDbg e s (ReaderT r m)Defined in megaparsec-9.7.0 · Text.Megaparsec.Debug
  • (Monoid w, Show w, MonadParsecDbg e s m) => MonadParsecDbg e s (WriterT w m)Defined in megaparsec-9.7.0 · Text.Megaparsec.Debug

    dbg (p :: WriterT st m) prints only log produced by p:

    Example2 expressions
    p = tell [0] >> dbg "a" (single 'a' >> tell [1])parseTest (runWriterT p) "a"a> IN: 'a'a> MATCH (COK): 'a'a> VALUE: () (LOG: [1])((),[0,1])
  • (Monoid w, Show w, MonadParsecDbg e s m) => MonadParsecDbg e s (WriterT w m)Defined in megaparsec-9.7.0 · Text.Megaparsec.Debug

    dbg (p :: WriterT st m) prints only log produced by p:

    Example2 expressions
    p = tell [0] >> dbg "a" (single 'a' >> tell [1])parseTest (runWriterT p) "a"a> IN: 'a'a> MATCH (COK): 'a'a> VALUE: () (LOG: [1])((),[0,1])
  • (Show st, MonadParsecDbg e s m) => MonadParsecDbg e s (StateT st m)Defined in megaparsec-9.7.0 · Text.Megaparsec.Debug

    dbg (p :: StateT st m) prints state after running p:

    Example2 expressions
    p = modify succ >> dbg "a" (single 'a' >> modify succ)parseTest (runStateT p 0) "a"a> IN: 'a'a> MATCH (COK): 'a'a> VALUE: () (STATE: 2)((),2)
  • (Show st, MonadParsecDbg e s m) => MonadParsecDbg e s (StateT st m)Defined in megaparsec-9.7.0 · Text.Megaparsec.Debug

    dbg (p :: StateT st m) prints state after running p:

    Example2 expressions
    p = modify succ >> dbg "a" (single 'a' >> modify succ)parseTest (runStateT p 0) "a"a> IN: 'a'a> MATCH (COK): 'a'a> VALUE: () (STATE: 2)((),2)
  • (VisualStream s, ShowErrorComponent e) => MonadParsecDbg e s (ParsecT e s m)Defined in megaparsec-9.7.0 · Text.Megaparsec.Debug
  • (Monoid w, Show w, Show st, MonadParsecDbg e s m) => MonadParsecDbg e s (RWST r w st m)Defined in megaparsec-9.7.0 · Text.Megaparsec.Debug

    RWST works like StateT inside a WriterT: subparser's log and its final state is printed:

    Example2 expressions
    p = tell [0] >> modify succ >> dbg "a" (single 'a' >> tell [1] >> modify succ)parseTest (runRWST p () 0) "a"a> IN: 'a'a> MATCH (COK): 'a'a> VALUE: () (STATE: 2) (LOG: [1])((),2,[0,1])
  • (Monoid w, Show w, Show st, MonadParsecDbg e s m) => MonadParsecDbg e s (RWST r w st m)Defined in megaparsec-9.7.0 · Text.Megaparsec.Debug

    RWST works like StateT inside a WriterT: subparser's log and its final state is printed:

    Example2 expressions
    p = tell [0] >> modify succ >> dbg "a" (single 'a' >> tell [1] >> modify succ)parseTest (runRWST p () 0) "a"a> IN: 'a'a> MATCH (COK): 'a'a> VALUE: () (STATE: 2) (LOG: [1])((),2,[0,1])
valuedbg'
  1. :: MonadParsecDbg e s m
  2. => String

    Debugging label

  3. -> m a

    Parser to debug

  4. -> m a

    Parser that prints debugging messages

#

Just like dbg, but doesn't require the return value of the parser to be Show-able.