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 Source #

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

data Reply e s a Source #

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

data Consumption Source #

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 Source #

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

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

``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) Source # |

Instance details

Defined in Text.Megaparsec.Internal

Methods

parseError :: ParseError s e -> ParsecT e s m a Source #

label :: String -> ParsecT e s m a -> ParsecT e s m a Source #

hidden :: ParsecT e s m a -> ParsecT e s m a Source #

try :: ParsecT e s m a -> ParsecT e s m a Source #

lookAhead :: ParsecT e s m a -> ParsecT e s m a Source #

notFollowedBy :: ParsecT e s m a -> ParsecT e s m () Source #

withRecovery :: (ParseError s e -> ParsecT e s m a) -> ParsecT e s m a -> ParsecT e s m a Source #

observing :: ParsecT e s m a -> ParsecT e s m (Either (ParseError s e) a) Source #

eof :: ParsecT e s m () Source #

token :: (Token s -> Maybe a) -> Set (ErrorItem (Token s)) -> ParsecT e s m a Source #

tokens :: (Tokens s -> Tokens s -> Bool) -> Tokens s -> ParsecT e s m (Tokens s) Source #

takeWhileP :: Maybe String -> (Token s -> Bool) -> ParsecT e s m (Tokens s) Source #

takeWhile1P :: Maybe String -> (Token s -> Bool) -> ParsecT e s m (Tokens s) Source #

takeP :: Maybe String -> Int -> ParsecT e s m (Tokens s) Source #

getParserState :: ParsecT e s m (State s e) Source #

updateParserState :: (State s e -> State s e) -> ParsecT e s m () Source #

mkParsec :: (State s e -> Reply e s a) -> ParsecT e s m a Source #

(VisualStream s, ShowErrorComponent e) => MonadParsecDbg e s (ParsecT e s m) Source # |
Instance details Defined in Text.Megaparsec.Debug Methods dbg :: Show a => String -> ParsecT e s m a -> ParsecT e s m a Source #
(Stream s, MonadError e' m) => MonadError e' (ParsecT e s m) Source # |
Instance details Defined in Text.Megaparsec.Internal Methods throwError :: e' -> ParsecT e s m a Source # catchError :: ParsecT e s m a -> (e' -> ParsecT e s m a) -> ParsecT e s m a Source #
(Stream s, MonadReader r m) => MonadReader r (ParsecT e s m) Source # |
Instance details Defined in Text.Megaparsec.Internal Methods ask :: ParsecT e s m r Source # local :: (r -> r) -> ParsecT e s m a -> ParsecT e s m a Source # reader :: (r -> a) -> ParsecT e s m a Source #
(Stream s, MonadState st m) => MonadState st (ParsecT e s m) Source # |
Instance details Defined in Text.Megaparsec.Internal Methods get :: ParsecT e s m st Source # put :: st -> ParsecT e s m () Source # state :: (st -> (a, st)) -> ParsecT e s m a Source #
(Stream s, MonadWriter w m) => MonadWriter w (ParsecT e s m) Source # | Since: 9.5.0
Instance details Defined in Text.Megaparsec.Internal Methods writer :: (a, w) -> ParsecT e s m a Source # tell :: w -> ParsecT e s m () Source # listen :: ParsecT e s m a -> ParsecT e s m (a, w) Source # pass :: ParsecT e s m (a, w -> w) -> ParsecT e s m a Source #
Stream s => MonadTrans (ParsecT e s) Source # |
Instance details Defined in Text.Megaparsec.Internal Methods lift :: Monad m => m a -> ParsecT e s m a Source #
(Stream s, MonadIO m) => MonadIO (ParsecT e s m) Source # |
Instance details Defined in Text.Megaparsec.Internal Methods liftIO :: IO a -> ParsecT e s m a Source #
(Ord e, Stream s) => Alternative (ParsecT e s m) Source # | `empty` is a parser that fails without consuming input.
Instance details Defined in Text.Megaparsec.Internal Methods empty :: ParsecT e s m a Source # (<|>) :: ParsecT e s m a -> ParsecT e s m a -> ParsecT e s m a Source # some :: ParsecT e s m a -> ParsecT e s m [a] Source # many :: ParsecT e s m a -> ParsecT e s m [a] Source #
Stream s => Applicative (ParsecT e s m) Source # | `pure` returns a parser that succeeds without consuming input.

Instance details

Defined in Text.Megaparsec.Internal

Methods

pure :: a -> ParsecT e s m a Source #

(<*>) :: ParsecT e s m (a -> b) -> ParsecT e s m a -> ParsecT e s m b Source #

liftA2 :: (a -> b -> c) -> ParsecT e s m a -> ParsecT e s m b -> ParsecT e s m c Source #

(*>) :: ParsecT e s m a -> ParsecT e s m b -> ParsecT e s m b Source #

(<*) :: ParsecT e s m a -> ParsecT e s m b -> ParsecT e s m a Source #

Functor (ParsecT e s m) Source # |
Instance details Defined in Text.Megaparsec.Internal Methods fmap :: (a -> b) -> ParsecT e s m a -> ParsecT e s m b Source # (<$) :: a -> ParsecT e s m b -> ParsecT e s m a Source #
Stream s => Monad (ParsecT e s m) Source # | `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 Source # (>>) :: ParsecT e s m a -> ParsecT e s m b -> ParsecT e s m b Source # return :: a -> ParsecT e s m a Source #

(Ord e, Stream s) => MonadPlus (ParsecT e s m) Source #

`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 Source # mplus :: ParsecT e s m a -> ParsecT e s m a -> ParsecT e s m a Source #
Stream s => MonadFail (ParsecT e s m) Source # |
Instance details Defined in Text.Megaparsec.Internal Methods fail :: String -> ParsecT e s m a Source #
(Stream s, MonadFix m) => MonadFix (ParsecT e s m) Source # | Since: 6.0.0
Instance details Defined in Text.Megaparsec.Internal Methods mfix :: (a -> ParsecT e s m a) -> ParsecT e s m a Source #
(Stream s, MonadCont m) => MonadCont (ParsecT e s m) Source # |
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 Source #
(Stream s, Monoid a) => Monoid (ParsecT e s m a) Source # | Since: 5.3.0
Instance details Defined in Text.Megaparsec.Internal Methods mempty :: ParsecT e s m a Source # mappend :: ParsecT e s m a -> ParsecT e s m a -> ParsecT e s m a Source # mconcat :: [ParsecT e s m a] -> ParsecT e s m a Source #
(Stream s, Semigroup a) => Semigroup (ParsecT e s m a) Source # | 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 Source # sconcat :: NonEmpty (ParsecT e s m a) -> ParsecT e s m a Source # stimes :: Integral b => b -> ParsecT e s m a -> ParsecT e s m a Source #
(a ~ Tokens s, IsString a, Eq a, Stream s, Ord e) => IsString (ParsecT e s m a) Source # | Since: 6.3.0
Instance details Defined in Text.Megaparsec.Internal Methods fromString :: String -> ParsecT e s m a Source #

# Helper functions

toHints Source #

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 Source #

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 Source #

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 Source #

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

runParsecT Source #

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 Source #

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