{-# LANGUAGE InstanceSigs, RankNTypes, ScopedTypeVariables, TypeFamilies, UndecidableInstances #-}
-- | Continuation-passing parser for context-free grammars
module Text.Grampa.ContextFree.Continued (Parser(..), Result(..), alt) where

import Control.Applicative (Applicative(..), Alternative(..), liftA2)
import Control.Monad (Monad(..), MonadPlus(..))

import Data.Functor.Classes (Show1(..))
import Data.Functor.Compose (Compose(..))
import Data.List (nub)
import Data.Semigroup (Semigroup(..))
import Data.Monoid (Monoid(mappend, mempty))
import Data.Monoid.Factorial(FactorialMonoid)
import Data.Monoid.Textual(TextualMonoid)
import Data.String (fromString)

import qualified Data.Monoid.Factorial as Factorial
import qualified Data.Monoid.Null as Null
import qualified Data.Monoid.Textual as Textual
import qualified Data.Semigroup.Cancellative as Cancellative

import qualified Rank2

import qualified Text.Parser.Char
import Text.Parser.Char (CharParsing)
import Text.Parser.Combinators (Parsing(..))
import Text.Parser.LookAhead (LookAheadParsing(..))
import Text.Grampa.Class (DeterministicParsing(..), InputParsing(..), InputCharParsing(..), MultiParsing(..),
                          ParseResults, ParseFailure(..), Expected(..))
import Text.Grampa.Internal (FailureInfo(..))

data Result (g :: (* -> *) -> *) s v = Parsed{Result g s v -> v
parsedPrefix :: !v,
                                              Result g s v -> s
parsedSuffix :: !s}
                                     | NoParse (FailureInfo s)

-- | Parser type for context-free grammars that uses a continuation-passing algorithm, fast for grammars in LL(1)
-- class but with potentially exponential performance for longer ambiguous prefixes.
newtype Parser (g :: (* -> *) -> *) s r =
   Parser{Parser g s r
-> forall x.
   s
   -> (r -> s -> (FailureInfo s -> x) -> x)
   -> (FailureInfo s -> x)
   -> x
applyParser :: forall x. s -> (r -> s -> (FailureInfo s -> x) -> x) -> (FailureInfo s -> x) -> x}

instance Show s => Show1 (Result g s) where
   liftShowsPrec :: (Int -> a -> ShowS)
-> ([a] -> ShowS) -> Int -> Result g s a -> ShowS
liftShowsPrec showsPrecSub :: Int -> a -> ShowS
showsPrecSub _showList :: [a] -> ShowS
_showList prec :: Int
prec Parsed{parsedPrefix :: forall (g :: (* -> *) -> *) s v. Result g s v -> v
parsedPrefix= a
r} rest :: String
rest = "Parsed " String -> ShowS
forall a. [a] -> [a] -> [a]
++ Int -> a -> ShowS
showsPrecSub Int
prec a
r String
rest
   liftShowsPrec _showsPrec :: Int -> a -> ShowS
_showsPrec _showList :: [a] -> ShowS
_showList _prec :: Int
_prec (NoParse f :: FailureInfo s
f) rest :: String
rest = "NoParse " String -> ShowS
forall a. [a] -> [a] -> [a]
++ FailureInfo s -> ShowS
forall a. Show a => a -> ShowS
shows FailureInfo s
f String
rest

instance Functor (Result g s) where
   fmap :: (a -> b) -> Result g s a -> Result g s b
fmap f :: a -> b
f (Parsed a :: a
a rest :: s
rest) = b -> s -> Result g s b
forall (g :: (* -> *) -> *) s v. v -> s -> Result g s v
Parsed (a -> b
f a
a) s
rest
   fmap _ (NoParse failure :: FailureInfo s
failure) = FailureInfo s -> Result g s b
forall (g :: (* -> *) -> *) s v. FailureInfo s -> Result g s v
NoParse FailureInfo s
failure
   
instance Functor (Parser g s) where
   fmap :: (a -> b) -> Parser g s a -> Parser g s b
fmap f :: a -> b
f (Parser p :: forall x.
s
-> (a -> s -> (FailureInfo s -> x) -> x)
-> (FailureInfo s -> x)
-> x
p) = (forall x.
 s
 -> (b -> s -> (FailureInfo s -> x) -> x)
 -> (FailureInfo s -> x)
 -> x)
-> Parser g s b
forall (g :: (* -> *) -> *) s r.
(forall x.
 s
 -> (r -> s -> (FailureInfo s -> x) -> x)
 -> (FailureInfo s -> x)
 -> x)
-> Parser g s r
Parser (\input :: s
input success :: b -> s -> (FailureInfo s -> x) -> x
success-> s
-> (a -> s -> (FailureInfo s -> x) -> x)
-> (FailureInfo s -> x)
-> x
forall x.
s
-> (a -> s -> (FailureInfo s -> x) -> x)
-> (FailureInfo s -> x)
-> x
p s
input (b -> s -> (FailureInfo s -> x) -> x
success (b -> s -> (FailureInfo s -> x) -> x)
-> (a -> b) -> a -> s -> (FailureInfo s -> x) -> x
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> b
f))
   {-# INLINABLE fmap #-}

instance Applicative (Parser g s) where
   pure :: a -> Parser g s a
pure a :: a
a = (forall x.
 s
 -> (a -> s -> (FailureInfo s -> x) -> x)
 -> (FailureInfo s -> x)
 -> x)
-> Parser g s a
forall (g :: (* -> *) -> *) s r.
(forall x.
 s
 -> (r -> s -> (FailureInfo s -> x) -> x)
 -> (FailureInfo s -> x)
 -> x)
-> Parser g s r
Parser (\input :: s
input success :: a -> s -> (FailureInfo s -> x) -> x
success failure :: FailureInfo s -> x
failure-> a -> s -> (FailureInfo s -> x) -> x
success a
a s
input FailureInfo s -> x
failure)
   (<*>) :: forall a b. Parser g s (a -> b) -> Parser g s a -> Parser g s b
   Parser p :: forall x.
s
-> ((a -> b) -> s -> (FailureInfo s -> x) -> x)
-> (FailureInfo s -> x)
-> x
p <*> :: Parser g s (a -> b) -> Parser g s a -> Parser g s b
<*> Parser q :: forall x.
s
-> (a -> s -> (FailureInfo s -> x) -> x)
-> (FailureInfo s -> x)
-> x
q = (forall x.
 s
 -> (b -> s -> (FailureInfo s -> x) -> x)
 -> (FailureInfo s -> x)
 -> x)
-> Parser g s b
forall (g :: (* -> *) -> *) s r.
(forall x.
 s
 -> (r -> s -> (FailureInfo s -> x) -> x)
 -> (FailureInfo s -> x)
 -> x)
-> Parser g s r
Parser forall x.
s
-> (b -> s -> (FailureInfo s -> x) -> x)
-> (FailureInfo s -> x)
-> x
r where
      r :: forall x. s -> (b -> s -> (FailureInfo s -> x) -> x) -> (FailureInfo s -> x) -> x
      r :: s
-> (b -> s -> (FailureInfo s -> x) -> x)
-> (FailureInfo s -> x)
-> x
r rest :: s
rest success :: b -> s -> (FailureInfo s -> x) -> x
success failure :: FailureInfo s -> x
failure = s
-> ((a -> b) -> s -> (FailureInfo s -> x) -> x)
-> (FailureInfo s -> x)
-> x
forall x.
s
-> ((a -> b) -> s -> (FailureInfo s -> x) -> x)
-> (FailureInfo s -> x)
-> x
p s
rest (\f :: a -> b
f rest' :: s
rest'-> s
-> (a -> s -> (FailureInfo s -> x) -> x)
-> (FailureInfo s -> x)
-> x
forall x.
s
-> (a -> s -> (FailureInfo s -> x) -> x)
-> (FailureInfo s -> x)
-> x
q s
rest' (b -> s -> (FailureInfo s -> x) -> x
success (b -> s -> (FailureInfo s -> x) -> x)
-> (a -> b) -> a -> s -> (FailureInfo s -> x) -> x
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> b
f)) FailureInfo s -> x
failure
   {-# INLINABLE (<*>) #-}

instance Factorial.FactorialMonoid s => Alternative (Parser g s) where
   empty :: Parser g s a
empty = (forall x.
 s
 -> (a -> s -> (FailureInfo s -> x) -> x)
 -> (FailureInfo s -> x)
 -> x)
-> Parser g s a
forall (g :: (* -> *) -> *) s r.
(forall x.
 s
 -> (r -> s -> (FailureInfo s -> x) -> x)
 -> (FailureInfo s -> x)
 -> x)
-> Parser g s r
Parser (\rest :: s
rest _ failure :: FailureInfo s -> x
failure-> FailureInfo s -> x
failure (FailureInfo s -> x) -> FailureInfo s -> x
forall a b. (a -> b) -> a -> b
$ Int -> [Expected s] -> FailureInfo s
forall s. Int -> [Expected s] -> FailureInfo s
FailureInfo (s -> Int
forall m. Factorial m => m -> Int
Factorial.length s
rest) [String -> Expected s
forall s. String -> Expected s
Expected "empty"])
   <|> :: Parser g s a -> Parser g s a -> Parser g s a
(<|>) = Parser g s a -> Parser g s a -> Parser g s a
forall (g :: (* -> *) -> *) s a.
Parser g s a -> Parser g s a -> Parser g s a
alt

-- | A named and unconstrained version of the '<|>' operator
alt :: forall g s a. Parser g s a -> Parser g s a -> Parser g s a
Parser p :: forall x.
s
-> (a -> s -> (FailureInfo s -> x) -> x)
-> (FailureInfo s -> x)
-> x
p alt :: Parser g s a -> Parser g s a -> Parser g s a
`alt` Parser q :: forall x.
s
-> (a -> s -> (FailureInfo s -> x) -> x)
-> (FailureInfo s -> x)
-> x
q = (forall x.
 s
 -> (a -> s -> (FailureInfo s -> x) -> x)
 -> (FailureInfo s -> x)
 -> x)
-> Parser g s a
forall (g :: (* -> *) -> *) s r.
(forall x.
 s
 -> (r -> s -> (FailureInfo s -> x) -> x)
 -> (FailureInfo s -> x)
 -> x)
-> Parser g s r
Parser forall x.
s
-> (a -> s -> (FailureInfo s -> x) -> x)
-> (FailureInfo s -> x)
-> x
r where
   r :: forall x. s -> (a -> s -> (FailureInfo s -> x) -> x) -> (FailureInfo s -> x) -> x
   r :: s
-> (a -> s -> (FailureInfo s -> x) -> x)
-> (FailureInfo s -> x)
-> x
r rest :: s
rest success :: a -> s -> (FailureInfo s -> x) -> x
success failure :: FailureInfo s -> x
failure = s
-> (a -> s -> (FailureInfo s -> x) -> x)
-> (FailureInfo s -> x)
-> x
forall x.
s
-> (a -> s -> (FailureInfo s -> x) -> x)
-> (FailureInfo s -> x)
-> x
p s
rest a -> s -> (FailureInfo s -> x) -> x
success' FailureInfo s -> x
failure'
      where success' :: a -> s -> (FailureInfo s -> x) -> x
success' a :: a
a rest' :: s
rest' _ = a -> s -> (FailureInfo s -> x) -> x
success a
a s
rest' FailureInfo s -> x
failure'
            failure' :: FailureInfo s -> x
failure' f1 :: FailureInfo s
f1 = s
-> (a -> s -> (FailureInfo s -> x) -> x)
-> (FailureInfo s -> x)
-> x
forall x.
s
-> (a -> s -> (FailureInfo s -> x) -> x)
-> (FailureInfo s -> x)
-> x
q s
rest a -> s -> (FailureInfo s -> x) -> x
success (\f2 :: FailureInfo s
f2 -> FailureInfo s -> x
failure (FailureInfo s
f1 FailureInfo s -> FailureInfo s -> FailureInfo s
forall a. Semigroup a => a -> a -> a
<> FailureInfo s
f2))

instance Monad (Parser g s) where
   return :: a -> Parser g s a
return = a -> Parser g s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure
   (>>=) :: forall a b. Parser g s a -> (a -> Parser g s b) -> Parser g s b
   Parser p :: forall x.
s
-> (a -> s -> (FailureInfo s -> x) -> x)
-> (FailureInfo s -> x)
-> x
p >>= :: Parser g s a -> (a -> Parser g s b) -> Parser g s b
>>= f :: a -> Parser g s b
f = (forall x.
 s
 -> (b -> s -> (FailureInfo s -> x) -> x)
 -> (FailureInfo s -> x)
 -> x)
-> Parser g s b
forall (g :: (* -> *) -> *) s r.
(forall x.
 s
 -> (r -> s -> (FailureInfo s -> x) -> x)
 -> (FailureInfo s -> x)
 -> x)
-> Parser g s r
Parser forall x.
s
-> (b -> s -> (FailureInfo s -> x) -> x)
-> (FailureInfo s -> x)
-> x
r where
      r :: forall x. s -> (b -> s -> (FailureInfo s -> x) -> x) -> (FailureInfo s -> x) -> x
      r :: s
-> (b -> s -> (FailureInfo s -> x) -> x)
-> (FailureInfo s -> x)
-> x
r rest :: s
rest success :: b -> s -> (FailureInfo s -> x) -> x
success failure :: FailureInfo s -> x
failure = s
-> (a -> s -> (FailureInfo s -> x) -> x)
-> (FailureInfo s -> x)
-> x
forall x.
s
-> (a -> s -> (FailureInfo s -> x) -> x)
-> (FailureInfo s -> x)
-> x
p s
rest (\a :: a
a rest' :: s
rest'-> Parser g s b
-> s
-> (b -> s -> (FailureInfo s -> x) -> x)
-> (FailureInfo s -> x)
-> x
forall (g :: (* -> *) -> *) s r.
Parser g s r
-> forall x.
   s
   -> (r -> s -> (FailureInfo s -> x) -> x)
   -> (FailureInfo s -> x)
   -> x
applyParser (a -> Parser g s b
f a
a) s
rest' b -> s -> (FailureInfo s -> x) -> x
success) FailureInfo s -> x
failure

instance Factorial.FactorialMonoid s => MonadPlus (Parser g s) where
   mzero :: Parser g s a
mzero = Parser g s a
forall (f :: * -> *) a. Alternative f => f a
empty
   mplus :: Parser g s a -> Parser g s a -> Parser g s a
mplus = Parser g s a -> Parser g s a -> Parser g s a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
(<|>)

instance Semigroup x => Semigroup (Parser g s x) where
   <> :: Parser g s x -> Parser g s x -> Parser g s x
(<>) = (x -> x -> x) -> Parser g s x -> Parser g s x -> Parser g s x
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 x -> x -> x
forall a. Semigroup a => a -> a -> a
(<>)

instance Monoid x => Monoid (Parser g s x) where
   mempty :: Parser g s x
mempty = x -> Parser g s x
forall (f :: * -> *) a. Applicative f => a -> f a
pure x
forall a. Monoid a => a
mempty
   mappend :: Parser g s x -> Parser g s x -> Parser g s x
mappend = (x -> x -> x) -> Parser g s x -> Parser g s x -> Parser g s x
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 x -> x -> x
forall a. Monoid a => a -> a -> a
mappend

instance Factorial.FactorialMonoid s => Parsing (Parser g s) where
   try :: forall a. Parser g s a -> Parser g s a
   try :: Parser g s a -> Parser g s a
try (Parser p :: forall x.
s
-> (a -> s -> (FailureInfo s -> x) -> x)
-> (FailureInfo s -> x)
-> x
p) = (forall x.
 s
 -> (a -> s -> (FailureInfo s -> x) -> x)
 -> (FailureInfo s -> x)
 -> x)
-> Parser g s a
forall (g :: (* -> *) -> *) s r.
(forall x.
 s
 -> (r -> s -> (FailureInfo s -> x) -> x)
 -> (FailureInfo s -> x)
 -> x)
-> Parser g s r
Parser forall x.
s
-> (a -> s -> (FailureInfo s -> x) -> x)
-> (FailureInfo s -> x)
-> x
q
      where q :: forall x. s -> (a -> s -> (FailureInfo s -> x) -> x) -> (FailureInfo s -> x) -> x
            q :: s
-> (a -> s -> (FailureInfo s -> x) -> x)
-> (FailureInfo s -> x)
-> x
q input :: s
input success :: a -> s -> (FailureInfo s -> x) -> x
success failure :: FailureInfo s -> x
failure = s
-> (a -> s -> (FailureInfo s -> x) -> x)
-> (FailureInfo s -> x)
-> x
forall x.
s
-> (a -> s -> (FailureInfo s -> x) -> x)
-> (FailureInfo s -> x)
-> x
p s
input a -> s -> (FailureInfo s -> x) -> x
success (FailureInfo s -> x
failure (FailureInfo s -> x)
-> (FailureInfo s -> FailureInfo s) -> FailureInfo s -> x
forall b c a. (b -> c) -> (a -> b) -> a -> c
. FailureInfo s -> FailureInfo s
rewindFailure)
               where rewindFailure :: FailureInfo s -> FailureInfo s
rewindFailure (FailureInfo _pos :: Int
_pos _msgs :: [Expected s]
_msgs) = Int -> [Expected s] -> FailureInfo s
forall s. Int -> [Expected s] -> FailureInfo s
FailureInfo (s -> Int
forall m. Factorial m => m -> Int
Factorial.length s
input) []
   (<?>) :: forall a. Parser g s a -> String -> Parser g s a
   Parser p :: forall x.
s
-> (a -> s -> (FailureInfo s -> x) -> x)
-> (FailureInfo s -> x)
-> x
p <?> :: Parser g s a -> String -> Parser g s a
<?> msg :: String
msg  = (forall x.
 s
 -> (a -> s -> (FailureInfo s -> x) -> x)
 -> (FailureInfo s -> x)
 -> x)
-> Parser g s a
forall (g :: (* -> *) -> *) s r.
(forall x.
 s
 -> (r -> s -> (FailureInfo s -> x) -> x)
 -> (FailureInfo s -> x)
 -> x)
-> Parser g s r
Parser forall x.
s
-> (a -> s -> (FailureInfo s -> x) -> x)
-> (FailureInfo s -> x)
-> x
q
      where q :: forall x. s -> (a -> s -> (FailureInfo s -> x) -> x) -> (FailureInfo s -> x) -> x
            q :: s
-> (a -> s -> (FailureInfo s -> x) -> x)
-> (FailureInfo s -> x)
-> x
q input :: s
input success :: a -> s -> (FailureInfo s -> x) -> x
success failure :: FailureInfo s -> x
failure = s
-> (a -> s -> (FailureInfo s -> x) -> x)
-> (FailureInfo s -> x)
-> x
forall x.
s
-> (a -> s -> (FailureInfo s -> x) -> x)
-> (FailureInfo s -> x)
-> x
p s
input a -> s -> (FailureInfo s -> x) -> x
success (FailureInfo s -> x
failure (FailureInfo s -> x)
-> (FailureInfo s -> FailureInfo s) -> FailureInfo s -> x
forall b c a. (b -> c) -> (a -> b) -> a -> c
. FailureInfo s -> FailureInfo s
replaceFailure)
               where replaceFailure :: FailureInfo s -> FailureInfo s
replaceFailure (FailureInfo pos :: Int
pos msgs :: [Expected s]
msgs) =
                        Int -> [Expected s] -> FailureInfo s
forall s. Int -> [Expected s] -> FailureInfo s
FailureInfo Int
pos (if Int
pos Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== s -> Int
forall m. Factorial m => m -> Int
Factorial.length s
input then [String -> Expected s
forall s. String -> Expected s
Expected String
msg] else [Expected s]
msgs)

   eof :: Parser g s ()
eof = (forall x.
 s
 -> (() -> s -> (FailureInfo s -> x) -> x)
 -> (FailureInfo s -> x)
 -> x)
-> Parser g s ()
forall (g :: (* -> *) -> *) s r.
(forall x.
 s
 -> (r -> s -> (FailureInfo s -> x) -> x)
 -> (FailureInfo s -> x)
 -> x)
-> Parser g s r
Parser forall x.
s
-> (() -> s -> (FailureInfo s -> x) -> x)
-> (FailureInfo s -> x)
-> x
forall m s p.
(MonoidNull m, Factorial m) =>
m
-> (() -> m -> (FailureInfo s -> p) -> p)
-> (FailureInfo s -> p)
-> p
p
      where p :: m
-> (() -> m -> (FailureInfo s -> p) -> p)
-> (FailureInfo s -> p)
-> p
p rest :: m
rest success :: () -> m -> (FailureInfo s -> p) -> p
success failure :: FailureInfo s -> p
failure
               | m -> Bool
forall m. MonoidNull m => m -> Bool
Null.null m
rest = () -> m -> (FailureInfo s -> p) -> p
success () m
rest FailureInfo s -> p
failure
               | Bool
otherwise = FailureInfo s -> p
failure (Int -> [Expected s] -> FailureInfo s
forall s. Int -> [Expected s] -> FailureInfo s
FailureInfo (m -> Int
forall m. Factorial m => m -> Int
Factorial.length m
rest) [String -> Expected s
forall s. String -> Expected s
Expected "end of input"])
   unexpected :: String -> Parser g s a
unexpected msg :: String
msg = (forall x.
 s
 -> (a -> s -> (FailureInfo s -> x) -> x)
 -> (FailureInfo s -> x)
 -> x)
-> Parser g s a
forall (g :: (* -> *) -> *) s r.
(forall x.
 s
 -> (r -> s -> (FailureInfo s -> x) -> x)
 -> (FailureInfo s -> x)
 -> x)
-> Parser g s r
Parser (\t :: s
t _ failure :: FailureInfo s -> x
failure -> FailureInfo s -> x
failure (FailureInfo s -> x) -> FailureInfo s -> x
forall a b. (a -> b) -> a -> b
$ Int -> [Expected s] -> FailureInfo s
forall s. Int -> [Expected s] -> FailureInfo s
FailureInfo (s -> Int
forall m. Factorial m => m -> Int
Factorial.length s
t) [String -> Expected s
forall s. String -> Expected s
Expected String
msg])
   notFollowedBy :: Parser g s a -> Parser g s ()
notFollowedBy (Parser p :: forall x.
s
-> (a -> s -> (FailureInfo s -> x) -> x)
-> (FailureInfo s -> x)
-> x
p) = (forall x.
 s
 -> (() -> s -> (FailureInfo s -> x) -> x)
 -> (FailureInfo s -> x)
 -> x)
-> Parser g s ()
forall (g :: (* -> *) -> *) s r.
(forall x.
 s
 -> (r -> s -> (FailureInfo s -> x) -> x)
 -> (FailureInfo s -> x)
 -> x)
-> Parser g s r
Parser forall x.
s
-> (() -> s -> (FailureInfo s -> x) -> x)
-> (FailureInfo s -> x)
-> x
q
      where q :: forall x. s -> (() -> s -> (FailureInfo s -> x) -> x) -> (FailureInfo s -> x) -> x
            q :: s
-> (() -> s -> (FailureInfo s -> x) -> x)
-> (FailureInfo s -> x)
-> x
q input :: s
input success :: () -> s -> (FailureInfo s -> x) -> x
success failure :: FailureInfo s -> x
failure = s
-> (a -> s -> (FailureInfo s -> x) -> x)
-> (FailureInfo s -> x)
-> x
forall x.
s
-> (a -> s -> (FailureInfo s -> x) -> x)
-> (FailureInfo s -> x)
-> x
p s
input a -> s -> (FailureInfo s -> x) -> x
success' FailureInfo s -> x
failure'
               where success' :: a -> s -> (FailureInfo s -> x) -> x
success' _ _ _ = FailureInfo s -> x
failure (Int -> [Expected s] -> FailureInfo s
forall s. Int -> [Expected s] -> FailureInfo s
FailureInfo (s -> Int
forall m. Factorial m => m -> Int
Factorial.length s
input) [String -> Expected s
forall s. String -> Expected s
Expected "notFollowedBy"])
                     failure' :: FailureInfo s -> x
failure' _ = () -> s -> (FailureInfo s -> x) -> x
success () s
input FailureInfo s -> x
failure

instance FactorialMonoid s => DeterministicParsing (Parser g s) where
   (<<|>) :: forall a. Parser g s a -> Parser g s a -> Parser g s a
   Parser p :: forall x.
s
-> (a -> s -> (FailureInfo s -> x) -> x)
-> (FailureInfo s -> x)
-> x
p <<|> :: Parser g s a -> Parser g s a -> Parser g s a
<<|> Parser q :: forall x.
s
-> (a -> s -> (FailureInfo s -> x) -> x)
-> (FailureInfo s -> x)
-> x
q = (forall x.
 s
 -> (a -> s -> (FailureInfo s -> x) -> x)
 -> (FailureInfo s -> x)
 -> x)
-> Parser g s a
forall (g :: (* -> *) -> *) s r.
(forall x.
 s
 -> (r -> s -> (FailureInfo s -> x) -> x)
 -> (FailureInfo s -> x)
 -> x)
-> Parser g s r
Parser forall x.
s
-> (a -> s -> (FailureInfo s -> x) -> x)
-> (FailureInfo s -> x)
-> x
r where
      r :: forall x. s -> (a -> s -> (FailureInfo s -> x) -> x) -> (FailureInfo s -> x) -> x
      r :: s
-> (a -> s -> (FailureInfo s -> x) -> x)
-> (FailureInfo s -> x)
-> x
r rest :: s
rest success :: a -> s -> (FailureInfo s -> x) -> x
success failure :: FailureInfo s -> x
failure = s
-> (a -> s -> (FailureInfo s -> x) -> x)
-> (FailureInfo s -> x)
-> x
forall x.
s
-> (a -> s -> (FailureInfo s -> x) -> x)
-> (FailureInfo s -> x)
-> x
p s
rest a -> s -> (FailureInfo s -> x) -> x
success' FailureInfo s -> x
failure'
         where success' :: a -> s -> (FailureInfo s -> x) -> x
success' a :: a
a rest' :: s
rest' _ = a -> s -> (FailureInfo s -> x) -> x
success a
a s
rest' FailureInfo s -> x
failure
               failure' :: FailureInfo s -> x
failure' f1 :: FailureInfo s
f1 = s
-> (a -> s -> (FailureInfo s -> x) -> x)
-> (FailureInfo s -> x)
-> x
forall x.
s
-> (a -> s -> (FailureInfo s -> x) -> x)
-> (FailureInfo s -> x)
-> x
q s
rest a -> s -> (FailureInfo s -> x) -> x
success (\f2 :: FailureInfo s
f2 -> FailureInfo s -> x
failure (FailureInfo s
f1 FailureInfo s -> FailureInfo s -> FailureInfo s
forall a. Semigroup a => a -> a -> a
<> FailureInfo s
f2))
   takeSome :: Parser g s a -> Parser g s [a]
takeSome p :: Parser g s a
p = (:) (a -> [a] -> [a]) -> Parser g s a -> Parser g s ([a] -> [a])
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser g s a
p Parser g s ([a] -> [a]) -> Parser g s [a] -> Parser g s [a]
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Parser g s a -> Parser g s [a]
forall (m :: * -> *) a. DeterministicParsing m => m a -> m [a]
takeMany Parser g s a
p
   takeMany :: Parser g s a -> Parser g s [a]
takeMany p :: Parser g s a
p = Parser g s a -> Parser g s [a]
forall (m :: * -> *) a. DeterministicParsing m => m a -> m [a]
takeSome Parser g s a
p Parser g s [a] -> Parser g s [a] -> Parser g s [a]
forall (m :: * -> *) a. DeterministicParsing m => m a -> m a -> m a
<<|> [a] -> Parser g s [a]
forall (f :: * -> *) a. Applicative f => a -> f a
pure []

instance Factorial.FactorialMonoid s => LookAheadParsing (Parser g s) where
   lookAhead :: forall a. Parser g s a -> Parser g s a
   lookAhead :: Parser g s a -> Parser g s a
lookAhead (Parser p :: forall x.
s
-> (a -> s -> (FailureInfo s -> x) -> x)
-> (FailureInfo s -> x)
-> x
p) = (forall x.
 s
 -> (a -> s -> (FailureInfo s -> x) -> x)
 -> (FailureInfo s -> x)
 -> x)
-> Parser g s a
forall (g :: (* -> *) -> *) s r.
(forall x.
 s
 -> (r -> s -> (FailureInfo s -> x) -> x)
 -> (FailureInfo s -> x)
 -> x)
-> Parser g s r
Parser forall x.
s
-> (a -> s -> (FailureInfo s -> x) -> x)
-> (FailureInfo s -> x)
-> x
q
      where q :: forall x. s -> (a -> s -> (FailureInfo s -> x) -> x) -> (FailureInfo s -> x) -> x
            q :: s
-> (a -> s -> (FailureInfo s -> x) -> x)
-> (FailureInfo s -> x)
-> x
q input :: s
input success :: a -> s -> (FailureInfo s -> x) -> x
success failure :: FailureInfo s -> x
failure = s
-> (a -> s -> (FailureInfo s -> x) -> x)
-> (FailureInfo s -> x)
-> x
forall x.
s
-> (a -> s -> (FailureInfo s -> x) -> x)
-> (FailureInfo s -> x)
-> x
p s
input a -> s -> (FailureInfo s -> x) -> x
success' FailureInfo s -> x
failure'
               where success' :: a -> s -> (FailureInfo s -> x) -> x
success' a :: a
a _ = a -> s -> (FailureInfo s -> x) -> x
success a
a s
input
                     failure' :: FailureInfo s -> x
failure' f :: FailureInfo s
f = FailureInfo s -> x
failure FailureInfo s
f

instance (Show s, TextualMonoid s) => CharParsing (Parser g s) where
   satisfy :: (Char -> Bool) -> Parser g s Char
satisfy predicate :: Char -> Bool
predicate = (forall x.
 s
 -> (Char -> s -> (FailureInfo s -> x) -> x)
 -> (FailureInfo s -> x)
 -> x)
-> Parser g s Char
forall (g :: (* -> *) -> *) s r.
(forall x.
 s
 -> (r -> s -> (FailureInfo s -> x) -> x)
 -> (FailureInfo s -> x)
 -> x)
-> Parser g s r
Parser forall x.
s
-> (Char -> s -> (FailureInfo s -> x) -> x)
-> (FailureInfo s -> x)
-> x
p
      where p :: forall x. s -> (Char -> s -> (FailureInfo s -> x) -> x) -> (FailureInfo s -> x) -> x
            p :: s
-> (Char -> s -> (FailureInfo s -> x) -> x)
-> (FailureInfo s -> x)
-> x
p rest :: s
rest success :: Char -> s -> (FailureInfo s -> x) -> x
success failure :: FailureInfo s -> x
failure =
               case s -> Maybe (Char, s)
forall t. TextualMonoid t => t -> Maybe (Char, t)
Textual.splitCharacterPrefix s
rest
               of Just (first :: Char
first, suffix :: s
suffix) | Char -> Bool
predicate Char
first -> Char -> s -> (FailureInfo s -> x) -> x
success Char
first s
suffix FailureInfo s -> x
failure
                  _ -> FailureInfo s -> x
failure (Int -> [Expected s] -> FailureInfo s
forall s. Int -> [Expected s] -> FailureInfo s
FailureInfo (s -> Int
forall m. Factorial m => m -> Int
Factorial.length s
rest) [String -> Expected s
forall s. String -> Expected s
Expected "Char.satisfy"])
   string :: String -> Parser g s String
string s :: String
s = (s -> String) -> s -> String
forall t. TextualMonoid t => (t -> String) -> t -> String
Textual.toString (String -> s -> String
forall a. HasCallStack => String -> a
error "unexpected non-character") (s -> String) -> Parser g s s -> Parser g s String
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParserInput (Parser g s) -> Parser g s (ParserInput (Parser g s))
forall (m :: * -> *).
InputParsing m =>
ParserInput m -> m (ParserInput m)
string (String -> s
forall a. IsString a => String -> a
fromString String
s)
   text :: Text -> Parser g s Text
text t :: Text
t = (String -> Text
forall a. IsString a => String -> a
fromString (String -> Text) -> (s -> String) -> s -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (s -> String) -> s -> String
forall t. TextualMonoid t => (t -> String) -> t -> String
Textual.toString (String -> s -> String
forall a. HasCallStack => String -> a
error "unexpected non-character")) (s -> Text) -> Parser g s s -> Parser g s Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ParserInput (Parser g s) -> Parser g s (ParserInput (Parser g s))
forall (m :: * -> *).
InputParsing m =>
ParserInput m -> m (ParserInput m)
string (Text -> s
forall t. TextualMonoid t => Text -> t
Textual.fromText Text
t)

instance (Cancellative.LeftReductive s, Factorial.FactorialMonoid s) => InputParsing (Parser g s) where
   type ParserInput (Parser g s) = s
   getInput :: Parser g s (ParserInput (Parser g s))
getInput = (forall x.
 s
 -> (s -> s -> (FailureInfo s -> x) -> x)
 -> (FailureInfo s -> x)
 -> x)
-> Parser g s s
forall (g :: (* -> *) -> *) s r.
(forall x.
 s
 -> (r -> s -> (FailureInfo s -> x) -> x)
 -> (FailureInfo s -> x)
 -> x)
-> Parser g s r
Parser forall x.
s
-> (s -> s -> (FailureInfo s -> x) -> x)
-> (FailureInfo s -> x)
-> x
forall t t t. t -> (t -> t -> t -> t) -> t -> t
p
      where p :: t -> (t -> t -> t -> t) -> t -> t
p rest :: t
rest success :: t -> t -> t -> t
success failure :: t
failure = t -> t -> t -> t
success t
rest t
rest t
failure
   anyToken :: Parser g s (ParserInput (Parser g s))
anyToken = (forall x.
 s
 -> (s -> s -> (FailureInfo s -> x) -> x)
 -> (FailureInfo s -> x)
 -> x)
-> Parser g s s
forall (g :: (* -> *) -> *) s r.
(forall x.
 s
 -> (r -> s -> (FailureInfo s -> x) -> x)
 -> (FailureInfo s -> x)
 -> x)
-> Parser g s r
Parser forall x.
s
-> (s -> s -> (FailureInfo s -> x) -> x)
-> (FailureInfo s -> x)
-> x
forall m s p.
FactorialMonoid m =>
m
-> (m -> m -> (FailureInfo s -> p) -> p)
-> (FailureInfo s -> p)
-> p
p
      where p :: m
-> (m -> m -> (FailureInfo s -> p) -> p)
-> (FailureInfo s -> p)
-> p
p rest :: m
rest success :: m -> m -> (FailureInfo s -> p) -> p
success failure :: FailureInfo s -> p
failure =
               case m -> Maybe (m, m)
forall m. FactorialMonoid m => m -> Maybe (m, m)
Factorial.splitPrimePrefix m
rest
               of Just (first :: m
first, suffix :: m
suffix) -> m -> m -> (FailureInfo s -> p) -> p
success m
first m
suffix FailureInfo s -> p
failure
                  _ -> FailureInfo s -> p
failure (Int -> [Expected s] -> FailureInfo s
forall s. Int -> [Expected s] -> FailureInfo s
FailureInfo (m -> Int
forall m. Factorial m => m -> Int
Factorial.length m
rest) [String -> Expected s
forall s. String -> Expected s
Expected "anyToken"])
   satisfy :: (ParserInput (Parser g s) -> Bool)
-> Parser g s (ParserInput (Parser g s))
satisfy predicate :: ParserInput (Parser g s) -> Bool
predicate = (forall x.
 s
 -> (s -> s -> (FailureInfo s -> x) -> x)
 -> (FailureInfo s -> x)
 -> x)
-> Parser g s s
forall (g :: (* -> *) -> *) s r.
(forall x.
 s
 -> (r -> s -> (FailureInfo s -> x) -> x)
 -> (FailureInfo s -> x)
 -> x)
-> Parser g s r
Parser forall x.
s
-> (s -> s -> (FailureInfo s -> x) -> x)
-> (FailureInfo s -> x)
-> x
p
      where p :: forall x. s -> (s -> s -> (FailureInfo s -> x) -> x) -> (FailureInfo s -> x) -> x
            p :: s
-> (s -> s -> (FailureInfo s -> x) -> x)
-> (FailureInfo s -> x)
-> x
p rest :: s
rest success :: s -> s -> (FailureInfo s -> x) -> x
success failure :: FailureInfo s -> x
failure =
               case s -> Maybe (s, s)
forall m. FactorialMonoid m => m -> Maybe (m, m)
Factorial.splitPrimePrefix s
rest
               of Just (first :: s
first, suffix :: s
suffix) | ParserInput (Parser g s) -> Bool
predicate s
ParserInput (Parser g s)
first -> s -> s -> (FailureInfo s -> x) -> x
success s
first s
suffix FailureInfo s -> x
failure
                  _ -> FailureInfo s -> x
failure (Int -> [Expected s] -> FailureInfo s
forall s. Int -> [Expected s] -> FailureInfo s
FailureInfo (s -> Int
forall m. Factorial m => m -> Int
Factorial.length s
rest) [String -> Expected s
forall s. String -> Expected s
Expected "satisfy"])
   notSatisfy :: (ParserInput (Parser g s) -> Bool) -> Parser g s ()
notSatisfy predicate :: ParserInput (Parser g s) -> Bool
predicate = (forall x.
 s
 -> (() -> s -> (FailureInfo s -> x) -> x)
 -> (FailureInfo s -> x)
 -> x)
-> Parser g s ()
forall (g :: (* -> *) -> *) s r.
(forall x.
 s
 -> (r -> s -> (FailureInfo s -> x) -> x)
 -> (FailureInfo s -> x)
 -> x)
-> Parser g s r
Parser forall x.
s
-> (() -> s -> (FailureInfo s -> x) -> x)
-> (FailureInfo s -> x)
-> x
p
      where p :: forall x. s -> (() -> s -> (FailureInfo s -> x) -> x) -> (FailureInfo s -> x) -> x
            p :: s
-> (() -> s -> (FailureInfo s -> x) -> x)
-> (FailureInfo s -> x)
-> x
p rest :: s
rest success :: () -> s -> (FailureInfo s -> x) -> x
success failure :: FailureInfo s -> x
failure =
               case s -> Maybe (s, s)
forall m. FactorialMonoid m => m -> Maybe (m, m)
Factorial.splitPrimePrefix s
rest
               of Just (first :: s
first, _)
                     | ParserInput (Parser g s) -> Bool
predicate s
ParserInput (Parser g s)
first -> FailureInfo s -> x
failure (Int -> [Expected s] -> FailureInfo s
forall s. Int -> [Expected s] -> FailureInfo s
FailureInfo (s -> Int
forall m. Factorial m => m -> Int
Factorial.length s
rest) [String -> Expected s
forall s. String -> Expected s
Expected "notSatisfy"])
                  _ -> () -> s -> (FailureInfo s -> x) -> x
success () s
rest FailureInfo s -> x
failure
   scan :: forall state. state -> (state -> s -> Maybe state) -> Parser g s s
   scan :: state -> (state -> s -> Maybe state) -> Parser g s s
scan s0 :: state
s0 f :: state -> s -> Maybe state
f = (forall x.
 s
 -> (s -> s -> (FailureInfo s -> x) -> x)
 -> (FailureInfo s -> x)
 -> x)
-> Parser g s s
forall (g :: (* -> *) -> *) s r.
(forall x.
 s
 -> (r -> s -> (FailureInfo s -> x) -> x)
 -> (FailureInfo s -> x)
 -> x)
-> Parser g s r
Parser (state
-> s
-> (s -> s -> (FailureInfo s -> x) -> x)
-> (FailureInfo s -> x)
-> x
forall x.
state
-> s
-> (s -> s -> (FailureInfo s -> x) -> x)
-> (FailureInfo s -> x)
-> x
p state
s0)
      where p :: forall x. state -> s -> (s -> s -> (FailureInfo s -> x) -> x) -> (FailureInfo s -> x) -> x
            p :: state
-> s
-> (s -> s -> (FailureInfo s -> x) -> x)
-> (FailureInfo s -> x)
-> x
p s :: state
s rest :: s
rest success :: s -> s -> (FailureInfo s -> x) -> x
success failure :: FailureInfo s -> x
failure = s -> s -> (FailureInfo s -> x) -> x
success s
prefix s
suffix FailureInfo s -> x
failure
               where (prefix :: s
prefix, suffix :: s
suffix, _) = state -> (state -> s -> Maybe state) -> s -> (s, s, state)
forall m s.
FactorialMonoid m =>
s -> (s -> m -> Maybe s) -> m -> (m, m, s)
Factorial.spanMaybe' state
s state -> s -> Maybe state
f s
rest
   take :: Int -> Parser g s (ParserInput (Parser g s))
take n :: Int
n = (forall x.
 s
 -> (s -> s -> (FailureInfo s -> x) -> x)
 -> (FailureInfo s -> x)
 -> x)
-> Parser g s s
forall (g :: (* -> *) -> *) s r.
(forall x.
 s
 -> (r -> s -> (FailureInfo s -> x) -> x)
 -> (FailureInfo s -> x)
 -> x)
-> Parser g s r
Parser forall x.
s
-> (s -> s -> (FailureInfo s -> x) -> x)
-> (FailureInfo s -> x)
-> x
p
      where p :: forall x. s -> (s -> s -> (FailureInfo s -> x) -> x) -> (FailureInfo s -> x) -> x
            p :: s
-> (s -> s -> (FailureInfo s -> x) -> x)
-> (FailureInfo s -> x)
-> x
p rest :: s
rest success :: s -> s -> (FailureInfo s -> x) -> x
success failure :: FailureInfo s -> x
failure
              | (prefix :: s
prefix, suffix :: s
suffix) <- Int -> s -> (s, s)
forall m. FactorialMonoid m => Int -> m -> (m, m)
Factorial.splitAt Int
n s
rest,
                s -> Int
forall m. Factorial m => m -> Int
Factorial.length s
prefix Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
n = s -> s -> (FailureInfo s -> x) -> x
success s
prefix s
suffix FailureInfo s -> x
failure
              | Bool
otherwise = FailureInfo s -> x
failure (Int -> [Expected s] -> FailureInfo s
forall s. Int -> [Expected s] -> FailureInfo s
FailureInfo (s -> Int
forall m. Factorial m => m -> Int
Factorial.length s
rest) [String -> Expected s
forall s. String -> Expected s
Expected (String -> Expected s) -> String -> Expected s
forall a b. (a -> b) -> a -> b
$ "take " String -> ShowS
forall a. [a] -> [a] -> [a]
++ Int -> String
forall a. Show a => a -> String
show Int
n])
   takeWhile :: (ParserInput (Parser g s) -> Bool)
-> Parser g s (ParserInput (Parser g s))
takeWhile predicate :: ParserInput (Parser g s) -> Bool
predicate = (forall x.
 s
 -> (s -> s -> (FailureInfo s -> x) -> x)
 -> (FailureInfo s -> x)
 -> x)
-> Parser g s s
forall (g :: (* -> *) -> *) s r.
(forall x.
 s
 -> (r -> s -> (FailureInfo s -> x) -> x)
 -> (FailureInfo s -> x)
 -> x)
-> Parser g s r
Parser forall x.
s
-> (s -> s -> (FailureInfo s -> x) -> x)
-> (FailureInfo s -> x)
-> x
p
      where p :: forall x. s -> (s -> s -> (FailureInfo s -> x) -> x) -> (FailureInfo s -> x) -> x
            p :: s
-> (s -> s -> (FailureInfo s -> x) -> x)
-> (FailureInfo s -> x)
-> x
p rest :: s
rest success :: s -> s -> (FailureInfo s -> x) -> x
success failure :: FailureInfo s -> x
failure | (prefix :: s
prefix, suffix :: s
suffix) <- (s -> Bool) -> s -> (s, s)
forall m. FactorialMonoid m => (m -> Bool) -> m -> (m, m)
Factorial.span s -> Bool
ParserInput (Parser g s) -> Bool
predicate s
rest = s -> s -> (FailureInfo s -> x) -> x
success s
prefix s
suffix FailureInfo s -> x
failure
   takeWhile1 :: (ParserInput (Parser g s) -> Bool)
-> Parser g s (ParserInput (Parser g s))
takeWhile1 predicate :: ParserInput (Parser g s) -> Bool
predicate = (forall x.
 s
 -> (s -> s -> (FailureInfo s -> x) -> x)
 -> (FailureInfo s -> x)
 -> x)
-> Parser g s s
forall (g :: (* -> *) -> *) s r.
(forall x.
 s
 -> (r -> s -> (FailureInfo s -> x) -> x)
 -> (FailureInfo s -> x)
 -> x)
-> Parser g s r
Parser forall x.
s
-> (s -> s -> (FailureInfo s -> x) -> x)
-> (FailureInfo s -> x)
-> x
p
      where p :: forall x. s -> (s -> s -> (FailureInfo s -> x) -> x) -> (FailureInfo s -> x) -> x
            p :: s
-> (s -> s -> (FailureInfo s -> x) -> x)
-> (FailureInfo s -> x)
-> x
p rest :: s
rest success :: s -> s -> (FailureInfo s -> x) -> x
success failure :: FailureInfo s -> x
failure
               | (prefix :: s
prefix, suffix :: s
suffix) <- (s -> Bool) -> s -> (s, s)
forall m. FactorialMonoid m => (m -> Bool) -> m -> (m, m)
Factorial.span s -> Bool
ParserInput (Parser g s) -> Bool
predicate s
rest =
                    if s -> Bool
forall m. MonoidNull m => m -> Bool
Null.null s
prefix
                    then FailureInfo s -> x
failure (Int -> [Expected s] -> FailureInfo s
forall s. Int -> [Expected s] -> FailureInfo s
FailureInfo (s -> Int
forall m. Factorial m => m -> Int
Factorial.length s
rest) [String -> Expected s
forall s. String -> Expected s
Expected "takeWhile1"])
                    else s -> s -> (FailureInfo s -> x) -> x
success s
prefix s
suffix FailureInfo s -> x
failure
   string :: ParserInput (Parser g s) -> Parser g s (ParserInput (Parser g s))
string s :: ParserInput (Parser g s)
s = (forall x.
 s
 -> (s -> s -> (FailureInfo s -> x) -> x)
 -> (FailureInfo s -> x)
 -> x)
-> Parser g s s
forall (g :: (* -> *) -> *) s r.
(forall x.
 s
 -> (r -> s -> (FailureInfo s -> x) -> x)
 -> (FailureInfo s -> x)
 -> x)
-> Parser g s r
Parser forall x.
s
-> (s -> s -> (FailureInfo s -> x) -> x)
-> (FailureInfo s -> x)
-> x
p where
      p :: forall x. s -> (s -> s -> (FailureInfo s -> x) -> x) -> (FailureInfo s -> x) -> x
      p :: s
-> (s -> s -> (FailureInfo s -> x) -> x)
-> (FailureInfo s -> x)
-> x
p s' :: s
s' success :: s -> s -> (FailureInfo s -> x) -> x
success failure :: FailureInfo s -> x
failure
         | Just suffix :: s
suffix <- s -> s -> Maybe s
forall m. LeftReductive m => m -> m -> Maybe m
Cancellative.stripPrefix s
ParserInput (Parser g s)
s s
s' = s -> s -> (FailureInfo s -> x) -> x
success s
ParserInput (Parser g s)
s s
suffix FailureInfo s -> x
failure
         | Bool
otherwise = FailureInfo s -> x
failure (Int -> [Expected s] -> FailureInfo s
forall s. Int -> [Expected s] -> FailureInfo s
FailureInfo (s -> Int
forall m. Factorial m => m -> Int
Factorial.length s
s') [s -> Expected s
forall s. s -> Expected s
ExpectedInput s
ParserInput (Parser g s)
s])
   {-# INLINABLE string #-}

instance (Show s, TextualMonoid s) => InputCharParsing (Parser g s) where
   satisfyCharInput :: (Char -> Bool) -> Parser g s (ParserInput (Parser g s))
satisfyCharInput predicate :: Char -> Bool
predicate = (forall x.
 s
 -> (s -> s -> (FailureInfo s -> x) -> x)
 -> (FailureInfo s -> x)
 -> x)
-> Parser g s s
forall (g :: (* -> *) -> *) s r.
(forall x.
 s
 -> (r -> s -> (FailureInfo s -> x) -> x)
 -> (FailureInfo s -> x)
 -> x)
-> Parser g s r
Parser forall x.
s
-> (s -> s -> (FailureInfo s -> x) -> x)
-> (FailureInfo s -> x)
-> x
p
      where p :: forall x. s -> (s -> s -> (FailureInfo s -> x) -> x) -> (FailureInfo s -> x) -> x
            p :: s
-> (s -> s -> (FailureInfo s -> x) -> x)
-> (FailureInfo s -> x)
-> x
p rest :: s
rest success :: s -> s -> (FailureInfo s -> x) -> x
success failure :: FailureInfo s -> x
failure =
               case s -> Maybe (Char, s)
forall t. TextualMonoid t => t -> Maybe (Char, t)
Textual.splitCharacterPrefix s
rest
               of Just (first :: Char
first, suffix :: s
suffix) | Char -> Bool
predicate Char
first -> s -> s -> (FailureInfo s -> x) -> x
success (s -> s
forall m. Factorial m => m -> m
Factorial.primePrefix s
rest) s
suffix FailureInfo s -> x
failure
                  _ -> FailureInfo s -> x
failure (Int -> [Expected s] -> FailureInfo s
forall s. Int -> [Expected s] -> FailureInfo s
FailureInfo (s -> Int
forall m. Factorial m => m -> Int
Factorial.length s
rest) [String -> Expected s
forall s. String -> Expected s
Expected "satisfyChar"])
   notSatisfyChar :: (Char -> Bool) -> Parser g s ()
notSatisfyChar predicate :: Char -> Bool
predicate = (forall x.
 s
 -> (() -> s -> (FailureInfo s -> x) -> x)
 -> (FailureInfo s -> x)
 -> x)
-> Parser g s ()
forall (g :: (* -> *) -> *) s r.
(forall x.
 s
 -> (r -> s -> (FailureInfo s -> x) -> x)
 -> (FailureInfo s -> x)
 -> x)
-> Parser g s r
Parser forall x.
s
-> (() -> s -> (FailureInfo s -> x) -> x)
-> (FailureInfo s -> x)
-> x
p
      where p :: forall x. s -> (() -> s -> (FailureInfo s -> x) -> x) -> (FailureInfo s -> x) -> x
            p :: s
-> (() -> s -> (FailureInfo s -> x) -> x)
-> (FailureInfo s -> x)
-> x
p rest :: s
rest success :: () -> s -> (FailureInfo s -> x) -> x
success failure :: FailureInfo s -> x
failure =
               case s -> Maybe Char
forall t. TextualMonoid t => t -> Maybe Char
Textual.characterPrefix s
rest
               of Just first :: Char
first | Char -> Bool
predicate Char
first
                               -> FailureInfo s -> x
failure (Int -> [Expected s] -> FailureInfo s
forall s. Int -> [Expected s] -> FailureInfo s
FailureInfo (s -> Int
forall m. Factorial m => m -> Int
Factorial.length s
rest) [String -> Expected s
forall s. String -> Expected s
Expected "notSatisfyChar"])
                  _ -> () -> s -> (FailureInfo s -> x) -> x
success () s
rest FailureInfo s -> x
failure
   scanChars :: forall state. state -> (state -> Char -> Maybe state) -> Parser g s s
   scanChars :: state -> (state -> Char -> Maybe state) -> Parser g s s
scanChars s0 :: state
s0 f :: state -> Char -> Maybe state
f = (forall x.
 s
 -> (s -> s -> (FailureInfo s -> x) -> x)
 -> (FailureInfo s -> x)
 -> x)
-> Parser g s s
forall (g :: (* -> *) -> *) s r.
(forall x.
 s
 -> (r -> s -> (FailureInfo s -> x) -> x)
 -> (FailureInfo s -> x)
 -> x)
-> Parser g s r
Parser (state
-> s
-> (s -> s -> (FailureInfo s -> x) -> x)
-> (FailureInfo s -> x)
-> x
forall x.
state
-> s
-> (s -> s -> (FailureInfo s -> x) -> x)
-> (FailureInfo s -> x)
-> x
p state
s0)
      where p :: forall x. state -> s -> (s -> s -> (FailureInfo s -> x) -> x) -> (FailureInfo s -> x) -> x
            p :: state
-> s
-> (s -> s -> (FailureInfo s -> x) -> x)
-> (FailureInfo s -> x)
-> x
p s :: state
s rest :: s
rest success :: s -> s -> (FailureInfo s -> x) -> x
success failure :: FailureInfo s -> x
failure = s -> s -> (FailureInfo s -> x) -> x
success s
prefix s
suffix FailureInfo s -> x
failure
               where (prefix :: s
prefix, suffix :: s
suffix, _) = state -> (state -> Char -> Maybe state) -> s -> (s, s, state)
forall t s.
TextualMonoid t =>
s -> (s -> Char -> Maybe s) -> t -> (t, t, s)
Textual.spanMaybe_' state
s state -> Char -> Maybe state
f s
rest
   takeCharsWhile :: (Char -> Bool) -> Parser g s (ParserInput (Parser g s))
takeCharsWhile predicate :: Char -> Bool
predicate = (forall x.
 s
 -> (s -> s -> (FailureInfo s -> x) -> x)
 -> (FailureInfo s -> x)
 -> x)
-> Parser g s s
forall (g :: (* -> *) -> *) s r.
(forall x.
 s
 -> (r -> s -> (FailureInfo s -> x) -> x)
 -> (FailureInfo s -> x)
 -> x)
-> Parser g s r
Parser forall x.
s
-> (s -> s -> (FailureInfo s -> x) -> x)
-> (FailureInfo s -> x)
-> x
p
      where p :: forall x. s -> (s -> s -> (FailureInfo s -> x) -> x) -> (FailureInfo s -> x) -> x
            p :: s
-> (s -> s -> (FailureInfo s -> x) -> x)
-> (FailureInfo s -> x)
-> x
p rest :: s
rest success :: s -> s -> (FailureInfo s -> x) -> x
success failure :: FailureInfo s -> x
failure
               | (prefix :: s
prefix, suffix :: s
suffix) <- Bool -> (Char -> Bool) -> s -> (s, s)
forall t. TextualMonoid t => Bool -> (Char -> Bool) -> t -> (t, t)
Textual.span_ Bool
False Char -> Bool
predicate s
rest = s -> s -> (FailureInfo s -> x) -> x
success s
prefix s
suffix FailureInfo s -> x
failure
   takeCharsWhile1 :: (Char -> Bool) -> Parser g s (ParserInput (Parser g s))
takeCharsWhile1 predicate :: Char -> Bool
predicate = (forall x.
 s
 -> (s -> s -> (FailureInfo s -> x) -> x)
 -> (FailureInfo s -> x)
 -> x)
-> Parser g s s
forall (g :: (* -> *) -> *) s r.
(forall x.
 s
 -> (r -> s -> (FailureInfo s -> x) -> x)
 -> (FailureInfo s -> x)
 -> x)
-> Parser g s r
Parser forall x.
s
-> (s -> s -> (FailureInfo s -> x) -> x)
-> (FailureInfo s -> x)
-> x
p
      where p :: forall x. s -> (s -> s -> (FailureInfo s -> x) -> x) -> (FailureInfo s -> x) -> x
            p :: s
-> (s -> s -> (FailureInfo s -> x) -> x)
-> (FailureInfo s -> x)
-> x
p rest :: s
rest success :: s -> s -> (FailureInfo s -> x) -> x
success failure :: FailureInfo s -> x
failure
               | s -> Bool
forall m. MonoidNull m => m -> Bool
Null.null s
prefix = FailureInfo s -> x
failure (Int -> [Expected s] -> FailureInfo s
forall s. Int -> [Expected s] -> FailureInfo s
FailureInfo (s -> Int
forall m. Factorial m => m -> Int
Factorial.length s
rest) [String -> Expected s
forall s. String -> Expected s
Expected "takeCharsWhile1"])
               | Bool
otherwise = s -> s -> (FailureInfo s -> x) -> x
success s
prefix s
suffix FailureInfo s -> x
failure
               where (prefix :: s
prefix, suffix :: s
suffix) = Bool -> (Char -> Bool) -> s -> (s, s)
forall t. TextualMonoid t => Bool -> (Char -> Bool) -> t -> (t, t)
Textual.span_ Bool
False Char -> Bool
predicate s
rest

-- | Continuation-passing context-free parser
--
-- @
-- 'parseComplete' :: ("Rank2".'Rank2.Functor' g, 'FactorialMonoid' s) =>
--                  g (Continued.'Parser' g s) -> s -> g ('ParseResults' s)
-- @
instance (Cancellative.LeftReductive s, Factorial.FactorialMonoid s) => MultiParsing (Parser g s) where
   type ResultFunctor (Parser g s) = ParseResults s
   -- | Returns an input prefix parse paired with the remaining input suffix.
   parsePrefix :: g (Parser g s)
-> s -> g (Compose (ResultFunctor (Parser g s)) ((,) s))
parsePrefix g :: g (Parser g s)
g input :: s
input = (forall a. Parser g s a -> Compose (ParseResults s) ((,) s) a)
-> g (Parser g s) -> g (Compose (ParseResults s) ((,) s))
forall k (g :: (k -> *) -> *) (p :: k -> *) (q :: k -> *).
Functor g =>
(forall (a :: k). p a -> q a) -> g p -> g q
Rank2.fmap (Either (ParseFailure s) (s, a)
-> Compose (Either (ParseFailure s)) ((,) s) a
forall k k1 (f :: k -> *) (g :: k1 -> k) (a :: k1).
f (g a) -> Compose f g a
Compose (Either (ParseFailure s) (s, a)
 -> Compose (Either (ParseFailure s)) ((,) s) a)
-> (Parser g s a -> Either (ParseFailure s) (s, a))
-> Parser g s a
-> Compose (Either (ParseFailure s)) ((,) s) a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (\p :: Parser g s a
p-> Parser g s a
-> s
-> (a
    -> s
    -> (FailureInfo s -> Either (ParseFailure s) (s, a))
    -> Either (ParseFailure s) (s, a))
-> (FailureInfo s -> Either (ParseFailure s) (s, a))
-> Either (ParseFailure s) (s, a)
forall (g :: (* -> *) -> *) s r.
Parser g s r
-> forall x.
   s
   -> (r -> s -> (FailureInfo s -> x) -> x)
   -> (FailureInfo s -> x)
   -> x
applyParser Parser g s a
p s
input (\a :: a
a rest :: s
rest _-> (s, a) -> Either (ParseFailure s) (s, a)
forall a b. b -> Either a b
Right (s
rest, a
a)) (ParseFailure s -> Either (ParseFailure s) (s, a)
forall a b. a -> Either a b
Left (ParseFailure s -> Either (ParseFailure s) (s, a))
-> (FailureInfo s -> ParseFailure s)
-> FailureInfo s
-> Either (ParseFailure s) (s, a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. s -> FailureInfo s -> ParseFailure s
forall s.
(Eq s, FactorialMonoid s) =>
s -> FailureInfo s -> ParseFailure s
fromFailure s
input))) g (Parser g s)
g
   parseComplete :: g (Parser g s) -> s -> g (ResultFunctor (Parser g s))
parseComplete g :: g (Parser g s)
g input :: s
input = (forall a. Parser g s a -> ParseResults s a)
-> g (Parser g s) -> g (ParseResults s)
forall k (g :: (k -> *) -> *) (p :: k -> *) (q :: k -> *).
Functor g =>
(forall (a :: k). p a -> q a) -> g p -> g q
Rank2.fmap (\p :: Parser g s a
p-> Parser g s a
-> s
-> (a
    -> s
    -> (FailureInfo s -> Either (ParseFailure s) a)
    -> Either (ParseFailure s) a)
-> (FailureInfo s -> Either (ParseFailure s) a)
-> Either (ParseFailure s) a
forall (g :: (* -> *) -> *) s r.
Parser g s r
-> forall x.
   s
   -> (r -> s -> (FailureInfo s -> x) -> x)
   -> (FailureInfo s -> x)
   -> x
applyParser Parser g s a
Parser g s a
p s
input (((FailureInfo s -> Either (ParseFailure s) a)
 -> Either (ParseFailure s) a)
-> s
-> (FailureInfo s -> Either (ParseFailure s) a)
-> Either (ParseFailure s) a
forall a b. a -> b -> a
const (((FailureInfo s -> Either (ParseFailure s) a)
  -> Either (ParseFailure s) a)
 -> s
 -> (FailureInfo s -> Either (ParseFailure s) a)
 -> Either (ParseFailure s) a)
-> (a
    -> (FailureInfo s -> Either (ParseFailure s) a)
    -> Either (ParseFailure s) a)
-> a
-> s
-> (FailureInfo s -> Either (ParseFailure s) a)
-> Either (ParseFailure s) a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Either (ParseFailure s) a
-> (FailureInfo s -> Either (ParseFailure s) a)
-> Either (ParseFailure s) a
forall a b. a -> b -> a
const (Either (ParseFailure s) a
 -> (FailureInfo s -> Either (ParseFailure s) a)
 -> Either (ParseFailure s) a)
-> (a -> Either (ParseFailure s) a)
-> a
-> (FailureInfo s -> Either (ParseFailure s) a)
-> Either (ParseFailure s) a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Either (ParseFailure s) a
forall a b. b -> Either a b
Right) (ParseFailure s -> Either (ParseFailure s) a
forall a b. a -> Either a b
Left (ParseFailure s -> Either (ParseFailure s) a)
-> (FailureInfo s -> ParseFailure s)
-> FailureInfo s
-> Either (ParseFailure s) a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. s -> FailureInfo s -> ParseFailure s
forall s.
(Eq s, FactorialMonoid s) =>
s -> FailureInfo s -> ParseFailure s
fromFailure s
input))
                                      ((forall a. Parser g s a -> Parser g s a)
-> g (Parser g s) -> g (Parser g s)
forall k (g :: (k -> *) -> *) (p :: k -> *) (q :: k -> *).
Functor g =>
(forall (a :: k). p a -> q a) -> g p -> g q
Rank2.fmap (Parser g s a -> Parser g s () -> Parser g s a
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* Parser g s ()
forall (m :: * -> *). Parsing m => m ()
eof) g (Parser g s)
g)

fromFailure :: (Eq s, FactorialMonoid s) => s -> FailureInfo s -> ParseFailure s
fromFailure :: s -> FailureInfo s -> ParseFailure s
fromFailure s :: s
s (FailureInfo pos :: Int
pos msgs :: [Expected s]
msgs) = Int -> [Expected s] -> ParseFailure s
forall s. Int -> [Expected s] -> ParseFailure s
ParseFailure (s -> Int
forall m. Factorial m => m -> Int
Factorial.length s
s Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
pos Int -> Int -> Int
forall a. Num a => a -> a -> a
+ 1) ([Expected s] -> [Expected s]
forall a. Eq a => [a] -> [a]
nub [Expected s]
msgs)