{-
    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, ScopedTypeVariables, 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.Logic (LogicT(LogicT), observeAllT)
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 :: forall s t r. Monoid s => s -> Parser t s r -> Parser t s r
feed s
s p :: Parser t s r
p@Failure{} = s
s seq :: forall a b. a -> b -> b
`seq` Parser t s r
p
feed s
s (Result s
s' r
r) = forall s r t. s -> r -> Parser t s r
Result (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) = 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) = forall s t r. Monoid s => s -> Parser t s r -> Parser t s r
feed s
s Parser t s r
p1 forall t s r. Parser t s r -> 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) = 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. Semigroup a => a -> a -> a
<> Maybe s
s') g (Parser t s)
r'
   where (g (Parser t s)
r', Maybe s
s'') = forall s a. State s a -> s -> (a, s)
runState (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 s t r.
Monoid s =>
Parser t s r -> State (Maybe s) (Parser t s r)
feedMaybe g (Parser t s)
r) (forall a. a -> Maybe a
Just s
s)

feedMaybe :: Monoid s => Parser t s r -> State (Maybe s) (Parser t s r)
feedMaybe :: forall s t r.
Monoid s =>
Parser t s r -> State (Maybe s) (Parser t s r)
feedMaybe Parser t s r
p = 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 forall b a. b -> (a -> b) -> Maybe a -> b
maybe forall a. a -> a
id 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 -> (forall s r t. s -> r -> Parser t s r
Result forall a. Monoid a => a
mempty r
a, forall a. a -> Maybe a
Just s
s'')
                                            Failure String
msg -> (forall t s r. String -> Parser t s r
Failure String
msg, forall a. Maybe a
Nothing)
                                            Parser t s r
p'' -> (Parser t s r
p'', 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 :: forall s t r. Monoid s => 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
_) = forall r t s. (r -> r) -> Parser t s r -> Parser t s r
prepend r -> r
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) = forall s t r. Monoid s => Parser t s r -> Parser t s r
feedEof Parser t s r
p1 forall 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
_) = 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) = forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either forall t s r. String -> Parser t s r
Failure [(r, Maybe s)] -> Parser t s r
collect (forall (m :: * -> *) a. Applicative m => LogicT m a -> m [a]
observeAllT forall a b. (a -> b) -> a -> b
$ forall s (m :: * -> *) a. StateT s m a -> s -> m (a, s)
runStateT (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 (m :: * -> *) t s r.
(Applicative m, Monoid s) =>
Parser t s r -> StateT (Maybe s) (LogicT (Either String)) (m r)
feedEofMaybe g (Parser t s)
r) forall a. Maybe a
Nothing)
   where collect :: [(r, Maybe s)] -> Parser t s r
collect = forall (t :: * -> *) a. Foldable t => (a -> a -> a) -> t a -> a
foldr1 forall t s r. Parser t s r -> Parser t s r -> Parser t s r
Choice forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a -> b) -> [a] -> [b]
map (r, Maybe s) -> Parser t s r
result
         result :: (r, Maybe s) -> Parser t s r
