{-# LANGUAGE MultiParamTypeClasses
 ,FlexibleInstances
 ,FlexibleContexts
 ,Rank2Types
 ,TypeFamilies
 ,ScopedTypeVariables
 ,DerivingVia
 ,CPP #-}
{-|
Module      : Text.Megaparsec.Simple
Description : primitive StateT parser with Megaparsec instance
Copyright   : (c) Lackmann Phymetric
License     : GPL-3
Maintainer  : olaf.klinke@phymetric.de
Stability   : experimental

This module defines a 'MonadParsec' instance for 
(a type isomorphic to) @'StateT' s 'Maybe'@
where @s@ is a Megaparsec 'Mega.Stream' type such as @String@, @Text@ or @ByteString@.
This parser can be faster than Cassava for csv parsing 
but at the cost of no error information whatsoever. 

If, however, you construct your parser in a generic 'MonadParsec' fashion, 
then with the help of 'tryFast' you can first attempt to specialize and run the fast parser 
supplied by this module and only on parse error specialize 
the parser to @ParsecT@ and parse again, yielding an informative 
error message.  
This buys you speed in the smooth case of successful parsing 
at the cost of double parse when something goes wrong. 

Beware that the behaviour of a 'SimpleParser' can differ from its 'Mega.Parsec' sibling 
because 

 * 'SimpleParser' is always backtracking since it does not know whether it has consumed tokens,
 * any fancy parsing that relies on inspecting parser state components such as offset will not work as intended.

-}
module Text.Megaparsec.Simple (
    SimpleParser,
    tryFast,
    -- * Conversion from/to StateT
    toSimpleParser,
    runSimpleParser) where
import Control.Monad.State.Strict
import Control.Applicative
import Data.String (IsString(..))
import Data.Maybe (isNothing)
import Data.Proxy (Proxy(..))
import Data.Void (Void)
import qualified Text.Megaparsec as Mega
import Text.Megaparsec (MonadParsec,State(..))

-- * Parsing

-- | This parser type is isomorphic to 
-- @StateT s Maybe@ 
-- but has roughly the same instances as 'Mega.Parsec'. 
-- Since it maintains no state besides the unconsumed input, 
-- it is considerably faster than 'Mega.Parsec' 
-- but can be built using the same combinators. 
newtype SimpleParser s a = SimpleParser (StateT s Maybe a)
    deriving (forall a b. a -> SimpleParser s b -> SimpleParser s a
forall a b. (a -> b) -> SimpleParser s a -> SimpleParser s b
forall s a b. a -> SimpleParser s b -> SimpleParser s a
forall s a b. (a -> b) -> SimpleParser s a -> SimpleParser s b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
<$ :: forall a b. a -> SimpleParser s b -> SimpleParser s a
$c<$ :: forall s a b. a -> SimpleParser s b -> SimpleParser s a
fmap :: forall a b. (a -> b) -> SimpleParser s a -> SimpleParser s b
$cfmap :: forall s a b. (a -> b) -> SimpleParser s a -> SimpleParser s b
Functor,forall s. Functor (SimpleParser s)
forall a. a -> SimpleParser s a
forall s a. a -> SimpleParser s a
forall a b.
SimpleParser s a -> SimpleParser s b -> SimpleParser s a
forall a b.
SimpleParser s a -> SimpleParser s b -> SimpleParser s b
forall a b.
SimpleParser s (a -> b) -> SimpleParser s a -> SimpleParser s b
forall s a b.
SimpleParser s a -> SimpleParser s b -> SimpleParser s a
forall s a b.
SimpleParser s a -> SimpleParser s b -> SimpleParser s b
forall s a b.
SimpleParser s (a -> b) -> SimpleParser s a -> SimpleParser s b
forall a b c.
(a -> b -> c)
-> SimpleParser s a -> SimpleParser s b -> SimpleParser s c
forall s a b c.
(a -> b -> c)
-> SimpleParser s a -> SimpleParser s b -> SimpleParser s c
forall (f :: * -> *).
Functor f
-> (forall a. a -> f a)
-> (forall a b. f (a -> b) -> f a -> f b)
-> (forall a b c. (a -> b -> c) -> f a -> f b -> f c)
-> (forall a b. f a -> f b -> f b)
-> (forall a b. f a -> f b -> f a)
-> Applicative f
<* :: forall a b.
SimpleParser s a -> SimpleParser s b -> SimpleParser s a
$c<* :: forall s a b.
SimpleParser s a -> SimpleParser s b -> SimpleParser s a
*> :: forall a b.
SimpleParser s a -> SimpleParser s b -> SimpleParser s b
$c*> :: forall s a b.
SimpleParser s a -> SimpleParser s b -> SimpleParser s b
liftA2 :: forall a b c.
(a -> b -> c)
-> SimpleParser s a -> SimpleParser s b -> SimpleParser s c
$cliftA2 :: forall s a b c.
(a -> b -> c)
-> SimpleParser s a -> SimpleParser s b -> SimpleParser s c
<*> :: forall a b.
SimpleParser s (a -> b) -> SimpleParser s a -> SimpleParser s b
$c<*> :: forall s a b.
SimpleParser s (a -> b) -> SimpleParser s a -> SimpleParser s b
pure :: forall a. a -> SimpleParser s a
$cpure :: forall s a. a -> SimpleParser s a
Applicative,forall s. Applicative (SimpleParser s)
forall a. SimpleParser s a
forall a. SimpleParser s a -> SimpleParser s [a]
forall a. SimpleParser s a -> SimpleParser s a -> SimpleParser s a
forall s a. SimpleParser s a
forall s a. SimpleParser s a -> SimpleParser s [a]
forall s a.
SimpleParser s a -> SimpleParser s a -> SimpleParser s a
forall (f :: * -> *).
Applicative f
-> (forall a. f a)
-> (forall a. f a -> f a -> f a)
-> (forall a. f a -> f [a])
-> (forall a. f a -> f [a])
-> Alternative f
many :: forall a. SimpleParser s a -> SimpleParser s [a]
$cmany :: forall s a. SimpleParser s a -> SimpleParser s [a]
some :: forall a. SimpleParser s a -> SimpleParser s [a]
$csome :: forall s a. SimpleParser s a -> SimpleParser s [a]
<|> :: forall a. SimpleParser s a -> SimpleParser s a -> SimpleParser s a
$c<|> :: forall s a.
SimpleParser s a -> SimpleParser s a -> SimpleParser s a
empty :: forall a. SimpleParser s a
$cempty :: forall s a. SimpleParser s a
Alternative,forall s. Applicative (SimpleParser s)
forall a. a -> SimpleParser s a
forall s a. a -> SimpleParser s a
forall a b.
SimpleParser s a -> SimpleParser s b -> SimpleParser s b
forall a b.
SimpleParser s a -> (a -> SimpleParser s b) -> SimpleParser s b
forall s a b.
SimpleParser s a -> SimpleParser s b -> SimpleParser s b
forall s a b.
SimpleParser s a -> (a -> SimpleParser s b) -> SimpleParser s b
forall (m :: * -> *).
Applicative m
-> (forall a b. m a -> (a -> m b) -> m b)
-> (forall a b. m a -> m b -> m b)
-> (forall a. a -> m a)
-> Monad m
return :: forall a. a -> SimpleParser s a
$creturn :: forall s a. a -> SimpleParser s a
>> :: forall a b.
SimpleParser s a -> SimpleParser s b -> SimpleParser s b
$c>> :: forall s a b.
SimpleParser s a -> SimpleParser s b -> SimpleParser s b
>>= :: forall a b.
SimpleParser s a -> (a -> SimpleParser s b) -> SimpleParser s b
$c>>= :: forall s a b.
SimpleParser s a -> (a -> SimpleParser s b) -> SimpleParser s b
Monad,forall s. Monad (SimpleParser s)
forall s. Alternative (SimpleParser s)
forall a. SimpleParser s a
forall a. SimpleParser s a -> SimpleParser s a -> SimpleParser s a
forall s a. SimpleParser s a
forall s a.
SimpleParser s a -> SimpleParser s a -> SimpleParser s a
forall (m :: * -> *).
Alternative m
-> Monad m
-> (forall a. m a)
-> (forall a. m a -> m a -> m a)
-> MonadPlus m
mplus :: forall a. SimpleParser s a -> SimpleParser s a -> SimpleParser s a
$cmplus :: forall s a.
SimpleParser s a -> SimpleParser s a -> SimpleParser s a
mzero :: forall a. SimpleParser s a
$cmzero :: forall s a. SimpleParser s a
MonadPlus,forall s. Monad (SimpleParser s)
forall a. String -> SimpleParser s a
forall s a. String -> SimpleParser s a
forall (m :: * -> *).
Monad m -> (forall a. String -> m a) -> MonadFail m
fail :: forall a. String -> SimpleParser s a
$cfail :: forall s a. String -> SimpleParser s a
MonadFail) 
    via (StateT s Maybe)

instance Semigroup a => Semigroup (SimpleParser s a) where
    SimpleParser s a
p <> :: SimpleParser s a -> SimpleParser s a -> SimpleParser s a
<> SimpleParser s a
q = forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 forall a. Semigroup a => a -> a -> a
(<>) SimpleParser s a
p SimpleParser s a
q
instance Monoid a => Monoid (SimpleParser s a) where
    mempty :: SimpleParser s a
mempty = forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a. Monoid a => a
mempty
    mappend :: SimpleParser s a -> SimpleParser s a -> SimpleParser s a
mappend = forall a. Semigroup a => a -> a -> a
(<>)
instance (a ~ Mega.Tokens s, IsString a, Eq a, Mega.Stream s)
    => IsString (SimpleParser s a) where
  fromString :: String -> SimpleParser s a
fromString String
s = forall e s (m :: * -> *).
MonadParsec e s m =>
(Tokens s -> Tokens s -> Bool) -> Tokens s -> m (Tokens s)
Mega.tokens forall a. Eq a => a -> a -> Bool
(==) (forall a. IsString a => String -> a
fromString String
s)

-- | The 'Parser' is an ordinary State transformer 
-- on Megaparsec streams.  
type Parser a = forall s. Mega.Stream s => StateT s Maybe a

-- | synonym for 'evalStateT'
parse :: SimpleParser s a -> s -> Maybe a
parse :: forall s a. SimpleParser s a -> s -> Maybe a
parse (SimpleParser StateT s Maybe a
p) = forall (m :: * -> *) s a. Monad m => StateT s m a -> s -> m a
evalStateT StateT s Maybe a
p

-- | Run the 'SimpleParser' on the given input. 
-- Consider using 'tryFast' instead if possible.  
runSimpleParser :: SimpleParser s a -> s -> Maybe (a,s)
runSimpleParser :: forall s a. SimpleParser s a -> s -> Maybe (a, s)
runSimpleParser (SimpleParser StateT s Maybe a
p) = forall s (m :: * -> *) a. StateT s m a -> s -> m (a, s)
runStateT StateT s Maybe a
p

-- | Use this to implement more parser combinators.
toSimpleParser :: StateT s Maybe a -> SimpleParser s a
toSimpleParser :: forall s a. StateT s Maybe a -> SimpleParser s a
toSimpleParser = forall s a. StateT s Maybe a -> SimpleParser s a
SimpleParser

-- | The @result@ type of Megaparsec has changed through 
-- the library versions 
-- (commonly @result = Either err@ for some @err@) 
-- whence we abstract over it. 
-- Instead of 
--
-- @
-- 'Mega.runParser' p
-- @
-- 
-- you should use 
--
-- @
-- 'tryFast' 'Mega.runParser' p
-- @
-- 
-- which tries the fast parser and falls back to 'Mega.Parsec' 
-- in case of a parse failure.
tryFast :: forall s a result. (Applicative result, Mega.Stream s) 
    => (Mega.Parsec Void s a -> String -> s -> result a) -- ^ function to run if fast parsing fails
    -> (forall p. (MonadParsec Void s p) => p a) -- ^ a generic parser
    -> String                                    -- ^ input stream name 
    -> s                                         -- ^ input stream
    -> result a
tryFast :: forall s a (result :: * -> *).
(Applicative result, Stream s) =>
(Parsec Void s a -> String -> s -> result a)
-> (forall (p :: * -> *). MonadParsec Void s p => p a)
-> String
-> s
-> result a
tryFast Parsec Void s a -> String -> s -> result a
runParser forall (p :: * -> *). MonadParsec Void s p => p a
p String
name s
s = case forall s a. SimpleParser s a -> s -> Maybe a
parse forall (p :: * -> *). MonadParsec Void s p => p a
p s
s of
    Just a
a -> forall (f :: * -> *) a. Applicative f => a -> f a
pure a
a
    Maybe a
Nothing -> Parsec Void s a -> String -> s -> result a
runParser forall (p :: * -> *). MonadParsec Void s p => p a
p String
name s
s

-- * Combinators

-- ** String parsing

-- | parses end of input
eof :: Parser ()
eof :: Parser ()
eof = forall s (m :: * -> *) a. (s -> m (a, s)) -> StateT s m a
StateT (\s
s -> if forall a. Maybe a -> Bool
isNothing (forall s. Stream s => s -> Maybe (Token s, s)
Mega.take1_ s
s) then forall a. a -> Maybe a
Just ((),s
s) else forall a. Maybe a
Nothing)
{-# INLINE eof #-}

-- | more efficient than, but equivalent to 'many' . 'satisfy'
takeWhileP :: (Mega.Stream s) => (Mega.Token s -> Bool) -> StateT s Maybe (Mega.Tokens s)
takeWhileP :: forall s.
Stream s =>
(Token s -> Bool) -> StateT s Maybe (Tokens s)
takeWhileP Token s -> Bool
p = forall s (m :: * -> *) a. (s -> m (a, s)) -> StateT s m a
StateT (forall a. a -> Maybe a
Just forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall s. Stream s => (Token s -> Bool) -> s -> (Tokens s, s)
Mega.takeWhile_ Token s -> Bool
p)
{-# INLINE takeWhileP #-}

-- | more efficient than, but equivalent to 'some' . 'satisfy'
takeWhile1P :: forall s. (Mega.Stream s) => (Mega.Token s -> Bool) -> StateT s Maybe (Mega.Tokens s)
takeWhile1P :: forall s.
Stream s =>
(Token s -> Bool) -> StateT s Maybe (Tokens s)
takeWhile1P = forall (m :: * -> *) a. MonadPlus m => (a -> Bool) -> m a -> m a
mfilter (Bool -> Bool
notforall b c a. (b -> c) -> (a -> b) -> a -> c
. forall s. Stream s => Proxy s -> Tokens s -> Bool
Mega.chunkEmpty (forall {k} (t :: k). Proxy t
Proxy :: Proxy s)) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall s.
Stream s =>
(Token s -> Bool) -> StateT s Maybe (Tokens s)
takeWhileP
{-# INLINE takeWhile1P #-}

-- | parse any string of given length, using 'splitAt'.  
-- More efficient than @\\n -> 'count' n ('satisfy' ('const' 'True'))@.
-- Fails if input does not have that many characters left. 
-- Since 'Mega.takeP' requests this parser to succeed only if 
-- the requested number of tokens can be returned, and we can never 
-- return a negative number of tokens, this parser fails for negative inputs. 
-- This behaviour changes in Megaparsec with version 9.3.0
#if MIN_VERSION_megaparsec(9,3,0)
countAny :: forall s. (Mega.Stream s) => Int -> StateT s Maybe (Mega.Tokens s)
countAny n = StateT (\s -> 
    Mega.takeN_ n s >>= 
        (\x@(taken,_) -> if Mega.chunkLength (Proxy :: Proxy s) taken < n then Nothing else Just x))
#else
countAny :: forall s. (Mega.Stream s) => Int -> StateT s Maybe (Mega.Tokens s)
countAny :: forall s. Stream s => Int -> StateT s Maybe (Tokens s)
countAny Int
n = if Int
n forall a. Ord a => a -> a -> Bool
< Int
0 then forall (m :: * -> *) a. MonadPlus m => m a
mzero else forall s (m :: * -> *) a. (s -> m (a, s)) -> StateT s m a
StateT (\s
s -> 
    forall s. Stream s => Int -> s -> Maybe (Tokens s, s)
Mega.takeN_ Int
n s
s forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= 
        (\x :: (Tokens s, s)
x@(Tokens s
taken,s
_) -> if forall s. Stream s => Proxy s -> Tokens s -> Int
Mega.chunkLength (forall {k} (t :: k). Proxy t
Proxy :: Proxy s) Tokens s
taken forall a. Ord a => a -> a -> Bool
< Int
n then forall a. Maybe a
Nothing else forall a. a -> Maybe a
Just (Tokens s, s)
x))
#endif
{-# INLINE countAny #-}

-- | (For the 'MonadParsec' instance) 'Parser' does not contain any state besides the input left to parse. 
#if MIN_VERSION_megaparsec(7,0,0)
#if MIN_VERSION_megaparsec(8,0,0) 
dummyState :: s -> Mega.State s e
dummyState :: forall s e. s -> State s e
dummyState s
s = Mega.State {
    stateInput :: s
stateInput = s
s,
    stateOffset :: Int
stateOffset = Int
0,
    statePosState :: PosState s
statePosState = Mega.PosState {
        pstateInput :: s
Mega.pstateInput = s
s,
        pstateOffset :: Int
Mega.pstateOffset = Int
0,
        pstateSourcePos :: SourcePos
Mega.pstateSourcePos = String -> SourcePos
Mega.initialPos String
"",
        pstateTabWidth :: Pos
Mega.pstateTabWidth = Pos
Mega.pos1,
        pstateLinePrefix :: String
Mega.pstateLinePrefix = String
""
        },
    stateParseErrors :: [ParseError s e]
stateParseErrors = []
}
getDummyState :: StateT s Maybe (Mega.State s e)
getDummyState :: forall s e. StateT s Maybe (State s e)
getDummyState = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall s e. s -> State s e
dummyState forall s (m :: * -> *). MonadState s m => m s
get
#else
dummyState :: s -> Mega.State s
dummyState s = Mega.State {
    stateInput = s,
    stateOffset = 0,
    statePosState = Mega.PosState {
        Mega.pstateInput = s,
        Mega.pstateOffset = 0,
        Mega.pstateSourcePos = Mega.initialPos "",
        Mega.pstateTabWidth = Mega.pos1,
        Mega.pstateLinePrefix = ""
        }
}
getDummyState :: StateT s Maybe (Mega.State s)
getDummyState = fmap dummyState get
#endif
#else
dummyState :: s -> Mega.State s
dummyState s = Mega.State {
    stateInput = s,
    statePos = return (Mega.initialPos "no source info"),
    stateTokensProcessed = 0,
    stateTabWidth = Mega.pos1
    }
getDummyState :: StateT s Maybe (Mega.State s)
getDummyState = fmap dummyState get
#endif

-- smart constructor
sStateT :: (s -> Maybe (a,s)) -> SimpleParser s a
sStateT :: forall s a. (s -> Maybe (a, s)) -> SimpleParser s a
sStateT = forall s a. StateT s Maybe a -> SimpleParser s a
SimpleParserforall b c a. (b -> c) -> (a -> b) -> a -> c
. forall s (m :: * -> *) a. (s -> m (a, s)) -> StateT s m a
StateT

#if MIN_VERSION_megaparsec(7,0,0) 
#if MIN_VERSION_megaparsec(8,0,0)
instance forall s. (Mega.Stream s) => MonadParsec Void s (SimpleParser s) where
    parseError :: forall a. ParseError s Void -> SimpleParser s a
parseError ParseError s Void
_ = forall s a. (s -> Maybe (a, s)) -> SimpleParser s a
sStateT (forall a b. a -> b -> a
const forall a. Maybe a
Nothing)
    label :: forall a. String -> SimpleParser s a -> SimpleParser s a
label String
_ = forall a. a -> a
id
    hidden :: forall a. SimpleParser s a -> SimpleParser s a
hidden = forall a. a -> a
id
    try :: forall a. SimpleParser s a -> SimpleParser s a
try = forall a. a -> a
id
    lookAhead :: forall a. SimpleParser s a -> SimpleParser s a
lookAhead SimpleParser s a
p = forall s a. (s -> Maybe (a, s)) -> SimpleParser s a
sStateT (\s
s -> case forall s a. SimpleParser s a -> s -> Maybe a
parse SimpleParser s a
p s
s of
        Maybe a
Nothing -> forall a. Maybe a
Nothing
        Just a
a  -> forall a. a -> Maybe a
Just (a
a,s
s))
    notFollowedBy :: forall a. SimpleParser s a -> SimpleParser s ()
notFollowedBy SimpleParser s a
p = forall s a. (s -> Maybe (a, s)) -> SimpleParser s a
sStateT (\s
s -> case forall s a. SimpleParser s a -> s -> Maybe a
parse SimpleParser s a
p s
s of
        Maybe a
Nothing -> forall a. a -> Maybe a
Just ((),s
s)
        Just a
_ ->  forall a. Maybe a
Nothing)
    withRecovery :: forall a.
(ParseError s Void -> SimpleParser s a)
-> SimpleParser s a -> SimpleParser s a
withRecovery ParseError s Void -> SimpleParser s a
handle SimpleParser s a
p = forall s a. (s -> Maybe (a, s)) -> SimpleParser s a
sStateT (\s
s -> forall b a. b -> (a -> b) -> Maybe a -> b
maybe (forall s a. SimpleParser s a -> s -> Maybe (a, s)
runSimpleParser (ParseError s Void -> SimpleParser s a
handle forall a. Monoid a => a
mempty) s
s) forall a. a -> Maybe a
Just (forall s a. SimpleParser s a -> s -> Maybe (a, s)
runSimpleParser SimpleParser s a
p s
s))
    observing :: forall a.
SimpleParser s a -> SimpleParser s (Either (ParseError s Void) a)
observing SimpleParser s a
p = forall s a. (s -> Maybe (a, s)) -> SimpleParser s a
sStateT (\s
s -> case forall s a. SimpleParser s a -> s -> Maybe (a, s)
runSimpleParser SimpleParser s a
p s
s of
        Maybe (a, s)
Nothing -> forall a. a -> Maybe a
Just (forall a b. a -> Either a b
Left forall a. Monoid a => a
mempty,s
s)
        Just (a
a,s
s') -> forall a. a -> Maybe a
Just (forall a b. b -> Either a b
Right a
a,s
s'))
    eof :: SimpleParser s ()
eof = forall s a. StateT s Maybe a -> SimpleParser s a
SimpleParser Parser ()
eof
    token :: forall a.
(Token s -> Maybe a)
-> Set (ErrorItem (Token s)) -> SimpleParser s a
token Token s -> Maybe a
test Set (ErrorItem (Token s))
_ = forall s a. (s -> Maybe (a, s)) -> SimpleParser s a
sStateT (\s
s -> case forall s. Stream s => s -> Maybe (Token s, s)
Mega.take1_ s
s of
        Maybe (Token s, s)
Nothing -> forall a. Maybe a
Nothing
        Just (Token s
t,s
s') -> case Token s -> Maybe a
test Token s
t of
            Maybe a
Nothing -> forall a. Maybe a
Nothing
            Just  a
a -> forall a. a -> Maybe a
Just (a
a,s
s'))
    tokens :: (Tokens s -> Tokens s -> Bool)
-> Tokens s -> SimpleParser s (Tokens s)
tokens Tokens s -> Tokens s -> Bool
cmp Tokens s
xs = forall s a. (s -> Maybe (a, s)) -> SimpleParser s a
sStateT (\s
s -> case forall s. Stream s => Int -> s -> Maybe (Tokens s, s)
Mega.takeN_ (forall s. Stream s => Proxy s -> Tokens s -> Int
Mega.chunkLength (forall {k} (t :: k). Proxy t
Proxy :: Proxy s) Tokens s
xs) s
s of
        Maybe (Tokens s, s)
Nothing -> forall a. Maybe a
Nothing
        Just (Tokens s
ys,s
s') -> if Tokens s -> Tokens s -> Bool
cmp Tokens s
xs Tokens s
ys then forall a. a -> Maybe a
Just (Tokens s
xs,s
s') else forall a. Maybe a
Nothing)
    getParserState :: SimpleParser s (State s Void)
getParserState = forall s a. StateT s Maybe a -> SimpleParser s a
SimpleParser forall s e. StateT s Maybe (State s e)
getDummyState
    updateParserState :: (State s Void -> State s Void) -> SimpleParser s ()
updateParserState State s Void -> State s Void
f = forall s a. (s -> Maybe (a, s)) -> SimpleParser s a
sStateT (\s
s -> forall a. a -> Maybe a
Just ((),(forall s e. State s e -> s
stateInputforall b c a. (b -> c) -> (a -> b) -> a -> c
.State s Void -> State s Void
fforall b c a. (b -> c) -> (a -> b) -> a -> c
.forall s e. s -> State s e
dummyState) s
s))
    takeWhileP :: Maybe String -> (Token s -> Bool) -> SimpleParser s (Tokens s)
takeWhileP Maybe String
_  = forall s a. StateT s Maybe a -> SimpleParser s a
SimpleParser forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall s.
Stream s =>
(Token s -> Bool) -> StateT s Maybe (Tokens s)
takeWhileP
    takeWhile1P :: Maybe String -> (Token s -> Bool) -> SimpleParser s (Tokens s)
takeWhile1P Maybe String
_ = forall s a. StateT s Maybe a -> SimpleParser s a
SimpleParser forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall s.
Stream s =>
(Token s -> Bool) -> StateT s Maybe (Tokens s)
takeWhile1P
    takeP :: Maybe String -> Int -> SimpleParser s (Tokens s)
takeP Maybe String
_ = forall s a. StateT s Maybe a -> SimpleParser s a
SimpleParser forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall s. Stream s => Int -> StateT s Maybe (Tokens s)
countAny
#else
instance forall s. (Mega.Stream s) => MonadParsec Void s (SimpleParser s) where
    failure _ _ = sStateT (const Nothing)
    fancyFailure _ = sStateT (const Nothing)
    label _ = id
    hidden = id
    try = id
    lookAhead p = sStateT (\s -> case parse p s of
        Nothing -> Nothing
        Just a  -> Just (a,s))
    notFollowedBy p = sStateT (\s -> case parse p s of
        Nothing -> Just ((),s)
        Just _ ->  Nothing)
    withRecovery handle p = sStateT (\s -> maybe (runSimpleParser (handle mempty) s) Just (runSimpleParser p s))
    observing p = sStateT (\s -> case runSimpleParser p s of
        Nothing -> Just (Left mempty,s)
        Just (a,s') -> Just (Right a,s'))
    eof = SimpleParser eof
    token test _ = sStateT (\s -> case Mega.take1_ s of
        Nothing -> Nothing
        Just (t,s') -> case test t of
            Nothing -> Nothing
            Just  a -> Just (a,s'))
    tokens cmp xs = sStateT (\s -> case Mega.takeN_ (Mega.chunkLength (Proxy :: Proxy s) xs) s of
        Nothing -> Nothing
        Just (ys,s') -> if cmp xs ys then Just (xs,s') else Nothing)
    getParserState = SimpleParser getDummyState
    updateParserState f = sStateT (\s -> Just ((),(stateInput.f.dummyState) s))
    takeWhileP _  = SimpleParser . takeWhileP
    takeWhile1P _ = SimpleParser . takeWhile1P
    takeP _ = SimpleParser . countAny
#endif
#else
instance forall s. (Mega.Stream s) => MonadParsec Void s (SimpleParser s) where
    failure _ _ = sStateT (const Nothing)
    fancyFailure _ = sStateT (const Nothing)
    label _ = id
    hidden = id
    try = id
    lookAhead p = sStateT (\s -> case parse p s of
        Nothing -> Nothing
        Just a  -> Just (a,s))
    notFollowedBy p = sStateT (\s -> case parse p s of
        Nothing -> Just ((),s)
        Just _ ->  Nothing)
    withRecovery handle p = sStateT (\s -> maybe (runSimpleParser (handle mempty) s) Just (runSimpleParser p s))
    observing p = sStateT (\s -> case runSimpleParser p s of
        Nothing -> Just (Left mempty,s)
        Just (a,s') -> Just (Right a,s'))
    eof = SimpleParser eof
    token test _ = sStateT (\s -> case Mega.take1_ s of
        Nothing -> Nothing
        Just (t,s') -> case test t of
            Nothing -> Nothing
            Just  a -> Just (a,s'))
    tokens cmp xs = sStateT (\s -> case Mega.takeN_ (Mega.chunkLength (Proxy :: Proxy s) xs) s of
        Nothing -> Nothing
        Just (ys,s') -> if cmp xs ys then Just (xs,s') else Nothing)
    getParserState = SimpleParser getDummyState
    updateParserState f = sStateT (\s -> Just ((),(stateInput.f.dummyState) s))
    takeWhileP _  = SimpleParser . takeWhileP
    takeWhile1P _ = SimpleParser . takeWhile1P
    takeP _ = SimpleParser . countAny
#endif