{-
    Copyright 2010-2020 Mario Blazevic
-}

-- | This module defines parsing combinators for incremental parsers.
-- 
-- The exported 'Parser' type can provide partial parsing results from partial input, as long as the output is a
-- 'Monoid'. Construct a parser using the primitives and combinators, supply it with input using functions 'feed' and
-- 'feedEof', and extract the parsed output using 'results'.
-- 
-- If your parser only ever uses the symmetric choice '<||>', import the "Text.ParserCombinators.Incremental.Symmetric"
-- module instead. Vice versa, if you always use the shortcutting '<<|>' choice, import
-- "Text.ParserCombinators.Incremental.LeftBiasedLocal" instead of this module.
-- 
-- Implementation is based on Brzozowski derivatives.

{-# LANGUAGE FlexibleContexts, FlexibleInstances, GADTs, RankNTypes, TypeFamilies, UndecidableInstances #-}

module Text.ParserCombinators.Incremental (
   -- * The Parser type
   Parser,
   -- * Using a Parser
   feed, feedEof, inspect, results, completeResults, resultPrefix,
   -- * Parser primitives
   failure, (<?>), more, eof, anyToken, token, satisfy, acceptAll, string, takeWhile, takeWhile1,
   -- ** Character primitives
   satisfyChar, takeCharsWhile, takeCharsWhile1,
   -- * Parser combinators
   count, skip, moptional, concatMany, concatSome, manyTill,
   (+<*>), (<||>), (<<|>), (><), lookAhead, notFollowedBy, and, andThen, record,
   -- * Parser mapping
   mapType, mapIncremental, mapInput, mapMaybeInput,
   -- * Utilities
   isInfallible, showWith, defaultMany, defaultSome
   )
where

import Control.Applicative (Applicative (pure, (<*>), (*>), (<*)), Alternative ((<|>)), (<$>))
import Control.Applicative.Monoid(MonoidApplicative(..), MonoidAlternative(..))
import Control.Monad.Fail (MonadFail(fail))
import Control.Monad.Fix (MonadFix(mfix))
import Control.Monad.Trans.List (ListT(ListT), runListT)
import Control.Monad.Trans.State.Strict (State, runState, state, StateT(StateT, runStateT))
import Data.Foldable (fold)
import Data.Maybe (fromMaybe)
import Data.Semigroup (Semigroup(..))
import Data.String (fromString)
import Data.Monoid (Monoid, mempty, mappend)
import Data.Monoid.Factorial (FactorialMonoid, length, span, splitAt, splitPrimePrefix, tails)
import Data.Monoid.Null (MonoidNull(null))
import Data.Monoid.Textual (TextualMonoid)
import qualified Data.Monoid.Textual as Textual
import Data.Semigroup.Cancellative (LeftReductive, isPrefixOf, stripPrefix)
import Text.Parser.Combinators (Parsing)
import Text.Parser.Char (CharParsing)
import Text.Parser.LookAhead (LookAheadParsing)
import Text.Parser.Deterministic (DeterministicParsing)
import Text.Parser.Input (InputParsing(take), InputCharParsing)
import qualified Text.Parser.Deterministic
import qualified Text.Parser.Input
import qualified Text.Parser.Combinators
import qualified Text.Parser.Char
import qualified Text.Parser.LookAhead
import qualified Rank2

import Prelude hiding (and, length, null, pred, span, splitAt, take, takeWhile)

-- | The central parser type. Its first parameter is the subtype of the parser, the second is the input monoid type, the
-- third the output type.
data Parser t s r where
   Failure :: String -> Parser t s r
   Result :: s -> r -> Parser t s r
   ResultPart :: (r -> r) -> Parser t s r -> (s -> Parser t s r) -> Parser t s r
   ResultStructure :: (Monoid s, Rank2.Traversable g, Applicative m) => Maybe s -> g (Parser t s) -> Parser t s (g m)
   Delay :: Parser t s r -> (s -> Parser t s r) -> Parser t s r
   Choice :: Parser t s r -> Parser t s r -> Parser t s r

-- | Feeds a chunk of the input to the parser.
feed :: Monoid s => s -> Parser t s r -> Parser t s r
feed :: s -> Parser t s r -> Parser t s r
feed s
s p :: Parser t s r
p@Failure{} = s
s s -> Parser t s r -> Parser t s r
`seq` Parser t s r
p
feed s
s (Result s
s' r
r) = s -> r -> Parser t s r
forall s r t. s -> r -> Parser t s r
Result (s -> s -> s
forall a. Monoid a => a -> a -> a
mappend s
s' s
s) r
r
feed s
s (ResultPart r -> r
r Parser t s r
_ s -> Parser t s r
f) = (r -> r) -> Parser t s r -> Parser t s r
forall s r t. Monoid s => (r -> r) -> Parser t s r -> Parser t s r
resultPart r -> r
r (s -> Parser t s r
f s
s)
feed s
s (Choice Parser t s r
p1 Parser t s r
p2) = s -> Parser t s r -> Parser t s r
forall s t r. Monoid s => s -> Parser t s r -> Parser t s r
feed s
s Parser t s r
p1 Parser t s r -> Parser t s r -> Parser t s r
forall t s r. Parser t s r -> Parser t s r -> Parser t s r
<||> s -> Parser t s r -> Parser t s r
forall s t r. Monoid s => s -> Parser t s r -> Parser t s r
feed s
s Parser t s r
p2
feed s
s (Delay Parser t s r
_ s -> Parser t s r
f) = s -> Parser t s r
f s
s
feed s
s (ResultStructure Maybe s
s' g (Parser t s)
r) = Maybe s -> g (Parser t s) -> Parser t s (g m)
forall s (g :: (* -> *) -> *) (m :: * -> *) t.
(Monoid s, Traversable g, Applicative m) =>
Maybe s -> g (Parser t s) -> Parser t s (g m)
ResultStructure (Maybe s
s'' Maybe s -> Maybe s -> Maybe s
forall a. Semigroup a => a -> a -> a
<> Maybe s
s') g (Parser t s)
r'
   where (g (Parser t s)
r', Maybe s
s'') = State (Maybe s) (g (Parser t s))
-> Maybe s -> (g (Parser t s), Maybe s)
forall s a. State s a -> s -> (a, s)
runState ((forall a.
 Parser t s a -> StateT (Maybe s) Identity (Parser t s a))
-> g (Parser t s) -> State (Maybe s) (g (Parser t s))
forall k (g :: (k -> *) -> *) (m :: * -> *) (p :: k -> *)
       (q :: k -> *).
(Traversable g, Applicative m) =>
(forall (a :: k). p a -> m (q a)) -> g p -> m (g q)
Rank2.traverse forall a. Parser t s a -> StateT (Maybe s) Identity (Parser t s a)
forall s t r.
Monoid s =>
Parser t s r -> State (Maybe s) (Parser t s r)
feedMaybe g (Parser t s)
r) (s -> Maybe s
forall a. a -> Maybe a
Just s
s)

feedMaybe :: Monoid s => Parser t s r -> State (Maybe s) (Parser t s r)
feedMaybe :: Parser t s r -> State (Maybe s) (Parser t s r)
feedMaybe Parser t s r
p = (Maybe s -> (Parser t s r, Maybe s))
-> State (Maybe s) (Parser t s r)
forall (m :: * -> *) s a. Monad m => (s -> (a, s)) -> StateT s m a
state (\Maybe s
s-> let (Parser t s r
p', Maybe s
s') = case (Parser t s r -> Parser t s r)
-> (s -> Parser t s r -> Parser t s r)
-> Maybe s
-> Parser t s r
-> Parser t s r
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Parser t s r -> Parser t s r
forall a. a -> a
id s -> Parser t s r -> Parser t s r
forall s t r. Monoid s => s -> Parser t s r -> Parser t s r
feed Maybe s
s Parser t s r
p
                                         of Result s
s'' r
a -> (s -> r -> Parser t s r
forall s r t. s -> r -> Parser t s r
Result s
forall a. Monoid a => a
mempty r
a, s -> Maybe s
forall a. a -> Maybe a
Just s
s'')
                                            Failure String
msg -> (String -> Parser t s r
forall t s r. String -> Parser t s r
Failure String
msg, Maybe s
forall a. Maybe a
Nothing)
                                            Parser t s r
p'' -> (Parser t s r
p'', Maybe s
forall a. Maybe a
Nothing)
                          in (Parser t s r
p', Maybe s
s'))

-- | Signals the end of the input.
feedEof :: Monoid s => Parser t s r -> Parser t s r
feedEof :: Parser t s r -> Parser t s r
feedEof p :: Parser t s r
p@Failure{} = Parser t s r
p
feedEof p :: Parser t s r
p@Result{} = Parser t s r
p
feedEof (ResultPart r -> r
r Parser t s r
e s -> Parser t s r
_) = (r -> r) -> Parser t s r -> Parser t s r
forall r t s. (r -> r) -> Parser t s r -> Parser t s r
prepend r -> r
r (Parser t s r -> Parser t s r
forall s t r. Monoid s => Parser t s r -> Parser t s r
feedEof Parser t s r
e)
feedEof (Choice Parser t s r
p1 Parser t s r
p2) = Parser t s r -> Parser t s r
forall s t r. Monoid s => Parser t s r -> Parser t s r
feedEof Parser t s r
p1 Parser t s r -> Parser t s r -> Parser t s r
forall t s r. Parser t s r -> Parser t s r -> Parser t s r
<||> Parser t s r -> Parser t s r
forall s t r. Monoid s => Parser t s r -> Parser t s r
feedEof Parser t s r
p2
feedEof (Delay Parser t s r
e s -> Parser t s r
_) = Parser t s r -> Parser t s r
forall s t r. Monoid s => Parser t s r -> Parser t s r
feedEof Parser t s r
e
feedEof (ResultStructure Maybe s
s g (Parser t s)
r) = (String -> Parser t s (g m))
-> ([(g m, Maybe s)] -> Parser t s (g m))
-> Either String [(g m, Maybe s)]
-> Parser t s (g m)
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either String -> Parser t s (g m)
forall t s r. String -> Parser t s r
Failure [(g m, Maybe s)] -> Parser t s (g m)
collect (ListT (Either String) (g m, Maybe s)
-> Either String [(g m, Maybe s)]
forall (m :: * -> *) a. ListT m a -> m [a]
runListT (ListT (Either String) (g m, Maybe s)
 -> Either String [(g m, Maybe s)])
-> ListT (Either String) (g m, Maybe s)
-> Either String [(g m, Maybe s)]
forall a b. (a -> b) -> a -> b
$ StateT (Maybe s) (ListT (Either String)) (g m)
-> Maybe s -> ListT (Either String) (g m, Maybe s)
forall s (m :: * -> *) a. StateT s m a -> s -> m (a, s)
runStateT ((forall a.
 Parser t s a -> StateT (Maybe s) (ListT (Either String)) (m a))
-> g (Parser t s) -> StateT (Maybe s) (ListT (Either String)) (g m)
forall k (g :: (k -> *) -> *) (m :: * -> *) (p :: k -> *)
       (q :: k -> *).
(Traversable g, Applicative m) =>
(forall (a :: k). p a -> m (q a)) -> g p -> m (g q)
Rank2.traverse forall a.
Parser t s a -> StateT (Maybe s) (ListT (Either String)) (m a)
forall (m :: * -> *) s t r.
(Applicative m, Monoid s) =>
Parser t s r -> StateT (Maybe s) (ListT (Either String)) (m r)
feedEofMaybe g (Parser t s)
r) Maybe s
forall a. Maybe a
Nothing)
   where collect :: [(g m, Maybe s)] -> Parser t s (g m)
collect = (Parser t s (g m) -> Parser t s (g m) -> Parser t s (g m))
-> [Parser t s (g m)] -> Parser t s (g m)
forall (t :: * -> *) a. Foldable t => (a -> a -> a) -> t a -> a
foldr1 Parser t s (g m) -> Parser t s (g m) -> Parser t s (g m)
forall t s r. Parser t s r -> Parser t s r -> Parser t s r
Choice ([Parser t s (g m)] -> Parser t s (g m))
-> ([(g m, Maybe s)] -> [Parser t s (g m)])
-> [(g m, Maybe s)]
-> Parser t s (g m)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((g m, Maybe s) -> Parser t s (g m))
-> [(g m, Maybe s)] -> [Parser t s (g m)]
forall a b. (a -> b) -> [a] -> [b]
map (g m, Maybe s) -> Parser t s (g m)
result
         result :: (g m, Maybe s) -> Parser t s (g m)
result (g m
r', Maybe s
s') = s -> g m -> Parser t s (g m)
forall s r t. s -> r -> Parser t s r
Result (Maybe s -> s
forall (t :: * -> *) m. (Foldable t, Monoid m) => t m -> m
fold Maybe s
s' s -> s -> s
forall a. Semigroup a => a -> a -> a
<> Maybe s -> s
forall (t :: * -> *) m. (Foldable t, Monoid m) => t m -> m
fold Maybe s
s) g m
r'


feedEofMaybe :: (Applicative m, Monoid s) => Parser t s r -> StateT (Maybe s) (ListT (Either String)) (m r)
feedEofMaybe :: Parser t s r -> StateT (Maybe s) (ListT (Either String)) (m r)
feedEofMaybe Parser t s r
p = (Maybe s -> ListT (Either String) (m r, Maybe s))
-> StateT (Maybe s) (ListT (Either String)) (m r)
forall s (m :: * -> *) a. (s -> m (a, s)) -> StateT s m a
StateT (\Maybe s
s-> Either String [(m r, Maybe s)]
-> ListT (Either String) (m r, Maybe s)
forall (m :: * -> *) a. m [a] -> ListT m a
ListT (Either String [(m r, Maybe s)]
 -> ListT (Either String) (m r, Maybe s))
-> Either String [(m r, Maybe s)]
-> ListT (Either String) (m r, Maybe s)
forall a b. (a -> b) -> a -> b
$ case Parser t s r -> Parser t s r
forall s t r. Monoid s => Parser t s r -> Parser t s r
feedEof ((Parser t s r -> Parser t s r)
-> (s -> Parser t s r -> Parser t s r)
-> Maybe s
-> Parser t s r
-> Parser t s r
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Parser t s r -> Parser t s r
forall a. a -> a
id s -> Parser t s r -> Parser t s r
forall s t r. Monoid s => s -> Parser t s r -> Parser t s r
feed Maybe s
s Parser t s r
p)
                                      of Failure String
msg -> String -> Either String [(m r, Maybe s)]
forall a b. a -> Either a b
Left String
msg
                                         Parser t s r
p' -> [(m r, Maybe s)] -> Either String [(m r, Maybe s)]
forall a b. b -> Either a b
Right (((r, s) -> (m r, Maybe s)) -> [(r, s)] -> [(m r, Maybe s)]
forall a b. (a -> b) -> [a] -> [b]
map (r, s) -> (m r, Maybe s)
forall (f :: * -> *) a a. Applicative f => (a, a) -> (f a, Maybe a)
wrap ([(r, s)] -> [(m r, Maybe s)]) -> [(r, s)] -> [(m r, Maybe s)]
forall a b. (a -> b) -> a -> b
$ Parser t s r -> [(r, s)]
forall s t r. Monoid s => Parser t s r -> [(r, s)]
completeResults Parser t s r
p'))
   where wrap :: (a, a) -> (f a, Maybe a)
wrap (a
r, a
s) = (a -> f a
forall (f :: * -> *) a. Applicative f => a -> f a
pure a
r, a -> Maybe a
forall a. a -> Maybe a
Just a
s)

-- | Extracts all available parsing results from a 'Parser'. The first component of the result pair is a list of
-- complete results together with the unconsumed remainder of the input. If the parsing can continue further, the second
-- component of the pair provides the partial result prefix together with the parser for the rest of the input.
results :: Monoid r => Parser t s r -> ([(r, s)], Maybe (r, Parser t s r))
results :: Parser t s r -> ([(r, s)], Maybe (r, Parser t s r))
results = (String -> ([(r, s)], Maybe (r, Parser t s r)))
-> (([(r, s)], Maybe (Maybe (r -> r), Parser t s r))
    -> ([(r, s)], Maybe (r, Parser t s r)))
-> Either String ([(r, s)], Maybe (Maybe (r -> r), Parser t s r))
-> ([(r, s)], Maybe (r, Parser t s r))
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (([(r, s)], Maybe (r, Parser t s r))
-> String -> ([(r, s)], Maybe (r, Parser t s r))
forall a b. a -> b -> a
const ([], Maybe (r, Parser t s r)
forall a. Maybe a
Nothing)) (((\(Maybe (r -> r)
mf, Parser t s r
p)-> ((r -> r) -> Maybe (r -> r) -> r -> r
forall a. a -> Maybe a -> a
fromMaybe r -> r
forall a. a -> a
id Maybe (r -> r)
mf r
forall a. Monoid a => a
mempty, Parser t s r
p)) ((Maybe (r -> r), Parser t s r) -> (r, Parser t s r))
-> Maybe (Maybe (r -> r), Parser t s r) -> Maybe (r, Parser t s r)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$>) (Maybe (Maybe (r -> r), Parser t s r) -> Maybe (r, Parser t s r))
-> ([(r, s)], Maybe (Maybe (r -> r), Parser t s r))
-> ([(r, s)], Maybe (r, Parser t s r))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$>) (Either String ([(r, s)], Maybe (Maybe (r -> r), Parser t s r))
 -> ([(r, s)], Maybe (r, Parser t s r)))
-> (Parser t s r
    -> Either String ([(r, s)], Maybe (Maybe (r -> r), Parser t s r)))
-> Parser t s r
-> ([(r, s)], Maybe (r, Parser t s r))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Parser t s r
-> Either String ([(r, s)], Maybe (Maybe (r -> r), Parser t s r))
forall t s r.
Parser t s r
-> Either String ([(r, s)], Maybe (Maybe (r -> r), Parser t s r))
inspect

-- | Like 'results', but more general: doesn't assume that the result type is a 'Monoid'.
inspect :: Parser t s r -> Either String ([(r, s)], Maybe (Maybe (r -> r), Parser t s r))
inspect :: Parser t s r
-> Either String ([(r, s)], Maybe (Maybe (r -> r), Parser t s r))
inspect (Failure String
msg) = String
-> Either String ([(r, s)], Maybe (Maybe (r -> r), Parser t s r))
forall a b. a -> Either a b
Left String
msg
inspect (Result s
s r
r) = ([(r, s)], Maybe (Maybe (r -> r), Parser t s r))
-> Either String ([(r, s)], Maybe (Maybe (r -> r), Parser t s r))
forall a b. b -> Either a b
Right ([(r
r, s
s)], Maybe (Maybe (r -> r), Parser t s r)
forall a. Maybe a
Nothing)
inspect (ResultPart r -> r
r Parser t s r
e s -> Parser t s r
f) = ([(r, s)], Maybe (Maybe (r -> r), Parser t s r))
-> Either String ([(r, s)], Maybe (Maybe (r -> r), Parser t s r))
forall a b. b -> Either a b
Right ([], (Maybe (r -> r), Parser t s r)
-> Maybe (Maybe (r -> r), Parser t s r)
forall a. a -> Maybe a
Just ((r -> r) -> Maybe (r -> r)
forall a. a -> Maybe a
Just r -> r
r, (r -> r) -> Parser t s r -> (s -> Parser t s r) -> Parser t s r
forall r t s.
(r -> r) -> Parser t s r -> (s -> Parser t s r) -> Parser t s r
ResultPart r -> r
forall a. a -> a
id Parser t s r
e s -> Parser t s r
f))
inspect (Choice Parser t s r
p1 Parser t s r
p2) | Parser t s r -> Bool
forall t s r. Parser t s r -> Bool
isInfallible Parser t s r
p1 = ([(r, s)], Maybe (Maybe (r -> r), Parser t s r))
-> Either String ([(r, s)], Maybe (Maybe (r -> r), Parser t s r))
forall a b. b -> Either a b
Right ([(r, s)]
results1 [(r, s)] -> [(r, s)] -> [(r, s)]
forall a. [a] -> [a] -> [a]
++ [(r, s)]
results2, Maybe (Maybe (r -> r), Parser t s r)
-> Maybe (Maybe (r -> r), Parser t s r)
-> Maybe (Maybe (r -> r), Parser t s r)
forall r t s.
Maybe (Maybe (r -> r), Parser t s r)
-> Maybe (Maybe (r -> r), Parser t s r)
-> Maybe (Maybe (r -> r), Parser t s r)
combine Maybe (Maybe (r -> r), Parser t s r)
rest1 Maybe (Maybe (r -> r), Parser t s r)
rest2)
   where ([(r, s)]
results1, Maybe (Maybe (r -> r), Parser t s r)
rest1) = (String -> ([(r, s)], Maybe (Maybe (r -> r), Parser t s r)))
-> (([(r, s)], Maybe (Maybe (r -> r), Parser t s r))
    -> ([(r, s)], Maybe (Maybe (r -> r), Parser t s r)))
-> Either String ([(r, s)], Maybe (Maybe (r -> r), Parser t s r))
-> ([(r, s)], Maybe (Maybe (r -> r), Parser t s r))
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (([(r, s)], Maybe (Maybe (r -> r), Parser t s r))
-> String -> ([(r, s)], Maybe (Maybe (r -> r), Parser t s r))
forall a b. a -> b -> a
const ([], Maybe (Maybe (r -> r), Parser t s r)
forall a. Maybe a
Nothing)) ([(r, s)], Maybe (Maybe (r -> r), Parser t s r))
-> ([(r, s)], Maybe (Maybe (r -> r), Parser t s r))
forall a. a -> a
id (Parser t s r
-> Either String ([(r, s)], Maybe (Maybe (r -> r), Parser t s r))
forall t s r.
Parser t s r
-> Either String ([(r, s)], Maybe (Maybe (r -> r), Parser t s r))
inspect Parser t s r
p1)
         ([(r, s)]
results2, Maybe (Maybe (r -> r), Parser t s r)
rest2) = (String -> ([(r, s)], Maybe (Maybe (r -> r), Parser t s r)))
-> (([(r, s)], Maybe (Maybe (r -> r), Parser t s r))
    -> ([(r, s)], Maybe (Maybe (r -> r), Parser t s r)))
-> Either String ([(r, s)], Maybe (Maybe (r -> r), Parser t s r))
-> ([(r, s)], Maybe (Maybe (r -> r), Parser t s r))
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (([(r, s)], Maybe (Maybe (r -> r), Parser t s r))
-> String -> ([(r, s)], Maybe (Maybe (r -> r), Parser t s r))
forall a b. a -> b -> a
const ([], Maybe (Maybe (r -> r), Parser t s r)
forall a. Maybe a
Nothing)) ([(r, s)], Maybe (Maybe (r -> r), Parser t s r))
-> ([(r, s)], Maybe (Maybe (r -> r), Parser t s r))
forall a. a -> a
id (Parser t s r
-> Either String ([(r, s)], Maybe (Maybe (r -> r), Parser t s r))
forall t s r.
Parser t s r
-> Either String ([(r, s)], Maybe (Maybe (r -> r), Parser t s r))
inspect Parser t s r
p2)
         combine :: Maybe (Maybe (r -> r), Parser t s r)
-> Maybe (Maybe (r -> r), Parser t s r)
-> Maybe (Maybe (r -> r), Parser t s r)
combine Maybe (Maybe (r -> r), Parser t s r)
Nothing Maybe (Maybe (r -> r), Parser t s r)
rest = Maybe (Maybe (r -> r), Parser t s r)
rest
         combine Maybe (Maybe (r -> r), Parser t s r)
rest Maybe (Maybe (r -> r), Parser t s r)
Nothing = Maybe (Maybe (r -> r), Parser t s r)
rest
         combine (Just (Maybe (r -> r)
r1, Parser t s r
p1')) (Just (Maybe (r -> r)
r2, Parser t s r
p2')) = 
            (Maybe (r -> r), Parser t s r)
-> Maybe (Maybe (r -> r), Parser t s r)
forall a. a -> Maybe a
Just ((r -> r) -> Maybe (r -> r)
forall a. a -> Maybe a
Just r -> r
forall a. a -> a
id, Parser t s r -> Parser t s r -> Parser t s r
forall t s r. Parser t s r -> Parser t s r -> Parser t s r
Choice ((r -> r) -> Parser t s r -> Parser t s r
forall r t s. (r -> r) -> Parser t s r -> Parser t s r
prepend ((r -> r) -> Maybe (r -> r) -> r -> r
forall a. a -> Maybe a -> a
fromMaybe r -> r
forall a. a -> a
id Maybe (r -> r)
r1) Parser t s r
p1') ((r -> r) -> Parser t s r -> Parser t s r
forall r t s. (r -> r) -> Parser t s r -> Parser t s r
prepend ((r -> r) -> Maybe (r -> r) -> r -> r
forall a. a -> Maybe a -> a
fromMaybe r -> r
forall a. a -> a
id Maybe (r -> r)
r2) Parser t s r
p2'))
inspect Parser t s r
p = ([(r, s)], Maybe (Maybe (r -> r), Parser t s r))
-> Either String ([(r, s)], Maybe (Maybe (r -> r), Parser t s r))
forall a b. b -> Either a b
Right ([], (Maybe (r -> r), Parser t s r)
-> Maybe (Maybe (r -> r), Parser t s r)
forall a. a -> Maybe a
Just (Maybe (r -> r)
forall a. Maybe a
Nothing, Parser t s r
p))

-- | Like 'results', but returns only the complete results with the corresponding unconsumed inputs.
completeResults :: Monoid s => Parser t s r -> [(r, s)]
completeResults :: Parser t s r -> [(r, s)]
completeResults (Result s
s r
r) = [(r
r, s
s)]
completeResults (ResultPart r -> r
r Parser t s r
e s -> Parser t s r
_) = ((r, s) -> (r, s)) -> [(r, s)] -> [(r, s)]
forall a b. (a -> b) -> [a] -> [b]
map (\(r
r', s
t)-> (r -> r
r r
r', s
t)) (Parser t s r -> [(r, s)]
forall s t r. Monoid s => Parser t s r -> [(r, s)]
completeResults Parser t s r
e)
completeResults (ResultStructure Maybe s
s g (Parser t s)
r) = ((s -> s -> s
forall a. Semigroup a => a -> a -> a
<> Maybe s -> s
forall (t :: * -> *) m. (Foldable t, Monoid m) => t m -> m
fold Maybe s
s) (s -> s) -> (g m, s) -> (g m, s)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$>) ((g m, s) -> (g m, s)) -> [(g m, s)] -> [(g m, s)]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> StateT s [] (g m) -> s -> [(g m, s)]
forall s (m :: * -> *) a. StateT s m a -> s -> m (a, s)
runStateT ((forall a. Parser t s a -> StateT s [] (m a))
-> g (Parser t s) -> StateT s [] (g m)
forall k (g :: (k -> *) -> *) (m :: * -> *) (p :: k -> *)
       (q :: k -> *).
(Traversable g, Applicative m) =>
(forall (a :: k). p a -> m (q a)) -> g p -> m (g q)
Rank2.traverse forall a. Parser t s a -> StateT s [] (m a)
forall (m :: * -> *) s t r.
(Applicative m, Monoid s) =>
Parser t s r -> StateT s [] (m r)
complete g (Parser t s)
r) s
forall a. Monoid a => a
mempty
   where complete :: (Applicative m, Monoid s) => Parser t s r -> StateT s [] (m r)
         complete :: Parser t s r -> StateT s [] (m r)
complete Parser t s r
p = (s -> [(m r, s)]) -> StateT s [] (m r)
forall s (m :: * -> *) a. (s -> m (a, s)) -> StateT s m a
StateT (\s
s'-> (r, s) -> (m r, s)
forall (f :: * -> *) a b. Applicative f => (a, b) -> (f a, b)
pureFst ((r, s) -> (m r, s)) -> [(r, s)] -> [(m r, s)]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser t s r -> [(r, s)]
forall s t r. Monoid s => Parser t s r -> [(r, s)]
completeResults (s -> Parser t s r -> Parser t s r
forall s t r. Monoid s => s -> Parser t s r -> Parser t s r
feed s
s' Parser t s r
p))
         pureFst :: (a, b) -> (f a, b)
pureFst (a
a, b
b) = (a -> f a
forall (f :: * -> *) a. Applicative f => a -> f a
pure a
a, b
b)
completeResults (Choice Parser t s r
p1 Parser t s r
p2) | Parser t s r -> Bool
forall t s r. Parser t s r -> Bool
isInfallible Parser t s r
p1 = Parser t s r -> [(r, s)]
forall s t r. Monoid s => Parser t s r -> [(r, s)]
completeResults Parser t s r
p1 [(r, s)] -> [(r, s)] -> [(r, s)]
forall a. [a] -> [a] -> [a]
++ Parser t s r -> [(r, s)]
forall s t r. Monoid s => Parser t s r -> [(r, s)]
completeResults Parser t s r
p2
completeResults Parser t s r
_ = []

-- | Like 'results', but returns only the partial result prefix.
resultPrefix :: Monoid r => Parser t s r -> (r, Parser t s r)
resultPrefix :: Parser t s r -> (r, Parser t s r)
resultPrefix (Result s
s r
r) = (r
r, s -> r -> Parser t s r
forall s r t. s -> r -> Parser t s r
Result s
s r
forall a. Monoid a => a
mempty)
resultPrefix (ResultPart r -> r
r Parser t s r
e s -> Parser t s r
f) = (r -> r
r r
forall a. Monoid a => a
mempty, (r -> r) -> Parser t s r -> (s -> Parser t s r) -> Parser t s r
forall r t s.
(r -> r) -> Parser t s r -> (s -> Parser t s r) -> Parser t s r
ResultPart r -> r
forall a. a -> a
id Parser t s r
e s -> Parser t s r
f)
resultPrefix Parser t s r
p = (r
forall a. Monoid a => a
mempty, Parser t s r
p)

failure :: Parser t s r
failure :: Parser t s r
failure = String -> Parser t s r
forall t s r. String -> Parser t s r
Failure String
"failure"

infix  0 <?>

-- | Name a parser for error reporting in case it fails.
(<?>) :: Monoid s => Parser t s r -> String -> Parser t s r
Failure String
old <?> :: Parser t s r -> String -> Parser t s r
<?> String
msg
   | [String
encountered] <- (String -> Bool) -> [String] -> [String]
forall a. (a -> Bool) -> [a] -> [a]
filter (String
"encountered " String -> String -> Bool
forall m. LeftReductive m => m -> m -> Bool
`isPrefixOf`) (String -> [String]
forall m. FactorialMonoid m => m -> [m]
tails String
old) =
        String -> Parser t s r
forall t s r. String -> Parser t s r
Failure (String
"expected " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
msg String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
", " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
encountered)
   | Bool
otherwise = String -> Parser t s r
forall t s r. String -> Parser t s r
Failure (String
"expected " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
msg)
p :: Parser t s r
p@Result{} <?> String
_ = Parser t s r
p
p :: Parser t s r
p@ResultPart{} <?> String
_ = Parser t s r
p
Parser t s r
p <?> String
msg = (Parser t s r -> Parser t s r) -> Parser t s r -> Parser t s r
forall s t r r'.
Monoid s =>
(Parser t s r -> Parser t s r') -> Parser t s r -> Parser t s r'
apply (Parser t s r -> String -> Parser t s r
forall s t r. Monoid s => Parser t s r -> String -> Parser t s r
<?> String
msg) Parser t s r
p

-- | Usage of 'fmap' destroys the incrementality of parsing results, if you need it use 'mapIncremental' instead.
instance Monoid s => Functor (Parser t s) where
   fmap :: (a -> b) -> Parser t s a -> Parser t s b
fmap a -> b
f (Result s
s a
r) = s -> b -> Parser t s b
forall s r t. s -> r -> Parser t s r
Result s
s (a -> b
f a
r)
   fmap a -> b
g (ResultPart a -> a
r Parser t s a
e s -> Parser t s a
f) = (b -> b) -> Parser t s b -> (s -> Parser t s b) -> Parser t s b
forall r t s.
(r -> r) -> Parser t s r -> (s -> Parser t s r) -> Parser t s r
ResultPart b -> b
forall a. a -> a
id ((a -> b) -> Parser t s a -> Parser t s b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
g (Parser t s a -> Parser t s b) -> Parser t s a -> Parser t s b
forall a b. (a -> b) -> a -> b
$ (a -> a) -> Parser t s a -> Parser t s a
forall r t s. (r -> r) -> Parser t s r -> Parser t s r
prepend a -> a
r (Parser t s a -> Parser t s a) -> Parser t s a -> Parser t s a
forall a b. (a -> b) -> a -> b
$ Parser t s a -> Parser t s a
forall s t r. Monoid s => Parser t s r -> Parser t s r
feedEof Parser t s a
e) ((a -> b) -> Parser t s a -> Parser t s b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
g (Parser t s a -> Parser t s b)
-> (s -> Parser t s a) -> s -> Parser t s b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> a) -> Parser t s a -> Parser t s a
forall r t s. (r -> r) -> Parser t s r -> Parser t s r
prepend a -> a
r (Parser t s a -> Parser t s a)
-> (s -> Parser t s a) -> s -> Parser t s a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. s -> Parser t s a
f)
   fmap a -> b
f Parser t s a
p = (Parser t s a -> Parser t s b) -> Parser t s a -> Parser t s b
forall s t r r'.
Monoid s =>
(Parser t s r -> Parser t s r') -> Parser t s r -> Parser t s r'
apply ((a -> b) -> Parser t s a -> Parser t s b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
f) Parser t s a
p

-- | The '<*>' combinator requires its both arguments to provide complete parsing results, whereas '*>' and '<*'
-- preserve the incremental results.
instance Monoid s => Applicative (Parser t s) where
   pure :: a -> Parser t s a
pure = s -> a -> Parser t s a
forall s r t. s -> r -> Parser t s r
Result s
forall a. Monoid a => a
mempty
   Result s
s a -> b
r <*> :: Parser t s (a -> b) -> Parser t s a -> Parser t s b
<*> Parser t s a
p = a -> b
r (a -> b) -> Parser t s a -> Parser t s b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> s -> Parser t s a -> Parser t s a
forall s t r. Monoid s => s -> Parser t s r -> Parser t s r
feed s
s Parser t s a
p
   Parser t s (a -> b)
p1 <*> Parser t s a
p2 = (Parser t s (a -> b) -> Parser t s b)
-> Parser t s (a -> b) -> Parser t s b
forall s t r r'.
Monoid s =>
(Parser t s r -> Parser t s r') -> Parser t s r -> Parser t s r'
apply (Parser t s (a -> b) -> Parser t s a -> Parser t s b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Parser t s a
p2) Parser t s (a -> b)
p1

   Result s
s a
_ *> :: Parser t s a -> Parser t s b -> Parser t s b
*> Parser t s b
p = s -> Parser t s b -> Parser t s b
forall s t r. Monoid s => s -> Parser t s r -> Parser t s r
feed s
s Parser t s b
p
   ResultPart a -> a
_ Parser t s a
e s -> Parser t s a
f *> Parser t s b
p | Parser t s b -> Bool
forall t s r. Parser t s r -> Bool
isInfallible Parser t s b
p = (b -> b) -> Parser t s b -> (s -> Parser t s b) -> Parser t s b
forall r t s.
(r -> r) -> Parser t s r -> (s -> Parser t s r) -> Parser t s r
ResultPart b -> b
forall a. a -> a
id (Parser t s a
e Parser t s a -> Parser t s b -> Parser t s b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> Parser t s b
p) ((Parser t s a -> Parser t s b -> Parser t s b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> Parser t s b
p) (Parser t s a -> Parser t s b)
-> (s -> Parser t s a) -> s -> Parser t s b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. s -> Parser t s a
f)
                         | Bool
otherwise = Parser t s b -> (s -> Parser t s b) -> Parser t s b
forall t s r. Parser t s r -> (s -> Parser t s r) -> Parser t s r
Delay (Parser t s a
e Parser t s a -> Parser t s b -> Parser t s b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> Parser t s b
p) ((Parser t s a -> Parser t s b -> Parser t s b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> Parser t s b
p) (Parser t s a -> Parser t s b)
-> (s -> Parser t s a) -> s -> Parser t s b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. s -> Parser t s a
f)
   Parser t s a
p1 *> Parser t s b
p2 = (Parser t s a -> Parser t s b) -> Parser t s a -> Parser t s b
forall s t r r'.
Monoid s =>
(Parser t s r -> Parser t s r') -> Parser t s r -> Parser t s r'
apply (Parser t s a -> Parser t s b -> Parser t s b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> Parser t s b
p2) Parser t s a
p1

   Result s
s a
r <* :: Parser t s a -> Parser t s b -> Parser t s a
<* Parser t s b
p = s -> Parser t s b -> Parser t s b
forall s t r. Monoid s => s -> Parser t s r -> Parser t s r
feed s
s Parser t s b
p Parser t s b -> Parser t s a -> Parser t s a
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> a -> Parser t s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure a
r
   ResultPart a -> a
r Parser t s a
e s -> Parser t s a
f <* Parser t s b
p | Parser t s b -> Bool
forall t s r. Parser t s r -> Bool
isInfallible Parser t s b
p = (a -> a) -> Parser t s a -> (s -> Parser t s a) -> Parser t s a
forall r t s.
(r -> r) -> Parser t s r -> (s -> Parser t s r) -> Parser t s r
ResultPart a -> a
r (Parser t s a
e Parser t s a -> Parser t s b -> Parser t s a
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* Parser t s b
p) ((Parser t s a -> Parser t s b -> Parser t s a
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* Parser t s b
p) (Parser t s a -> Parser t s a)
-> (s -> Parser t s a) -> s -> Parser t s a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. s -> Parser t s a
f)
   Parser t s a
p1 <* Parser t s b
p2 = (Parser t s a -> Parser t s a) -> Parser t s a -> Parser t s a
forall s t r r'.
Monoid s =>
(Parser t s r -> Parser t s r') -> Parser t s r -> Parser t s r'
apply (Parser t s a -> Parser t s b -> Parser t s a
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* Parser t s b
p2) Parser t s a
p1

-- | Usage of '>>=' destroys the incrementality of its left argument's parsing results, but '>>' is safe to use.
instance Monoid s => Monad (Parser t s) where
   return :: a -> Parser t s a
return = a -> Parser t s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure
   Result s
s a
r >>= :: Parser t s a -> (a -> Parser t s b) -> Parser t s b
>>= a -> Parser t s b
f = s -> Parser t s b -> Parser t s b
forall s t r. Monoid s => s -> Parser t s r -> Parser t s r
feed s
s (a -> Parser t s b
f a
r)
   Parser t s a
p >>= a -> Parser t s b
f = (Parser t s a -> Parser t s b) -> Parser t s a -> Parser t s b
forall s t r r'.
Monoid s =>
(Parser t s r -> Parser t s r') -> Parser t s r -> Parser t s r'
apply (Parser t s a -> (a -> Parser t s b) -> Parser t s b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= a -> Parser t s b
f) Parser t s a
p
   >> :: Parser t s a -> Parser t s b -> Parser t s b
(>>) = Parser t s a -> Parser t s b -> Parser t s b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
(*>)

instance Monoid s => MonadFail (Parser t s) where
   fail :: String -> Parser t s a
fail = String -> Parser t s a
forall t s r. String -> Parser t s r
Failure

instance Monoid s => MonadFix (Parser t s) where
   mfix :: (a -> Parser t s a) -> Parser t s a
mfix a -> Parser t s a
f = Parser t s a -> (s -> Parser t s a) -> Parser t s a
forall t s r. Parser t s r -> (s -> Parser t s r) -> Parser t s r
Delay Parser t s a
fixEof s -> Parser t s a
fixInput
      where fixEof :: Parser t s a
fixEof = let r :: Parser t s a
r = a -> Parser t s a
f (Parser t s a -> a
forall t s r. Parser t s r -> r
atEof Parser t s a
r) in Parser t s a
r
            fixInput :: s -> Parser t s a
fixInput s
s = (a -> Parser t s a) -> Parser t s a
forall (m :: * -> *) a. MonadFix m => (a -> m a) -> m a
mfix (s -> Parser t s a -> Parser t s a
forall s t r. Monoid s => s -> Parser t s r -> Parser t s r
feed s
s (Parser t s a -> Parser t s a)
-> (a -> Parser t s a) -> a -> Parser t s a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Parser t s a
f)
            atEof :: Parser t s r -> r
            atEof :: Parser t s r -> r
atEof (Result s
_ r
r) = r
r
            atEof (ResultPart r -> r
r Parser t s r
e s -> Parser t s r
_) = r -> r
r (Parser t s r -> r
forall t s r. Parser t s r -> r
atEof Parser t s r
e)
            atEof (ResultStructure Maybe s
_ g (Parser t s)
r) = (a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (a -> m a) -> (Parser t s a -> a) -> Parser t s a -> m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Parser t s a -> a
forall t s r. Parser t s r -> r
atEof) (forall a. Parser t s a -> m a) -> g (Parser t s) -> g m
forall k (g :: (k -> *) -> *) (p :: k -> *) (q :: k -> *).
Functor g =>
(forall (a :: k). p a -> q a) -> g p -> g q
Rank2.<$> g (Parser t s)
r
            atEof (Delay Parser t s r
e s -> Parser t s r
_) = Parser t s r -> r
forall t s r. Parser t s r -> r
atEof Parser t s r
e
            atEof (Failure String
msg) = String -> r
forall a. HasCallStack => String -> a
error (String
"mfix on Failure " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
msg)
            atEof Choice{} = String -> r
forall a. HasCallStack => String -> a
error String
"mfix on Choice"

-- | The '+<*>' operator is specialized to return incremental parsing results.
instance Monoid s => MonoidApplicative (Parser t s) where
   Result s
s a -> a
r +<*> :: Parser t s (a -> a) -> Parser t s a -> Parser t s a
+<*> Parser t s a
p = (a -> a) -> Parser t s a -> Parser t s a
forall s r t. Monoid s => (r -> r) -> Parser t s r -> Parser t s r
resultPart a -> a
r (s -> Parser t s a -> Parser t s a
forall s t r. Monoid s => s -> Parser t s r -> Parser t s r
feed s
s Parser t s a
p)
   Parser t s (a -> a)
p1 +<*> Parser t s a
p2 = (Parser t s (a -> a) -> Parser t s a)
-> Parser t s (a -> a) -> Parser t s a
forall s t r r'.
Monoid s =>
(Parser t s r -> Parser t s r') -> Parser t s r -> Parser t s r'
apply (Parser t s (a -> a) -> Parser t s a -> Parser t s a
forall (f :: * -> *) a.
MonoidApplicative f =>
f (a -> a) -> f a -> f a
+<*> Parser t s a
p2) Parser t s (a -> a)
p1
   -- | Join operator on two parsers of the same type, preserving the incremental results.
   Parser t s a
_ >< :: Parser t s a -> Parser t s a -> Parser t s a
>< p :: Parser t s a
p@Failure{} = Parser t s a
p
   Parser t s a
p1 >< Parser t s a
p2 | Parser t s a -> Bool
forall t s r. Parser t s r -> Bool
isInfallible Parser t s a
p2 = Parser t s a -> Parser t s a -> Parser t s a
forall s r t.
(Monoid s, Semigroup r) =>
Parser t s r -> Parser t s r -> Parser t s r
appendIncremental Parser t s a
p1 Parser t s a
p2
            | Bool
otherwise       = Parser t s a -> Parser t s a -> Parser t s a
forall s r t.
(Monoid s, Semigroup r) =>
Parser t s r -> Parser t s r -> Parser t s r
append Parser t s a
p1 Parser t s a
p2

instance (Alternative (Parser t s), MonoidNull s) => Parsing (Parser t s) where
   try :: Parser t s a -> Parser t s a
try = Parser t s a -> Parser t s a
forall a. a -> a
id
   <?> :: Parser t s a -> String -> Parser t s a
(<?>) = Parser t s a -> String -> Parser t s a
forall s t r. Monoid s => Parser t s r -> String -> Parser t s r
(<?>)
   notFollowedBy :: Parser t s a -> Parser t s ()
notFollowedBy = Parser t s a -> Parser t s ()
forall s r t r'.
(Monoid s, Monoid r) =>
Parser t s r' -> Parser t s r
notFollowedBy
   skipMany :: Parser t s a -> Parser t s ()
skipMany = Parser t s () -> Parser t s ()
forall (f :: * -> *) a.
(MonoidAlternative f, Semigroup a, Monoid a) =>
f a -> f a
concatMany (Parser t s () -> Parser t s ())
-> (Parser t s a -> Parser t s ()) -> Parser t s a -> Parser t s ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Parser t s a -> Parser t s ()
forall s r t r'.
(Monoid s, Monoid r, Semigroup r) =>
Parser t s r' -> Parser t s r
skip
   skipSome :: Parser t s a -> Parser t s ()
skipSome = Parser t s () -> Parser t s ()
forall (f :: * -> *) a.
(MonoidAlternative f, Semigroup a, Monoid a) =>
f a -> f a
concatSome (Parser t s () -> Parser t s ())
-> (Parser t s a -> Parser t s ()) -> Parser t s a -> Parser t s ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Parser t s a -> Parser t s ()
forall s r t r'.
(Monoid s, Monoid r, Semigroup r) =>
Parser t s r' -> Parser t s r
skip
   eof :: Parser t s ()
eof = Parser t s ()
forall s r t. (MonoidNull s, Monoid r, Semigroup r) => Parser t s r
eof
   unexpected :: String -> Parser t s a
unexpected = String -> Parser t s a
forall t s r. String -> Parser t s r
Failure (String -> Parser t s a)
-> (String -> String) -> String -> Parser t s a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (String
"encountered " String -> String -> String
forall a. Semigroup a => a -> a -> a
<>)

instance (Alternative (Parser t s), MonoidNull s) => LookAheadParsing (Parser t s) where
   lookAhead :: Parser t s a -> Parser t s a
lookAhead = Parser t s a -> Parser t s a
forall s t r. Monoid s => Parser t s r -> Parser t s r
lookAhead

instance (Alternative (Parser t s), MonoidNull s) => DeterministicParsing (Parser t s) where
   <<|> :: Parser t s a -> Parser t s a -> Parser t s a
(<<|>) = Parser t s a -> Parser t s a -> Parser t s a
forall s t r.
Monoid s =>
Parser t s r -> Parser t s r -> Parser t s r
(<<|>)

instance (Alternative (Parser t s), TextualMonoid s) => CharParsing (Parser t s) where
   satisfy :: (Char -> Bool) -> Parser t s Char
satisfy = (s -> Char) -> Parser t s s -> Parser t s Char
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Char -> Maybe Char -> Char
forall a. a -> Maybe a -> a
fromMaybe (String -> Char
forall a. HasCallStack => String -> a
error String
"isNothing . characterPrefix") (Maybe Char -> Char) -> (s -> Maybe Char) -> s -> Char
forall b c a. (b -> c) -> (a -> b) -> a -> c
. s -> Maybe Char
forall t. TextualMonoid t => t -> Maybe Char
Textual.characterPrefix) (Parser t s s -> Parser t s Char)
-> ((Char -> Bool) -> Parser t s s)
-> (Char -> Bool)
-> Parser t s Char
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Char -> Bool) -> Parser t s s
forall s t. TextualMonoid s => (Char -> Bool) -> Parser t s s
satisfyChar
   string :: String -> Parser t s String
string String
s = s -> Parser t s s
forall s t.
(LeftReductive s, MonoidNull s, Semigroup s) =>
s -> Parser t s s
string (String -> s
forall a. IsString a => String -> a
fromString String
s) Parser t s s -> Parser t s String -> Parser t s String
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> String -> Parser t s String
forall (f :: * -> *) a. Applicative f => a -> f a
pure String
s
   text :: Text -> Parser t s Text
text Text
t = s -> Parser t s s
forall s t.
(LeftReductive s, MonoidNull s, Semigroup s) =>
s -> Parser t s s
string (Text -> s
forall t. TextualMonoid t => Text -> t
Textual.fromText Text
t) Parser t s s -> Parser t s Text -> Parser t s Text
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> Text -> Parser t s Text
forall (f :: * -> *) a. Applicative f => a -> f a
pure Text
t

instance (Alternative (Parser t s), FactorialMonoid s, LeftReductive s) => InputParsing (Parser t s) where
   type ParserInput (Parser t s) = s
   getInput :: Parser t s (ParserInput (Parser t s))
getInput = Parser t s s -> Parser t s s
forall s t r. Monoid s => Parser t s r -> Parser t s r
lookAhead Parser t s s
forall s t. (Semigroup s, Monoid s) => Parser t s s
acceptAll
   anyToken :: Parser t s (ParserInput (Parser t s))
anyToken = Parser t s (ParserInput (Parser t s))
forall s t. FactorialMonoid s => Parser t s s
anyToken
   take :: Int -> Parser t s (ParserInput (Parser t s))
take Int
n = (s -> Parser t s s) -> Parser t s s
forall s t r. (s -> Parser t s r) -> Parser t s r
more ((s, s) -> Parser t s s
f ((s, s) -> Parser t s s) -> (s -> (s, s)) -> s -> Parser t s s
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> s -> (s, s)
forall m. FactorialMonoid m => Int -> m -> (m, m)
splitAt Int
n)
     where f :: (s, s) -> Parser t s s
f (s
prefix, s
suffix)
             | Int
n' Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0 = s -> s -> Parser t s s
forall s r t. s -> r -> Parser t s r
Result s
suffix s
prefix
             | Bool
otherwise = (s -> s) -> Parser t s s -> Parser t s s
forall s r t. Monoid s => (r -> r) -> Parser t s r -> Parser t s r
resultPart (s
prefix s -> s -> s
forall a. Semigroup a => a -> a -> a
<>) (Int -> Parser t s (ParserInput (Parser t s))
forall (m :: * -> *). InputParsing m => Int -> m (ParserInput m)
take Int
n')
             where n' :: Int
n' = Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
- s -> Int
forall m. Factorial m => m -> Int
length s
prefix
   satisfy :: (ParserInput (Parser t s) -> Bool)
-> Parser t s (ParserInput (Parser t s))
satisfy ParserInput (Parser t s) -> Bool
predicate = Parser t s s
Parser t s (ParserInput (Parser t s))
p
     where p :: Parser t s s
p = (s -> Parser t s s) -> Parser t s s
forall s t r. (s -> Parser t s r) -> Parser t s r
more (Maybe (s, s) -> Parser t s s
f (Maybe (s, s) -> Parser t s s)
-> (s -> Maybe (s, s)) -> s -> Parser t s s
forall b c a. (b -> c) -> (a -> b) -> a -> c
. s -> Maybe (s, s)
forall m. FactorialMonoid m => m -> Maybe (m, m)
splitPrimePrefix)
           f :: Maybe (s, s) -> Parser t s s
f (Just (s
first, s
rest)) = if ParserInput (Parser t s) -> Bool
predicate s
ParserInput (Parser t s)
first then s -> s -> Parser t s s
forall s r t. s -> r -> Parser t s r
Result s
rest s
first else String -> Parser t s s
forall t s r. String -> Parser t s r
Failure String
"satisfy"
           f Maybe (s, s)
Nothing = Parser t s s
p
   notSatisfy :: (ParserInput (Parser t s) -> Bool) -> Parser t s ()
notSatisfy ParserInput (Parser t s) -> Bool
predicate = Parser t s ()
p
     where p :: Parser t s ()
p = Parser t s () -> (s -> Parser t s ()) -> Parser t s ()
forall t s r. Parser t s r -> (s -> Parser t s r) -> Parser t s r
Delay Parser t s ()
forall a. Monoid a => a
mempty (Maybe (s, s) -> Parser t s ()
f (Maybe (s, s) -> Parser t s ())
-> (s -> Maybe (s, s)) -> s -> Parser t s ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. s -> Maybe (s, s)
forall m. FactorialMonoid m => m -> Maybe (m, m)
splitPrimePrefix)
           f :: Maybe (s, s) -> Parser t s ()
f (Just (s
first, s
rest)) = if ParserInput (Parser t s) -> Bool
predicate s
ParserInput (Parser t s)
first then String -> Parser t s ()
forall t s r. String -> Parser t s r
Failure String
"satisfy" else s -> () -> Parser t s ()
forall s r t. s -> r -> Parser t s r
Result s
rest ()
           f Maybe (s, s)
Nothing = Parser t s ()
p
   string :: ParserInput (Parser t s) -> Parser t s (ParserInput (Parser t s))
string = ParserInput (Parser t s) -> Parser t s (ParserInput (Parser t s))
forall s t.
(LeftReductive s, MonoidNull s, Semigroup s) =>
s -> Parser t s s
string
   takeWhile :: (ParserInput (Parser t s) -> Bool)
-> Parser t s (ParserInput (Parser t s))
takeWhile = (ParserInput (Parser t s) -> Bool)
-> Parser t s (ParserInput (Parser t s))
forall s t.
(FactorialMonoid s, MonoidNull s) =>
(s -> Bool) -> Parser t s s
takeWhile
   takeWhile1 :: (ParserInput (Parser t s) -> Bool)
-> Parser t s (ParserInput (Parser t s))
takeWhile1 = (ParserInput (Parser t s) -> Bool)
-> Parser t s (ParserInput (Parser t s))
forall s t.
(FactorialMonoid s, MonoidNull s) =>
(s -> Bool) -> Parser t s s
takeWhile1

instance (TextualMonoid s, LeftReductive s, LookAheadParsing (Parser t s)) => InputCharParsing (Parser t s) where
   satisfyCharInput :: (Char -> Bool) -> Parser t s (ParserInput (Parser t s))
satisfyCharInput = (Char -> Bool) -> Parser t s (ParserInput (Parser t s))
forall s t. TextualMonoid s => (Char -> Bool) -> Parser t s s
satisfyChar
   takeCharsWhile :: (Char -> Bool) -> Parser t s (ParserInput (Parser t s))
takeCharsWhile = (Char -> Bool) -> Parser t s (ParserInput (Parser t s))
forall s t.
(TextualMonoid s, MonoidNull s) =>
(Char -> Bool) -> Parser t s s
takeCharsWhile
   takeCharsWhile1 :: (Char -> Bool) -> Parser t s (ParserInput (Parser t s))
takeCharsWhile1 = (Char -> Bool) -> Parser t s (ParserInput (Parser t s))
forall s t.
(TextualMonoid s, MonoidNull s) =>
(Char -> Bool) -> Parser t s s
takeCharsWhile1

appendIncremental :: (Monoid s, Semigroup r) => Parser t s r -> Parser t s r -> Parser t s r
appendIncremental :: Parser t s r -> Parser t s r -> Parser t s r
appendIncremental (Result s
s r
r) Parser t s r
p = (r -> r) -> Parser t s r -> Parser t s r
forall s r t. Monoid s => (r -> r) -> Parser t s r -> Parser t s r
resultPart (r
r r -> r -> r
forall a. Semigroup a => a -> a -> a
<>) (s -> Parser t s r -> Parser t s r
forall s t r. Monoid s => s -> Parser t s r -> Parser t s r
feed s
s Parser t s r
p)
appendIncremental (ResultPart r -> r
r Parser t s r
e s -> Parser t s r
f) Parser t s r
p2 = (r -> r) -> Parser t s r -> (s -> Parser t s r) -> Parser t s r
forall r t s.
(r -> r) -> Parser t s r -> (s -> Parser t s r) -> Parser t s r
ResultPart r -> r
r (Parser t s r -> Parser t s r -> Parser t s r
forall s r t.
(Monoid s, Semigroup r) =>
Parser t s r -> Parser t s r -> Parser t s r
appendIncremental Parser t s r
e Parser t s r
p2) ((Parser t s r -> Parser t s r -> Parser t s r)
-> Parser t s r -> Parser t s r -> Parser t s r
forall a b c. (a -> b -> c) -> b -> a -> c
flip Parser t s r -> Parser t s r -> Parser t s r
forall s r t.
(Monoid s, Semigroup r) =>
Parser t s r -> Parser t s r -> Parser t s r
appendIncremental Parser t s r
p2 (Parser t s r -> Parser t s r)
-> (s -> Parser t s r) -> s -> Parser t s r
forall b c a. (b -> c) -> (a -> b) -> a -> c
. s -> Parser t s r
f)
appendIncremental Parser t s r
p1 Parser t s r
p2 = (Parser t s r -> Parser t s r) -> Parser t s r -> Parser t s r
forall s t r r'.
Monoid s =>
(Parser t s r -> Parser t s r') -> Parser t s r -> Parser t s r'
apply (Parser t s r -> Parser t s r -> Parser t s r
forall s r t.
(Monoid s, Semigroup r) =>
Parser t s r -> Parser t s r -> Parser t s r
`appendIncremental` Parser t s r
p2) Parser t s r
p1

append :: (Monoid s, Semigroup r) => Parser t s r -> Parser t s r -> Parser t s r
append :: Parser t s r -> Parser t s r -> Parser t s r
append (Result s
s r
r) Parser t s r
p2 = (r -> r) -> Parser t s r -> Parser t s r
forall r t s. (r -> r) -> Parser t s r -> Parser t s r
prepend (r
r r -> r -> r
forall a. Semigroup a => a -> a -> a
<>) (s -> Parser t s r -> Parser t s r
forall s t r. Monoid s => s -> Parser t s r -> Parser t s r
feed s
s Parser t s r
p2)
append Parser t s r
p1 Parser t s r
p2 = (Parser t s r -> Parser t s r) -> Parser t s r -> Parser t s r
forall s t r r'.
Monoid s =>
(Parser t s r -> Parser t s r') -> Parser t s r -> Parser t s r'
apply (Parser t s r -> Parser t s r -> Parser t s r
forall s r t.
(Monoid s, Semigroup r) =>
Parser t s r -> Parser t s r -> Parser t s r
`append` Parser t s r
p2) Parser t s r
p1

-- | Two parsers can be sequentially joined.
instance (Monoid s, Semigroup r) => Semigroup (Parser t s r) where
   <> :: Parser t s r -> Parser t s r -> Parser t s r
(<>) = Parser t s r -> Parser t s r -> Parser t s r
forall (f :: * -> *) a.
(MonoidApplicative f, Semigroup a) =>
f a -> f a -> f a
(><)

instance (Monoid s, Monoid r, Semigroup r) => Monoid (Parser t s r) where
   mempty :: Parser t s r
mempty = r -> Parser t s r
forall (m :: * -> *) a. Monad m => a -> m a
return r
forall a. Monoid a => a
mempty
   mappend :: Parser t s r -> Parser t s r -> Parser t s r
mappend = Parser t s r -> Parser t s r -> Parser t s r
forall (f :: * -> *) a.
(MonoidApplicative f, Semigroup a) =>
f a -> f a -> f a
(><)

instance (Alternative (Parser t s), Monoid s) => MonoidAlternative (Parser t s) where
   moptional :: Parser t s a -> Parser t s a
moptional Parser t s a
p = Parser t s a
p Parser t s a -> Parser t s a -> Parser t s a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Parser t s a
forall a. Monoid a => a
mempty
   concatMany :: Parser t s a -> Parser t s a
concatMany = (Parser t s a, Parser t s a) -> Parser t s a
forall a b. (a, b) -> a
fst ((Parser t s a, Parser t s a) -> Parser t s a)
-> (Parser t s a -> (Parser t s a, Parser t s a))
-> Parser t s a
-> Parser t s a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Parser t s a -> (Parser t s a, Parser t s a)
forall t s r.
(Alternative (Parser t s), Monoid s, Monoid r, Semigroup r) =>
Parser t s r -> (Parser t s r, Parser t s r)
manies
   concatSome :: Parser t s a -> Parser t s a
concatSome = (Parser t s a, Parser t s a) -> Parser t s a
forall a b. (a, b) -> b
snd ((Parser t s a, Parser t s a) -> Parser t s a)
-> (Parser t s a -> (Parser t s a, Parser t s a))
-> Parser t s a
-> Parser t s a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Parser t s a -> (Parser t s a, Parser t s a)
forall t s r.
(Alternative (Parser t s), Monoid s, Monoid r, Semigroup r) =>
Parser t s r -> (Parser t s r, Parser t s r)
manies

manies :: (Alternative (Parser t s), Monoid s, Monoid r, Semigroup r) => Parser t s r -> (Parser t s r, Parser t s r)
manies :: Parser t s r -> (Parser t s r, Parser t s r)
manies Parser t s r
p = (Parser t s r
many, Parser t s r
some)
   where many :: Parser t s r
many = (r -> r) -> Parser t s r -> Parser t s r
forall s r t. Monoid s => (r -> r) -> Parser t s r -> Parser t s r
resultPart r -> r
forall a. a -> a
id (Parser t s r
some Parser t s r -> Parser t s r -> Parser t s r
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Parser t s r
forall a. Monoid a => a
mempty)
         some :: Parser t s r
some = Parser t s r -> Parser t s r -> Parser t s r
forall s r t.
(Monoid s, Semigroup r) =>
Parser t s r -> Parser t s r -> Parser t s r
appendIncremental Parser t s r
p Parser t s r
many

infixl 3 <||>
infixl 3 <<|>

(<||>) :: Parser t s r -> Parser t s r -> Parser t s r
Delay Parser t s r
e1 s -> Parser t s r
f1 <||> :: Parser t s r -> Parser t s r -> Parser t s r
<||> Delay Parser t s r
e2 s -> Parser t s r
f2 = Parser t s r -> (s -> Parser t s r) -> Parser t s r
forall t s r. Parser t s r -> (s -> Parser t s r) -> Parser t s r
Delay (Parser t s r
e1 Parser t s r -> Parser t s r -> Parser t s r
forall t s r. Parser t s r -> Parser t s r -> Parser t s r
<||> Parser t s r
e2) (\s
s-> s -> Parser t s r
f1 s
s Parser t s r -> Parser t s r -> Parser t s r
forall t s r. Parser t s r -> Parser t s r -> Parser t s r
<||> s -> Parser t s r
f2 s
s)
Failure{} <||> Parser t s r
p = Parser t s r
p
Parser t s r
p <||> Failure{} = Parser t s r
p
p1 :: Parser t s r
p1@Result{} <||> Parser t s r
p2 = Parser t s r -> Parser t s r -> Parser t s r
forall t s r. Parser t s r -> Parser t s r -> Parser t s r
Choice Parser t s r
p1 Parser t s r
p2
p1 :: Parser t s r
p1@ResultPart{} <||> Parser t s r
p2 = Parser t s r -> Parser t s r -> Parser t s r
forall t s r. Parser t s r -> Parser t s r -> Parser t s r
Choice Parser t s r
p1 Parser t s r
p2
Choice Parser t s r
p1a Parser t s r
p1b <||> Parser t s r
p2 | Parser t s r -> Bool
forall t s r. Parser t s r -> Bool
isInfallible Parser t s r
p1a = Parser t s r -> Parser t s r -> Parser t s r
forall t s r. Parser t s r -> Parser t s r -> Parser t s r
Choice Parser t s r
p1a (Parser t s r
p1b Parser t s r -> Parser t s r -> Parser t s r
forall t s r. Parser t s r -> Parser t s r -> Parser t s r
<||> Parser t s r
p2)
Parser t s r
p1 <||> p2 :: Parser t s r
p2@Result{} = Parser t s r -> Parser t s r -> Parser t s r
forall t s r. Parser t s r -> Parser t s r -> Parser t s r
Choice Parser t s r
p2 Parser t s r
p1
Parser t s r
p1 <||> p2 :: Parser t s r
p2@ResultPart{} = Parser t s r -> Parser t s r -> Parser t s r
forall t s r. Parser t s r -> Parser t s r -> Parser t s r
Choice Parser t s r
p2 Parser t s r
p1
Parser t s r
p1 <||> Choice Parser t s r
p2a Parser t s r
p2b | Parser t s r -> Bool
forall t s r. Parser t s r -> Bool
isInfallible Parser t s r
p2a = Parser t s r -> Parser t s r -> Parser t s r
forall t s r. Parser t s r -> Parser t s r -> Parser t s r
Choice Parser t s r
p2a (Parser t s r
p1 Parser t s r -> Parser t s r -> Parser t s r
forall t s r. Parser t s r -> Parser t s r -> Parser t s r
<||> Parser t s r
p2b)
Parser t s r
p1 <||> Parser t s r
p2 = Parser t s r -> Parser t s r -> Parser t s r
forall t s r. Parser t s r -> Parser t s r -> Parser t s r
Choice Parser t s r
p1 Parser t s r
p2

(<<|>) :: Monoid s => Parser t s r -> Parser t s r -> Parser t s r
Failure{} <<|> :: Parser t s r -> Parser t s r -> Parser t s r
<<|> Parser t s r
p = Parser t s r
p
Parser t s r
p <<|> Parser t s r
_ | Parser t s r -> Bool
forall t s r. Parser t s r -> Bool
isInfallible Parser t s r
p = Parser t s r
p
Parser t s r
p <<|> Failure{} = Parser t s r
p
Parser t s r
p1 <<|> Parser t s r
p2 = if Parser t s r -> Bool
forall t s r. Parser t s r -> Bool
isInfallible Parser t s r
p2 then (r -> r) -> Parser t s r -> (s -> Parser t s r) -> Parser t s r
forall r t s.
(r -> r) -> Parser t s r -> (s -> Parser t s r) -> Parser t s r
ResultPart r -> r
forall a. a -> a
id Parser t s r
e s -> Parser t s r
f else Parser t s r -> (s -> Parser t s r) -> Parser t s r
forall t s r. Parser t s r -> (s -> Parser t s r) -> Parser t s r
Delay Parser t s r
e s -> Parser t s r
f
   where e :: Parser t s r
e = Parser t s r -> Parser t s r
forall s t r. Monoid s => Parser t s r -> Parser t s r
feedEof Parser t s r
p1 Parser t s r -> Parser t s r -> Parser t s r
forall s t r.
Monoid s =>
Parser t s r -> Parser t s r -> Parser t s r
<<|> Parser t s r -> Parser t s r
forall s t r. Monoid s => Parser t s r -> Parser t s r
feedEof Parser t s r
p2
         f :: s -> Parser t s r
f s
s = s -> Parser t s r -> Parser t s r
forall s t r. Monoid s => s -> Parser t s r -> Parser t s r
feed s
s Parser t s r
p1 Parser t s r -> Parser t s r -> Parser t s r
forall s t r.
Monoid s =>
Parser t s r -> Parser t s r -> Parser t s r
<<|> s -> Parser t s r -> Parser t s r
forall s t r. Monoid s => s -> Parser t s r -> Parser t s r
feed s
s Parser t s r
p2

defaultMany :: (Monoid s, Alternative (Parser t s)) => Parser t s r -> Parser t s [r]
defaultMany :: Parser t s r -> Parser t s [r]
defaultMany = (Parser t s [r], Parser t s [r]) -> Parser t s [r]
forall a b. (a, b) -> a
fst ((Parser t s [r], Parser t s [r]) -> Parser t s [r])
-> (Parser t s r -> (Parser t s [r], Parser t s [r]))
-> Parser t s r
-> Parser t s [r]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Parser t s r -> (Parser t s [r], Parser t s [r])
forall s t r.
(Monoid s, Alternative (Parser t s)) =>
Parser t s r -> (Parser t s [r], Parser t s [r])
defaultManySome

defaultSome :: (Monoid s, Alternative (Parser t s)) => Parser t s r -> Parser t s [r]
defaultSome :: Parser t s r -> Parser t s [r]
defaultSome = (Parser t s [r], Parser t s [r]) -> Parser t s [r]
forall a b. (a, b) -> b
snd ((Parser t s [r], Parser t s [r]) -> Parser t s [r])
-> (Parser t s r -> (Parser t s [r], Parser t s [r]))
-> Parser t s r
-> Parser t s [r]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Parser t s r -> (Parser t s [r], Parser t s [r])
forall s t r.
(Monoid s, Alternative (Parser t s)) =>
Parser t s r -> (Parser t s [r], Parser t s [r])
defaultManySome

defaultManySome :: (Monoid s, Alternative (Parser t s)) => Parser t s r -> (Parser t s [r], Parser t s [r])
defaultManySome :: Parser t s r -> (Parser t s [r], Parser t s [r])
defaultManySome Parser t s r
p = (Parser t s [r]
many, Parser t s [r]
some)
   where many :: Parser t s [r]
many = ([r] -> [r]) -> Parser t s [r] -> Parser t s [r]
forall s r t. Monoid s => (r -> r) -> Parser t s r -> Parser t s r
resultPart [r] -> [r]
forall a. a -> a
id (Parser t s [r]
some Parser t s [r] -> Parser t s [r] -> Parser t s [r]
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> [r] -> Parser t s [r]
forall (f :: * -> *) a. Applicative f => a -> f a
pure [])
         some :: Parser t s [r]
some = (:) (r -> [r] -> [r]) -> Parser t s r -> Parser t s ([r] -> [r])
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser t s r
p Parser t s ([r] -> [r]) -> Parser t s [r] -> Parser t s [r]
forall (f :: * -> *) a.
MonoidApplicative f =>
f (a -> a) -> f a -> f a
+<*> Parser t s [r]
many
{-# INLINE defaultManySome #-}

-- instance (Monoid s, Monoid r, Show s, Show r) => Show (Parser t s r) where
--    show = showWith (show . ($ mempty)) show

showWith :: (Monoid s, Monoid r, Show s) => ((s -> Parser t s r) -> String) -> (r -> String) -> Parser t s r -> String
showWith :: ((s -> Parser t s r) -> String)
-> (r -> String) -> Parser t s r -> String
showWith (s -> Parser t s r) -> String
_ r -> String
_ (Failure String
s) = String
"Failure " String -> String -> String
forall a. [a] -> [a] -> [a]
++ String -> String
forall a. Show a => a -> String
show String
s
showWith (s -> Parser t s r) -> String
_ r -> String
sr (Result s
s r
r) = String
"(Result " String -> String -> String
forall a. [a] -> [a] -> [a]
++ s -> String -> String
forall a. Show a => a -> String -> String
shows s
s (String
" " String -> String -> String
forall a. [a] -> [a] -> [a]
++ r -> String
sr r
r String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
")")
showWith (s -> Parser t s r) -> String
sm r -> String
sr (ResultPart r -> r
r Parser t s r
e s -> Parser t s r
f) =
   String
"(ResultPart (mappend " String -> String -> String
forall a. [a] -> [a] -> [a]
++ r -> String
sr (r -> r
r r
forall a. Monoid a => a
mempty) String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
") " String -> String -> String
forall a. [a] -> [a] -> [a]
++ ((s -> Parser t s r) -> String)
-> (r -> String) -> Parser t s r -> String
forall s r t.
(Monoid s, Monoid r, Show s) =>
((s -> Parser t s r) -> String)
-> (r -> String) -> Parser t s r -> String
showWith (s -> Parser t s r) -> String
sm r -> String
sr Parser t s r
e String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" " String -> String -> String
forall a. [a] -> [a] -> [a]
++ (s -> Parser t s r) -> String
sm s -> Parser t s r
f String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
")"
showWith (s -> Parser t s r) -> String
_ r -> String
_ (ResultStructure Maybe s
s g (Parser t s)
_) = String
"(ResultStructure " String -> String -> String
forall a. [a] -> [a] -> [a]
++ Maybe s -> String -> String
forall a. Show a => a -> String -> String
shows Maybe s
s String
")"
showWith (s -> Parser t s r) -> String
sm r -> String
sr (Choice Parser t s r
p1 Parser t s r
p2) = String
"(Choice " String -> String -> String
forall a. [a] -> [a] -> [a]
++ ((s -> Parser t s r) -> String)
-> (r -> String) -> Parser t s r -> String
forall s r t.
(Monoid s, Monoid r, Show s) =>
((s -> Parser t s r) -> String)
-> (r -> String) -> Parser t s r -> String
showWith (s -> Parser t s r) -> String
sm r -> String
sr Parser t s r
p1 String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" " String -> String -> String
forall a. [a] -> [a] -> [a]
++ ((s -> Parser t s r) -> String)
-> (r -> String) -> Parser t s r -> String
forall s r t.
(Monoid s, Monoid r, Show s) =>
((s -> Parser t s r) -> String)
-> (r -> String) -> Parser t s r -> String
showWith (s -> Parser t s r) -> String
sm r -> String
sr Parser t s r
p2 String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
")"
showWith (s -> Parser t s r) -> String
sm r -> String
sr (Delay Parser t s r
e s -> Parser t s r
f) = String
"(Delay " String -> String -> String
forall a. [a] -> [a] -> [a]
++ ((s -> Parser t s r) -> String)
-> (r -> String) -> Parser t s r -> String
forall s r t.
(Monoid s, Monoid r, Show s) =>
((s -> Parser t s r) -> String)
-> (r -> String) -> Parser t s r -> String
showWith (s -> Parser t s r) -> String
sm r -> String
sr Parser t s r
e String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" " String -> String -> String
forall a. [a] -> [a] -> [a]
++ (s -> Parser t s r) -> String
sm s -> Parser t s r
f String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
")"

-- | Like 'fmap', but capable of mapping partial results, being restricted to 'Monoid' types only.
mapIncremental :: (Monoid s, Monoid a, Monoid b) => (a -> b) -> Parser p s a -> Parser p s b
mapIncremental :: (a -> b) -> Parser p s a -> Parser p s b
mapIncremental a -> b
f (Result s
s a
r) = s -> b -> Parser p s b
forall s r t. s -> r -> Parser t s r
Result s
s (a -> b
f a
r)
mapIncremental a -> b
g (ResultPart a -> a
r Parser p s a
e s -> Parser p s a
f) = 
   (b -> b) -> Parser p s b -> (s -> Parser p s b) -> Parser p s b
forall r t s.
(r -> r) -> Parser t s r -> (s -> Parser t s r) -> Parser t s r
ResultPart (b -> b -> b
forall a. Monoid a => a -> a -> a
mappend (b -> b -> b) -> b -> b -> b
forall a b. (a -> b) -> a -> b
$ a -> b
g (a -> b) -> a -> b
forall a b. (a -> b) -> a -> b
$ a -> a
r a
forall a. Monoid a => a
mempty) ((a -> b) -> Parser p s a -> Parser p s b
forall s a b p.
(Monoid s, Monoid a, Monoid b) =>
(a -> b) -> Parser p s a -> Parser p s b
mapIncremental a -> b
g Parser p s a
e) ((a -> b) -> Parser p s a -> Parser p s b
forall s a b p.
(Monoid s, Monoid a, Monoid b) =>
(a -> b) -> Parser p s a -> Parser p s b
mapIncremental a -> b
g (Parser p s a -> Parser p s b)
-> (s -> Parser p s a) -> s -> Parser p s b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. s -> Parser p s a
f)
mapIncremental a -> b
f Parser p s a
p = (Parser p s a -> Parser p s b) -> Parser p s a -> Parser p s b
forall s t r r'.
Monoid s =>
(Parser t s r -> Parser t s r') -> Parser t s r -> Parser t s r'
apply ((a -> b) -> Parser p s a -> Parser p s b
forall s a b p.
(Monoid s, Monoid a, Monoid b) =>
(a -> b) -> Parser p s a -> Parser p s b
mapIncremental a -> b
f) Parser p s a
p

-- | Behaves like the argument parser, but without consuming any input.
lookAhead :: Monoid s => Parser t s r -> Parser t s r
lookAhead :: Parser t s r -> Parser t s r
lookAhead Parser t s r
p = s -> Parser t s r -> Parser t s r
forall s t r. Monoid s => s -> Parser t s r -> Parser t s r
lookAheadInto s
forall a. Monoid a => a
mempty Parser t s r
p

lookAheadInto :: Monoid s => s -> Parser t s r -> Parser t s r
lookAheadInto :: s -> Parser t s r -> Parser t s r
lookAheadInto s
_ p :: Parser t s r
p@Failure{}        = Parser t s r
p
lookAheadInto s
t (Result s
_ r
r)       = s -> r -> Parser t s r
forall s r t. s -> r -> Parser t s r
Result s
t r
r
lookAheadInto s
t (ResultPart r -> r
r Parser t s r
e s -> Parser t s r
f) = (r -> r) -> Parser t s r -> (s -> Parser t s r) -> Parser t s r
forall r t s.
(r -> r) -> Parser t s r -> (s -> Parser t s r) -> Parser t s r
ResultPart r -> r
r (s -> Parser t s r -> Parser t s r
forall s t r. Monoid s => s -> Parser t s r -> Parser t s r
lookAheadInto s
t Parser t s r
e) (\s
s-> s -> Parser t s r -> Parser t s r
forall s t r. Monoid s => s -> Parser t s r -> Parser t s r
lookAheadInto (s -> s -> s
forall a. Monoid a => a -> a -> a
mappend s
t s
s) (s -> Parser t s r
f s
s))
lookAheadInto s
t (ResultStructure Maybe s
_ g (Parser t s)
r) = Maybe s -> g (Parser t s) -> Parser t s (g m)
forall s (g :: (* -> *) -> *) (m :: * -> *) t.
(Monoid s, Traversable g, Applicative m) =>
Maybe s -> g (Parser t s) -> Parser t s (g m)
ResultStructure (s -> Maybe s
forall a. a -> Maybe a
Just s
t) g (Parser t s)
r
lookAheadInto s
t (Choice Parser t s r
p1 Parser t s r
p2)     = s -> Parser t s r -> Parser t s r
forall s t r. Monoid s => s -> Parser t s r -> Parser t s r
lookAheadInto s
t Parser t s r
p1 Parser t s r -> Parser t s r -> Parser t s r
forall t s r. Parser t s r -> Parser t s r -> Parser t s r
<||> s -> Parser t s r -> Parser t s r
forall s t r. Monoid s => s -> Parser t s r -> Parser t s r
lookAheadInto s
t Parser t s r
p2
lookAheadInto s
t (Delay Parser t s r
e s -> Parser t s r
f)        = Parser t s r -> (s -> Parser t s r) -> Parser t s r
forall t s r. Parser t s r -> (s -> Parser t s r) -> Parser t s r
Delay (s -> Parser t s r -> Parser t s r
forall s t r. Monoid s => s -> Parser t s r -> Parser t s r
lookAheadInto s
t Parser t s r
e) (\s
s-> s -> Parser t s r -> Parser t s r
forall s t r. Monoid s => s -> Parser t s r -> Parser t s r
lookAheadInto (s -> s -> s
forall a. Monoid a => a -> a -> a
mappend s
t s
s) (s -> Parser t s r
f s
s))

-- | Does not consume any input; succeeds (with 'mempty' result) iff the argument parser fails.
notFollowedBy :: (Monoid s, Monoid r) => Parser t s r' -> Parser t s r
notFollowedBy :: Parser t s r' -> Parser t s r
notFollowedBy = s -> Parser t s r' -> Parser t s r
forall s r t r'.
(Monoid s, Monoid r) =>
s -> Parser t s r' -> Parser t s r
lookAheadNotInto s
forall a. Monoid a => a
mempty
   where lookAheadNotInto :: (Monoid s, Monoid r) => s -> Parser t s r' -> Parser t s r
         lookAheadNotInto :: s -> Parser t s r' -> Parser t s r
lookAheadNotInto s
t Failure{}   = s -> r -> Parser t s r
forall s r t. s -> r -> Parser t s r
Result s
t r
forall a. Monoid a => a
mempty
         lookAheadNotInto s
t (Delay Parser t s r'
e s -> Parser t s r'
f) = Parser t s r -> (s -> Parser t s r) -> Parser t s r
forall t s r. Parser t s r -> (s -> Parser t s r) -> Parser t s r
Delay (s -> Parser t s r' -> Parser t s r
forall s r t r'.
(Monoid s, Monoid r) =>
s -> Parser t s r' -> Parser t s r
lookAheadNotInto s
t Parser t s r'
e) (\s
s-> s -> Parser t s r' -> Parser t s r
forall s r t r'.
(Monoid s, Monoid r) =>
s -> Parser t s r' -> Parser t s r
lookAheadNotInto (s -> s -> s
forall a. Monoid a => a -> a -> a
mappend s
t s
s) (s -> Parser t s r'
f s
s))
         lookAheadNotInto s
t Parser t s r'
p | Parser t s r' -> Bool
forall t s r. Parser t s r -> Bool
isInfallible Parser t s r'
p = String -> Parser t s r
forall t s r. String -> Parser t s r
Failure String
"notFollowedBy"
                              | Bool
otherwise = Parser t s r -> (s -> Parser t s r) -> Parser t s r
forall t s r. Parser t s r -> (s -> Parser t s r) -> Parser t s r
Delay (s -> Parser t s r' -> Parser t s r
forall s r t r'.
(Monoid s, Monoid r) =>
s -> Parser t s r' -> Parser t s r
lookAheadNotInto s
t (Parser t s r' -> Parser t s r) -> Parser t s r' -> Parser t s r
forall a b. (a -> b) -> a -> b
$ Parser t s r' -> Parser t s r'
forall s t r. Monoid s => Parser t s r -> Parser t s r
feedEof Parser t s r'
p) 
                                                  (\s
s-> s -> Parser t s r' -> Parser t s r
forall s r t r'.
(Monoid s, Monoid r) =>
s -> Parser t s r' -> Parser t s r
lookAheadNotInto (s -> s -> s
forall a. Monoid a => a -> a -> a
mappend s
t s
s) (s -> Parser t s r' -> Parser t s r'
forall s t r. Monoid s => s -> Parser t s r -> Parser t s r
feed s
s Parser t s r'
p))

-- | Provides a partial parsing result.
resultPart :: Monoid s => (r -> r) -> Parser t s r -> Parser t s r
resultPart :: (r -> r) -> Parser t s r -> Parser t s r
resultPart r -> r
_ Failure{} = String -> Parser t s r
forall a. HasCallStack => String -> a
error String
"Internal contradiction"
resultPart r -> r
f (Result s
s r
r) = s -> r -> Parser t s r
forall s r t. s -> r -> Parser t s r
Result s
s (r -> r
f r
r)
resultPart r -> r
r1 (ResultPart r -> r
r2 Parser t s r
e s -> Parser t s r
f) = (r -> r) -> Parser t s r -> (s -> Parser t s r) -> Parser t s r
forall r t s.
(r -> r) -> Parser t s r -> (s -> Parser t s r) -> Parser t s r
ResultPart (r -> r
r1 (r -> r) -> (r -> r) -> r -> r
forall b c a. (b -> c) -> (a -> b) -> a -> c
. r -> r
r2) Parser t s r
e s -> Parser t s r
f
resultPart r -> r
r Parser t s r
p = (r -> r) -> Parser t s r -> (s -> Parser t s r) -> Parser t s r
forall r t s.
(r -> r) -> Parser t s r -> (s -> Parser t s r) -> Parser t s r
ResultPart r -> r
r (Parser t s r -> Parser t s r
forall s t r. Monoid s => Parser t s r -> Parser t s r
feedEof Parser t s r
p) ((s -> Parser t s r -> Parser t s r)
-> Parser t s r -> s -> Parser t s r
forall a b c. (a -> b -> c) -> b -> a -> c
flip s -> Parser t s r -> Parser t s r
forall s t r. Monoid s => s -> Parser t s r -> Parser t s r
feed Parser t s r
p)

-- | Combine a record of parsers into a record parser.
record :: (Rank2.Traversable g, Applicative m, Monoid s) => g (Parser t s) -> Parser t s (g m)
record :: g (Parser t s) -> Parser t s (g m)
record = Maybe s -> g (Parser t s) -> Parser t s (g m)
forall s (g :: (* -> *) -> *) (m :: * -> *) t.
(Monoid s, Traversable g, Applicative m) =>
Maybe s -> g (Parser t s) -> Parser t s (g m)
ResultStructure Maybe s
forall a. Maybe a
Nothing

isInfallible :: Parser t s r -> Bool
isInfallible :: Parser t s r -> Bool
isInfallible Result{} = Bool
True
isInfallible ResultPart{} = Bool
True
isInfallible (Choice Parser t s r
p Parser t s r
_) = Parser t s r -> Bool
forall t s r. Parser t s r -> Bool
isInfallible Parser t s r
p
isInfallible Parser t s r
_ = Bool
False

prepend :: (r -> r) -> Parser t s r -> Parser t s r
prepend :: (r -> r) -> Parser t s r -> Parser t s r
prepend r -> r
_ p :: Parser t s r
p@Failure{} = Parser t s r
p
prepend r -> r
r1 (Result s
s r
r2) = s -> r -> Parser t s r
forall s r t. s -> r -> Parser t s r
Result s
s (r -> r
r1 r
r2)
prepend r -> r
r1 (ResultPart r -> r
r2 Parser t s r
e s -> Parser t s r
f) = (r -> r) -> Parser t s r -> (s -> Parser t s r) -> Parser t s r
forall r t s.
(r -> r) -> Parser t s r -> (s -> Parser t s r) -> Parser t s r
ResultPart (r -> r
r1 (r -> r) -> (r -> r) -> r -> r
forall b c a. (b -> c) -> (a -> b) -> a -> c
. r -> r
r2) Parser t s r
e s -> Parser t s r
f
prepend r -> r
r p :: Parser t s r
p@ResultStructure{} = Parser t s r -> (s -> Parser t s r) -> Parser t s r
forall t s r. Parser t s r -> (s -> Parser t s r) -> Parser t s r
Delay ((r -> r) -> Parser t s r -> Parser t s r
forall r t s. (r -> r) -> Parser t s r -> Parser t s r
prepend r -> r
r (Parser t s r -> Parser t s r) -> Parser t s r -> Parser t s r
forall a b. (a -> b) -> a -> b
$ Parser t s r -> Parser t s r
forall s t r. Monoid s => Parser t s r -> Parser t s r
feedEof Parser t s r
p) (\s
s-> (r -> r) -> Parser t s r -> Parser t s r
forall r t s. (r -> r) -> Parser t s r -> Parser t s r
prepend r -> r
r (Parser t s r -> Parser t s r) -> Parser t s r -> Parser t s r
forall a b. (a -> b) -> a -> b
$ s -> Parser t s r -> Parser t s r
forall s t r. Monoid s => s -> Parser t s r -> Parser t s r
feed s
s Parser t s r
p)
prepend r -> r
r (Choice Parser t s r
p1 Parser t s r
p2) = Parser t s r -> Parser t s r -> Parser t s r
forall t s r. Parser t s r -> Parser t s r -> Parser t s r
Choice ((r -> r) -> Parser t s r -> Parser t s r
forall r t s. (r -> r) -> Parser t s r -> Parser t s r
prepend r -> r
r Parser t s r
p1) ((r -> r) -> Parser t s r -> Parser t s r
forall r t s. (r -> r) -> Parser t s r -> Parser t s r
prepend r -> r
r Parser t s r
p2)
prepend r -> r
r (Delay Parser t s r
e s -> Parser t s r
f) = Parser t s r -> (s -> Parser t s r) -> Parser t s r
forall t s r. Parser t s r -> (s -> Parser t s r) -> Parser t s r
Delay ((r -> r) -> Parser t s r -> Parser t s r
forall r t s. (r -> r) -> Parser t s r -> Parser t s r
prepend r -> r
r Parser t s r
e) ((r -> r) -> Parser t s r -> Parser t s r
forall r t s. (r -> r) -> Parser t s r -> Parser t s r
prepend r -> r
r (Parser t s r -> Parser t s r)
-> (s -> Parser t s r) -> s -> Parser t s r
forall b c a. (b -> c) -> (a -> b) -> a -> c
. s -> Parser t s r
f)

apply :: Monoid s => (Parser t s r -> Parser t s r') -> Parser t s r -> Parser t s r'
apply :: (Parser t s r -> Parser t s r') -> Parser t s r -> Parser t s r'
apply Parser t s r -> Parser t s r'
_ (Failure String
s) = String -> Parser t s r'
forall t s r. String -> Parser t s r
Failure String
s
apply Parser t s r -> Parser t s r'
f (Choice Parser t s r
p1 Parser t s r
p2) = Parser t s r -> Parser t s r'
f Parser t s r
p1 Parser t s r' -> Parser t s r' -> Parser t s r'
forall t s r. Parser t s r -> Parser t s r -> Parser t s r
<||> Parser t s r -> Parser t s r'
f Parser t s r
p2
apply Parser t s r -> Parser t s r'
g (Delay Parser t s r
e s -> Parser t s r
f) = Parser t s r' -> (s -> Parser t s r') -> Parser t s r'
forall t s r. Parser t s r -> (s -> Parser t s r) -> Parser t s r
Delay (Parser t s r -> Parser t s r'
g Parser t s r
e) (Parser t s r -> Parser t s r'
g (Parser t s r -> Parser t s r')
-> (s -> Parser t s r) -> s -> Parser t s r'
forall b c a. (b -> c) -> (a -> b) -> a -> c
. s -> Parser t s r
f)
apply Parser t s r -> Parser t s r'
g (ResultPart r -> r
r Parser t s r
e s -> Parser t s r
f) = Parser t s r' -> (s -> Parser t s r') -> Parser t s r'
forall t s r. Parser t s r -> (s -> Parser t s r) -> Parser t s r
Delay (Parser t s r -> Parser t s r'
g (Parser t s r -> Parser t s r') -> Parser t s r -> Parser t s r'
forall a b. (a -> b) -> a -> b
$ (r -> r) -> Parser t s r -> Parser t s r
forall r t s. (r -> r) -> Parser t s r -> Parser t s r
prepend r -> r
r Parser t s r
e) (Parser t s r -> Parser t s r'
g (Parser t s r -> Parser t s r')
-> (s -> Parser t s r) -> s -> Parser t s r'
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (r -> r) -> Parser t s r -> Parser t s r
forall r t s. (r -> r) -> Parser t s r -> Parser t s r
prepend r -> r
r (Parser t s r -> Parser t s r)
-> (s -> Parser t s r) -> s -> Parser t s r
forall b c a. (b -> c) -> (a -> b) -> a -> c
. s -> Parser t s r
f)
apply Parser t s r -> Parser t s r'
f Parser t s r
p = Parser t s r' -> (s -> Parser t s r') -> Parser t s r'
forall t s r. Parser t s r -> (s -> Parser t s r) -> Parser t s r
Delay (Parser t s r -> Parser t s r'
f (Parser t s r -> Parser t s r') -> Parser t s r -> Parser t s r'
forall a b. (a -> b) -> a -> b
$ Parser t s r -> Parser t s r
forall s t r. Monoid s => Parser t s r -> Parser t s r
feedEof Parser t s r
p) (\s
s-> Parser t s r -> Parser t s r'
f (Parser t s r -> Parser t s r') -> Parser t s r -> Parser t s r'
forall a b. (a -> b) -> a -> b
$ s -> Parser t s r -> Parser t s r
forall s t r. Monoid s => s -> Parser t s r -> Parser t s r
feed s
s Parser t s r
p)

-- | Modifies the parser type
mapType :: (forall a. Parser t s a -> Parser b s a) -> Parser t s r -> Parser b s r
mapType :: (forall a. Parser t s a -> Parser b s a)
-> Parser t s r -> Parser b s r
mapType forall a. Parser t s a -> Parser b s a
_ (Failure String
s) = String -> Parser b s r
forall t s r. String -> Parser t s r
Failure String
s
mapType forall a. Parser t s a -> Parser b s a
_ (Result s
s r
r) = s -> r -> Parser b s r
forall s r t. s -> r -> Parser t s r
Result s
s r
r
mapType forall a. Parser t s a -> Parser b s a
g (ResultPart r -> r
r Parser t s r
e s -> Parser t s r
f) = (r -> r) -> Parser b s r -> (s -> Parser b s r) -> Parser b s r
forall r t s.
(r -> r) -> Parser t s r -> (s -> Parser t s r) -> Parser t s r
ResultPart r -> r
r (Parser t s r -> Parser b s r
forall a. Parser t s a -> Parser b s a
g Parser t s r
e) (Parser t s r -> Parser b s r
forall a. Parser t s a -> Parser b s a
g (Parser t s r -> Parser b s r)
-> (s -> Parser t s r) -> s -> Parser b s r
forall b c a. (b -> c) -> (a -> b) -> a -> c
. s -> Parser t s r
f)
mapType forall a. Parser t s a -> Parser b s a
f (ResultStructure Maybe s
s g (Parser t s)
r) = Maybe s -> g (Parser b s) -> Parser b s (g m)
forall s (g :: (* -> *) -> *) (m :: * -> *) t.
(Monoid s, Traversable g, Applicative m) =>
Maybe s -> g (Parser t s) -> Parser t s (g m)
ResultStructure Maybe s
s ((forall a. Parser t s a -> Parser b s a)
-> Parser t s a -> Parser b s a
forall t s b r.
(forall a. Parser t s a -> Parser b s a)
-> Parser t s r -> Parser b s r
mapType forall a. Parser t s a -> Parser b s a
f (forall a. Parser t s a -> Parser b s a)
-> g (Parser t s) -> g (Parser b s)
forall k (g :: (k -> *) -> *) (p :: k -> *) (q :: k -> *).
Functor g =>
(forall (a :: k). p a -> q a) -> g p -> g q
Rank2.<$> g (Parser t s)
r)
mapType forall a. Parser t s a -> Parser b s a
f (Choice Parser t s r
p1 Parser t s r
p2) = Parser b s r -> Parser b s r -> Parser b s r
forall t s r. Parser t s r -> Parser t s r -> Parser t s r
Choice (Parser t s r -> Parser b s r
forall a. Parser t s a -> Parser b s a
f Parser t s r
p1) (Parser t s r -> Parser b s r
forall a. Parser t s a -> Parser b s a
f Parser t s r
p2)
mapType forall a. Parser t s a -> Parser b s a
g (Delay Parser t s r
e s -> Parser t s r
f) = Parser b s r -> (s -> Parser b s r) -> Parser b s r
forall t s r. Parser t s r -> (s -> Parser t s r) -> Parser t s r
Delay (Parser t s r -> Parser b s r
forall a. Parser t s a -> Parser b s a
g Parser t s r
e) (Parser t s r -> Parser b s r
forall a. Parser t s a -> Parser b s a
g (Parser t s r -> Parser b s r)
-> (s -> Parser t s r) -> s -> Parser b s r
forall b c a. (b -> c) -> (a -> b) -> a -> c
. s -> Parser t s r
f)

-- | Converts a parser accepting one input type to another. The argument functions @forth@ and @back@ must be inverses
-- of each other and they must distribute through @<>@:
--
-- > f (s1 <> s2) == f s1 <> f s2
mapInput :: (Monoid s, Monoid s') => (s -> s') -> (s' -> s) -> Parser t s r -> Parser t s' r
mapInput :: (s -> s') -> (s' -> s) -> Parser t s r -> Parser t s' r
mapInput s -> s'
_ s' -> s
_        (Failure String
msg) = String -> Parser t s' r
forall t s r. String -> Parser t s r
Failure String
msg
mapInput s -> s'
forth s' -> s
_    (Result s
s r
r) = s' -> r -> Parser t s' r
forall s r t. s -> r -> Parser t s r
Result (s -> s'
forth s
s) r
r
mapInput s -> s'
forth s' -> s
back (ResultPart r -> r
r Parser t s r
e s -> Parser t s r
f) = (r -> r) -> Parser t s' r -> (s' -> Parser t s' r) -> Parser t s' r
forall r t s.
(r -> r) -> Parser t s r -> (s -> Parser t s r) -> Parser t s r
ResultPart r -> r
r ((s -> s') -> (s' -> s) -> Parser t s r -> Parser t s' r
forall s s' t r.
(Monoid s, Monoid s') =>
(s -> s') -> (s' -> s) -> Parser t s r -> Parser t s' r
mapInput s -> s'
forth s' -> s
back Parser t s r
e) ((s -> s') -> (s' -> s) -> Parser t s r -> Parser t s' r
forall s s' t r.
(Monoid s, Monoid s') =>
(s -> s') -> (s' -> s) -> Parser t s r -> Parser t s' r
mapInput s -> s'
forth s' -> s
back (Parser t s r -> Parser t s' r)
-> (s' -> Parser t s r) -> s' -> Parser t s' r
forall b c a. (b -> c) -> (a -> b) -> a -> c
. s -> Parser t s r
f (s -> Parser t s r) -> (s' -> s) -> s' -> Parser t s r
forall b c a. (b -> c) -> (a -> b) -> a -> c
. s' -> s
back)
mapInput s -> s'
forth s' -> s
back (Delay Parser t s r
e s -> Parser t s r
f) = Parser t s' r -> (s' -> Parser t s' r) -> Parser t s' r
forall t s r. Parser t s r -> (s -> Parser t s r) -> Parser t s r
Delay ((s -> s') -> (s' -> s) -> Parser t s r -> Parser t s' r
forall s s' t r.
(Monoid s, Monoid s') =>
(s -> s') -> (s' -> s) -> Parser t s r -> Parser t s' r
mapInput s -> s'
forth s' -> s
back Parser t s r
e) ((s -> s') -> (s' -> s) -> Parser t s r -> Parser t s' r
forall s s' t r.
(Monoid s, Monoid s') =>
(s -> s') -> (s' -> s) -> Parser t s r -> Parser t s' r
mapInput s -> s'
forth s' -> s
back (Parser t s r -> Parser t s' r)
-> (s' -> Parser t s r) -> s' -> Parser t s' r
forall b c a. (b -> c) -> (a -> b) -> a -> c
. s -> Parser t s r
f (s -> Parser t s r) -> (s' -> s) -> s' -> Parser t s r
forall b c a. (b -> c) -> (a -> b) -> a -> c
. s' -> s
back)
mapInput s -> s'
forth s' -> s
back (Choice Parser t s r
p1 Parser t s r
p2) = Parser t s' r -> Parser t s' r -> Parser t s' r
forall t s r. Parser t s r -> Parser t s r -> Parser t s r
Choice ((s -> s') -> (s' -> s) -> Parser t s r -> Parser t s' r
forall s s' t r.
(Monoid s, Monoid s') =>
(s -> s') -> (s' -> s) -> Parser t s r -> Parser t s' r
mapInput s -> s'
forth s' -> s
back Parser t s r
p1) ((s -> s') -> (s' -> s) -> Parser t s r -> Parser t s' r
forall s s' t r.
(Monoid s, Monoid s') =>
(s -> s') -> (s' -> s) -> Parser t s r -> Parser t s' r
mapInput s -> s'
forth s' -> s
back Parser t s r
p2)
mapInput s -> s'
forth s' -> s
back (ResultStructure Maybe s
s g (Parser t s)
r) = Maybe s' -> g (Parser t s') -> Parser t s' (g m)
forall s (g :: (* -> *) -> *) (m :: * -> *) t.
(Monoid s, Traversable g, Applicative m) =>
Maybe s -> g (Parser t s) -> Parser t s (g m)
ResultStructure (s -> s'
forth (s -> s') -> Maybe s -> Maybe s'
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe s
s) ((s -> s') -> (s' -> s) -> Parser t s a -> Parser t s' a
forall s s' t r.
(Monoid s, Monoid s') =>
(s -> s') -> (s' -> s) -> Parser t s r -> Parser t s' r
mapInput s -> s'
forth s' -> s
back (forall a. Parser t s a -> Parser t s' a)
-> g (Parser t s) -> g (Parser t s')
forall k (g :: (k -> *) -> *) (p :: k -> *) (q :: k -> *).
Functor g =>
(forall (a :: k). p a -> q a) -> g p -> g q
Rank2.<$> g (Parser t s)
r)

-- | Converts a parser accepting one input type to another, just like 'mapMaybeInput except the two argument functions can
-- demand more input by returning @Nothing@. If 'mapMaybeInput is defined for the two input inputs, then
--
-- > mapInput f g == mapMaybeInput (Just . f) (Just . g)
mapMaybeInput :: (Monoid s, Monoid s') => (s -> Maybe s') -> (s' -> Maybe s) -> Parser t s r -> Parser t s' r
mapMaybeInput :: (s -> Maybe s') -> (s' -> Maybe s) -> Parser t s r -> Parser t s' r
mapMaybeInput s -> Maybe s'
_ s' -> Maybe s
_ (Failure String
msg) = String -> Parser t s' r
forall t s r. String -> Parser t s r
Failure String
msg
mapMaybeInput s -> Maybe s'
forth s' -> Maybe s
back (Result s
s r
r) = (s -> Maybe s')
-> (s' -> Maybe s) -> (s' -> Parser t s' r) -> s -> Parser t s' r
forall s s' t r.
(Monoid s, Monoid s') =>
(s -> Maybe s')
-> (s' -> Maybe s) -> (s' -> Parser t s' r) -> s -> Parser t s' r
delayIncompletePositive s -> Maybe s'
forth s' -> Maybe s
back (s' -> r -> Parser t s' r
forall s r t. s -> r -> Parser t s r
`Result` r
r) s
s
mapMaybeInput s -> Maybe s'
forth s' -> Maybe s
back (ResultPart r -> r
r Parser t s r
e s -> Parser t s r
f) =
   (r -> r) -> Parser t s' r -> (s' -> Parser t s' r) -> Parser t s' r
forall r t s.
(r -> r) -> Parser t s r -> (s -> Parser t s r) -> Parser t s r
ResultPart r -> r
r ((s -> Maybe s') -> (s' -> Maybe s) -> Parser t s r -> Parser t s' r
forall s s' t r.
(Monoid s, Monoid s') =>
(s -> Maybe s') -> (s' -> Maybe s) -> Parser t s r -> Parser t s' r
mapMaybeInput s -> Maybe s'
forth s' -> Maybe s
back Parser t s r
e) ((s' -> Maybe s) -> (s -> Parser t s' r) -> s' -> Parser t s' r
forall s s' t r.
(Monoid s, Monoid s') =>
(s' -> Maybe s) -> (s -> Parser t s' r) -> s' -> Parser t s' r
delayIncompleteNegative s' -> Maybe s
back ((s -> Parser t s' r) -> s' -> Parser t s' r)
-> (s -> Parser t s' r) -> s' -> Parser t s' r
forall a b. (a -> b) -> a -> b
$ (s -> Maybe s') -> (s' -> Maybe s) -> Parser t s r -> Parser t s' r
forall s s' t r.
(Monoid s, Monoid s') =>
(s -> Maybe s') -> (s' -> Maybe s) -> Parser t s r -> Parser t s' r
mapMaybeInput s -> Maybe s'
forth s' -> Maybe s
back (Parser t s r -> Parser t s' r)
-> (s -> Parser t s r) -> s -> Parser t s' r
forall b c a. (b -> c) -> (a -> b) -> a -> c
. s -> Parser t s r
f)
mapMaybeInput s -> Maybe s'
forth s' -> Maybe s
back (Delay Parser t s r
e s -> Parser t s r
f) =
   Parser t s' r -> (s' -> Parser t s' r) -> Parser t s' r
forall t s r. Parser t s r -> (s -> Parser t s r) -> Parser t s r
Delay ((s -> Maybe s') -> (s' -> Maybe s) -> Parser t s r -> Parser t s' r
forall s s' t r.
(Monoid s, Monoid s') =>
(s -> Maybe s') -> (s' -> Maybe s) -> Parser t s r -> Parser t s' r
mapMaybeInput s -> Maybe s'
forth s' -> Maybe s
back Parser t s r
e) ((s' -> Maybe s) -> (s -> Parser t s' r) -> s' -> Parser t s' r
forall s s' t r.
(Monoid s, Monoid s') =>
(s' -> Maybe s) -> (s -> Parser t s' r) -> s' -> Parser t s' r
delayIncompleteNegative s' -> Maybe s
back ((s -> Parser t s' r) -> s' -> Parser t s' r)
-> (s -> Parser t s' r) -> s' -> Parser t s' r
forall a b. (a -> b) -> a -> b
$ (s -> Maybe s') -> (s' -> Maybe s) -> Parser t s r -> Parser t s' r
forall s s' t r.
(Monoid s, Monoid s') =>
(s -> Maybe s') -> (s' -> Maybe s) -> Parser t s r -> Parser t s' r
mapMaybeInput s -> Maybe s'
forth s' -> Maybe s
back (Parser t s r -> Parser t s' r)
-> (s -> Parser t s r) -> s -> Parser t s' r
forall b c a. (b -> c) -> (a -> b) -> a -> c
. s -> Parser t s r
f)
mapMaybeInput s -> Maybe s'
forth s' -> Maybe s
back (Choice Parser t s r
p1 Parser t s r
p2) = Parser t s' r -> Parser t s' r -> Parser t s' r
forall t s r. Parser t s r -> Parser t s r -> Parser t s r
Choice ((s -> Maybe s') -> (s' -> Maybe s) -> Parser t s r -> Parser t s' r
forall s s' t r.
(Monoid s, Monoid s') =>
(s -> Maybe s') -> (s' -> Maybe s) -> Parser t s r -> Parser t s' r
mapMaybeInput s -> Maybe s'
forth s' -> Maybe s
back Parser t s r
p1) ((s -> Maybe s') -> (s' -> Maybe s) -> Parser t s r -> Parser t s' r
forall s s' t r.
(Monoid s, Monoid s') =>
(s -> Maybe s') -> (s' -> Maybe s) -> Parser t s r -> Parser t s' r
mapMaybeInput s -> Maybe s'
forth s' -> Maybe s
back Parser t s r
p2)
mapMaybeInput s -> Maybe s'
forth s' -> Maybe s
back (ResultStructure (Just s
s) g (Parser t s)
r) =
   (s -> Maybe s')
-> (s' -> Maybe s)
-> (s' -> Parser t s' (g m))
-> s
-> Parser t s' (g m)
forall s s' t r.
(Monoid s, Monoid s') =>
(s -> Maybe s')
-> (s' -> Maybe s) -> (s' -> Parser t s' r) -> s -> Parser t s' r
delayIncompletePositive s -> Maybe s'
forth s' -> Maybe s
back (\s'
s'-> Maybe s' -> g (Parser t s') -> Parser t s' (g m)
forall s (g :: (* -> *) -> *) (m :: * -> *) t.
(Monoid s, Traversable g, Applicative m) =>
Maybe s -> g (Parser t s) -> Parser t s (g m)
ResultStructure (s' -> Maybe s'
forall a. a -> Maybe a
Just s'
s') ((s -> Maybe s') -> (s' -> Maybe s) -> Parser t s a -> Parser t s' a
forall s s' t r.
(Monoid s, Monoid s') =>
(s -> Maybe s') -> (s' -> Maybe s) -> Parser t s r -> Parser t s' r
mapMaybeInput s -> Maybe s'
forth s' -> Maybe s
back (forall a. Parser t s a -> Parser t s' a)
-> g (Parser t s) -> g (Parser t s')
forall k (g :: (k -> *) -> *) (p :: k -> *) (q :: k -> *).
Functor g =>
(forall (a :: k). p a -> q a) -> g p -> g q
Rank2.<$> g (Parser t s)
r)) s
s
mapMaybeInput s -> Maybe s'
forth s' -> Maybe s
back p :: Parser t s r
p@(ResultStructure Maybe s
Nothing g (Parser t s)
_) =
   Parser t s' r -> (s' -> Parser t s' r) -> Parser t s' r
forall t s r. Parser t s r -> (s -> Parser t s r) -> Parser t s r
Delay ((s -> Maybe s') -> (s' -> Maybe s) -> Parser t s r -> Parser t s' r
forall s s' t r.
(Monoid s, Monoid s') =>
(s -> Maybe s') -> (s' -> Maybe s) -> Parser t s r -> Parser t s' r
mapMaybeInput s -> Maybe s'
forth s' -> Maybe s
back (Parser t s r -> Parser t s' r) -> Parser t s r -> Parser t s' r
forall a b. (a -> b) -> a -> b
$ Parser t s r -> Parser t s r
forall s t r. Monoid s => Parser t s r -> Parser t s r
feedEof Parser t s r
p) ((s' -> Maybe s) -> (s -> Parser t s' r) -> s' -> Parser t s' r
forall s s' t r.
(Monoid s, Monoid s') =>
(s' -> Maybe s) -> (s -> Parser t s' r) -> s' -> Parser t s' r
delayIncompleteNegative s' -> Maybe s
back ((s -> Parser t s' r) -> s' -> Parser t s' r)
-> (s -> Parser t s' r) -> s' -> Parser t s' r
forall a b. (a -> b) -> a -> b
$ (s -> Maybe s') -> (s' -> Maybe s) -> Parser t s r -> Parser t s' r
forall s s' t r.
(Monoid s, Monoid s') =>
(s -> Maybe s') -> (s' -> Maybe s) -> Parser t s r -> Parser t s' r
mapMaybeInput s -> Maybe s'
forth s' -> Maybe s
back (Parser t s r -> Parser t s' r)
-> (s -> Parser t s r) -> s -> Parser t s' r
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (s -> Parser t s r -> Parser t s r
forall s t r. Monoid s => s -> Parser t s r -> Parser t s r
`feed` Parser t s r
p))

delayIncompletePositive :: (Monoid s, Monoid s') =>
                           (s -> Maybe s') -> (s' -> Maybe s) -> (s' -> Parser t s' r) -> s -> Parser t s' r
delayIncompletePositive :: (s -> Maybe s')
-> (s' -> Maybe s) -> (s' -> Parser t s' r) -> s -> Parser t s' r
delayIncompletePositive s -> Maybe s'
forth s' -> Maybe s
back s' -> Parser t s' r
f s
s =
   Parser t s' r -> (s' -> Parser t s' r) -> Maybe s' -> Parser t s' r
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (Parser t s' r -> (s' -> Parser t s' r) -> Parser t s' r
forall t s r. Parser t s r -> (s -> Parser t s r) -> Parser t s r
Delay (String -> Parser t s' r
forall a. HasCallStack => String -> a
error String
"incomplete old input") s' -> Parser t s' r
f') s' -> Parser t s' r
f (s -> Maybe s'
forth s
s)
   where f' :: s' -> Parser t s' r
f' = (s' -> Maybe s) -> (s -> Parser t s' r) -> s' -> Parser t s' r
forall s s' t r.
(Monoid s, Monoid s') =>
(s' -> Maybe s) -> (s -> Parser t s' r) -> s' -> Parser t s' r
delayIncompleteNegative s' -> Maybe s
back ((s -> Maybe s')
-> (s' -> Maybe s) -> (s' -> Parser t s' r) -> s -> Parser t s' r
forall s s' t r.
(Monoid s, Monoid s') =>
(s -> Maybe s')
-> (s' -> Maybe s) -> (s' -> Parser t s' r) -> s -> Parser t s' r
delayIncompletePositive s -> Maybe s'
forth s' -> Maybe s
back s' -> Parser t s' r
f (s -> Parser t s' r) -> (s -> s) -> s -> Parser t s' r
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (s
s s -> s -> s
forall a. Semigroup a => a -> a -> a
<>))
delayIncompleteNegative :: (Monoid s, Monoid s') => (s' -> Maybe s) -> (s -> Parser t s' r) -> s' -> Parser t s' r
delayIncompleteNegative :: (s' -> Maybe s) -> (s -> Parser t s' r) -> s' -> Parser t s' r
delayIncompleteNegative s' -> Maybe s
back s -> Parser t s' r
f s'
t =
   Parser t s' r -> (s -> Parser t s' r) -> Maybe s -> Parser t s' r
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (Parser t s' r -> (s' -> Parser t s' r) -> Parser t s' r
forall t s r. Parser t s r -> (s -> Parser t s r) -> Parser t s r
Delay (String -> Parser t s' r
forall a. HasCallStack => String -> a
error String
"incomplete new input") ((s' -> Maybe s) -> (s -> Parser t s' r) -> s' -> Parser t s' r
forall s s' t r.
(Monoid s, Monoid s') =>
(s' -> Maybe s) -> (s -> Parser t s' r) -> s' -> Parser t s' r
delayIncompleteNegative s' -> Maybe s
back s -> Parser t s' r
f (s' -> Parser t s' r) -> (s' -> s') -> s' -> Parser t s' r
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (s'
t s' -> s' -> s'
forall a. Semigroup a => a -> a -> a
<>))) s -> Parser t s' r
f (s' -> Maybe s
back s'
t)

more :: (s -> Parser t s r) -> Parser t s r
more :: (s -> Parser t s r) -> Parser t s r
more = Parser t s r -> (s -> Parser t s r) -> Parser t s r
forall t s r. Parser t s r -> (s -> Parser t s r) -> Parser t s r
Delay (String -> Parser t s r
forall t s r. String -> Parser t s r
Failure String
"expected more input, encountered end of input")

-- | A parser that fails on any non-empty input and succeeds at its end.
eof :: (MonoidNull s, Monoid r, Semigroup r) => Parser t s r
eof :: Parser t s r
eof = Parser t s r -> (s -> Parser t s r) -> Parser t s r
forall t s r. Parser t s r -> (s -> Parser t s r) -> Parser t s r
Delay Parser t s r
forall a. Monoid a => a
mempty (\s
s-> if s -> Bool
forall m. MonoidNull m => m -> Bool
null s
s then Parser t s r
forall s r t. (MonoidNull s, Monoid r, Semigroup r) => Parser t s r
eof else String -> Parser t s r
forall t s r. String -> Parser t s r
Failure String
"expected end of input")

-- | A parser that accepts any single input atom.
anyToken :: FactorialMonoid s => Parser t s s
anyToken :: Parser t s s
anyToken = (s -> Parser t s s) -> Parser t s s
forall s t r. (s -> Parser t s r) -> Parser t s r
more s -> Parser t s s
forall r t. FactorialMonoid r => r -> Parser t r r
f
   where f :: r -> Parser t r r
f r
s = case r -> Maybe (r, r)
forall m. FactorialMonoid m => m -> Maybe (m, m)
splitPrimePrefix r
s
               of Just (r
first, r
rest) -> r -> r -> Parser t r r
forall s r t. s -> r -> Parser t s r
Result r
rest r
first
                  Maybe (r, r)
Nothing -> Parser t r r
forall s t. FactorialMonoid s => Parser t s s
anyToken

-- | A parser that accepts a specific input atom.
token :: (Eq s, FactorialMonoid s) => s -> Parser t s s
token :: s -> Parser t s s
token s
x = (s -> Bool) -> Parser t s s
forall s t. FactorialMonoid s => (s -> Bool) -> Parser t s s
satisfy (s -> s -> Bool
forall a. Eq a => a -> a -> Bool
== s
x)

-- | A parser that accepts an input atom only if it satisfies the given predicate.
satisfy :: FactorialMonoid s => (s -> Bool) -> Parser t s s
satisfy :: (s -> Bool) -> Parser t s s
satisfy s -> Bool
predicate = Parser t s s
p
   where p :: Parser t s s
p = (s -> Parser t s s) -> Parser t s s
forall s t r. (s -> Parser t s r) -> Parser t s r
more s -> Parser t s s
f
         f :: s -> Parser t s s
f s
s = case s -> Maybe (s, s)
forall m. FactorialMonoid m => m -> Maybe (m, m)
splitPrimePrefix s
s
               of Just (s
first, s
rest) -> if s -> Bool
predicate s
first then s -> s -> Parser t s s
forall s r t. s -> r -> Parser t s r
Result s
rest s
first else String -> Parser t s s
forall t s r. String -> Parser t s r
Failure String
"satisfy"
                  Maybe (s, s)
Nothing -> Parser t s s
p

-- | Specialization of 'satisfy' on 'TextualMonoid' inputs, accepting an input character only if it satisfies the given
-- predicate.
satisfyChar :: TextualMonoid s => (Char -> Bool) -> Parser t s s
satisfyChar :: (Char -> Bool) -> Parser t s s
satisfyChar Char -> Bool
predicate = Parser t s s
p
   where p :: Parser t s s
p = (s -> Parser t s s) -> Parser t s s
forall s t r. (s -> Parser t s r) -> Parser t s r
more s -> Parser t s s
f
         f :: s -> Parser t s s
f s
s = case s -> Maybe (s, s)
forall m. FactorialMonoid m => m -> Maybe (m, m)
splitPrimePrefix s
s
               of Just (s
first, s
rest) -> case s -> Maybe Char
forall t. TextualMonoid t => t -> Maybe Char
Textual.characterPrefix s
first
                                        of Just Char
c -> if Char -> Bool
predicate Char
c then s -> s -> Parser t s s
forall s r t. s -> r -> Parser t s r
Result s
rest s
first
                                                     else String -> Parser t s s
forall t s r. String -> Parser t s r
Failure (String
"expected satisfyChar, encountered " String -> String -> String
forall a. [a] -> [a] -> [a]
++ Char -> String
forall a. Show a => a -> String
show Char
c)
                                           Maybe Char
Nothing -> if s -> Bool
forall m. MonoidNull m => m -> Bool
null s
rest then Parser t s s
p else String -> Parser t s s
forall t s r. String -> Parser t s r
Failure String
"satisfyChar"
                  Maybe (s, s)
Nothing -> Parser t s s
p

-- | A parser that consumes and returns the given prefix of the input.
string :: (LeftReductive s, MonoidNull s, Semigroup s) => s -> Parser t s s
string :: s -> Parser t s s
string s
x | s -> Bool
forall m. MonoidNull m => m -> Bool
null s
x = Parser t s s
forall a. Monoid a => a
mempty
string s
x = (s -> Parser t s s) -> Parser t s s
forall s t r. (s -> Parser t s r) -> Parser t s r
more (\s
y-> case (s -> s -> Maybe s
forall m. LeftReductive m => m -> m -> Maybe m
stripPrefix s
x s
y, s -> s -> Maybe s
forall m. LeftReductive m => m -> m -> Maybe m
stripPrefix s
y s
x)
                      of (Just s
y', Maybe s
_) -> s -> s -> Parser t s s
forall s r t. s -> r -> Parser t s r
Result s
y' s
x
                         (Maybe s
Nothing, Maybe s
Nothing) -> String -> Parser t s s
forall t s r. String -> Parser t s r
Failure String
"string"
                         (Maybe s
Nothing, Just s
x') -> s -> Parser t s s
forall s t.
(LeftReductive s, MonoidNull s, Semigroup s) =>
s -> Parser t s s
string s
x' Parser t s s -> Parser t s s -> Parser t s s
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> s -> Parser t s s
forall (m :: * -> *) a. Monad m => a -> m a
return s
x)

-- | A parser accepting the longest sequence of input atoms that match the given predicate; an optimized version of
-- 'concatMany . satisfy'.
takeWhile :: (FactorialMonoid s, MonoidNull s) => (s -> Bool) -> Parser t s s
takeWhile :: (s -> Bool) -> Parser t s s
takeWhile s -> Bool
pred = Parser t s s
while
   where while :: Parser t s s
while = (s -> s) -> Parser t s s -> (s -> Parser t s s) -> Parser t s s
forall r t s.
(r -> r) -> Parser t s r -> (s -> Parser t s r) -> Parser t s r
ResultPart s -> s
forall a. a -> a
id (s -> Parser t s s
forall (m :: * -> *) a. Monad m => a -> m a
return s
forall a. Monoid a => a
mempty) s -> Parser t s s
f
         f :: s -> Parser t s s
f s
s = let (s
prefix, s
suffix) = (s -> Bool) -> s -> (s, s)
forall m. FactorialMonoid m => (m -> Bool) -> m -> (m, m)
span s -> Bool
pred s
s
               in if s -> Bool
forall m. MonoidNull m => m -> Bool
null s
suffix then (s -> s) -> Parser t s s -> Parser t s s
forall s r t. Monoid s => (r -> r) -> Parser t s r -> Parser t s r
resultPart (s -> s -> s
forall a. Monoid a => a -> a -> a
mappend s
prefix) Parser t s s
while
                  else s -> s -> Parser t s s
forall s r t. s -> r -> Parser t s r
Result s
suffix s
prefix

-- | A parser accepting the longest non-empty sequence of input atoms that match the given predicate; an optimized
-- version of 'concatSome . satisfy'.
takeWhile1 :: (FactorialMonoid s, MonoidNull s) => (s -> Bool) -> Parser t s s
takeWhile1 :: (s -> Bool) -> Parser t s s
takeWhile1 s -> Bool
pred = (s -> Parser t s s) -> Parser t s s
forall s t r. (s -> Parser t s r) -> Parser t s r
more s -> Parser t s s
f
   where f :: s -> Parser t s s
f s
s | s -> Bool
forall m. MonoidNull m => m -> Bool
null s
s = (s -> Bool) -> Parser t s s
forall s t.
(FactorialMonoid s, MonoidNull s) =>
(s -> Bool) -> Parser t s s
takeWhile1 s -> Bool
pred
             | Bool
otherwise = let (s
prefix, s
suffix) = (s -> Bool) -> s -> (s, s)
forall m. FactorialMonoid m => (m -> Bool) -> m -> (m, m)
span s -> Bool
pred s
s
                           in if s -> Bool
forall m. MonoidNull m => m -> Bool
null s
prefix then String -> Parser t s s
forall t s r. String -> Parser t s r
Failure String
"takeWhile1"
                              else if s -> Bool
forall m. MonoidNull m => m -> Bool
null s
suffix then (s -> s) -> Parser t s s -> Parser t s s
forall s r t. Monoid s => (r -> r) -> Parser t s r -> Parser t s r
resultPart (s -> s -> s
forall a. Monoid a => a -> a -> a
mappend s
prefix) ((s -> Bool) -> Parser t s s
forall s t.
(FactorialMonoid s, MonoidNull s) =>
(s -> Bool) -> Parser t s s
takeWhile s -> Bool
pred)
                                   else s -> s -> Parser t s s
forall s r t. s -> r -> Parser t s r
Result s
suffix s
prefix

-- | Specialization of 'takeWhile' on 'TextualMonoid' inputs, accepting the longest sequence of input characters that
-- match the given predicate; an optimized version of 'concatMany . satisfyChar'.
takeCharsWhile :: (TextualMonoid s, MonoidNull s) => (Char -> Bool) -> Parser t s s
takeCharsWhile :: (Char -> Bool) -> Parser t s s
takeCharsWhile Char -> Bool
pred = Parser t s s
while
   where while :: Parser t s s
while = (s -> s) -> Parser t s s -> (s -> Parser t s s) -> Parser t s s
forall r t s.
(r -> r) -> Parser t s r -> (s -> Parser t s r) -> Parser t s r
ResultPart s -> s
forall a. a -> a
id (s -> Parser t s s
forall (m :: * -> *) a. Monad m => a -> m a
return s
forall a. Monoid a => a
mempty) s -> Parser t s s
f
         f :: s -> Parser t s s
f s
s = let (s
prefix, s
suffix) = (s -> Bool) -> (Char -> Bool) -> s -> (s, s)
forall t.
TextualMonoid t =>
(t -> Bool) -> (Char -> Bool) -> t -> (t, t)
Textual.span (Bool -> s -> Bool
forall a b. a -> b -> a
const Bool
False) Char -> Bool
pred s
s
               in if s -> Bool
forall m. MonoidNull m => m -> Bool
null s
suffix then (s -> s) -> Parser t s s -> Parser t s s
forall s r t. Monoid s => (r -> r) -> Parser t s r -> Parser t s r
resultPart (s -> s -> s
forall a. Monoid a => a -> a -> a
mappend s
prefix) Parser t s s
while
                  else let (s
prefix', s
suffix') = (s -> Bool) -> (Char -> Bool) -> s -> (s, s)
forall t.
TextualMonoid t =>
(t -> Bool) -> (Char -> Bool) -> t -> (t, t)
Textual.span (Bool -> s -> Bool
forall a b. a -> b -> a
const Bool
True) (Bool -> Char -> Bool
forall a b. a -> b -> a
const Bool
False) s
suffix
                       in if s -> Bool
forall m. MonoidNull m => m -> Bool
null s
prefix' then s -> s -> Parser t s s
forall s r t. s -> r -> Parser t s r
Result s
suffix s
prefix
                          else (s -> s) -> Parser t s s -> Parser t s s
forall s r t. Monoid s => (r -> r) -> Parser t s r -> Parser t s r
resultPart (s -> s -> s
forall a. Monoid a => a -> a -> a
mappend s
prefix (s -> s) -> (s -> s) -> s -> s
forall b c a. (b -> c) -> (a -> b) -> a -> c
. s -> s -> s
forall a. Monoid a => a -> a -> a
mappend s
prefix') (s -> Parser t s s
f s
suffix')

-- | Specialization of 'takeWhile1' on 'TextualMonoid' inputs, accepting the longest non-empty sequence of input atoms
-- that match the given predicate; an optimized version of 'concatSome . satisfyChar'.
takeCharsWhile1 :: (TextualMonoid s, MonoidNull s) => (Char -> Bool) -> Parser t s s
takeCharsWhile1 :: (Char -> Bool) -> Parser t s s
takeCharsWhile1 Char -> Bool
pred = (s -> Parser t s s) -> Parser t s s
forall s t r. (s -> Parser t s r) -> Parser t s r
more s -> Parser t s s
f
   where f :: s -> Parser t s s
f s
s | s -> Bool
forall m. MonoidNull m => m -> Bool
null s
s = (Char -> Bool) -> Parser t s s
forall s t.
(TextualMonoid s, MonoidNull s) =>
(Char -> Bool) -> Parser t s s
takeCharsWhile1 Char -> Bool
pred
             | Bool
otherwise = let (s
prefix, s
suffix) = (s -> Bool) -> (Char -> Bool) -> s -> (s, s)
forall t.
TextualMonoid t =>
(t -> Bool) -> (Char -> Bool) -> t -> (t, t)
Textual.span (Bool -> s -> Bool
forall a b. a -> b -> a
const Bool
False) Char -> Bool
pred s
s
                               (s
prefix', s
suffix') = (s -> Bool) -> (Char -> Bool) -> s -> (s, s)
forall t.
TextualMonoid t =>
(t -> Bool) -> (Char -> Bool) -> t -> (t, t)
Textual.span (Bool -> s -> Bool
forall a b. a -> b -> a
const Bool
True) (Bool -> Char -> Bool
forall a b. a -> b -> a
const Bool
False) s
suffix
                           in if s -> Bool
forall m. MonoidNull m => m -> Bool
null s
prefix
                              then if s -> Bool
forall m. MonoidNull m => m -> Bool
null s
prefix'
                                   then String -> Parser t s s
forall t s r. String -> Parser t s r
Failure (String
"takeCharsWhile1 encountered "
                                                  String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String -> (Char -> String) -> Maybe Char -> String
forall b a. b -> (a -> b) -> Maybe a -> b
maybe String
"a non-character" Char -> String
forall a. Show a => a -> String
show (s -> Maybe Char
forall t. TextualMonoid t => t -> Maybe Char
Textual.characterPrefix s
s))
                                   else (s -> s) -> Parser t s s -> Parser t s s
forall r t s. (r -> r) -> Parser t s r -> Parser t s r
prepend (s -> s -> s
forall a. Monoid a => a -> a -> a
mappend s
prefix') (s -> Parser t s s
f s
suffix')
                              else if s -> Bool
forall m. MonoidNull m => m -> Bool
null s
suffix then (s -> s) -> Parser t s s -> Parser t s s
forall s r t. Monoid s => (r -> r) -> Parser t s r -> Parser t s r
resultPart (s -> s -> s
forall a. Monoid a => a -> a -> a
mappend s
prefix) ((Char -> Bool) -> Parser t s s
forall s t.
(TextualMonoid s, MonoidNull s) =>
(Char -> Bool) -> Parser t s s
takeCharsWhile Char -> Bool
pred)
                                   else if s -> Bool
forall m. MonoidNull m => m -> Bool
null s
prefix' then s -> s -> Parser t s s
forall s r t. s -> r -> Parser t s r
Result s
suffix s
prefix
                                        else (s -> s) -> Parser t s s -> Parser t s s
forall s r t. Monoid s => (r -> r) -> Parser t s r -> Parser t s r
resultPart (s -> s -> s
forall a. Monoid a => a -> a -> a
mappend s
prefix (s -> s) -> (s -> s) -> s -> s
forall b c a. (b -> c) -> (a -> b) -> a -> c
. s -> s -> s
forall a. Monoid a => a -> a -> a
mappend s
prefix')
                                                        (s -> Parser t s s -> Parser t s s
forall s t r. Monoid s => s -> Parser t s r -> Parser t s r
feed s
suffix' (Parser t s s -> Parser t s s) -> Parser t s s -> Parser t s s
forall a b. (a -> b) -> a -> b
$ (Char -> Bool) -> Parser t s s
forall s t.
(TextualMonoid s, MonoidNull s) =>
(Char -> Bool) -> Parser t s s
takeCharsWhile Char -> Bool
pred)

-- | Accepts the given number of occurrences of the argument parser.
count :: (Monoid s, Monoid r, Semigroup r) => Int -> Parser t s r -> Parser t s r
count :: Int -> Parser t s r -> Parser t s r
count Int
n Parser t s r
p | Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
0 = Parser t s r
p Parser t s r -> Parser t s r -> Parser t s r
forall (f :: * -> *) a.
(MonoidApplicative f, Semigroup a) =>
f a -> f a -> f a
>< Int -> Parser t s r -> Parser t s r
forall s r t.
(Monoid s, Monoid r, Semigroup r) =>
Int -> Parser t s r -> Parser t s r
count (Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) Parser t s r
p
          | Bool
otherwise = Parser t s r
forall a. Monoid a => a
mempty

-- | Discards the results of the argument parser.
skip :: (Monoid s, Monoid r, Semigroup r) => Parser t s r' -> Parser t s r
skip :: Parser t s r' -> Parser t s r
skip Parser t s r'
p = Parser t s r'
p Parser t s r' -> Parser t s r -> Parser t s r
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> Parser t s r
forall a. Monoid a => a
mempty

-- | Repeats matching the first argument until the second one succeeds.
manyTill :: (Monoid s, Monoid r, Semigroup r) => Parser t s r -> Parser t s r' -> Parser t s r
manyTill :: Parser t s r -> Parser t s r' -> Parser t s r
manyTill Parser t s r
next Parser t s r'
end = if Parser t s r -> Bool
forall t s r. Parser t s r -> Bool
isInfallible Parser t s r
next then Parser t s r
t1 else Parser t s r
t2
   where t1 :: Parser t s r
t1 = Parser t s r' -> Parser t s r
forall s r t r'.
(Monoid s, Monoid r, Semigroup r) =>
Parser t s r' -> Parser t s r
skip Parser t s r'
end Parser t s r -> Parser t s r -> Parser t s r
forall s t r.
Monoid s =>
Parser t s r -> Parser t s r -> Parser t s r
<<|> Parser t s r -> Parser t s r -> Parser t s r
forall s r t.
(Monoid s, Semigroup r) =>
Parser t s r -> Parser t s r -> Parser t s r
appendIncremental Parser t s r
next Parser t s r
t1
         t2 :: Parser t s r
t2 = Parser t s r' -> Parser t s r
forall s r t r'.
(Monoid s, Monoid r, Semigroup r) =>
Parser t s r' -> Parser t s r
skip Parser t s r'
end Parser t s r -> Parser t s r -> Parser t s r
forall s t r.
Monoid s =>
Parser t s r -> Parser t s r -> Parser t s r
<<|> Parser t s r -> Parser t s r -> Parser t s r
forall s r t.
(Monoid s, Semigroup r) =>
Parser t s r -> Parser t s r -> Parser t s r
append Parser t s r
next Parser t s r
t2

-- | A parser that accepts and consumes all input.
acceptAll :: (Semigroup s, Monoid s) => Parser t s s
acceptAll :: Parser t s s
acceptAll = (s -> s) -> Parser t s s -> (s -> Parser t s s) -> Parser t s s
forall r t s.
(r -> r) -> Parser t s r -> (s -> Parser t s r) -> Parser t s r
ResultPart s -> s
forall a. a -> a
id Parser t s s
forall a. Monoid a => a
mempty s -> Parser t s s
forall s t. Monoid s => s -> Parser t s s
f
   where f :: s -> Parser t s s
f s
s = (s -> s) -> Parser t s s -> (s -> Parser t s s) -> Parser t s s
forall r t s.
(r -> r) -> Parser t s r -> (s -> Parser t s r) -> Parser t s r
ResultPart (s -> s -> s
forall a. Monoid a => a -> a -> a
mappend s
s) Parser t s s
forall a. Monoid a => a
mempty s -> Parser t s s
f

-- | Parallel parser conjunction: the combined parser keeps accepting input as long as both arguments do.
and :: (Monoid s, Monoid r1, Monoid r2) => Parser t s r1 -> Parser t s r2 -> Parser t s (r1, r2)
Failure String
s and :: Parser t s r1 -> Parser t s r2 -> Parser t s (r1, r2)
`and` Parser t s r2
_ = String -> Parser t s (r1, r2)
forall t s r. String -> Parser t s r
Failure String
s
Parser t s r1
_ `and` Failure String
s = String -> Parser t s (r1, r2)
forall t s r. String -> Parser t s r
Failure String
s
Parser t s r1
p `and` Result s
_ r2
r = (r1 -> (r1, r2)) -> Parser t s r1 -> Parser t s (r1, r2)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (\r1
x-> (r1
x, r2
r)) (Parser t s r1 -> Parser t s r1
forall s t r. Monoid s => Parser t s r -> Parser t s r
feedEof Parser t s r1
p)
Result s
_ r1
r `and` Parser t s r2
p = (r2 -> (r1, r2)) -> Parser t s r2 -> Parser t s (r1, r2)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (\r2
x-> (r1
r, r2
x)) (Parser t s r2 -> Parser t s r2
forall s t r. Monoid s => Parser t s r -> Parser t s r
feedEof Parser t s r2
p)
ResultPart r1 -> r1
r Parser t s r1
e s -> Parser t s r1
f `and` Parser t s r2
p | Parser t s r2 -> Bool
forall t s r. Parser t s r -> Bool
isInfallible Parser t s r2
p =
   ((r1, r2) -> (r1, r2))
-> Parser t s (r1, r2)
-> (s -> Parser t s (r1, r2))
-> Parser t s (r1, r2)
forall r t s.
(r -> r) -> Parser t s r -> (s -> Parser t s r) -> Parser t s r
ResultPart (\(r1
r1, r2
r2)-> (r1 -> r1
r r1
r1, r2
r2)) (Parser t s r1
e Parser t s r1 -> Parser t s r2 -> Parser t s (r1, r2)
forall s r1 r2 t.
(Monoid s, Monoid r1, Monoid r2) =>
Parser t s r1 -> Parser t s r2 -> Parser t s (r1, r2)
`and` Parser t s r2 -> Parser t s r2
forall s t r. Monoid s => Parser t s r -> Parser t s r
feedEof Parser t s r2
p) (\s
s-> s -> Parser t s r1
f s
s Parser t s r1 -> Parser t s r2 -> Parser t s (r1, r2)
forall s r1 r2 t.
(Monoid s, Monoid r1, Monoid r2) =>
Parser t s r1 -> Parser t s r2 -> Parser t s (r1, r2)
`and` s -> Parser t s r2 -> Parser t s r2
forall s t r. Monoid s => s -> Parser t s r -> Parser t s r
feed s
s Parser t s r2
p)
Parser t s r1
p `and` ResultPart r2 -> r2
r Parser t s r2
e s -> Parser t s r2
f | Parser t s r1 -> Bool
forall t s r. Parser t s r -> Bool
isInfallible Parser t s r1
p =
   ((r1, r2) -> (r1, r2))
-> Parser t s (r1, r2)
-> (s -> Parser t s (r1, r2))
-> Parser t s (r1, r2)
forall r t s.
(r -> r) -> Parser t s r -> (s -> Parser t s r) -> Parser t s r
ResultPart (\(r1
r1, r2
r2)-> (r1
r1, r2 -> r2
r r2
r2)) (Parser t s r1 -> Parser t s r1
forall s t r. Monoid s => Parser t s r -> Parser t s r
feedEof Parser t s r1
p Parser t s r1 -> Parser t s r2 -> Parser t s (r1, r2)
forall s r1 r2 t.
(Monoid s, Monoid r1, Monoid r2) =>
Parser t s r1 -> Parser t s r2 -> Parser t s (r1, r2)
`and` Parser t s r2
e) (\s
s-> s -> Parser t s r1 -> Parser t s r1
forall s t r. Monoid s => s -> Parser t s r -> Parser t s r
feed s
s Parser t s r1
p Parser t s r1 -> Parser t s r2 -> Parser t s (r1, r2)
forall s r1 r2 t.
(Monoid s, Monoid r1, Monoid r2) =>
Parser t s r1 -> Parser t s r2 -> Parser t s (r1, r2)
`and` s -> Parser t s r2
f s
s)
Choice Parser t s r1
p1a Parser t s r1
p1b `and` Parser t s r2
p2 = (Parser t s r1
p1a Parser t s r1 -> Parser t s r2 -> Parser t s (r1, r2)
forall s r1 r2 t.
(Monoid s, Monoid r1, Monoid r2) =>
Parser t s r1 -> Parser t s r2 -> Parser t s (r1, r2)
`and` Parser t s r2
p2) Parser t s (r1, r2) -> Parser t s (r1, r2) -> Parser t s (r1, r2)
forall t s r. Parser t s r -> Parser t s r -> Parser t s r
<||> (Parser t s r1
p1b Parser t s r1 -> Parser t s r2 -> Parser t s (r1, r2)
forall s r1 r2 t.
(Monoid s, Monoid r1, Monoid r2) =>
Parser t s r1 -> Parser t s r2 -> Parser t s (r1, r2)
`and` Parser t s r2
p2)
Parser t s r1
p1 `and` Choice Parser t s r2
p2a Parser t s r2
p2b = (Parser t s r1
p1 Parser t s r1 -> Parser t s r2 -> Parser t s (r1, r2)
forall s r1 r2 t.
(Monoid s, Monoid r1, Monoid r2) =>
Parser t s r1 -> Parser t s r2 -> Parser t s (r1, r2)
`and` Parser t s r2
p2a) Parser t s (r1, r2) -> Parser t s (r1, r2) -> Parser t s (r1, r2)
forall t s r. Parser t s r -> Parser t s r -> Parser t s r
<||> (Parser t s r1
p1 Parser t s r1 -> Parser t s r2 -> Parser t s (r1, r2)
forall s r1 r2 t.
(Monoid s, Monoid r1, Monoid r2) =>
Parser t s r1 -> Parser t s r2 -> Parser t s (r1, r2)
`and` Parser t s r2
p2b)
Parser t s r1
p1 `and` Parser t s r2
p2 = Parser t s (r1, r2)
-> (s -> Parser t s (r1, r2)) -> Parser t s (r1, r2)
forall t s r. Parser t s r -> (s -> Parser t s r) -> Parser t s r
Delay (Parser t s r1 -> Parser t s r1
forall s t r. Monoid s => Parser t s r -> Parser t s r
feedEof Parser t s r1
p1 Parser t s r1 -> Parser t s r2 -> Parser t s (r1, r2)
forall s r1 r2 t.
(Monoid s, Monoid r1, Monoid r2) =>
Parser t s r1 -> Parser t s r2 -> Parser t s (r1, r2)
`and` Parser t s r2 -> Parser t s r2
forall s t r. Monoid s => Parser t s r -> Parser t s r
feedEof Parser t s r2
p2) (\s
s-> s -> Parser t s r1 -> Parser t s r1
forall s t r. Monoid s => s -> Parser t s r -> Parser t s r
feed s
s Parser t s r1
p1 Parser t s r1 -> Parser t s r2 -> Parser t s (r1, r2)
forall s r1 r2 t.
(Monoid s, Monoid r1, Monoid r2) =>
Parser t s r1 -> Parser t s r2 -> Parser t s (r1, r2)
`and` s -> Parser t s r2 -> Parser t s r2
forall s t r. Monoid s => s -> Parser t s r -> Parser t s r
feed s
s Parser t s r2
p2)

-- | A sequence parser that preserves incremental results, otherwise equivalent to 'Alternative.liftA2' (,)
andThen :: (Monoid s, Monoid r1, Monoid r2) => Parser t s r1 -> Parser t s r2 -> Parser t s (r1, r2)
Result s
s r1
r andThen :: Parser t s r1 -> Parser t s r2 -> Parser t s (r1, r2)
`andThen` Parser t s r2
p | Parser t s r2 -> Bool
forall t s r. Parser t s r -> Bool
isInfallible Parser t s r2
p = ((r1, r2) -> (r1, r2))
-> Parser t s (r1, r2) -> Parser t s (r1, r2)
forall s r t. Monoid s => (r -> r) -> Parser t s r -> Parser t s r
resultPart ((r1, r2) -> (r1, r2) -> (r1, r2)
forall a. Monoid a => a -> a -> a
mappend (r1
r, r2
forall a. Monoid a => a
mempty)) (s -> Parser t s (r1, r2) -> Parser t s (r1, r2)
forall s t r. Monoid s => s -> Parser t s r -> Parser t s r
feed s
s ((r2 -> (r1, r2)) -> Parser t s r2 -> Parser t s (r1, r2)
forall s a b p.
(Monoid s, Monoid a, Monoid b) =>
(a -> b) -> Parser p s a -> Parser p s b
mapIncremental ((,) r1
forall a. Monoid a => a
mempty) Parser t s r2
p))
ResultPart r1 -> r1
r Parser t s r1
e s -> Parser t s r1
f `andThen` Parser t s r2
p | Parser t s r2 -> Bool
forall t s r. Parser t s r -> Bool
isInfallible Parser t s r2
p = ((r1, r2) -> (r1, r2))
-> Parser t s (r1, r2)
-> (s -> Parser t s (r1, r2))
-> Parser t s (r1, r2)
forall r t s.
(r -> r) -> Parser t s r -> (s -> Parser t s r) -> Parser t s r
ResultPart (\(r1
r1, r2
r2)-> (r1 -> r1
r r1
r1, r2
r2)) (Parser t s r1
e Parser t s r1 -> Parser t s r2 -> Parser t s (r1, r2)
forall s r1 r2 t.
(Monoid s, Monoid r1, Monoid r2) =>
Parser t s r1 -> Parser t s r2 -> Parser t s (r1, r2)
`andThen` Parser t s r2
p) ((Parser t s r1 -> Parser t s r2 -> Parser t s (r1, r2)
forall s r1 r2 t.
(Monoid s, Monoid r1, Monoid r2) =>
Parser t s r1 -> Parser t s r2 -> Parser t s (r1, r2)
`andThen` Parser t s r2
p) (Parser t s r1 -> Parser t s (r1, r2))
-> (s -> Parser t s r1) -> s -> Parser t s (r1, r2)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. s -> Parser t s r1
f)
Parser t s r1
p1 `andThen` Parser t s r2
p2 = (Parser t s r1 -> Parser t s (r1, r2))
-> Parser t s r1 -> Parser t s (r1, r2)
forall s t r r'.
Monoid s =>
(Parser t s r -> Parser t s r') -> Parser t s r -> Parser t s r'
apply (Parser t s r1 -> Parser t s r2 -> Parser t s (r1, r2)
forall s r1 r2 t.
(Monoid s, Monoid r1, Monoid r2) =>
Parser t s r1 -> Parser t s r2 -> Parser t s (r1, r2)
`andThen` Parser t s r2
p2) Parser t s r1
p1