megaparsec-9.7.0: Monadic parser combinators

Copyright | © 2015–present Megaparsec contributors © 2007 Paolo Martini © 1999–2001 Daan Leijen
License | FreeBSD
Maintainer | Mark Karpov <markkarpov92@gmail.com>
Stability | experimental
Portability | portable
Safe Haskell | Safe
Language | Haskell2010

Text.Megaparsec.Internal

Contents

- Data types
- Helper functions

Description

Internal definitions. Versioning rules do not apply here. Please do not rely on these unless you really know what you're doing.

Since: 6.5.0

Synopsis

- newtype Hints t = Hints (Set (ErrorItem t))
- data Reply e s a = Reply (State s e) Consumption (Result s e a)
- data Consumption
  - = Consumed
  - | NotConsumed
- data Result s e a
  - = OK (Hints (Token s)) a
  - | Error (ParseError s e)
- newtype ParsecT e s (m :: Type -> Type) a = ParsecT {
  - unParser :: forall b. State s e -> (a -> State s e -> Hints (Token s) -> m b) -> (ParseError s e -> State s e -> m b) -> (a -> State s e -> Hints (Token s) -> m b) -> (ParseError s e -> State s e -> m b) -> m b
  }
- toHints :: Stream s => Int -> ParseError s e -> Hints (Token s)
- withHints :: Stream s => Hints (Token s) -> (ParseError s e -> State s e -> m b) -> ParseError s e -> State s e -> m b
- accHints :: Stream s => Hints (Token s) -> (a -> State s e -> Hints (Token s) -> m b) -> a -> State s e -> Hints (Token s) -> m b
- refreshHints :: Hints t -> Maybe (ErrorItem t) -> Hints t
- runParsecT :: Monad m => ParsecT e s m a -> State s e -> m (Reply e s a)
- withParsecT :: forall e e' s (m :: Type -> Type) a. Ord e' => (e -> e') -> ParsecT e s m a -> ParsecT e' s m a

# Data types

newtype Hints t #

`Hints` represent a collection of `ErrorItem`s to be included into `ParseError` (when it's a `TrivialError`) as “expected” message items when a parser fails without consuming input right after successful parser that produced the hints.

For example, without hints you could get:

    >>> parseTest (many (char 'r') <* eof) "ra"
    1:2:
    unexpected 'a'
    expecting end of input

We're getting better error messages with the help of hints:

    >>> parseTest (many (char 'r') <* eof) "ra"
    1:2:
    unexpected 'a'
    expecting 'r' or end of input

Constructors

Hints (Set (ErrorItem t)) |

#### Instances

Instances details

Ord t => Monoid (Hints t) # |
Instance details Defined in Text.Megaparsec.Internal Methods mempty :: Hints t # mappend :: Hints t -> Hints t -> Hints t # mconcat :: [Hints t] -> Hints t #
Ord t => Semigroup (Hints t) # |
Instance details Defined in Text.Megaparsec.Internal Methods (<>) :: Hints t -> Hints t -> Hints t # sconcat :: NonEmpty (Hints t) -> Hints t # stimes :: Integral b => b -> Hints t -> Hints t #

data Reply e s a #

All information available after parsing. This includes consumption of input, success (with the returned value) or failure (with the parse error), and parser state at the end of parsing. `Reply` can also be used to resume parsing.

See also: `Consumption`, `Result`.

Constructors

Reply (State s e) Consumption (Result s e a) |

#### Instances

Instances details

Functor (Reply e s) # |
Instance details Defined in Text.Megaparsec.Internal Methods fmap :: (a -> b) -> Reply e s a -> Reply e s b # (<$) :: a -> Reply e s b -> Reply e s a #

data Consumption #

Whether the input has been consumed or not.

See also: `Result`, `Reply`.

Constructors

Consumed | Some part of input stream was consumed
NotConsumed | No input was consumed

data Result s e a #

Whether the parser has failed or not. On success we include the resulting value, on failure we include a `ParseError`.

See also: `Consumption`, `Reply`.

Constructors

OK (Hints (Token s)) a | Parser succeeded (includes hints)
Error (ParseError s e) | Parser failed

#### Instances

Instances details

Functor (Result s e) # |
Instance details Defined in Text.Megaparsec.Internal Methods fmap :: (a -> b) -> Result s e a -> Result s e b # (<$) :: a -> Result s e b -> Result s e a #

newtype ParsecT e s (m :: Type -> Type) a #

``ParsecT` e s m a` is a parser with custom data component of error `e`, stream type `s`, underlying monad `m` and return type `a`.

Constructors

ParsecT |
Fields unParser :: forall b. State s e -> (a -> State s e -> Hints (Token s) -> m b) -> (ParseError s e -> State s e -> m b) -> (a -> State s e -> Hints (Token s) -> m b) -> (ParseError s e -> State s e -> m b) -> m b

#### Instances

Instances details

(Ord e, Stream s) => MonadParsec e s (ParsecT e s m) # |
Instance details Defined in Text.Megaparsec.Internal Methods parseError :: ParseError s e -> ParsecT e s m a # label :: String -> ParsecT e s m a -> ParsecT e s m a # hidden :: ParsecT e s m a -> ParsecT e s m a # try :: ParsecT e s m a -> ParsecT e s m a # lookAhead :: ParsecT e s m a -> ParsecT e s m a # notFollowedBy :: ParsecT e s m a -> ParsecT e s m () # withRecovery :: (ParseError s e -> ParsecT e s m a) -> ParsecT e s m a -> ParsecT e s m a # observing :: ParsecT e s m a -> ParsecT e s m (Either (ParseError s e) a) # eof :: ParsecT e s m () # token :: (Token s -> Maybe a) -> Set (ErrorItem (Token s)) -> ParsecT e s m a # tokens :: (Tokens s -> Tokens s -> Bool) -> Tokens s -> ParsecT e s m (Tokens s) # takeWhileP :: Maybe String -> (Token s -> Bool) -> ParsecT e s m (Tokens s) # takeWhile1P :: Maybe String -> (Token s -> Bool) -> ParsecT e s m (Tokens s) # takeP :: Maybe String -> Int -> ParsecT e s m (Tokens s) # getParserState :: ParsecT e s m (State s e) # updateParserState :: (State s e -> State s e) -> ParsecT e s m () # mkParsec :: (State s e -> Reply e s a) -> ParsecT e s m a #
(VisualStream s, ShowErrorComponent e) => MonadParsecDbg e s (ParsecT e s m) # |
Instance details Defined in Text.Megaparsec.Debug Methods dbg :: Show a => String -> ParsecT e s m a -> ParsecT e s m a #
(Stream s, MonadError e' m) => MonadError e' (ParsecT e s m) # |
Instance details Defined in Text.Megaparsec.Internal Methods throwError :: e' -> ParsecT e s m a # catchError :: ParsecT e s m a -> (e' -> ParsecT e s m a) -> ParsecT e s m a #
(Stream s, MonadReader r m) => MonadReader r (ParsecT e s m) # |
Instance details Defined in Text.Megaparsec.Internal Methods ask :: ParsecT e s m r # local :: (r -> r) -> ParsecT e s m a -> ParsecT e s m a # reader :: (r -> a) -> ParsecT e s m a #
(Stream s, MonadState st m) => MonadState st (ParsecT e s m) # |
Instance details Defined in Text.Megaparsec.Internal Methods get :: ParsecT e s m st # put :: st -> ParsecT e s m () # state :: (st -> (a, st)) -> ParsecT e s m a #
(Stream s, MonadWriter w m) => MonadWriter w (ParsecT e s m) # | Since: 9.5.0
Instance details Defined in Text.Megaparsec.Internal Methods writer :: (a, w) -> ParsecT e s m a # tell :: w -> ParsecT e s m () # listen :: ParsecT e s m a -> ParsecT e s m (a, w) # pass :: ParsecT e s m (a, w -> w) -> ParsecT e s m a #
Stream s => MonadTrans (ParsecT e s) # |
Instance details Defined in Text.Megaparsec.Internal Methods lift :: Monad m => m a -> ParsecT e s m a #
(Stream s, MonadIO m) => MonadIO (ParsecT e s m) # |
Instance details Defined in Text.Megaparsec.Internal Methods liftIO :: IO a -> ParsecT e s m a #
(Ord e, Stream s) => Alternative (ParsecT e s m) # | `empty` is a parser that fails without consuming input.
Instance details Defined in Text.Megaparsec.Internal Methods empty :: ParsecT e s m a # (<|>) :: ParsecT e s m a -> ParsecT e s m a -> ParsecT e s m a # some :: ParsecT e s m a -> ParsecT e s m [a] # many :: ParsecT e s m a -> ParsecT e s m [a] #
Stream s => Applicative (ParsecT e s m) # | `pure` returns a parser that succeeds without consuming input.
Instance details Defined in Text.Megaparsec.Internal Methods pure :: a -> ParsecT e s m a # (<*>) :: ParsecT e s m (a -> b) -> ParsecT e s m a -> ParsecT e s m b # liftA2 :: (a -> b -> c) -> ParsecT e s m a -> ParsecT e s m b -> ParsecT e s m c # (*>) :: ParsecT e s m a -> ParsecT e s m b -> ParsecT e s m b # (<*) :: ParsecT e s m a -> ParsecT e s m b -> ParsecT e s m a #
Functor (ParsecT e s m) # |
Instance details Defined in Text.Megaparsec.Internal Methods fmap :: (a -> b) -> ParsecT e s m a -> ParsecT e s m b # (<$) :: a -> ParsecT e s m b -> ParsecT e s m a #
Stream s => Monad (ParsecT e s m) # | `return` returns a parser that succeeds without consuming input.
Instance details Defined in Text.Megaparsec.Internal Methods (>>=) :: ParsecT e s m a -> (a -> ParsecT e s m b) -> ParsecT e s m b # (>>) :: ParsecT e s m a -> ParsecT e s m b -> ParsecT e s m b # return :: a -> ParsecT e s m a #
(Ord e, Stream s) => MonadPlus (ParsecT e s m) # | `mzero` is a parser that fails without consuming input. Note: strictly speaking, this instance is unlawful. The right identity law does not hold, e.g. in general this is not true:
    v >> mzero = mzero
However the following holds:
    try v >> mzero = mzero
Instance details Defined in Text.Megaparsec.Internal Methods mzero :: ParsecT e s m a # mplus :: ParsecT e s m a -> ParsecT e s m a -> ParsecT e s m a #
Stream s => MonadFail (ParsecT e s m) # |
Instance details Defined in Text.Megaparsec.Internal Methods fail :: String -> ParsecT e s m a #
(Stream s, MonadFix m) => MonadFix (ParsecT e s m) # | Since: 6.0.0
Instance details Defined in Text.Megaparsec.Internal Methods mfix :: (a -> ParsecT e s m a) -> ParsecT e s m a #
(Stream s, MonadCont m) => MonadCont (ParsecT e s m) # |
Instance details Defined in Text.Megaparsec.Internal Methods callCC :: ((a -> ParsecT e s m b) -> ParsecT e s m a) -> ParsecT e s m a #
(Stream s, Monoid a) => Monoid (ParsecT e s m a) # | Since: 5.3.0
Instance details Defined in Text.Megaparsec.Internal Methods mempty :: ParsecT e s m a # mappend :: ParsecT e s m a -> ParsecT e s m a -> ParsecT e s m a # mconcat :: [ParsecT e s m a] -> ParsecT e s m a #
(Stream s, Semigroup a) => Semigroup (ParsecT e s m a) # | Since: 5.3.0
Instance details Defined in Text.Megaparsec.Internal Methods (<>) :: ParsecT e s m a -> ParsecT e s m a -> ParsecT e s m a # sconcat :: NonEmpty (ParsecT e s m a) -> ParsecT e s m a # stimes :: Integral b => b -> ParsecT e s m a -> ParsecT e s m a #
(a ~ Tokens s, IsString a, Eq a, Stream s, Ord e) => IsString (ParsecT e s m a) # | Since: 6.3.0
Instance details Defined in Text.Megaparsec.Internal Methods fromString :: String -> ParsecT e s m a #

# Helper functions

toHints #

Arguments

:: Stream s |
=> Int | Current offset in input stream
-> ParseError s e | Parse error to convert
-> Hints (Token s) |

Convert a `ParseError` record into `Hints`.

withHints #

Arguments

:: Stream s |
=> Hints (Token s) | Hints to use
-> (ParseError s e -> State s e -> m b) | Continuation to influence
-> ParseError s e | First argument of resulting continuation
-> State s e | Second argument of resulting continuation
-> m b |

``withHints` hs c` makes “error” continuation `c` use given hints `hs`.

Note that if resulting continuation gets `ParseError` that has custom data in it, hints are ignored.

accHints #

Arguments

:: Stream s |
=> Hints (Token s) | `Hints` to add
-> (a -> State s e -> Hints (Token s) -> m b) | An “OK” continuation to alter
-> a | Altered “OK” continuation
-> State s e |
-> Hints (Token s) |
-> m b |

``accHints` hs c` results in “OK” continuation that will add given hints `hs` to third argument of original continuation `c`.

refreshHints :: Hints t -> Maybe (ErrorItem t) -> Hints t #

Replace the hints with the given `ErrorItem` (or delete it if `Nothing` is given). This is used in the `label` primitive.

runParsecT #

Arguments

:: Monad m |
=> ParsecT e s m a | Parser to run
-> State s e | Initial state
-> m (Reply e s a) |

Low-level unpacking of the `ParsecT` type.

withParsecT #

Arguments

:: forall e e' s (m :: Type -> Type) a. Ord e' |
=> (e -> e') |
-> ParsecT e s m a | Inner parser
-> ParsecT e' s m a | Outer parser

Transform any custom errors thrown by the parser using the given function. Similar in function and purpose to `withExceptT`.

Note that the inner parser will start with an empty collection of “delayed” `ParseError`s. Any delayed `ParseError`s produced in the inner parser will be lifted by applying the provided function and added to the collection of delayed parse errors of the outer parser.

Since: 7.0.0