{-# LANGUAGE CPP #-}
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE ExistentialQuantification #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE PolymorphicComponents #-}
{-# LANGUAGE Safe #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE UndecidableInstances #-}
{-# OPTIONS_GHC -fno-warn-name-shadowing #-}
{-# OPTIONS_HADDOCK not-home #-}
module Text.Parsec.Prim
( unknownError
, sysUnExpectError
, unexpected
, ParsecT
, runParsecT
, mkPT
, Parsec
, Consumed(..)
, Reply(..)
, State(..)
, parsecMap
, parserReturn
, parserBind
, mergeErrorReply
, parserFail
, parserZero
, parserPlus
, (<?>)
, (<|>)
, label
, labels
, lookAhead
, Stream(..)
, tokens
, try
, token
, tokenPrim
, tokenPrimEx
, many
, skipMany
, manyAccum
, runPT
, runP
, runParserT
, runParser
, parse
, parseTest
, getPosition
, getInput
, setPosition
, setInput
, getParserState
, setParserState
, updateParserState
, getState
, putState
, modifyState
, setState
, updateState
) where
import Prelude hiding (sequence)
import qualified Data.ByteString.Lazy.Char8 as CL
import qualified Data.ByteString.Char8 as C
import Data.Typeable ( Typeable )
import qualified Data.Text as Text
import qualified Data.Text.Lazy as TextL
import qualified Data.List.NonEmpty as NE
import Data.List ( genericReplicate )
import Data.Traversable (sequence)
import qualified Data.Functor as Functor ( Functor(..) )
import qualified Data.Semigroup as Semigroup ( Semigroup(..) )
import qualified Data.Monoid as Monoid ( Monoid(..) )
import qualified Control.Applicative as Applicative ( Applicative(..), Alternative(..), liftA2 )
import Control.Monad (MonadPlus (..), ap, void, liftM)
import Control.Monad.Trans (MonadTrans (lift), MonadIO (liftIO))
import Control.Monad.Identity (Identity, runIdentity)
import qualified Control.Monad.Fail as Fail
import Control.Monad.Reader.Class (MonadReader (..))
import Control.Monad.State.Class (MonadState (..))
import Control.Monad.Cont.Class (MonadCont (..))
import Control.Monad.Error.Class (MonadError (..))
import Text.Parsec.Pos
import Text.Parsec.Error
unknownError :: State s u -> ParseError
unknownError :: forall s u. State s u -> ParseError
unknownError State s u
state = SourcePos -> ParseError
newErrorUnknown (forall s u. State s u -> SourcePos
statePos State s u
state)
sysUnExpectError :: String -> SourcePos -> Reply s u a
sysUnExpectError :: forall s u a. String -> SourcePos -> Reply s u a
sysUnExpectError String
msg SourcePos
pos = forall s u a. ParseError -> Reply s u a
Error (Message -> SourcePos -> ParseError
newErrorMessage (String -> Message
SysUnExpect String
msg) SourcePos
pos)
unexpected :: (Stream s m t) => String -> ParsecT s u m a
unexpected :: forall s (m :: * -> *) t u a.
Stream s m t =>
String -> ParsecT s u m a
unexpected String
msg
= forall s u (m :: * -> *) a.
(forall b.
State s u
-> (a -> State s u -> ParseError -> m b)
-> (ParseError -> m b)
-> (a -> State s u -> ParseError -> m b)
-> (ParseError -> m b)
-> m b)
-> ParsecT s u m a
ParsecT forall a b. (a -> b) -> a -> b
$ \State s u
s a -> State s u -> ParseError -> m b
_ ParseError -> m b
_ a -> State s u -> ParseError -> m b
_ ParseError -> m b
eerr ->
ParseError -> m b
eerr forall a b. (a -> b) -> a -> b
$ Message -> SourcePos -> ParseError
newErrorMessage (String -> Message
UnExpect String
msg) (forall s u. State s u -> SourcePos
statePos State s u
s)
newtype ParsecT s u m a
= ParsecT {forall s u (m :: * -> *) a.
ParsecT s u m a
-> forall b.
State s u
-> (a -> State s u -> ParseError -> m b)
-> (ParseError -> m b)
-> (a -> State s u -> ParseError -> m b)
-> (ParseError -> m b)
-> m b
unParser :: forall b .
State s u
-> (a -> State s u -> ParseError -> m b)
-> (ParseError -> m b)
-> (a -> State s u -> ParseError -> m b)
-> (ParseError -> m b)
-> m b
}
#if MIN_VERSION_base(4,7,0)
deriving ( Typeable )
#endif
runParsecT :: Monad m => ParsecT s u m a -> State s u -> m (Consumed (m (Reply s u a)))
{-# INLINABLE runParsecT #-}
runParsecT :: forall (m :: * -> *) s u a.
Monad m =>
ParsecT s u m a -> State s u -> m (Consumed (m (Reply s u a)))
runParsecT ParsecT s u m a
p State s u
s = forall s u (m :: * -> *) a.
ParsecT s u m a
-> forall b.
State s u
-> (a -> State s u -> ParseError -> m b)
-> (ParseError -> m b)
-> (a -> State s u -> ParseError -> m b)
-> (ParseError -> m b)
-> m b
unParser ParsecT s u m a
p State s u
s forall {m :: * -> *} {m :: * -> *} {a} {s} {u}.
(Monad m, Monad m) =>
a -> State s u -> ParseError -> m (Consumed (m (Reply s u a)))
cok forall {m :: * -> *} {m :: * -> *} {s} {u} {a}.
(Monad m, Monad m) =>
ParseError -> m (Consumed (m (Reply s u a)))
cerr forall {m :: * -> *} {m :: * -> *} {a} {s} {u}.
(Monad m, Monad m) =>
a -> State s u -> ParseError -> m (Consumed (m (Reply s u a)))
eok forall {m :: * -> *} {m :: * -> *} {s} {u} {a}.
(Monad m, Monad m) =>
ParseError -> m (Consumed (m (Reply s u a)))
eerr
where cok :: a -> State s u -> ParseError -> m (Consumed (m (Reply s u a)))
cok a
a State s u
s' ParseError
err = forall (m :: * -> *) a. Monad m => a -> m a
return forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. a -> Consumed a
Consumed forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s u a. a -> State s u -> ParseError -> Reply s u a
Ok a
a State s u
s' ParseError
err
cerr :: ParseError -> m (Consumed (m (Reply s u a)))
cerr ParseError
err = forall (m :: * -> *) a. Monad m => a -> m a
return forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. a -> Consumed a
Consumed forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s u a. ParseError -> Reply s u a
Error ParseError
err
eok :: a -> State s u -> ParseError -> m (Consumed (m (Reply s u a)))
eok a
a State s u
s' ParseError
err = forall (m :: * -> *) a. Monad m => a -> m a
return forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. a -> Consumed a
Empty forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s u a. a -> State s u -> ParseError -> Reply s u a
Ok a
a State s u
s' ParseError
err
eerr :: ParseError -> m (Consumed (m (Reply s u a)))
eerr ParseError
err = forall (m :: * -> *) a. Monad m => a -> m a
return forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. a -> Consumed a
Empty forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall s u a. ParseError -> Reply s u a
Error ParseError
err
mkPT :: Monad m => (State s u -> m (Consumed (m (Reply s u a)))) -> ParsecT s u m a
{-# INLINABLE mkPT #-}
mkPT :: forall (m :: * -> *) s u a.
Monad m =>
(State s u -> m (Consumed (m (Reply s u a)))) -> ParsecT s u m a
mkPT State s u -> m (Consumed (m (Reply s u a)))
k = forall s u (m :: * -> *) a.
(forall b.
State s u
-> (a -> State s u -> ParseError -> m b)
-> (ParseError -> m b)
-> (a -> State s u -> ParseError -> m b)
-> (ParseError -> m b)
-> m b)
-> ParsecT s u m a
ParsecT forall a b. (a -> b) -> a -> b
$ \State s u
s a -> State s u -> ParseError -> m b
cok ParseError -> m b
cerr a -> State s u -> ParseError -> m b
eok ParseError -> m b
eerr -> do
Consumed (m (Reply s u a))
cons <- State s u -> m (Consumed (m (Reply s u a)))
k State s u
s
case Consumed (m (Reply s u a))
cons of
Consumed m (Reply s u a)
mrep -> do
Reply s u a
rep <- m (Reply s u a)
mrep
case Reply s u a
rep of
Ok a
x State s u
s' ParseError
err -> a -> State s u -> ParseError -> m b
cok a
x State s u
s' ParseError
err
Error ParseError
err -> ParseError -> m b
cerr ParseError
err
Empty m (Reply s u a)
mrep -> do
Reply s u a
rep <- m (Reply s u a)
mrep
case Reply s u a
rep of
Ok a
x State s u
s' ParseError
err -> a -> State s u -> ParseError -> m b
eok a
x State s u
s' ParseError
err
Error ParseError
err -> ParseError -> m b
eerr ParseError
err
type Parsec s u = ParsecT s u Identity
data Consumed a = Consumed a
| Empty !a
deriving ( Typeable )
data Reply s u a = Ok a !(State s u) ParseError
| Error ParseError
deriving ( Typeable )
data State s u = State {
forall s u. State s u -> s
stateInput :: s,
forall s u. State s u -> SourcePos
statePos :: !SourcePos,
forall s u. State s u -> u
stateUser :: !u
}
deriving ( Typeable )
instance Semigroup.Semigroup a => Semigroup.Semigroup (ParsecT s u m a) where
<> :: ParsecT s u m a -> ParsecT s u m a -> ParsecT s u m a
(<>) = forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
Applicative.liftA2 forall a. Semigroup a => a -> a -> a
(Semigroup.<>)
#if MIN_VERSION_base(4,8,0)
sconcat :: NonEmpty (ParsecT s u m a) -> ParsecT s u m a
sconcat = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a. Semigroup a => NonEmpty a -> a
Semigroup.sconcat forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: * -> *) (m :: * -> *) a.
(Traversable t, Monad m) =>
t (m a) -> m (t a)
sequence
#else
sconcat = fmap (Semigroup.sconcat . NE.fromList) . sequence . NE.toList
#endif
stimes :: forall b. Integral b => b -> ParsecT s u m a -> ParsecT s u m a
stimes b
b = forall a. Semigroup a => NonEmpty a -> a
Semigroup.sconcat forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. [a] -> NonEmpty a
NE.fromList forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall i a. Integral i => i -> a -> [a]
genericReplicate b
b
instance ( Monoid.Monoid a
, Semigroup.Semigroup (ParsecT s u m a)
) => Monoid.Monoid (ParsecT s u m a) where
mempty :: ParsecT s u m a
mempty = forall (f :: * -> *) a. Applicative f => a -> f a
Applicative.pure forall a. Monoid a => a
Monoid.mempty
mappend :: ParsecT s u m a -> ParsecT s u m a -> ParsecT s u m a
mappend = forall a. Semigroup a => a -> a -> a
(Semigroup.<>)
mconcat :: [ParsecT s u m a] -> ParsecT s u m a
mconcat = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
Functor.fmap forall a. Monoid a => [a] -> a
Monoid.mconcat forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: * -> *) (m :: * -> *) a.
(Traversable t, Monad m) =>
t (m a) -> m (t a)
sequence
instance Functor Consumed where
fmap :: forall a b. (a -> b) -> Consumed a -> Consumed b
fmap a -> b
f (Consumed a
x) = forall a. a -> Consumed a
Consumed (a -> b
f a
x)
fmap a -> b
f (Empty a
x) = forall a. a -> Consumed a
Empty (a -> b
f a
x)
instance Functor (Reply s u) where
fmap :: forall a b. (a -> b) -> Reply s u a -> Reply s u b
fmap a -> b
f (Ok a
x State s u
s ParseError
e) = forall s u a. a -> State s u -> ParseError -> Reply s u a
Ok (a -> b
f a
x) State s u
s ParseError
e
fmap a -> b
_ (Error ParseError
e) = forall s u a. ParseError -> Reply s u a
Error ParseError
e
instance Functor (ParsecT s u m) where
fmap :: forall a b. (a -> b) -> ParsecT s u m a -> ParsecT s u m b
fmap a -> b
f ParsecT s u m a
p = forall a b s u (m :: * -> *).
(a -> b) -> ParsecT s u m a -> ParsecT s u m b
parsecMap a -> b
f ParsecT s u m a
p
parsecMap :: (a -> b) -> ParsecT s u m a -> ParsecT s u m b
parsecMap :: forall a b s u (m :: * -> *).
(a -> b) -> ParsecT s u m a -> ParsecT s u m b
parsecMap a -> b
f ParsecT s u m a
p
= forall s u (m :: * -> *) a.
(forall b.
State s u
-> (a -> State s u -> ParseError -> m b)
-> (ParseError -> m b)
-> (a -> State s u -> ParseError -> m b)
-> (ParseError -> m b)
-> m b)
-> ParsecT s u m a
ParsecT forall a b. (a -> b) -> a -> b
$ \State s u
s b -> State s u -> ParseError -> m b
cok ParseError -> m b
cerr b -> State s u -> ParseError -> m b
eok ParseError -> m b
eerr ->
forall s u (m :: * -> *) a.
ParsecT s u m a
-> forall b.
State s u
-> (a -> State s u -> ParseError -> m b)
-> (ParseError -> m b)
-> (a -> State s u -> ParseError -> m b)
-> (ParseError -> m b)
-> m b
unParser ParsecT s u m a
p State s u
s (b -> State s u -> ParseError -> m b
cok forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> b
f) ParseError -> m b
cerr (b -> State s u -> ParseError -> m b
eok forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> b
f) ParseError -> m b
eerr
instance Applicative.Applicative (ParsecT s u m) where
pure :: forall a. a -> ParsecT s u m a
pure = forall a s u (m :: * -> *). a -> ParsecT s u m a
parserReturn
<*> :: forall a b.
ParsecT s u m (a -> b) -> ParsecT s u m a -> ParsecT s u m b
(<*>) = forall (m :: * -> *) a b. Monad m => m (a -> b) -> m a -> m b
ap
ParsecT s u m a
p1 *> :: forall a b. ParsecT s u m a -> ParsecT s u m b -> ParsecT s u m b
*> ParsecT s u m b
p2 = ParsecT s u m a
p1 forall s u (m :: * -> *) a b.
ParsecT s u m a -> (a -> ParsecT s u m b) -> ParsecT s u m b
`parserBind` forall a b. a -> b -> a
const ParsecT s u m b
p2
ParsecT s u m a
p1 <* :: forall a b. ParsecT s u m a -> ParsecT s u m b -> ParsecT s u m a
<* ParsecT s u m b
p2 = do { a
x1 <- ParsecT s u m a
p1 ; forall (f :: * -> *) a. Functor f => f a -> f ()
void ParsecT s u m b
p2 ; forall (m :: * -> *) a. Monad m => a -> m a
return a
x1 }
instance Applicative.Alternative (ParsecT s u m) where
empty :: forall a. ParsecT s u m a
empty = forall (m :: * -> *) a. MonadPlus m => m a
mzero
<|> :: forall a. ParsecT s u m a -> ParsecT s u m a -> ParsecT s u m a
(<|>) = forall (m :: * -> *) a. MonadPlus m => m a -> m a -> m a
mplus
instance Monad (ParsecT s u m) where
return :: forall a. a -> ParsecT s u m a
return = forall (f :: * -> *) a. Applicative f => a -> f a
Applicative.pure
ParsecT s u m a
p >>= :: forall a b.
ParsecT s u m a -> (a -> ParsecT s u m b) -> ParsecT s u m b
>>= a -> ParsecT s u m b
f = forall s u (m :: * -> *) a b.
ParsecT s u m a -> (a -> ParsecT s u m b) -> ParsecT s u m b
parserBind ParsecT s u m a
p a -> ParsecT s u m b
f
>> :: forall a b. ParsecT s u m a -> ParsecT s u m b -> ParsecT s u m b
(>>) = forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
(Applicative.*>)
#if !MIN_VERSION_base(4,13,0)
fail = Fail.fail
#endif
instance Fail.MonadFail (ParsecT s u m) where
fail :: forall a. String -> ParsecT s u m a
fail = forall s u (m :: * -> *) a. String -> ParsecT s u m a
parserFail
instance (MonadIO m) => MonadIO (ParsecT s u m) where
liftIO :: forall a. IO a -> ParsecT s u m a
liftIO = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO
instance (MonadReader r m) => MonadReader r (ParsecT s u m) where
ask :: ParsecT s u m r
ask = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall r (m :: * -> *). MonadReader r m => m r
ask
local :: forall a. (r -> r) -> ParsecT s u m a -> ParsecT s u m a
local r -> r
f ParsecT s u m a
p = forall (m :: * -> *) s u a.
Monad m =>
(State s u -> m (Consumed (m (Reply s u a)))) -> ParsecT s u m a
mkPT forall a b. (a -> b) -> a -> b
$ \State s u
s -> forall r (m :: * -> *) a. MonadReader r m => (r -> r) -> m a -> m a
local r -> r
f (forall (m :: * -> *) s u a.
Monad m =>
ParsecT s u m a -> State s u -> m (Consumed (m (Reply s u a)))
runParsecT ParsecT s u m a
p State s u
s)
instance (MonadState s m) => MonadState s (ParsecT s' u m) where
get :: ParsecT s' u m s
get = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall s (m :: * -> *). MonadState s m => m s
get
put :: s -> ParsecT s' u m ()
put = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall s (m :: * -> *). MonadState s m => s -> m ()
put
instance (MonadCont m) => MonadCont (ParsecT s u m) where
callCC :: forall a b.
((a -> ParsecT s u m b) -> ParsecT s u m a) -> ParsecT s u m a
callCC (a -> ParsecT s u m b) -> ParsecT s u m a
f = forall (m :: * -> *) s u a.
Monad m =>
(State s u -> m (Consumed (m (Reply s u a)))) -> ParsecT s u m a
mkPT forall a b. (a -> b) -> a -> b
$ \State s u
s ->
forall (m :: * -> *) a b. MonadCont m => ((a -> m b) -> m a) -> m a
callCC forall a b. (a -> b) -> a -> b
$ \Consumed (m (Reply s u a)) -> m (Consumed (m (Reply s u b)))
c ->
forall (m :: * -> *) s u a.
Monad m =>
ParsecT s u m a -> State s u -> m (Consumed (m (Reply s u a)))
runParsecT ((a -> ParsecT s u m b) -> ParsecT s u m a
f (\a
a -> forall (m :: * -> *) s u a.
Monad m =>
(State s u -> m (Consumed (m (Reply s u a)))) -> ParsecT s u m a
mkPT forall a b. (a -> b) -> a -> b
$ \State s u
s' -> Consumed (m (Reply s u a)) -> m (Consumed (m (Reply s u b)))
c (forall {m :: * -> *} {s} {u} {a}.
Monad m =>
State s u -> a -> Consumed (m (Reply s u a))
pack State s u
s' a
a))) State s u
s
where pack :: State s u -> a -> Consumed (m (Reply s u a))
pack State s u
s a
a= forall a. a -> Consumed a
Empty forall a b. (a -> b) -> a -> b
$ forall (m :: * -> *) a. Monad m => a -> m a
return (forall s u a. a -> State s u -> ParseError -> Reply s u a
Ok a
a State s u
s (forall s u. State s u -> ParseError
unknownError State s u
s))
instance (MonadError e m) => MonadError e (ParsecT s u m) where
throwError :: forall a. e -> ParsecT s u m a
throwError = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError
ParsecT s u m a
p catchError :: forall a.
ParsecT s u m a -> (e -> ParsecT s u m a) -> ParsecT s u m a
`catchError` e -> ParsecT s u m a
h = forall (m :: * -> *) s u a.
Monad m =>
(State s u -> m (Consumed (m (Reply s u a)))) -> ParsecT s u m a
mkPT forall a b. (a -> b) -> a -> b
$ \State s u
s ->
forall (m :: * -> *) s u a.
Monad m =>
ParsecT s u m a -> State s u -> m (Consumed (m (Reply s u a)))
runParsecT ParsecT s u m a
p State s u
s forall e (m :: * -> *) a.
MonadError e m =>
m a -> (e -> m a) -> m a
`catchError` \e
e ->
forall (m :: * -> *) s u a.
Monad m =>
ParsecT s u m a -> State s u -> m (Consumed (m (Reply s u a)))
runParsecT (e -> ParsecT s u m a
h e
e) State s u
s
parserReturn :: a -> ParsecT s u m a
parserReturn :: forall a s u (m :: * -> *). a -> ParsecT s u m a
parserReturn a
x
= forall s u (m :: * -> *) a.
(forall b.
State s u
-> (a -> State s u -> ParseError -> m b)
-> (ParseError -> m b)
-> (a -> State s u -> ParseError -> m b)
-> (ParseError -> m b)
-> m b)
-> ParsecT s u m a
ParsecT forall a b. (a -> b) -> a -> b
$ \State s u
s a -> State s u -> ParseError -> m b
_ ParseError -> m b
_ a -> State s u -> ParseError -> m b
eok ParseError -> m b
_ ->
a -> State s u -> ParseError -> m b
eok a
x State s u
s (forall s u. State s u -> ParseError
unknownError State s u
s)
parserBind :: ParsecT s u m a -> (a -> ParsecT s u m b) -> ParsecT s u m b
{-# INLINE parserBind #-}
parserBind :: forall s u (m :: * -> *) a b.
ParsecT s u m a -> (a -> ParsecT s u m b) -> ParsecT s u m b
parserBind ParsecT s u m a
m a -> ParsecT s u m b
k
= forall s u (m :: * -> *) a.
(forall b.
State s u
-> (a -> State s u -> ParseError -> m b)
-> (ParseError -> m b)
-> (a -> State s u -> ParseError -> m b)
-> (ParseError -> m b)
-> m b)
-> ParsecT s u m a
ParsecT forall a b. (a -> b) -> a -> b
$ \State s u
s b -> State s u -> ParseError -> m b
cok ParseError -> m b
cerr b -> State s u -> ParseError -> m b
eok ParseError -> m b
eerr ->
let
mcok :: a -> State s u -> ParseError -> m b
mcok a
x State s u
s ParseError
err
| ParseError -> Bool
errorIsUnknown ParseError
err = forall s u (m :: * -> *) a.
ParsecT s u m a
-> forall b.
State s u
-> (a -> State s u -> ParseError -> m b)
-> (ParseError -> m b)
-> (a -> State s u -> ParseError -> m b)
-> (ParseError -> m b)
-> m b
unParser (a -> ParsecT s u m b
k a
x) State s u
s b -> State s u -> ParseError -> m b
cok ParseError -> m b
cerr b -> State s u -> ParseError -> m b
cok ParseError -> m b
cerr
| Bool
otherwise =
let
pcok :: b -> State s u -> ParseError -> m b
pcok = b -> State s u -> ParseError -> m b
cok
pcerr :: ParseError -> m b
pcerr = ParseError -> m b
cerr
peok :: b -> State s u -> ParseError -> m b
peok b
x State s u
s ParseError
err' = b -> State s u -> ParseError -> m b
cok b
x State s u
s (ParseError -> ParseError -> ParseError
mergeError ParseError
err ParseError
err')
peerr :: ParseError -> m b
peerr ParseError
err' = ParseError -> m b
cerr (ParseError -> ParseError -> ParseError
mergeError ParseError
err ParseError
err')
in forall s u (m :: * -> *) a.
ParsecT s u m a
-> forall b.
State s u
-> (a -> State s u -> ParseError -> m b)
-> (ParseError -> m b)
-> (a -> State s u -> ParseError -> m b)
-> (ParseError -> m b)
-> m b
unParser (a -> ParsecT s u m b
k a
x) State s u
s b -> State s u -> ParseError -> m b
pcok ParseError -> m b
pcerr b -> State s u -> ParseError -> m b
peok ParseError -> m b
peerr
meok :: a -> State s u -> ParseError -> m b
meok a
x State s u
s ParseError
err
| ParseError -> Bool
errorIsUnknown ParseError
err = forall s u (m :: * -> *) a.
ParsecT s u m a
-> forall b.
State s u
-> (a -> State s u -> ParseError -> m b)
-> (ParseError -> m b)
-> (a -> State s u -> ParseError -> m b)
-> (ParseError -> m b)
-> m b
unParser (a -> ParsecT s u m b
k a
x) State s u
s b -> State s u -> ParseError -> m b
cok ParseError -> m b
cerr b -> State s u -> ParseError -> m b
eok ParseError -> m b
eerr
| Bool
otherwise =
let
pcok :: b -> State s u -> ParseError -> m b
pcok = b -> State s u -> ParseError -> m b
cok
peok :: b -> State s u -> ParseError -> m b
peok b
x State s u
s ParseError
err' = b -> State s u -> ParseError -> m b
eok b
x State s u
s (ParseError -> ParseError -> ParseError
mergeError ParseError
err ParseError
err')
pcerr :: ParseError -> m b
pcerr = ParseError -> m b
cerr
peerr :: ParseError -> m b
peerr ParseError
err' = ParseError -> m b
eerr (ParseError -> ParseError -> ParseError
mergeError ParseError
err ParseError
err')
in forall s u (m :: * -> *) a.
ParsecT s u m a
-> forall b.
State s u
-> (a -> State s u -> ParseError -> m b)
-> (ParseError -> m b)
-> (a -> State s u -> ParseError -> m b)
-> (ParseError -> m b)
-> m b
unParser (a -> ParsecT s u m b
k a
x) State s u
s b -> State s u -> ParseError -> m b
pcok ParseError -> m b
pcerr b -> State s u -> ParseError -> m b
peok ParseError -> m b
peerr
mcerr :: ParseError -> m b
mcerr = ParseError -> m b
cerr
meerr :: ParseError -> m b
meerr = ParseError -> m b
eerr
in forall s u (m :: * -> *) a.
ParsecT s u m a
-> forall b.
State s u
-> (a -> State s u -> ParseError -> m b)
-> (ParseError -> m b)
-> (a -> State s u -> ParseError -> m b)
-> (ParseError -> m b)
-> m b
unParser ParsecT s u m a
m State s u
s a -> State s u -> ParseError -> m b
mcok ParseError -> m b
mcerr a -> State s u -> ParseError -> m b
meok ParseError -> m b
meerr
mergeErrorReply :: ParseError -> Reply s u a -> Reply s u a
mergeErrorReply :: forall s u a. ParseError -> Reply s u a -> Reply s u a
mergeErrorReply ParseError
err1 Reply s u a
reply
= case Reply s u a
reply of
Ok a
x State s u
state ParseError
err2 -> forall s u a. a -> State s u -> ParseError -> Reply s u a
Ok a
x State s u
state (ParseError -> ParseError -> ParseError
mergeError ParseError
err1 ParseError
err2)
Error ParseError
err2 -> forall s u a. ParseError -> Reply s u a
Error (ParseError -> ParseError -> ParseError
mergeError ParseError
err1 ParseError
err2)
parserFail :: String -> ParsecT s u m a
parserFail :: forall s u (m :: * -> *) a. String -> ParsecT s u m a
parserFail String
msg
= forall s u (m :: * -> *) a.
(forall b.
State s u
-> (a -> State s u -> ParseError -> m b)
-> (ParseError -> m b)
-> (a -> State s u -> ParseError -> m b)
-> (ParseError -> m b)
-> m b)
-> ParsecT s u m a
ParsecT forall a b. (a -> b) -> a -> b
$ \State s u
s a -> State s u -> ParseError -> m b
_ ParseError -> m b
_ a -> State s u -> ParseError -> m b
_ ParseError -> m b
eerr ->
ParseError -> m b
eerr forall a b. (a -> b) -> a -> b
$ Message -> SourcePos -> ParseError
newErrorMessage (String -> Message
Message String
msg) (forall s u. State s u -> SourcePos
statePos State s u
s)
instance MonadPlus (ParsecT s u m) where
mzero :: forall a. ParsecT s u m a
mzero = forall s u (m :: * -> *) a. ParsecT s u m a
parserZero
mplus :: forall a. ParsecT s u m a -> ParsecT s u m a -> ParsecT s u m a
mplus ParsecT s u m a
p1 ParsecT s u m a
p2 = forall s u (m :: * -> *) a.
ParsecT s u m a -> ParsecT s u m a -> ParsecT s u m a
parserPlus ParsecT s u m a
p1 ParsecT s u m a
p2
parserZero :: ParsecT s u m a
parserZero :: forall s u (m :: * -> *) a. ParsecT s u m a
parserZero
= forall s u (m :: * -> *) a.
(forall b.
State s u
-> (a -> State s u -> ParseError -> m b)
-> (ParseError -> m b)
-> (a -> State s u -> ParseError -> m b)
-> (ParseError -> m b)
-> m b)
-> ParsecT s u m a
ParsecT forall a b. (a -> b) -> a -> b
$ \State s u
s a -> State s u -> ParseError -> m b
_ ParseError -> m b
_ a -> State s u -> ParseError -> m b
_ ParseError -> m b
eerr ->
ParseError -> m b
eerr forall a b. (a -> b) -> a -> b
$ forall s u. State s u -> ParseError
unknownError State s u
s
parserPlus :: ParsecT s u m a -> ParsecT s u m a -> ParsecT s u m a
{-# INLINE parserPlus #-}
parserPlus :: forall s u (m :: * -> *) a.
ParsecT s u m a -> ParsecT s u m a -> ParsecT s u m a
parserPlus ParsecT s u m a
m ParsecT s u m a
n
= forall s u (m :: * -> *) a.
(forall b.
State s u
-> (a -> State s u -> ParseError -> m b)
-> (ParseError -> m b)
-> (a -> State s u -> ParseError -> m b)
-> (ParseError -> m b)
-> m b)
-> ParsecT s u m a
ParsecT forall a b. (a -> b) -> a -> b
$ \State s u
s a -> State s u -> ParseError -> m b
cok ParseError -> m b
cerr a -> State s u -> ParseError -> m b
eok ParseError -> m b
eerr ->
let
meerr :: ParseError -> m b
meerr ParseError
err =
let
neok :: a -> State s u -> ParseError -> m b
neok a
y State s u
s' ParseError
err' = a -> State s u -> ParseError -> m b
eok a
y State s u
s' (ParseError -> ParseError -> ParseError
mergeError ParseError
err ParseError
err')
neerr :: ParseError -> m b
neerr ParseError
err' = ParseError -> m b
eerr forall a b. (a -> b) -> a -> b
$ ParseError -> ParseError -> ParseError
mergeError ParseError
err ParseError
err'
in forall s u (m :: * -> *) a.
ParsecT s u m a
-> forall b.
State s u
-> (a -> State s u -> ParseError -> m b)
-> (ParseError -> m b)
-> (a -> State s u -> ParseError -> m b)
-> (ParseError -> m b)
-> m b
unParser ParsecT s u m a
n State s u
s a -> State s u -> ParseError -> m b
cok ParseError -> m b
cerr a -> State s u -> ParseError -> m b
neok ParseError -> m b
neerr
in forall s u (m :: * -> *) a.
ParsecT s u m a
-> forall b.
State s u
-> (a -> State s u -> ParseError -> m b)
-> (ParseError -> m b)
-> (a -> State s u -> ParseError -> m b)
-> (ParseError -> m b)
-> m b
unParser ParsecT s u m a
m State s u
s a -> State s u -> ParseError -> m b
cok ParseError -> m b
cerr a -> State s u -> ParseError -> m b
eok ParseError -> m b
meerr
instance MonadTrans (ParsecT s u) where
lift :: forall (m :: * -> *) a. Monad m => m a -> ParsecT s u m a
lift m a
amb = forall s u (m :: * -> *) a.
(forall b.
State s u
-> (a -> State s u -> ParseError -> m b)
-> (ParseError -> m b)
-> (a -> State s u -> ParseError -> m b)
-> (ParseError -> m b)
-> m b)
-> ParsecT s u m a
ParsecT forall a b. (a -> b) -> a -> b
$ \State s u
s a -> State s u -> ParseError -> m b
_ ParseError -> m b
_ a -> State s u -> ParseError -> m b
eok ParseError -> m b
_ -> do
a
a <- m a
amb
a -> State s u -> ParseError -> m b
eok a
a State s u
s forall a b. (a -> b) -> a -> b
$ forall s u. State s u -> ParseError
unknownError State s u
s
infix 0 <?>
infixr 1 <|>
(<?>) :: (ParsecT s u m a) -> String -> (ParsecT s u m a)
ParsecT s u m a
p <?> :: forall s u (m :: * -> *) a.
ParsecT s u m a -> String -> ParsecT s u m a
<?> String
msg = forall s u (m :: * -> *) a.
ParsecT s u m a -> String -> ParsecT s u m a
label ParsecT s u m a
p String
msg
(<|>) :: (ParsecT s u m a) -> (ParsecT s u m a) -> (ParsecT s u m a)
ParsecT s u m a
p1 <|> :: forall s u (m :: * -> *) a.
ParsecT s u m a -> ParsecT s u m a -> ParsecT s u m a
<|> ParsecT s u m a
p2 = forall (m :: * -> *) a. MonadPlus m => m a -> m a -> m a
mplus ParsecT s u m a
p1 ParsecT s u m a
p2
label :: ParsecT s u m a -> String -> ParsecT s u m a
label :: forall s u (m :: * -> *) a.
ParsecT s u m a -> String -> ParsecT s u m a
label ParsecT s u m a
p String
msg
= forall s u (m :: * -> *) a.
ParsecT s u m a -> [String] -> ParsecT s u m a
labels ParsecT s u m a
p [String
msg]
labels :: ParsecT s u m a -> [String] -> ParsecT s u m a
labels :: forall s u (m :: * -> *) a.
ParsecT s u m a -> [String] -> ParsecT s u m a
labels ParsecT s u m a
p [String]
msgs =
forall s u (m :: * -> *) a.
(forall b.
State s u
-> (a -> State s u -> ParseError -> m b)
-> (ParseError -> m b)
-> (a -> State s u -> ParseError -> m b)
-> (ParseError -> m b)
-> m b)
-> ParsecT s u m a
ParsecT forall a b. (a -> b) -> a -> b
$ \State s u
s a -> State s u -> ParseError -> m b
cok ParseError -> m b
cerr a -> State s u -> ParseError -> m b
eok ParseError -> m b
eerr ->
let eok' :: a -> State s u -> ParseError -> m b
eok' a
x State s u
s' ParseError
error = a -> State s u -> ParseError -> m b
eok a
x State s u
s' forall a b. (a -> b) -> a -> b
$ if ParseError -> Bool
errorIsUnknown ParseError
error
then ParseError
error
else ParseError -> [String] -> ParseError
setExpectErrors ParseError
error [String]
msgs
eerr' :: ParseError -> m b
eerr' ParseError
err = ParseError -> m b
eerr forall a b. (a -> b) -> a -> b
$ ParseError -> [String] -> ParseError
setExpectErrors ParseError
err [String]
msgs
in forall s u (m :: * -> *) a.
ParsecT s u m a
-> forall b.
State s u
-> (a -> State s u -> ParseError -> m b)
-> (ParseError -> m b)
-> (a -> State s u -> ParseError -> m b)
-> (ParseError -> m b)
-> m b
unParser ParsecT s u m a
p State s u
s a -> State s u -> ParseError -> m b
cok ParseError -> m b
cerr a -> State s u -> ParseError -> m b
eok' ParseError -> m b
eerr'
where
setExpectErrors :: ParseError -> [String] -> ParseError
setExpectErrors ParseError
err [] = Message -> ParseError -> ParseError
setErrorMessage (String -> Message
Expect String
"") ParseError
err
setExpectErrors ParseError
err [String
msg] = Message -> ParseError -> ParseError
setErrorMessage (String -> Message
Expect String
msg) ParseError
err
setExpectErrors ParseError
err (String
msg:[String]
msgs)
= forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr (\String
msg' ParseError
err' -> Message -> ParseError -> ParseError
addErrorMessage (String -> Message
Expect String
msg') ParseError
err')
(Message -> ParseError -> ParseError
setErrorMessage (String -> Message
Expect String
msg) ParseError
err) [String]
msgs
class (Monad m) => Stream s m t | s -> t where
uncons :: s -> m (Maybe (t,s))
instance (Monad m) => Stream [tok] m tok where
uncons :: [tok] -> m (Maybe (tok, [tok]))
uncons [] = forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a. Maybe a
Nothing
uncons (tok
t:[tok]
ts) = forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a. a -> Maybe a
Just (tok
t,[tok]
ts)
{-# INLINE uncons #-}
instance (Monad m) => Stream CL.ByteString m Char where
uncons :: ByteString -> m (Maybe (Char, ByteString))
uncons = forall (m :: * -> *) a. Monad m => a -> m a
return forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Maybe (Char, ByteString)
CL.uncons
instance (Monad m) => Stream C.ByteString m Char where
uncons :: ByteString -> m (Maybe (Char, ByteString))
uncons = forall (m :: * -> *) a. Monad m => a -> m a
return forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Maybe (Char, ByteString)
C.uncons
instance (Monad m) => Stream Text.Text m Char where
uncons :: Text -> m (Maybe (Char, Text))
uncons = forall (m :: * -> *) a. Monad m => a -> m a
return forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Maybe (Char, Text)
Text.uncons
{-# INLINE uncons #-}
instance (Monad m) => Stream TextL.Text m Char where
uncons :: Text -> m (Maybe (Char, Text))
uncons = forall (m :: * -> *) a. Monad m => a -> m a
return forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Maybe (Char, Text)
TextL.uncons
{-# INLINE uncons #-}
tokens :: (Stream s m t, Eq t)
=> ([t] -> String)
-> (SourcePos -> [t] -> SourcePos)
-> [t]
-> ParsecT s u m [t]
{-# INLINE tokens #-}
tokens :: forall s (m :: * -> *) t u.
(Stream s m t, Eq t) =>
([t] -> String)
-> (SourcePos -> [t] -> SourcePos) -> [t] -> ParsecT s u m [t]
tokens [t] -> String
_ SourcePos -> [t] -> SourcePos
_ []
= forall s u (m :: * -> *) a.
(forall b.
State s u
-> (a -> State s u -> ParseError -> m b)
-> (ParseError -> m b)
-> (a -> State s u -> ParseError -> m b)
-> (ParseError -> m b)
-> m b)
-> ParsecT s u m a
ParsecT forall a b. (a -> b) -> a -> b
$ \State s u
s [t] -> State s u -> ParseError -> m b
_ ParseError -> m b
_ [t] -> State s u -> ParseError -> m b
eok ParseError -> m b
_ ->
[t] -> State s u -> ParseError -> m b
eok [] State s u
s forall a b. (a -> b) -> a -> b
$ forall s u. State s u -> ParseError
unknownError State s u
s
tokens [t] -> String
showTokens SourcePos -> [t] -> SourcePos
nextposs tts :: [t]
tts@(t
tok:[t]
toks)
= forall s u (m :: * -> *) a.
(forall b.
State s u
-> (a -> State s u -> ParseError -> m b)
-> (ParseError -> m b)
-> (a -> State s u -> ParseError -> m b)
-> (ParseError -> m b)
-> m b)
-> ParsecT s u m a
ParsecT forall a b. (a -> b) -> a -> b
$ \(State s
input SourcePos
pos u
u) [t] -> State s u -> ParseError -> m b
cok ParseError -> m b
cerr [t] -> State s u -> ParseError -> m b
_eok ParseError -> m b
eerr ->
let
errEof :: ParseError
errEof = (Message -> ParseError -> ParseError
setErrorMessage (String -> Message
Expect ([t] -> String
showTokens [t]
tts))
(Message -> SourcePos -> ParseError
newErrorMessage (String -> Message
SysUnExpect String
"") SourcePos
pos))
errExpect :: t -> ParseError
errExpect t
x = (Message -> ParseError -> ParseError
setErrorMessage (String -> Message
Expect ([t] -> String
showTokens [t]
tts))
(Message -> SourcePos -> ParseError
newErrorMessage (String -> Message
SysUnExpect ([t] -> String
showTokens [t
x])) SourcePos
pos))
walk :: [t] -> s -> m b
walk [] s
rs = s -> m b
ok s
rs
walk (t
t:[t]
ts) s
rs = do
Maybe (t, s)
sr <- forall s (m :: * -> *) t. Stream s m t => s -> m (Maybe (t, s))
uncons s
rs
case Maybe (t, s)
sr of
Maybe (t, s)
Nothing -> ParseError -> m b
cerr forall a b. (a -> b) -> a -> b
$ ParseError
errEof
Just (t
x,s
xs) | t
t forall a. Eq a => a -> a -> Bool
== t
x -> [t] -> s -> m b
walk [t]
ts s
xs
| Bool
otherwise -> ParseError -> m b
cerr forall a b. (a -> b) -> a -> b
$ t -> ParseError
errExpect t
x
ok :: s -> m b
ok s
rs = let pos' :: SourcePos
pos' = SourcePos -> [t] -> SourcePos
nextposs SourcePos
pos [t]
tts
s' :: State s u
s' = forall s u. s -> SourcePos -> u -> State s u
State s
rs SourcePos
pos' u
u
in [t] -> State s u -> ParseError -> m b
cok [t]
tts State s u
s' (SourcePos -> ParseError
newErrorUnknown SourcePos
pos')
in do
Maybe (t, s)
sr <- forall s (m :: * -> *) t. Stream s m t => s -> m (Maybe (t, s))
uncons s
input
case Maybe (t, s)
sr of
Maybe (t, s)
Nothing -> ParseError -> m b
eerr forall a b. (a -> b) -> a -> b
$ ParseError
errEof
Just (t
x,s
xs)
| t
tok forall a. Eq a => a -> a -> Bool
== t
x -> [t] -> s -> m b
walk [t]
toks s
xs
| Bool
otherwise -> ParseError -> m b
eerr forall a b. (a -> b) -> a -> b
$ t -> ParseError
errExpect t
x
try :: ParsecT s u m a -> ParsecT s u m a
try :: forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m a
try ParsecT s u m a
p =
forall s u (m :: * -> *) a.
(forall b.
State s u
-> (a -> State s u -> ParseError -> m b)
-> (ParseError -> m b)
-> (a -> State s u -> ParseError -> m b)
-> (ParseError -> m b)
-> m b)
-> ParsecT s u m a
ParsecT forall a b. (a -> b) -> a -> b
$ \State s u
s a -> State s u -> ParseError -> m b
cok ParseError -> m b
_ a -> State s u -> ParseError -> m b
eok ParseError -> m b
eerr ->
forall s u (m :: * -> *) a.
ParsecT s u m a
-> forall b.
State s u
-> (a -> State s u -> ParseError -> m b)
-> (ParseError -> m b)
-> (a -> State s u -> ParseError -> m b)
-> (ParseError -> m b)
-> m b
unParser ParsecT s u m a
p State s u
s a -> State s u -> ParseError -> m b
cok ParseError -> m b
eerr a -> State s u -> ParseError -> m b
eok ParseError -> m b
eerr
lookAhead :: (Stream s m t) => ParsecT s u m a -> ParsecT s u m a
lookAhead :: forall s (m :: * -> *) t u a.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m a
lookAhead ParsecT s u m a
p =
forall s u (m :: * -> *) a.
(forall b.
State s u
-> (a -> State s u -> ParseError -> m b)
-> (ParseError -> m b)
-> (a -> State s u -> ParseError -> m b)
-> (ParseError -> m b)
-> m b)
-> ParsecT s u m a
ParsecT forall a b. (a -> b) -> a -> b
$ \State s u
s a -> State s u -> ParseError -> m b
_ ParseError -> m b
cerr a -> State s u -> ParseError -> m b
eok ParseError -> m b
eerr -> do
let eok' :: a -> p -> p -> m b
eok' a
a p
_ p
_ = a -> State s u -> ParseError -> m b
eok a
a State s u
s (SourcePos -> ParseError
newErrorUnknown (forall s u. State s u -> SourcePos
statePos State s u
s))
forall s u (m :: * -> *) a.
ParsecT s u m a
-> forall b.
State s u
-> (a -> State s u -> ParseError -> m b)
-> (ParseError -> m b)
-> (a -> State s u -> ParseError -> m b)
-> (ParseError -> m b)
-> m b
unParser ParsecT s u m a
p State s u
s forall {p} {p}. a -> p -> p -> m b
eok' ParseError -> m b
cerr forall {p} {p}. a -> p -> p -> m b
eok' ParseError -> m b
eerr
token :: (Stream s Identity t)
=> (t -> String)
-> (t -> SourcePos)
-> (t -> Maybe a)
-> Parsec s u a
{-# INLINABLE token #-}
token :: forall s t a u.
Stream s Identity t =>
(t -> String) -> (t -> SourcePos) -> (t -> Maybe a) -> Parsec s u a
token t -> String
showToken t -> SourcePos
tokpos t -> Maybe a
test = forall s (m :: * -> *) t a u.
Stream s m t =>
(t -> String)
-> (SourcePos -> t -> s -> SourcePos)
-> (t -> Maybe a)
-> ParsecT s u m a
tokenPrim t -> String
showToken forall {s} {p}. Stream s Identity t => p -> t -> s -> SourcePos
nextpos t -> Maybe a
test
where
nextpos :: p -> t -> s -> SourcePos
nextpos p
_ t
tok s
ts = case forall a. Identity a -> a
runIdentity (forall s (m :: * -> *) t. Stream s m t => s -> m (Maybe (t, s))
uncons s
ts) of
Maybe (t, s)
Nothing -> t -> SourcePos
tokpos t
tok
Just (t
tok',s
_) -> t -> SourcePos
tokpos t
tok'
tokenPrim :: (Stream s m t)
=> (t -> String)
-> (SourcePos -> t -> s -> SourcePos)
-> (t -> Maybe a)
-> ParsecT s u m a
{-# INLINE tokenPrim #-}
tokenPrim :: forall s (m :: * -> *) t a u.
Stream s m t =>
(t -> String)
-> (SourcePos -> t -> s -> SourcePos)
-> (t -> Maybe a)
-> ParsecT s u m a
tokenPrim t -> String
showToken SourcePos -> t -> s -> SourcePos
nextpos t -> Maybe a
test = forall s (m :: * -> *) t u a.
Stream s m t =>
(t -> String)
-> (SourcePos -> t -> s -> SourcePos)
-> Maybe (SourcePos -> t -> s -> u -> u)
-> (t -> Maybe a)
-> ParsecT s u m a
tokenPrimEx t -> String
showToken SourcePos -> t -> s -> SourcePos
nextpos forall a. Maybe a
Nothing t -> Maybe a
test
tokenPrimEx :: (Stream s m t)
=> (t -> String)
-> (SourcePos -> t -> s -> SourcePos)
-> Maybe (SourcePos -> t -> s -> u -> u)
-> (t -> Maybe a)
-> ParsecT s u m a
{-# INLINE tokenPrimEx #-}
tokenPrimEx :: forall s (m :: * -> *) t u a.
Stream s m t =>
(t -> String)
-> (SourcePos -> t -> s -> SourcePos)
-> Maybe (SourcePos -> t -> s -> u -> u)
-> (t -> Maybe a)
-> ParsecT s u m a
tokenPrimEx t -> String
showToken SourcePos -> t -> s -> SourcePos
nextpos Maybe (SourcePos -> t -> s -> u -> u)
Nothing t -> Maybe a
test
= forall s u (m :: * -> *) a.
(forall b.
State s u
-> (a -> State s u -> ParseError -> m b)
-> (ParseError -> m b)
-> (a -> State s u -> ParseError -> m b)
-> (ParseError -> m b)
-> m b)
-> ParsecT s u m a
ParsecT forall a b. (a -> b) -> a -> b
$ \(State s
input SourcePos
pos u
user) a -> State s u -> ParseError -> m b
cok ParseError -> m b
_cerr a -> State s u -> ParseError -> m b
_eok ParseError -> m b
eerr -> do
Maybe (t, s)
r <- forall s (m :: * -> *) t. Stream s m t => s -> m (Maybe (t, s))
uncons s
input
case Maybe (t, s)
r of
Maybe (t, s)
Nothing -> ParseError -> m b
eerr forall a b. (a -> b) -> a -> b
$ String -> SourcePos -> ParseError
unexpectError String
"" SourcePos
pos
Just (t
c,s
cs)
-> case t -> Maybe a
test t
c of
Just a
x -> let newpos :: SourcePos
newpos = SourcePos -> t -> s -> SourcePos
nextpos SourcePos
pos t
c s
cs
newstate :: State s u
newstate = forall s u. s -> SourcePos -> u -> State s u
State s
cs SourcePos
newpos u
user
in seq :: forall a b. a -> b -> b
seq SourcePos
newpos forall a b. (a -> b) -> a -> b
$ seq :: forall a b. a -> b -> b
seq State s u
newstate forall a b. (a -> b) -> a -> b
$
a -> State s u -> ParseError -> m b
cok a
x State s u
newstate (SourcePos -> ParseError
newErrorUnknown SourcePos
newpos)
Maybe a
Nothing -> ParseError -> m b
eerr forall a b. (a -> b) -> a -> b
$ String -> SourcePos -> ParseError
unexpectError (t -> String
showToken t
c) SourcePos
pos
tokenPrimEx t -> String
showToken SourcePos -> t -> s -> SourcePos
nextpos (Just SourcePos -> t -> s -> u -> u
nextState) t -> Maybe a
test
= forall s u (m :: * -> *) a.
(forall b.
State s u
-> (a -> State s u -> ParseError -> m b)
-> (ParseError -> m b)
-> (a -> State s u -> ParseError -> m b)
-> (ParseError -> m b)
-> m b)
-> ParsecT s u m a
ParsecT forall a b. (a -> b) -> a -> b
$ \(State s
input SourcePos
pos u
user) a -> State s u -> ParseError -> m b
cok ParseError -> m b
_cerr a -> State s u -> ParseError -> m b
_eok ParseError -> m b
eerr -> do
Maybe (t, s)
r <- forall s (m :: * -> *) t. Stream s m t => s -> m (Maybe (t, s))
uncons s
input
case Maybe (t, s)
r of
Maybe (t, s)
Nothing -> ParseError -> m b
eerr forall a b. (a -> b) -> a -> b
$ String -> SourcePos -> ParseError
unexpectError String
"" SourcePos
pos
Just (t
c,s
cs)
-> case t -> Maybe a
test t
c of
Just a
x -> let newpos :: SourcePos
newpos = SourcePos -> t -> s -> SourcePos
nextpos SourcePos
pos t
c s
cs
newUser :: u
newUser = SourcePos -> t -> s -> u -> u
nextState SourcePos
pos t
c s
cs u
user
newstate :: State s u
newstate = forall s u. s -> SourcePos -> u -> State s u
State s
cs SourcePos
newpos u
newUser
in seq :: forall a b. a -> b -> b
seq SourcePos
newpos forall a b. (a -> b) -> a -> b
$ seq :: forall a b. a -> b -> b
seq State s u
newstate forall a b. (a -> b) -> a -> b
$
a -> State s u -> ParseError -> m b
cok a
x State s u
newstate forall a b. (a -> b) -> a -> b
$ SourcePos -> ParseError
newErrorUnknown SourcePos
newpos
Maybe a
Nothing -> ParseError -> m b
eerr forall a b. (a -> b) -> a -> b
$ String -> SourcePos -> ParseError
unexpectError (t -> String
showToken t
c) SourcePos
pos
unexpectError :: String -> SourcePos -> ParseError
unexpectError :: String -> SourcePos -> ParseError
unexpectError String
msg SourcePos
pos = Message -> SourcePos -> ParseError
newErrorMessage (String -> Message
SysUnExpect String
msg) SourcePos
pos
many :: ParsecT s u m a -> ParsecT s u m [a]
many :: forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m [a]
many ParsecT s u m a
p
= do [a]
xs <- forall a s u (m :: * -> *).
(a -> [a] -> [a]) -> ParsecT s u m a -> ParsecT s u m [a]
manyAccum (:) ParsecT s u m a
p
forall (m :: * -> *) a. Monad m => a -> m a
return (forall a. [a] -> [a]
reverse [a]
xs)
skipMany :: ParsecT s u m a -> ParsecT s u m ()
skipMany :: forall s u (m :: * -> *) a. ParsecT s u m a -> ParsecT s u m ()
skipMany ParsecT s u m a
p
= do [a]
_ <- forall a s u (m :: * -> *).
(a -> [a] -> [a]) -> ParsecT s u m a -> ParsecT s u m [a]
manyAccum (\a
_ [a]
_ -> []) ParsecT s u m a
p
forall (m :: * -> *) a. Monad m => a -> m a
return ()
manyAccum :: (a -> [a] -> [a])
-> ParsecT s u m a
-> ParsecT s u m [a]
manyAccum :: forall a s u (m :: * -> *).
(a -> [a] -> [a]) -> ParsecT s u m a -> ParsecT s u m [a]
manyAccum a -> [a] -> [a]
acc ParsecT s u m a
p =
forall s u (m :: * -> *) a.
(forall b.
State s u
-> (a -> State s u -> ParseError -> m b)
-> (ParseError -> m b)
-> (a -> State s u -> ParseError -> m b)
-> (ParseError -> m b)
-> m b)
-> ParsecT s u m a
ParsecT forall a b. (a -> b) -> a -> b
$ \State s u
s [a] -> State s u -> ParseError -> m b
cok ParseError -> m b
cerr [a] -> State s u -> ParseError -> m b
eok ParseError -> m b
_eerr ->
let walk :: [a] -> a -> State s u -> ParseError -> m b
walk [a]
xs a
x State s u
s' ParseError
_err =
forall s u (m :: * -> *) a.
ParsecT s u m a
-> forall b.
State s u
-> (a -> State s u -> ParseError -> m b)
-> (ParseError -> m b)
-> (a -> State s u -> ParseError -> m b)
-> (ParseError -> m b)
-> m b
unParser ParsecT s u m a
p State s u
s'
(seq :: forall a b. a -> b -> b
seq [a]
xs forall a b. (a -> b) -> a -> b
$ [a] -> a -> State s u -> ParseError -> m b
walk forall a b. (a -> b) -> a -> b
$ a -> [a] -> [a]
acc a
x [a]
xs)
ParseError -> m b
cerr
forall a. a
manyErr
(\ParseError
e -> [a] -> State s u -> ParseError -> m b
cok (a -> [a] -> [a]
acc a
x [a]
xs) State s u
s' ParseError
e)
in forall s u (m :: * -> *) a.
ParsecT s u m a
-> forall b.
State s u
-> (a -> State s u -> ParseError -> m b)
-> (ParseError -> m b)
-> (a -> State s u -> ParseError -> m b)
-> (ParseError -> m b)
-> m b
unParser ParsecT s u m a
p State s u
s ([a] -> a -> State s u -> ParseError -> m b
walk []) ParseError -> m b
cerr forall a. a
manyErr (\ParseError
e -> [a] -> State s u -> ParseError -> m b
eok [] State s u
s ParseError
e)
manyErr :: a
manyErr :: forall a. a
manyErr = forall a. HasCallStack => String -> a
error String
"Text.ParserCombinators.Parsec.Prim.many: combinator 'many' is applied to a parser that accepts an empty string."
runPT :: (Stream s m t)
=> ParsecT s u m a -> u -> SourceName -> s -> m (Either ParseError a)
{-# INLINABLE runPT #-}
runPT :: forall s (m :: * -> *) t u a.
Stream s m t =>
ParsecT s u m a -> u -> String -> s -> m (Either ParseError a)
runPT ParsecT s u m a
p u
u String
name s
s
= do Consumed (m (Reply s u a))
res <- forall (m :: * -> *) s u a.
Monad m =>
ParsecT s u m a -> State s u -> m (Consumed (m (Reply s u a)))
runParsecT ParsecT s u m a
p (forall s u. s -> SourcePos -> u -> State s u
State s
s (String -> SourcePos
initialPos String
name) u
u)
Reply s u a
r <- forall {a}. Consumed a -> a
parserReply Consumed (m (Reply s u a))
res
case Reply s u a
r of
Ok a
x State s u
_ ParseError
_ -> forall (m :: * -> *) a. Monad m => a -> m a
return (forall a b. b -> Either a b
Right a
x)
Error ParseError
err -> forall (m :: * -> *) a. Monad m => a -> m a
return (forall a b. a -> Either a b
Left ParseError
err)
where
parserReply :: Consumed a -> a
parserReply Consumed a
res
= case Consumed a
res of
Consumed a
r -> a
r
Empty a
r -> a
r
runP :: (Stream s Identity t)
=> Parsec s u a -> u -> SourceName -> s -> Either ParseError a
runP :: forall s t u a.
Stream s Identity t =>
Parsec s u a -> u -> String -> s -> Either ParseError a
runP Parsec s u a
p u
u String
name s
s = forall a. Identity a -> a
runIdentity forall a b. (a -> b) -> a -> b
$ forall s (m :: * -> *) t u a.
Stream s m t =>
ParsecT s u m a -> u -> String -> s -> m (Either ParseError a)
runPT Parsec s u a
p u
u String
name s
s
runParserT :: (Stream s m t)
=> ParsecT s u m a -> u -> SourceName -> s -> m (Either ParseError a)
runParserT :: forall s (m :: * -> *) t u a.
Stream s m t =>
ParsecT s u m a -> u -> String -> s -> m (Either ParseError a)
runParserT = forall s (m :: * -> *) t u a.
Stream s m t =>
ParsecT s u m a -> u -> String -> s -> m (Either ParseError a)
runPT
runParser :: (Stream s Identity t)
=> Parsec s u a -> u -> SourceName -> s -> Either ParseError a
runParser :: forall s t u a.
Stream s Identity t =>
Parsec s u a -> u -> String -> s -> Either ParseError a
runParser = forall s t u a.
Stream s Identity t =>
Parsec s u a -> u -> String -> s -> Either ParseError a
runP
parse :: (Stream s Identity t)
=> Parsec s () a -> SourceName -> s -> Either ParseError a
parse :: forall s t a.
Stream s Identity t =>
Parsec s () a -> String -> s -> Either ParseError a
parse Parsec s () a
p = forall s t u a.
Stream s Identity t =>
Parsec s u a -> u -> String -> s -> Either ParseError a
runP Parsec s () a
p ()
parseTest :: (Stream s Identity t, Show a)
=> Parsec s () a -> s -> IO ()
parseTest :: forall s t a.
(Stream s Identity t, Show a) =>
Parsec s () a -> s -> IO ()
parseTest Parsec s () a
p s
input
= case forall s t a.
Stream s Identity t =>
Parsec s () a -> String -> s -> Either ParseError a
parse Parsec s () a
p String
"" s
input of
Left ParseError
err -> do String -> IO ()
putStr String
"parse error at "
forall a. Show a => a -> IO ()
print ParseError
err
Right a
x -> forall a. Show a => a -> IO ()
print a
x
getPosition :: (Monad m) => ParsecT s u m SourcePos
getPosition :: forall (m :: * -> *) s u. Monad m => ParsecT s u m SourcePos
getPosition = do State s u
state <- forall (m :: * -> *) s u. Monad m => ParsecT s u m (State s u)
getParserState
forall (m :: * -> *) a. Monad m => a -> m a
return (forall s u. State s u -> SourcePos
statePos State s u
state)
getInput :: (Monad m) => ParsecT s u m s
getInput :: forall (m :: * -> *) s u. Monad m => ParsecT s u m s
getInput = do State s u
state <- forall (m :: * -> *) s u. Monad m => ParsecT s u m (State s u)
getParserState
forall (m :: * -> *) a. Monad m => a -> m a
return (forall s u. State s u -> s
stateInput State s u
state)
setPosition :: (Monad m) => SourcePos -> ParsecT s u m ()
setPosition :: forall (m :: * -> *) s u. Monad m => SourcePos -> ParsecT s u m ()
setPosition SourcePos
pos
= do State s u
_ <- forall s u (m :: * -> *).
(State s u -> State s u) -> ParsecT s u m (State s u)
updateParserState (\(State s
input SourcePos
_ u
user) -> forall s u. s -> SourcePos -> u -> State s u
State s
input SourcePos
pos u
user)
forall (m :: * -> *) a. Monad m => a -> m a
return ()
setInput :: (Monad m) => s -> ParsecT s u m ()
setInput :: forall (m :: * -> *) s u. Monad m => s -> ParsecT s u m ()
setInput s
input
= do State s u
_ <- forall s u (m :: * -> *).
(State s u -> State s u) -> ParsecT s u m (State s u)
updateParserState (\(State s
_ SourcePos
pos u
user) -> forall s u. s -> SourcePos -> u -> State s u
State s
input SourcePos
pos u
user)
forall (m :: * -> *) a. Monad m => a -> m a
return ()
getParserState :: (Monad m) => ParsecT s u m (State s u)
getParserState :: forall (m :: * -> *) s u. Monad m => ParsecT s u m (State s u)
getParserState = forall s u (m :: * -> *).
(State s u -> State s u) -> ParsecT s u m (State s u)
updateParserState forall a. a -> a
id
setParserState :: (Monad m) => State s u -> ParsecT s u m (State s u)
setParserState :: forall (m :: * -> *) s u.
Monad m =>
State s u -> ParsecT s u m (State s u)
setParserState State s u
st = forall s u (m :: * -> *).
(State s u -> State s u) -> ParsecT s u m (State s u)
updateParserState (forall a b. a -> b -> a
const State s u
st)
updateParserState :: (State s u -> State s u) -> ParsecT s u m (State s u)
updateParserState :: forall s u (m :: * -> *).
(State s u -> State s u) -> ParsecT s u m (State s u)
updateParserState State s u -> State s u
f =
forall s u (m :: * -> *) a.
(forall b.
State s u
-> (a -> State s u -> ParseError -> m b)
-> (ParseError -> m b)
-> (a -> State s u -> ParseError -> m b)
-> (ParseError -> m b)
-> m b)
-> ParsecT s u m a
ParsecT forall a b. (a -> b) -> a -> b
$ \State s u
s State s u -> State s u -> ParseError -> m b
_ ParseError -> m b
_ State s u -> State s u -> ParseError -> m b
eok ParseError -> m b
_ ->
let s' :: State s u
s' = State s u -> State s u
f State s u
s
in State s u -> State s u -> ParseError -> m b
eok State s u
s' State s u
s' forall a b. (a -> b) -> a -> b
$ forall s u. State s u -> ParseError
unknownError State s u
s'
getState :: (Monad m) => ParsecT s u m u
getState :: forall (m :: * -> *) s u. Monad m => ParsecT s u m u
getState = forall s u. State s u -> u
stateUser forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
`liftM` forall (m :: * -> *) s u. Monad m => ParsecT s u m (State s u)
getParserState
putState :: (Monad m) => u -> ParsecT s u m ()
putState :: forall (m :: * -> *) u s. Monad m => u -> ParsecT s u m ()
putState u
u = do State s u
_ <- forall s u (m :: * -> *).
(State s u -> State s u) -> ParsecT s u m (State s u)
updateParserState forall a b. (a -> b) -> a -> b
$ \State s u
s -> State s u
s { stateUser :: u
stateUser = u
u }
forall (m :: * -> *) a. Monad m => a -> m a
return ()
modifyState :: (Monad m) => (u -> u) -> ParsecT s u m ()
modifyState :: forall (m :: * -> *) u s. Monad m => (u -> u) -> ParsecT s u m ()
modifyState u -> u
f = do State s u
_ <- forall s u (m :: * -> *).
(State s u -> State s u) -> ParsecT s u m (State s u)
updateParserState forall a b. (a -> b) -> a -> b
$ \State s u
s -> State s u
s { stateUser :: u
stateUser = u -> u
f (forall s u. State s u -> u
stateUser State s u
s) }
forall (m :: * -> *) a. Monad m => a -> m a
return ()
setState :: (Monad m) => u -> ParsecT s u m ()
setState :: forall (m :: * -> *) u s. Monad m => u -> ParsecT s u m ()
setState = forall (m :: * -> *) u s. Monad m => u -> ParsecT s u m ()
putState
updateState :: (Monad m) => (u -> u) -> ParsecT s u m ()
updateState :: forall (m :: * -> *) u s. Monad m => (u -> u) -> ParsecT s u m ()
updateState = forall (m :: * -> *) u s. Monad m => (u -> u) -> ParsecT s u m ()
modifyState