{-# LANGUAGE Trustworthy #-}
{-# LANGUAGE TypeFamilies #-}
module Text.Gigaparsec (
Parsec, Result(..), result, parse, parseRepl,
atomic, lookAhead, notFollowedBy, eof,
unit,
pure, empty,
($>),
(<$>), (<$), void,
(<~>), (<:>),
(<*>), liftA2, (*>), (<*), (<**>),
(<+>),
(<|>),
select, branch,
many, some, manyl, manyr, somel, somer,
) where
import Text.Gigaparsec.Internal (Parsec(Parsec), emptyState, manyr, somer)
import Text.Gigaparsec.Internal qualified as Internal (State(..), useHints, expectedErr)
import Text.Gigaparsec.Internal.RT qualified as Internal (RT, runRT)
import Text.Gigaparsec.Internal.Errors qualified as Internal (ParseError, ExpectItem(ExpectEndOfInput), fromParseError)
import Text.Gigaparsec.Errors.ErrorBuilder (ErrorBuilder)
import Data.Functor (void)
import Control.Applicative (liftA2, (<|>), empty, many, some, (<**>))
import Control.Selective (select, branch)
import Data.Set qualified as Set (singleton, empty)
type Result :: * -> * -> *
data Result e a = Success a | Failure e deriving stock (Int -> Result e a -> ShowS
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
forall e a. (Show a, Show e) => Int -> Result e a -> ShowS
forall e a. (Show a, Show e) => [Result e a] -> ShowS
forall e a. (Show a, Show e) => Result e a -> String
showList :: [Result e a] -> ShowS
$cshowList :: forall e a. (Show a, Show e) => [Result e a] -> ShowS
show :: Result e a -> String
$cshow :: forall e a. (Show a, Show e) => Result e a -> String
showsPrec :: Int -> Result e a -> ShowS
$cshowsPrec :: forall e a. (Show a, Show e) => Int -> Result e a -> ShowS
Show, Result e a -> Result e a -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
forall e a. (Eq a, Eq e) => Result e a -> Result e a -> Bool
/= :: Result e a -> Result e a -> Bool
$c/= :: forall e a. (Eq a, Eq e) => Result e a -> Result e a -> Bool
== :: Result e a -> Result e a -> Bool
$c== :: forall e a. (Eq a, Eq e) => Result e a -> Result e a -> Bool
Eq)
result :: (e -> b) -> (a -> b) -> Result e a -> b
result :: forall e b a. (e -> b) -> (a -> b) -> Result e a -> b
result e -> b
_ a -> b
success (Success a
x) = a -> b
success a
x
result e -> b
failure a -> b
_ (Failure e
err) = e -> b
failure e
err
{-# SPECIALISE parse :: Parsec a -> String -> Result String a #-}
{-# INLINABLE parse #-}
parse :: forall err a. ErrorBuilder err => Parsec a -> String -> Result err a
parse :: forall err a.
ErrorBuilder err =>
Parsec a -> String -> Result err a
parse (Parsec forall r.
State
-> (a -> State -> RT r) -> (ParseError -> State -> RT r) -> RT r
p) String
inp = forall a. RT a -> a
Internal.runRT forall a b. (a -> b) -> a -> b
$ forall r.
State
-> (a -> State -> RT r) -> (ParseError -> State -> RT r) -> RT r
p (String -> State
emptyState String
inp) a -> State -> RT (Result err a)
good ParseError -> State -> RT (Result err a)
bad
where good :: a -> Internal.State -> Internal.RT (Result err a)
good :: a -> State -> RT (Result err a)
good a
x State
_ = forall (m :: * -> *) a. Monad m => a -> m a
return (forall e a. a -> Result e a
Success a
x)
bad :: Internal.ParseError -> Internal.State -> Internal.RT (Result err a)
bad :: ParseError -> State -> RT (Result err a)
bad ParseError
err State
_ = forall (m :: * -> *) a. Monad m => a -> m a
return (forall e a. e -> Result e a
Failure (forall err.
ErrorBuilder err =>
Maybe String -> String -> ParseError -> err
Internal.fromParseError forall a. Maybe a
Nothing String
inp ParseError
err))
parseRepl :: Show a => Parsec a -> String -> IO ()
parseRepl :: forall a. Show a => Parsec a -> String -> IO ()
parseRepl Parsec a
p String
inp = forall e b a. (e -> b) -> (a -> b) -> Result e a -> b
result String -> IO ()
putStrLn forall a. Show a => a -> IO ()
print (forall err a.
ErrorBuilder err =>
Parsec a -> String -> Result err a
parse Parsec a
p String
inp)
atomic :: Parsec a
-> Parsec a
atomic :: forall a. Parsec a -> Parsec a
atomic (Parsec forall r.
State
-> (a -> State -> RT r) -> (ParseError -> State -> RT r) -> RT r
p) = forall a.
(forall r.
State
-> (a -> State -> RT r) -> (ParseError -> State -> RT r) -> RT r)
-> Parsec a
Parsec forall a b. (a -> b) -> a -> b
$ \State
st a -> State -> RT r
ok ParseError -> State -> RT r
bad -> forall r.
State
-> (a -> State -> RT r) -> (ParseError -> State -> RT r) -> RT r
p State
st a -> State -> RT r
ok (\ParseError
err State
_ -> ParseError -> State -> RT r
bad ParseError
err State
st)
lookAhead :: Parsec a
-> Parsec a
lookAhead :: forall a. Parsec a -> Parsec a
lookAhead (Parsec forall r.
State
-> (a -> State -> RT r) -> (ParseError -> State -> RT r) -> RT r
p) = forall a.
(forall r.
State
-> (a -> State -> RT r) -> (ParseError -> State -> RT r) -> RT r)
-> Parsec a
Parsec forall a b. (a -> b) -> a -> b
$ \State
st a -> State -> RT r
ok ParseError -> State -> RT r
err -> forall r.
State
-> (a -> State -> RT r) -> (ParseError -> State -> RT r) -> RT r
p State
st (\a
x State
_ -> a -> State -> RT r
ok a
x State
st) ParseError -> State -> RT r
err
notFollowedBy :: Parsec a
-> Parsec ()
notFollowedBy :: forall a. Parsec a -> Parsec ()
notFollowedBy (Parsec forall r.
State
-> (a -> State -> RT r) -> (ParseError -> State -> RT r) -> RT r
p) = forall a.
(forall r.
State
-> (a -> State -> RT r) -> (ParseError -> State -> RT r) -> RT r)
-> Parsec a
Parsec forall a b. (a -> b) -> a -> b
$ \State
st () -> State -> RT r
ok ParseError -> State -> RT r
bad ->
forall r.
State
-> (a -> State -> RT r) -> (ParseError -> State -> RT r) -> RT r
p State
st (\a
_ State
st' -> let !width :: Word
width = State -> Word
Internal.consumed State
st' forall a. Num a => a -> a -> a
- State -> Word
Internal.consumed State
st
in forall r.
(ParseError -> State -> RT r) -> ParseError -> State -> RT r
Internal.useHints ParseError -> State -> RT r
bad (State -> Set ExpectItem -> Word -> ParseError
Internal.expectedErr State
st forall a. Set a
Set.empty Word
width) State
st)
(\ParseError
_ State
_ -> () -> State -> RT r
ok () State
st)
eof :: Parsec ()
eof :: Parsec ()
eof = forall a.
(forall r.
State
-> (a -> State -> RT r) -> (ParseError -> State -> RT r) -> RT r)
-> Parsec a
Parsec forall a b. (a -> b) -> a -> b
$ \State
st () -> State -> RT r
good ParseError -> State -> RT r
bad -> case State -> String
Internal.input State
st of
(:){} -> forall r.
(ParseError -> State -> RT r) -> ParseError -> State -> RT r
Internal.useHints ParseError -> State -> RT r
bad
(State -> Set ExpectItem -> Word -> ParseError
Internal.expectedErr State
st (forall a. a -> Set a
Set.singleton ExpectItem
Internal.ExpectEndOfInput) Word
1) State
st
[] -> () -> State -> RT r
good () State
st
unit :: Parsec ()
unit :: Parsec ()
unit = forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
infixl 4 <~>
(<~>) :: Parsec a -> Parsec b -> Parsec (a, b)
<~> :: forall a b. Parsec a -> Parsec b -> Parsec (a, b)
(<~>) = forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 (,)
infixl 4 $>
($>) :: Parsec a -> b -> Parsec b
$> :: forall a b. Parsec a -> b -> Parsec b
($>) = forall a b c. (a -> b -> c) -> b -> a -> c
flip forall (f :: * -> *) a b. Functor f => a -> f b -> f a
(<$)
infixl 4 <:>
(<:>) :: Parsec a -> Parsec [a] -> Parsec [a]
<:> :: forall a. Parsec a -> Parsec [a] -> Parsec [a]
(<:>) = forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 (:)
infixl 3 <+>
(<+>) :: Parsec a -> Parsec b -> Parsec (Either a b)
Parsec a
p <+> :: forall a b. Parsec a -> Parsec b -> Parsec (Either a b)
<+> Parsec b
q = forall a b. a -> Either a b
Left forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parsec a
p forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> forall a b. b -> Either a b
Right forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parsec b
q
manyl :: (b -> a -> b) -> b -> Parsec a -> Parsec b
manyl :: forall b a. (b -> a -> b) -> b -> Parsec a -> Parsec b
manyl b -> a -> b
f b
k = forall b a. (b -> a -> b) -> Parsec b -> Parsec a -> Parsec b
_repl b -> a -> b
f (forall (f :: * -> *) a. Applicative f => a -> f a
pure b
k)
somel :: (b -> a -> b) -> b -> Parsec a -> Parsec b
somel :: forall b a. (b -> a -> b) -> b -> Parsec a -> Parsec b
somel b -> a -> b
f b
k Parsec a
p = forall b a. (b -> a -> b) -> Parsec b -> Parsec a -> Parsec b
_repl b -> a -> b
f (b -> a -> b
f b
k forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parsec a
p) Parsec a
p
_repl :: (b -> a -> b) -> Parsec b -> Parsec a -> Parsec b
_repl :: forall b a. (b -> a -> b) -> Parsec b -> Parsec a -> Parsec b
_repl b -> a -> b
f Parsec b
k Parsec a
p = Parsec b
k forall (f :: * -> *) a b. Applicative f => f a -> f (a -> b) -> f b
<**> forall a b. (a -> b -> b) -> b -> Parsec a -> Parsec b
manyr (\a
x b -> b
next !b
acc -> b -> b
next (b -> a -> b
f b
acc a
x)) forall a. a -> a
id Parsec a
p