result (r
r', Maybe s
s') = forall s r t. s -> r -> Parser t s r
Result (forall (t :: * -> *) m. (Foldable t, Monoid m) => t m -> m
fold Maybe s
s' forall a. Semigroup a => a -> a -> a
<> forall (t :: * -> *) m. (Foldable t, Monoid m) => t m -> m
fold Maybe s
s) r
r'


feedEofMaybe :: forall m t s r. (Applicative m, Monoid s) => Parser t s r -> StateT (Maybe s) (LogicT (Either String)) (m r)
feedEofMaybe :: forall (m :: * -> *) t s r.
(Applicative m, Monoid s) =>
Parser t s r -> StateT (Maybe s) (LogicT (Either String)) (m r)
feedEofMaybe Parser t s r
p = forall s (m :: * -> *) a. (s -> m (a, s)) -> StateT s m a
StateT Maybe s -> LogicT (Either String) (m r, Maybe s)
logic
   where logic :: Maybe s -> LogicT (Either String) (m r, Maybe s)
         f :: Maybe s -> forall a. ((m r, Maybe s) -> Either String a -> Either String a) -> Either String a -> Either String a
         wrap :: (r, s) -> (m r, Maybe s)
         logic :: Maybe s -> LogicT (Either String) (m r, Maybe s)
logic Maybe s
s = forall (m :: * -> *) a.
(forall r. (a -> m r -> m r) -> m r -> m r) -> LogicT m a
LogicT (Maybe s
-> forall a.
   ((m r, Maybe s) -> Either String a -> Either String a)
   -> Either String a -> Either String a
f Maybe s
s)
         f :: Maybe s
-> forall a.
   ((m r, Maybe s) -> Either String a -> Either String a)
   -> Either String a -> Either String a
f Maybe s
s (m r, Maybe s) -> Either String a -> Either String a
cons Either String a
nil = case forall s t r. Monoid s => Parser t s r -> Parser t s r
feedEof (forall b a. b -> (a -> b) -> Maybe a -> b
maybe forall a. a -> a
id 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 -> forall a b. a -> Either a b
Left String
msg
                           Parser t s r
p' -> forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr (m r, Maybe s) -> Either String a -> Either String a
cons Either String a
nil ((r, s) -> (m r, Maybe s)
wrap forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall s t r. Monoid s => Parser t s r -> [(r, s)]
completeResults Parser t s r
p')
         wrap :: (r, s) -> (m r, Maybe s)
wrap (r
r, s
s) = (forall (f :: * -> *) a. Applicative f => a -> f a
pure r
r, forall a. a -> Maybe a
Just s
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 :: forall r t s.
Monoid r =>
Parser t s r -> ([(r, s)], Maybe (r, Parser t s r))
results = forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (forall a b. a -> b -> a
const ([], forall a. Maybe a
Nothing)) (((\(Maybe (r -> r)
mf, Parser t s r
p)-> (forall a. a -> Maybe a -> a
fromMaybe forall a. a -> a
id Maybe (r -> r)
mf forall a. Monoid a => a
mempty, Parser t s r
p)) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$>) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$>) forall b c a. (b -> c) -> (a -> b) -> a -> c
. 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 :: forall t s r.
Parser t s r
-> Either String ([(r, s)], Maybe (Maybe (r -> r), Parser t s r))
inspect (Failure String
msg) = forall a b. a -> Either a b
Left String
msg
inspect (Result s
s r
r) = forall a b. b -> Either a b
Right ([(r
r, s
s)], forall a. Maybe a
Nothing)
inspect (ResultPart r -> r
r Parser t s r
e s -> Parser t s r
f) = forall a b. b -> Either a b
Right ([], forall a. a -> Maybe a
Just (forall a. a -> Maybe a
Just r -> r
r, forall r t s.
(r -> r) -> Parser t s r -> (s -> Parser t s r) -> Parser t s r
ResultPart 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) | forall t s r. Parser t s r -> Bool
isInfallible Parser t s r
p1 = forall a b. b -> Either a b
Right ([(r, s)]
results1 forall a. [a] -> [a] -> [a]
++ [(r, s)]
results2, 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) = forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (forall a b. a -> b -> a
const ([], forall a. Maybe a
Nothing)) forall a. a -> a
id (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) = forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (forall a b. a -> b -> a
const ([], forall a. Maybe a
Nothing)) forall a. a -> a
id (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')) = 
            forall a. a -> Maybe a
Just (forall a. a -> Maybe a
Just forall a. a -> a
id, forall t s r. Parser t s r -> Parser t s r -> Parser t s r
Choice (forall r t s. (r -> r) -> Parser t s r -> Parser t s r
prepend (forall a. a -> Maybe a -> a
fromMaybe forall a. a -> a
id Maybe (r -> r)
r1) Parser t s r
p1') (forall r t s. (r -> r) -> Parser t s r -> Parser t s r
prepend (forall a. a -> Maybe a -> a
fromMaybe forall a. a -> a
id Maybe (r -> r)
r2) Parser t s r
p2'))
inspect Parser t s r
p = forall a b. b -> Either a b
Right ([], forall a. a -> Maybe a
Just (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 :: forall s t r. Monoid s => 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
_) = forall a b. (a -> b) -> [a] -> [b]
map (\(r
r', s
t)-> (r -> r
r r
r', s
t)) (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) = ((forall a. Semigroup a => a -> a -> a
<> forall (t :: * -> *) m. (Foldable t, Monoid m) => t m -> m
fold Maybe s
s) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$>) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall s (m :: * -> *) a. StateT s m a -> s -> m (a, s)
runStateT (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 (m :: * -> *) s t r.
(Applicative m, Monoid s) =>
Parser t s r -> StateT s [] (m r)
complete g (Parser t s)
r) forall a. Monoid a => a
mempty
   where complete :: (Applicative m, Monoid s) => Parser t s r -> StateT s [] (m r)
         complete :: forall (m :: * -> *) s t r.
(Applicative m, Monoid s) =>
Parser t s r -> StateT s [] (m r)
complete Parser t s r
p = forall s (m :: * -> *) a. (s -> m (a, s)) -> StateT s m a
StateT (\s
s'-> forall {f :: * -> *} {a} {b}. Applicative f => (a, b) -> (f a, b)
pureFst forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall s t r. Monoid s => Parser t s r -> [(r, s)]
completeResults (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) = (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) | forall t s r. Parser t s r -> Bool
isInfallible Parser t s r
p1 = forall s t r. Monoid s => Parser t s r -> [(r, s)]
completeResults Parser t s r
p1 forall a. [a] -> [a] -> [a]
++ 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 :: forall r t s. Monoid r => Parser t s r -> (r, Parser t s r)
resultPrefix (Result s
s r
r) = (r
r, forall s r t. s -> r -> Parser t s r
Result s
s 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 forall a. Monoid a => a
mempty, forall r t s.
(r -> r) -> Parser t s r -> (s -> Parser t s r) -> Parser t s r
ResultPart forall a. a -> a
id Parser t s r
e s -> Parser t s r
f)
resultPrefix Parser t s r
p = (forall a. Monoid a => a
mempty, Parser t s r
p)

failure :: Parser t s r
failure :: forall t s r. Parser t s r
failure = 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 <?> :: forall s t r. Monoid s => Parser t s r -> String -> Parser t s r
<?> String
msg
   | [String
encountered] <- forall a. (a -> Bool) -> [a] -> [a]
filter (String
"encountered " forall m. LeftReductive m => m -> m -> Bool
`isPrefixOf`) (forall m. FactorialMonoid m => m -> [m]
tails String
old) =
        forall t s r. String -> Parser t s r
Failure (String
"expected " forall a. Semigroup a => a -> a -> a
<> String
msg forall a. Semigroup a => a -> a -> a
<> String
", " forall a. Semigroup a => a -> a -> a
<> String
encountered)
   | Bool
otherwise = forall t s r. String -> Parser t s r
Failure (String
"expected " 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 = forall s t r r'.
Monoid s =>
(Parser t s r -> Parser t s r') -> Parser t s r -> Parser t s r'
apply (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 :: forall a b. (a -> b) -> Parser t s a -> Parser t s b
fmap a -> b
f (Result s
s a
r) = 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) = forall r t s.
(r -> r) -> Parser t s r -> (s -> Parser t s r) -> Parser t s r
ResultPart forall a. a -> a
id (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
g forall a b. (a -> b) -> a -> b
$ forall r t s. (r -> r) -> Parser t s r -> Parser t s r
prepend a -> a
r forall a b. (a -> b) -> a -> b
$ forall s t r. Monoid s => Parser t s r -> Parser t s r
feedEof Parser t s a
e) (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
g forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall r t s. (r -> r) -> Parser t s r -> Parser t s r
prepend a -> a
r 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 = forall s t r r'.
Monoid s =>
(Parser t s r -> Parser t s r') -> Parser t s r -> Parser t s r'
apply (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 :: forall a. a -> Parser t s a
pure = forall s r t. s -> r -> Parser t s r
Result forall a. Monoid a => a
mempty
   Result s
s a -> b
r <*> :: forall a b. Parser t s (a -> b) -> Parser t s a -> Parser t s b
<*> Parser t s a
p = a -> b
r forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> 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 = forall s t r r'.
Monoid s =>
(Parser t s r -> Parser t s r') -> Parser t s r -> Parser t s r'
apply (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
_ *> :: forall a b. Parser t s a -> Parser t s b -> Parser t s b
*> Parser t s b
p = 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 | forall t s r. Parser t s r -> Bool
isInfallible Parser t s b
p = forall r t s.
(r -> r) -> Parser t s r -> (s -> Parser t s r) -> Parser t s r
ResultPart forall a. a -> a
id (Parser t s a
e forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> Parser t s b
p) ((forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> Parser t s b
p) forall b c a. (b -> c) -> (a -> b) -> a -> c
. s -> Parser t s a
f)
                         | Bool
otherwise = forall t s r. Parser t s r -> (s -> Parser t s r) -> Parser t s r
Delay (Parser t s a
e forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> Parser t s b
p) ((forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> Parser t s b
p) 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 = forall s t r r'.
Monoid s =>
(Parser t s r -> Parser t s r') -> Parser t s r -> Parser t s r'
apply (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 <* :: forall a b. Parser t s a -> Parser t s b -> Parser t s a
<* Parser t s b
p = forall s t r. Monoid s => s -> Parser t s r -> Parser t s r
feed s
s Parser t s b
p forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> 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 | forall t s r. Parser t s r -> Bool
isInfallible Parser t s b
p = 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 forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* Parser t s b
p) ((forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* Parser t s b
p) 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 = forall s t r r'.
Monoid s =>
(Parser t s r -> Parser t s r') -> Parser t s r -> Parser t s r'
apply (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 :: forall a. a -> Parser t s a
return = forall (f :: * -> *) a. Applicative f => a -> f a
pure
   Result s
s a
r >>= :: forall a b. Parser t s a -> (a -> Parser t s b) -> Parser t s b
>>= a -> Parser t s b
f = 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 = forall s t r r'.
Monoid s =>
(Parser t s r -> Parser t s r') -> Parser t s r -> Parser t s r'
apply (forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= a -> Parser t s b
f) Parser t s a
p
   >> :: forall a 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 :: forall a. String -> Parser t s a
fail = forall t s r. String -> Parser t s r
Failure

instance Monoid s => MonadFix (Parser t s) where
   mfix :: forall a. (a -> Parser t s a) -> Parser t s a
mfix a -> Parser t s a
f = 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 (forall 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 = forall (m :: * -> *) a. MonadFix m => (a -> m a) -> m a
mfix (forall s t r. Monoid s => s -> Parser t s r -> Parser t s r
feed s
s forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Parser t s a
f)
            atEof :: Parser t s r -> r
            atEof :: forall r. 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 (forall r. Parser t s r -> r
atEof Parser t s r
e)
            atEof (ResultStructure Maybe s
_ g (Parser t s)
r) = (forall (f :: * -> *) a. Applicative f => a -> f a
pure forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall r. Parser t s r -> r
atEof) 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
_) = forall r. Parser t s r -> r
atEof Parser t s r
e
            atEof (Failure String
msg) = forall a. HasCallStack => String -> a
error (String
"mfix on Failure " forall a. Semigroup a => a -> a -> a
<> String
msg)
            atEof Choice{} = 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 +<*> :: forall a. Parser t s (a -> a) -> Parser t s a -> Parser t s a
+<*> Parser t s a
p = forall s r t. Monoid s => (r -> r) -> Parser t s r -> Parser t s r
resultPart a -> a
r (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 = forall s t r r'.
Monoid s =>
(Parser t s r -> Parser t s r') -> Parser t s r -> Parser t s r'
apply (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
_ >< :: forall a.
Semigroup 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 | forall t s r. Parser t s r -> Bool
isInfallible Parser t s a
p2 = 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       = 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 :: forall a. Parser t s a -> Parser t s a
try = forall a. a -> a
id
   <?> :: forall 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 :: forall a. Show a => Parser t s a -> Parser t s ()
notFollowedBy = forall s r t r'.
(Monoid s, Monoid r) =>
Parser t s r' -> Parser t s r
notFollowedBy
   skipMany :: forall a. Parser t s a -> Parser t s ()
skipMany = forall (f :: * -> *) a.
(MonoidAlternative f, Semigroup a, Monoid a) =>
f a -> f a
concatMany forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall s r t r'.
(Monoid s, Monoid r, Semigroup r) =>
Parser t s r' -> Parser t s r
skip
   skipSome :: forall a. Parser t s a -> Parser t s ()
skipSome = forall (f :: * -> *) a.
(MonoidAlternative f, Semigroup a, Monoid a) =>
f a -> f a
concatSome forall b c a. (b -> c) -> (a -> b) -> a -> c
. 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 = forall s r t. (MonoidNull s, Monoid r, Semigroup r) => Parser t s r
eof
   unexpected :: forall a. String -> Parser t s a
unexpected = forall t s r. String -> Parser t s r
Failure forall b c a. (b -> c) -> (a -> b) -> a -> c
. (String
"encountered " forall a. Semigroup a => a -> a -> a
<>)

instance (Alternative (Parser t s), MonoidNull s) => LookAheadParsing (Parser t s) where
   lookAhead :: forall a. Parser t s a -> Parser t s a
lookAhead = 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
   <<|> :: forall 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 = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (forall a. a -> Maybe a -> a
fromMaybe (forall a. HasCallStack => String -> a
error String
"isNothing . characterPrefix") forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall t. TextualMonoid t => t -> Maybe Char
Textual.characterPrefix) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall s t. TextualMonoid s => (Char -> Bool) -> Parser t s s
satisfyChar
   string :: String -> Parser t s String
string String
s = forall s t.
(LeftReductive s, MonoidNull s, Semigroup s) =>
s -> Parser t s s
string (forall a. IsString a => String -> a
fromString String
s) forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> forall (f :: * -> *) a. Applicative f => a -> f a
pure String
s
   text :: Text -> Parser t s Text
text Text
t = forall s t.
(LeftReductive s, MonoidNull s, Semigroup s) =>
s -> Parser t s s
string (forall t. TextualMonoid t => Text -> t
Textual.fromText Text
t) forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> 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 = forall s t r. Monoid s => Parser t s r -> Parser t s r
lookAhead forall s t. (Semigroup s, Monoid s) => Parser t s s
acceptAll
   anyToken :: Parser t s (ParserInput (Parser t s))
anyToken = forall s t. FactorialMonoid s => Parser t s s
anyToken
   take :: Int -> Parser t s (ParserInput (Parser t s))
take Int
n = forall s t r. (s -> Parser t s r) -> Parser t s r
more ((s, s) -> Parser t s s
f forall b c a. (b -> c) -> (a -> b) -> a -> c
. 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' forall a. Eq a => a -> a -> Bool
== Int
0 = forall s r t. s -> r -> Parser t s r
Result s
suffix s
prefix
             | Bool
otherwise = forall s r t. Monoid s => (r -> r) -> Parser t s r -> Parser t s r
resultPart (s
prefix forall a. Semigroup a => a -> a -> a
<>) (forall (m :: * -> *). InputParsing m => Int -> m (ParserInput m)
take Int
n')
             where n' :: Int
n' = Int
n forall a. Num a => a -> a -> a
- 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
p
     where p :: Parser t s s
p = forall s t r. (s -> Parser t s r) -> Parser t s r
more (Maybe (s, s) -> Parser t s s
f forall b c a. (b -> c) -> (a -> b) -> a -> c
. 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
first then forall s r t. s -> r -> Parser t s r
Result s
rest s
first else 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 = forall t s r. Parser t s r -> (s -> Parser t s r) -> Parser t s r
Delay forall a. Monoid a => a
mempty (Maybe (s, s) -> Parser t s ()
f forall b c a. (b -> c) -> (a -> b) -> a -> c
. 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
first then forall t s r. String -> Parser t s r
Failure String
"satisfy" else 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 = 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 = 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 = 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 = forall s t. TextualMonoid s => (Char -> Bool) -> Parser t s s
satisfyChar
   takeCharsWhile :: (Char -> Bool) -> Parser t s (ParserInput (Parser t s))
takeCharsWhile = 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 = 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 :: forall s r t.
(Monoid s, Semigroup r) =>
Parser t s r -> Parser t s r -> Parser t s r
appendIncremental (Result s
s r
r) Parser t s r
p = forall s r t. Monoid s => (r -> r) -> Parser t s r -> Parser t s r
resultPart (r
r forall a. Semigroup a => a -> a -> a
<>) (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 = forall r t s.
(r -> r) -> Parser t s r -> (s -> Parser t s r) -> Parser t s r
ResultPart r -> r
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) (forall a b c. (a -> b -> c) -> b -> a -> c
flip 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 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 = forall s t r r'.
Monoid s =>
(Parser t s r -> Parser t s r') -> Parser t s r -> Parser t s r'
apply (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 :: forall s r t.
(Monoid s, Semigroup r) =>
Parser t s r -> Parser t s r -> Parser t s r
append (Result s
s r
r) Parser t s r
p2 = forall r t s. (r -> r) -> Parser t s r -> Parser t s r
prepend (r
r forall a. Semigroup a => a -> a -> a
<>) (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 = forall s t r r'.
Monoid s =>
(Parser t s r -> Parser t s r') -> Parser t s r -> Parser t s r'
apply (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
(<>) = 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 = forall (m :: * -> *) a. Monad m => a -> m a
return forall a. Monoid a => a
mempty
   mappend :: Parser t s r -> Parser t s r -> Parser t s r
mappend = forall a. Semigroup a => a -> a -> a
(<>)

instance (Alternative (Parser t s), Monoid s) => MonoidAlternative (Parser t s) where
   moptional :: forall a. (Semigroup a, Monoid a) => Parser t s a -> Parser t s a
moptional Parser t s a
p = Parser t s a
p forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> forall a. Monoid a => a
mempty
   concatMany :: forall a. (Semigroup a, Monoid a) => Parser t s a -> Parser t s a
concatMany = forall a b. (a, b) -> a
fst forall b c a. (b -> c) -> (a -> b) -> a -> c
. 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 :: forall a. (Semigroup a, Monoid a) => Parser t s a -> Parser t s a
concatSome = forall a b. (a, b) -> b
snd forall b c a. (b -> c) -> (a -> b) -> a -> c
. 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 :: 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 Parser t s r
p = (Parser t s r
many, Parser t s r
some)
   where many :: Parser t s r
many = forall s r t. Monoid s => (r -> r) -> Parser t s r -> Parser t s r
resultPart forall a. a -> a
id (Parser t s r
some forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> forall a. Monoid a => a
mempty)
         some :: Parser t s r
some = 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 <||> :: forall t s r. Parser t s r -> Parser t s r -> Parser t s r
<||> Delay Parser t s r
e2 s -> Parser t s r
f2 = forall t s r. Parser t s r -> (s -> Parser t s r) -> Parser t s r
Delay (Parser t s r
e1 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 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 = 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 = 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 | forall t s r. Parser t s r -> Bool
isInfallible Parser t s r
p1a = 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 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{} = 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{} = 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 | forall t s r. Parser t s r -> Bool
isInfallible Parser t s r
p2a = 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 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 = 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{} <<|> :: forall s t r.
Monoid s =>
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
_ | 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 forall t s r. Parser t s r -> Bool
isInfallible Parser t s r
p2 then forall r t s.
(r -> r) -> Parser t s r -> (s -> Parser t s r) -> Parser t s r
ResultPart forall a. a -> a
id Parser t s r
e s -> Parser t s r
f else 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 = forall s t r. Monoid s => Parser t s r -> Parser t s r
feedEof Parser t s r
p1 forall s t r.
Monoid 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
p2
         f :: s -> Parser t s r
f s
s = forall s t r. Monoid s => s -> Parser t s r -> Parser t s r
feed s
s Parser t s r
p1 forall s t r.
Monoid s =>
Parser t s r -> 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 :: forall s t r.
(Monoid s, Alternative (Parser t s)) =>
Parser t s r -> Parser t s [r]
defaultMany = forall a b. (a, b) -> a
fst forall b c a. (b -> c) -> (a -> b) -> a -> c
. 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 :: forall s t r.
(Monoid s, Alternative (Parser t s)) =>
Parser t s r -> Parser t s [r]
defaultSome = forall a b. (a, b) -> b
snd forall b c a. (b -> c) -> (a -> b) -> a -> c
. 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 :: forall s t r.
(Monoid s, Alternative (Parser t s)) =>
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 = forall s r t. Monoid s => (r -> r) -> Parser t s r -> Parser t s r
resultPart forall a. a -> a
id (Parser t s [r]
some forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> forall (f :: * -> *) a. Applicative f => a -> f a
pure [])
         some :: Parser t s [r]
some = (:) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser t s r
p 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 :: 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
_ r -> String
_ (Failure String
s) = String
"Failure " forall a. [a] -> [a] -> [a]
++ 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 " forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String -> String
shows s
s (String
" " forall a. [a] -> [a] -> [a]
++ r -> String
sr r
r 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 " forall a. [a] -> [a] -> [a]
++ r -> String
sr (r -> r
r forall a. Monoid a => a
mempty) forall a. [a] -> [a] -> [a]
++ String
") " forall a. [a] -> [a] -> [a]
++ 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 forall a. [a] -> [a] -> [a]
++ String
" " forall a. [a] -> [a] -> [a]
++ (s -> Parser t s r) -> String
sm s -> Parser t s r
f 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 " forall a. [a] -> [a] -> [a]
++ 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 " forall a. [a] -> [a] -> [a]
++ 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 forall a. [a] -> [a] -> [a]
++ String
" " forall a. [a] -> [a] -> [a]
++ 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 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 " forall a. [a] -> [a] -> [a]
++ 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 forall a. [a] -> [a] -> [a]
++ String
" " forall a. [a] -> [a] -> [a]
++ (s -> Parser t s r) -> String
sm s -> Parser t s r
f 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 :: 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 (Result s
s a
r) = 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) = 
   forall r t s.
(r -> r) -> Parser t s r -> (s -> Parser t s r) -> Parser t s r
ResultPart (forall a. Monoid a => a -> a -> a
mappend forall a b. (a -> b) -> a -> b
$ a -> b
g forall a b. (a -> b) -> a -> b
$ a -> a
r forall a. Monoid a => a
mempty) (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) (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 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 = forall s t r r'.
Monoid s =>
(Parser t s r -> Parser t s r') -> Parser t s r -> Parser t s r'
apply (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 :: forall s t r. Monoid s => Parser t s r -> Parser t s r
lookAhead Parser t s r
p = forall s t r. Monoid s => s -> Parser t s r -> Parser t s r
lookAheadInto forall a. Monoid a => a
mempty Parser t s r
p

lookAheadInto :: Monoid s => s -> Parser t s r -> Parser t s r
lookAheadInto :: forall s t r. Monoid s => 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)       = 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) = forall r t s.
(r -> r) -> Parser t s r -> (s -> Parser t s r) -> Parser t s r
ResultPart r -> r
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-> forall s t r. Monoid s => s -> Parser t s r -> Parser t s r
lookAheadInto (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) = forall s (g :: (* -> *) -> *) (m :: * -> *) t.
(Monoid s, Traversable g, Applicative m) =>
Maybe s -> g (Parser t s) -> Parser t s (g m)
ResultStructure (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)     = forall s t r. Monoid s => s -> Parser t s r -> Parser t s r
lookAheadInto s
t Parser t s r
p1 forall t s r. Parser t s r -> 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)        = forall t s r. Parser t s r -> (s -> Parser t s r) -> Parser t s r
Delay (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-> forall s t r. Monoid s => s -> Parser t s r -> Parser t s r
lookAheadInto (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 :: forall s r t r'.
(Monoid s, Monoid r) =>
Parser t s r' -> Parser t s r
notFollowedBy = forall s r t r'.
(Monoid s, Monoid r) =>
s -> Parser t s r' -> Parser t s r
lookAheadNotInto forall a. Monoid a => a
mempty
   where lookAheadNotInto :: (Monoid s, Monoid r) => s -> Parser t s r' -> Parser t s r
         lookAheadNotInto :: forall s r t r'.
(Monoid s, Monoid r) =>
s -> Parser t s r' -> Parser t s r
lookAheadNotInto s
t Failure{}   = forall s r t. s -> r -> Parser t s r
Result s
t forall a. Monoid a => a
mempty
         lookAheadNotInto s
t (Delay Parser t s r'
e s -> Parser t s r'
f) = forall t s r. Parser t s r -> (s -> Parser t s r) -> Parser t s r
Delay (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-> forall s r t r'.
(Monoid s, Monoid r) =>
s -> Parser t s r' -> Parser t s r
lookAheadNotInto (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 | forall t s r. Parser t s r -> Bool
isInfallible Parser t s r'
p = forall t s r. String -> Parser t s r
Failure String
"notFollowedBy"
                              | Bool
otherwise = forall t s r. Parser t s r -> (s -> Parser t s r) -> Parser t s r
Delay (forall s r t r'.
(Monoid s, Monoid r) =>
s -> Parser t s r' -> Parser t s r
lookAheadNotInto s
t forall a b. (a -> b) -> a -> b
$ forall s t r. Monoid s => Parser t s r -> Parser t s r
feedEof Parser t s r'
p) 
                                                  (\s
s-> forall s r t r'.
(Monoid s, Monoid r) =>
s -> Parser t s r' -> Parser t s r
lookAheadNotInto (forall a. Monoid a => a -> a -> a
mappend s
t s
s) (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 :: forall s r t. Monoid s => (r -> r) -> Parser t s r -> Parser t s r
resultPart r -> r
_ Failure{} = forall a. HasCallStack => String -> a
error String
"Internal contradiction"
resultPart r -> r
f (Result s
s r
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) = forall r t s.
(r -> r) -> Parser t s r -> (s -> Parser t s r) -> Parser t s r
ResultPart (r -> r
r1 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 = forall r t s.
(r -> r) -> Parser t s r -> (s -> Parser t s r) -> Parser t s r
ResultPart r -> r
r (forall s t r. Monoid s => Parser t s r -> Parser t s r
feedEof Parser t s r
p) (forall a b c. (a -> b -> c) -> b -> a -> c
flip 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 :: forall (g :: (* -> *) -> *) (m :: * -> *) s t.
(Traversable g, Applicative m, Monoid s) =>
g (Parser t s) -> Parser t s (g m)
record = forall s (g :: (* -> *) -> *) (m :: * -> *) t.
(Monoid s, Traversable g, Applicative m) =>
Maybe s -> g (Parser t s) -> Parser t s (g m)
ResultStructure forall a. Maybe a
Nothing

isInfallible :: Parser t s r -> Bool
isInfallible :: forall t s r. Parser t s r -> Bool
isInfallible Result{} = Bool
True
isInfallible ResultPart{} = Bool
True
isInfallible (Choice Parser t s r
p Parser t s r
_) = 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 :: forall r t s. (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) = 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) = forall r t s.
(r -> r) -> Parser t s r -> (s -> Parser t s r) -> Parser t s r
ResultPart (r -> r
r1 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{} = forall t s r. Parser t s r -> (s -> Parser t s r) -> Parser t s r
Delay (forall r t s. (r -> r) -> Parser t s r -> Parser t s r
prepend r -> r
r forall a b. (a -> b) -> a -> b
$ forall s t r. Monoid s => Parser t s r -> Parser t s r
feedEof Parser t s r
p) (\s
s-> forall r t s. (r -> r) -> Parser t s r -> Parser t s r
prepend r -> r
r forall a b. (a -> b) -> a -> b
$ 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) = forall t s r. Parser t s r -> Parser t s r -> Parser t s r
Choice (forall r t s. (r -> r) -> Parser t s r -> Parser t s r
prepend r -> r
r Parser t s r
p1) (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) = forall t s r. Parser t s r -> (s -> Parser t s r) -> Parser t s r
Delay (forall r t s. (r -> r) -> Parser t s r -> Parser t s r
prepend r -> r
r Parser t s r
e) (forall r t s. (r -> r) -> Parser t s r -> Parser t s r
prepend r -> r
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 :: 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'
_ (Failure String
s) = 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 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) = 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 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) = 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 forall a b. (a -> b) -> a -> b
$ 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 forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall r t s. (r -> r) -> Parser t s r -> Parser t s r
prepend r -> r
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 = 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 forall a b. (a -> b) -> a -> b
$ 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 forall a b. (a -> b) -> a -> b
$ 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 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
_ (Failure String
s) = 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) = 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) = forall r t s.
(r -> r) -> Parser t s r -> (s -> Parser t s r) -> Parser t s r
ResultPart r -> r
r (forall a. Parser t s a -> Parser b s a
g Parser t s r
e) (forall a. Parser t s a -> Parser b s a
g 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) = 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 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 {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) = forall t s r. Parser t s r -> Parser t s r -> Parser t s r
Choice (forall a. Parser t s a -> Parser b s a
f Parser t s r
p1) (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) = forall t s r. Parser t s r -> (s -> Parser t s r) -> Parser t s r
Delay (forall a. Parser t s a -> Parser b s a
g Parser t s r
e) (forall a. Parser t s a -> Parser b s a
g 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 :: forall s s' t r.
(Monoid s, Monoid s') =>
(s -> s') -> (s' -> s) -> Parser t s r -> Parser t s' r
mapInput s -> s'
_ s' -> s
_        (Failure String
msg) = forall t s r. String -> Parser t s r
Failure String
msg
mapInput s -> s'
forth s' -> s
_    (Result s
s r
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) = forall r t s.
(r -> r) -> Parser t s r -> (s -> Parser t s r) -> Parser t s r
ResultPart r -> r
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) (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 b c a. (b -> c) -> (a -> b) -> a -> c
. s -> Parser t s r
f 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) = forall t s r. Parser t s r -> (s -> Parser t s r) -> Parser t s r
Delay (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) (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 b c a. (b -> c) -> (a -> b) -> a -> c
. s -> Parser t s r
f 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) = forall t s r. Parser t s r -> Parser t s r -> Parser t s r
Choice (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) (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) = 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 forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe s
s) (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 {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 :: 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'
_ s' -> Maybe s
_ (Failure String
msg) = 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) = 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 (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) =
   forall r t s.
(r -> r) -> Parser t s r -> (s -> Parser t s r) -> Parser t s r
ResultPart r -> r
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) (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 forall a b. (a -> b) -> a -> b
$ 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 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) =
   forall t s r. Parser t s r -> (s -> Parser t s r) -> Parser t s r
Delay (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) (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 forall a b. (a -> b) -> a -> b
$ 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 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) = forall t s r. Parser t s r -> Parser t s r -> Parser t s r
Choice (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) (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) =
   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'-> forall s (g :: (* -> *) -> *) (m :: * -> *) t.
(Monoid s, Traversable g, Applicative m) =>
Maybe s -> g (Parser t s) -> Parser t s (g m)
ResultStructure (forall a. a -> Maybe a
Just s'
s') (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 {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)
_) =
   forall t s r. Parser t s r -> (s -> Parser t s r) -> Parser t s r
Delay (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 b. (a -> b) -> a -> b
$ forall s t r. Monoid s => Parser t s r -> Parser t s r
feedEof Parser t s r
p) (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 forall a b. (a -> b) -> a -> b
$ 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 b c a. (b -> c) -> (a -> b) -> a -> c
. (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 :: 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
s =
   forall b a. b -> (a -> b) -> Maybe a -> b
maybe (forall t s r. Parser t s r -> (s -> Parser t s r) -> Parser t s r
Delay (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' = 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 (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 forall b c a. (b -> c) -> (a -> b) -> a -> c
. (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 :: 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'
t =
   forall b a. b -> (a -> b) -> Maybe a -> b
maybe (forall t s r. Parser t s r -> (s -> Parser t s r) -> Parser t s r
Delay (forall a. HasCallStack => String -> a
error String
"incomplete new input") (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 forall b c a. (b -> c) -> (a -> b) -> a -> c
. (s'
t 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 :: forall s t r. (s -> Parser t s r) -> Parser t s r
more = forall t s r. Parser t s r -> (s -> Parser t s r) -> Parser t s r
Delay (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 :: forall s r t. (MonoidNull s, Monoid r, Semigroup r) => Parser t s r
eof = forall t s r. Parser t s r -> (s -> Parser t s r) -> Parser t s r
Delay forall a. Monoid a => a
mempty (\s
s-> if forall m. MonoidNull m => m -> Bool
null s
s then forall s r t. (MonoidNull s, Monoid r, Semigroup r) => Parser t s r
eof else 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 :: forall s t. FactorialMonoid s => Parser t s s
anyToken = forall s t r. (s -> Parser t s r) -> Parser t s r
more forall {r} {t}. FactorialMonoid r => r -> Parser t r r
f
   where f :: r -> Parser t r r
f r
s = case forall m. FactorialMonoid m => m -> Maybe (m, m)
splitPrimePrefix r
s
               of Just (r
first, r
rest) -> forall s r t. s -> r -> Parser t s r
Result r
rest r
first
                  Maybe (r, r)
Nothing -> 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 :: forall s t. (Eq s, FactorialMonoid s) => s -> Parser t s s
token s
x = forall s t. FactorialMonoid s => (s -> Bool) -> Parser t s s
satisfy (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 :: forall s t. FactorialMonoid s => (s -> Bool) -> Parser t s s
satisfy s -> Bool
predicate = Parser t s s
p
   where p :: Parser t s s
p = 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 forall m. FactorialMonoid m => m -> Maybe (m, m)
splitPrimePrefix s
s
               of Just (s
first, s
rest) -> if s -> Bool
predicate s
first then forall s r t. s -> r -> Parser t s r
Result s
rest s
first else 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 :: forall s t. TextualMonoid s => (Char -> Bool) -> Parser t s s
satisfyChar Char -> Bool
predicate = Parser t s s
p
   where p :: Parser t s s
p = 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 forall m. FactorialMonoid m => m -> Maybe (m, m)
splitPrimePrefix s
s
               of Just (s
first, s
rest) -> case forall t. TextualMonoid t => t -> Maybe Char
Textual.characterPrefix s
first
                                        of Just Char
c -> if Char -> Bool
predicate Char
c then forall s r t. s -> r -> Parser t s r
Result s
rest s
first
                                                     else forall t s r. String -> Parser t s r
Failure (String
"expected satisfyChar, encountered " forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show Char
c)
                                           Maybe Char
Nothing -> if forall m. MonoidNull m => m -> Bool
null s
rest then Parser t s s
p else 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 :: forall s t.
(LeftReductive s, MonoidNull s, Semigroup s) =>
s -> Parser t s s
string s
x | forall m. MonoidNull m => m -> Bool
null s
x = forall a. Monoid a => a
mempty
string s
x = forall s t r. (s -> Parser t s r) -> Parser t s r
more (\s
y-> case (forall m. LeftReductive m => m -> m -> Maybe m
stripPrefix s
x s
y, forall m. LeftReductive m => m -> m -> Maybe m
stripPrefix s
y s
x)
                      of (Just s
y', Maybe s
_) -> forall s r t. s -> r -> Parser t s r
Result s
y' s
x
                         (Maybe s
Nothing, Maybe s
Nothing) -> forall t s r. String -> Parser t s r
Failure String
"string"
                         (Maybe s
Nothing, Just s
x') -> forall s t.
(LeftReductive s, MonoidNull s, Semigroup s) =>
s -> Parser t s s
string s
x' forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> 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 :: forall s t.
(FactorialMonoid s, MonoidNull s) =>
(s -> Bool) -> Parser t s s
takeWhile s -> Bool
pred = Parser t s s
while
   where while :: Parser t s s
while = forall r t s.
(r -> r) -> Parser t s r -> (s -> Parser t s r) -> Parser t s r
ResultPart forall a. a -> a
id (forall (m :: * -> *) a. Monad m => a -> m a
return 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) = forall m. FactorialMonoid m => (m -> Bool) -> m -> (m, m)
span s -> Bool
pred s
s
               in if forall m. MonoidNull m => m -> Bool
null s
suffix then forall s r t. Monoid s => (r -> r) -> Parser t s r -> Parser t s r
resultPart (forall a. Monoid a => a -> a -> a
mappend s
prefix) Parser t s s
while
                  else 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 :: forall s t.
(FactorialMonoid s, MonoidNull s) =>
(s -> Bool) -> Parser t s s
takeWhile1 s -> Bool
pred = 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 | forall m. MonoidNull m => m -> Bool
null 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) = forall m. FactorialMonoid m => (m -> Bool) -> m -> (m, m)
span s -> Bool
pred s
s
                           in if forall m. MonoidNull m => m -> Bool
null s
prefix then forall t s r. String -> Parser t s r
Failure String
"takeWhile1"
                              else if forall m. MonoidNull m => m -> Bool
null s
suffix then forall s r t. Monoid s => (r -> r) -> Parser t s r -> Parser t s r
resultPart (forall a. Monoid a => a -> a -> a
mappend s
prefix) (forall s t.
(FactorialMonoid s, MonoidNull s) =>
(s -> Bool) -> Parser t s s
takeWhile s -> Bool
pred)
                                   else 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 :: forall s t.
(TextualMonoid s, MonoidNull s) =>
(Char -> Bool) -> Parser t s s
takeCharsWhile Char -> Bool
pred = Parser t s s
while
   where while :: Parser t s s
while = forall r t s.
(r -> r) -> Parser t s r -> (s -> Parser t s r) -> Parser t s r
ResultPart forall a. a -> a
id (forall (m :: * -> *) a. Monad m => a -> m a
return 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) = forall t.
TextualMonoid t =>
(t -> Bool) -> (Char -> Bool) -> t -> (t, t)
Textual.span (forall a b. a -> b -> a
const Bool
False) Char -> Bool
pred s
s
               in if forall m. MonoidNull m => m -> Bool
null s
suffix then forall s r t. Monoid s => (r -> r) -> Parser t s r -> Parser t s r
resultPart (forall a. Monoid a => a -> a -> a
mappend s
prefix) Parser t s s
while
                  else let (s
prefix', s
suffix') = forall t.
TextualMonoid t =>
(t -> Bool) -> (Char -> Bool) -> t -> (t, t)
Textual.span (forall a b. a -> b -> a
const Bool
True) (forall a b. a -> b -> a
const Bool
False) s
suffix
                       in if forall m. MonoidNull m => m -> Bool
null s
prefix' then forall s r t. s -> r -> Parser t s r
Result s
suffix s
prefix
                          else forall s r t. Monoid s => (r -> r) -> Parser t s r -> Parser t s r
resultPart (forall a. Monoid a => a -> a -> a
mappend s
prefix forall b c a. (b -> c) -> (a -> b) -> a -> c
. 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 :: forall s t.
(TextualMonoid s, MonoidNull s) =>
(Char -> Bool) -> Parser t s s
takeCharsWhile1 Char -> Bool
pred = 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 | forall m. MonoidNull m => m -> Bool
null 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) = forall t.
TextualMonoid t =>
(t -> Bool) -> (Char -> Bool) -> t -> (t, t)
Textual.span (forall a b. a -> b -> a
const Bool
False) Char -> Bool
pred s
s
                               (s
prefix', s
suffix') = forall t.
TextualMonoid t =>
(t -> Bool) -> (Char -> Bool) -> t -> (t, t)
Textual.span (forall a b. a -> b -> a
const Bool
True) (forall a b. a -> b -> a
const Bool
False) s
suffix
                           in if forall m. MonoidNull m => m -> Bool
null s
prefix
                              then if forall m. MonoidNull m => m -> Bool
null s
prefix'
                                   then forall t s r. String -> Parser t s r
Failure (String
"takeCharsWhile1 encountered "
                                                  forall a. Semigroup a => a -> a -> a
<> forall b a. b -> (a -> b) -> Maybe a -> b
maybe String
"a non-character" forall a. Show a => a -> String
show (forall t. TextualMonoid t => t -> Maybe Char
Textual.characterPrefix s
s))
                                   else forall r t s. (r -> r) -> Parser t s r -> Parser t s r
prepend (forall a. Monoid a => a -> a -> a
mappend s
prefix') (s -> Parser t s s
f s
suffix')
                              else if forall m. MonoidNull m => m -> Bool
null s
suffix then forall s r t. Monoid s => (r -> r) -> Parser t s r -> Parser t s r
resultPart (forall a. Monoid a => a -> a -> a
mappend s
prefix) (forall s t.
(TextualMonoid s, MonoidNull s) =>
(Char -> Bool) -> Parser t s s
takeCharsWhile Char -> Bool
pred)
                                   else if forall m. MonoidNull m => m -> Bool
null s
prefix' then forall s r t. s -> r -> Parser t s r
Result s
suffix s
prefix
                                        else forall s r t. Monoid s => (r -> r) -> Parser t s r -> Parser t s r
resultPart (forall a. Monoid a => a -> a -> a
mappend s
prefix forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Monoid a => a -> a -> a
mappend s
prefix')
                                                        (forall s t r. Monoid s => s -> Parser t s r -> Parser t s r
feed s
suffix' forall a b. (a -> b) -> a -> b
$ 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 :: forall s r t.
(Monoid s, Monoid r, Semigroup r) =>
Int -> Parser t s r -> Parser t s r
count Int
n Parser t s r
p | Int
n forall a. Ord a => a -> a -> Bool
> Int
0 = Parser t s r
p forall (f :: * -> *) a.
(MonoidApplicative f, Semigroup a) =>
f a -> f a -> f a
>< forall s r t.
(Monoid s, Monoid r, Semigroup r) =>
Int -> Parser t s r -> Parser t s r
count (Int
n forall a. Num a => a -> a -> a
- Int
1) Parser t s r
p
          | Bool
otherwise = 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 :: forall s r t r'.
(Monoid s, Monoid r, Semigroup r) =>
Parser t s r' -> Parser t s r
skip Parser t s r'
p = Parser t s r'
p forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
*> 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 :: forall s r t r'.
(Monoid s, Monoid r, Semigroup r) =>
Parser t s r -> Parser t s r' -> Parser t s r
manyTill Parser t s r
next Parser t s r'
end = if 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 = 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 forall s t r.
Monoid s =>
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 = 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 forall s t r.
Monoid s =>
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 :: forall s t. (Semigroup s, Monoid s) => Parser t s s
acceptAll = forall r t s.
(r -> r) -> Parser t s r -> (s -> Parser t s r) -> Parser t s r
ResultPart forall a. a -> a
id forall a. Monoid a => a
mempty forall {s} {t}. Monoid s => s -> Parser t s s
f
   where f :: s -> Parser t s s
f s
s = forall r t s.
(r -> r) -> Parser t s r -> (s -> Parser t s r) -> Parser t s r
ResultPart (forall a. Monoid a => a -> a -> a
mappend 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 :: 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
_ = forall t s r. String -> Parser t s r
Failure String
s
Parser t s r1
_ `and` Failure String
s = forall t s r. String -> Parser t s r
Failure String
s
Parser t s r1
p `and` Result s
_ r2
r = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (\r1
x-> (r1
x, r2
r)) (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 = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (\r2
x-> (r1
r, r2
x)) (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 | forall t s r. Parser t s r -> Bool
isInfallible Parser t s r2
p =
   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 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` 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 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` 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 | forall t s r. Parser t s r -> Bool
isInfallible Parser t s r1
p =
   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)) (forall s t r. Monoid s => Parser t s r -> Parser t s r
feedEof Parser t s r1
p 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-> forall s t r. Monoid s => s -> Parser t s r -> Parser t s r
feed s
s Parser t s r1
p 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 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) forall t s r. Parser t s r -> Parser t s r -> Parser t s r
<||> (Parser t s r1
p1b 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 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) forall t s r. Parser t s r -> Parser t s r -> Parser t s r
<||> (Parser t s r1
p1 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 = forall t s r. Parser t s r -> (s -> Parser t s r) -> Parser t s r
Delay (forall s t r. Monoid s => Parser t s r -> Parser t s r
feedEof Parser t s r1
p1 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` forall s t r. Monoid s => Parser t s r -> Parser t s r
feedEof Parser t s r2
p2) (\s
s-> forall s t r. Monoid s => s -> Parser t s r -> Parser t s r
feed s
s Parser t s r1
p1 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` 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 :: 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 | forall t s r. Parser t s r -> Bool
isInfallible Parser t s r2
p = forall s r t. Monoid s => (r -> r) -> Parser t s r -> Parser t s r
resultPart (forall a. Monoid a => a -> a -> a
mappend (r1
r, forall a. Monoid a => a
mempty)) (forall s t r. Monoid s => s -> Parser t s r -> Parser t s r
feed s
s (forall s a b p.
(Monoid s, Monoid a, Monoid b) =>
(a -> b) -> Parser p s a -> Parser p s b
mapIncremental ((,) 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 | forall t s r. Parser t s r -> Bool
isInfallible Parser t s r2
p = 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 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) ((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) 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 = forall s t r r'.
Monoid s =>
(Parser t s r -> Parser t s r') -> Parser t s r -> Parser t s r'
apply (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