{-# LANGUAGE Trustworthy #-}
{-# LANGUAGE DeriveFunctor, StandaloneDeriving, NamedFieldPuns, CPP #-}
#include "portable-unlifted.h"
{-# OPTIONS_HADDOCK hide #-}
module Text.Gigaparsec.Internal (module Text.Gigaparsec.Internal) where
import Control.Monad.RT (RT)
import Text.Gigaparsec.Internal.Errors (Error, Hints, ExpectItem, CaretWidth)
import Text.Gigaparsec.Internal.Errors qualified as Errors (
emptyErr, expectedErr, specialisedErr, mergeErr, unexpectedErr,
isExpectedEmpty, presentationOffset, useHints, DefuncHints(Blank), addError
)
import Control.Applicative (Applicative(liftA2), Alternative(empty, (<|>), many, some))
import Control.Selective (Selective(select))
import Data.Set (Set)
CPP_import_PortableUnlifted
type Parsec :: * -> *
newtype Parsec a = Parsec {
forall a.
Parsec a
-> forall r.
State -> (a -> State -> RT r) -> (Error -> State -> RT r) -> RT r
unParsec :: forall r. State
-> (a -> State -> RT r)
-> (Error -> State -> RT r)
-> RT r
}
deriving stock instance Functor Parsec
instance Applicative Parsec where
pure :: a -> Parsec a
pure :: forall a. a -> Parsec a
pure a
x = (forall r.
State -> (a -> State -> RT r) -> (Error -> State -> RT r) -> RT r)
-> Parsec a
forall a.
(forall r.
State -> (a -> State -> RT r) -> (Error -> State -> RT r) -> RT r)
-> Parsec a
Parsec ((forall r.
State -> (a -> State -> RT r) -> (Error -> State -> RT r) -> RT r)
-> Parsec a)
-> (forall r.
State -> (a -> State -> RT r) -> (Error -> State -> RT r) -> RT r)
-> Parsec a
forall a b. (a -> b) -> a -> b
$ \State
st a -> State -> RT r
ok Error -> State -> RT r
_ -> a -> State -> RT r
ok a
x State
st
liftA2 :: (a -> b -> c) -> Parsec a -> Parsec b -> Parsec c
liftA2 :: forall a b c. (a -> b -> c) -> Parsec a -> Parsec b -> Parsec c
liftA2 a -> b -> c
f (Parsec forall r.
State -> (a -> State -> RT r) -> (Error -> State -> RT r) -> RT r
p) (Parsec forall r.
State -> (b -> State -> RT r) -> (Error -> State -> RT r) -> RT r
q) = (forall r.
State -> (c -> State -> RT r) -> (Error -> State -> RT r) -> RT r)
-> Parsec c
forall a.
(forall r.
State -> (a -> State -> RT r) -> (Error -> State -> RT r) -> RT r)
-> Parsec a
Parsec ((forall r.
State -> (c -> State -> RT r) -> (Error -> State -> RT r) -> RT r)
-> Parsec c)
-> (forall r.
State -> (c -> State -> RT r) -> (Error -> State -> RT r) -> RT r)
-> Parsec c
forall a b. (a -> b) -> a -> b
$ \State
st c -> State -> RT r
ok Error -> State -> RT r
err ->
let ok' :: a -> State -> RT r
ok' a
x State
st' = State -> (b -> State -> RT r) -> (Error -> State -> RT r) -> RT r
forall r.
State -> (b -> State -> RT r) -> (Error -> State -> RT r) -> RT r
q State
st' (c -> State -> RT r
ok (c -> State -> RT r) -> (b -> c) -> b -> State -> RT r
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> b -> c
f a
x) Error -> State -> RT r
err
in State -> (a -> State -> RT r) -> (Error -> State -> RT r) -> RT r
forall r.
State -> (a -> State -> RT r) -> (Error -> State -> RT r) -> RT r
p State
st a -> State -> RT r
ok' Error -> State -> RT r
err
(*>) :: Parsec a -> Parsec b -> Parsec b
*> :: forall a b. Parsec a -> Parsec b -> Parsec b
(*>) = (a -> b -> b) -> Parsec a -> Parsec b -> Parsec b
forall a b c. (a -> b -> c) -> Parsec a -> Parsec b -> Parsec c
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 ((b -> b) -> a -> b -> b
forall a b. a -> b -> a
const b -> b
forall a. a -> a
id)
(<*) :: Parsec a -> Parsec b -> Parsec a
<* :: forall a b. Parsec a -> Parsec b -> Parsec a
(<*) = (a -> b -> a) -> Parsec a -> Parsec b -> Parsec a
forall a b c. (a -> b -> c) -> Parsec a -> Parsec b -> Parsec c
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 a -> b -> a
forall a b. a -> b -> a
const
{-# INLINE pure #-}
{-# INLINE liftA2 #-}
{-# INLINE (<*) #-}
{-# INLINE (*>) #-}
instance Selective Parsec where
select :: Parsec (Either a b) -> Parsec (a -> b) -> Parsec b
select :: forall a b. Parsec (Either a b) -> Parsec (a -> b) -> Parsec b
select Parsec (Either a b)
p Parsec (a -> b)
q = Parsec (Either a b)
-> Parsec (a -> b) -> Parsec (b -> b) -> Parsec b
forall a b c.
Parsec (Either a b)
-> Parsec (a -> c) -> Parsec (b -> c) -> Parsec c
_branch Parsec (Either a b)
p Parsec (a -> b)
q ((b -> b) -> Parsec (b -> b)
forall a. a -> Parsec a
forall (f :: * -> *) a. Applicative f => a -> f a
pure b -> b
forall a. a -> a
id)
{-# INLINE select #-}
{-# INLINE _branch #-}
_branch :: Parsec (Either a b) -> Parsec (a -> c) -> Parsec (b -> c) -> Parsec c
_branch :: forall a b c.
Parsec (Either a b)
-> Parsec (a -> c) -> Parsec (b -> c) -> Parsec c
_branch (Parsec forall r.
State
-> (Either a b -> State -> RT r)
-> (Error -> State -> RT r)
-> RT r
p) (Parsec forall r.
State
-> ((a -> c) -> State -> RT r) -> (Error -> State -> RT r) -> RT r
q1) (Parsec forall r.
State
-> ((b -> c) -> State -> RT r) -> (Error -> State -> RT r) -> RT r
q2) = (forall r.
State -> (c -> State -> RT r) -> (Error -> State -> RT r) -> RT r)
-> Parsec c
forall a.
(forall r.
State -> (a -> State -> RT r) -> (Error -> State -> RT r) -> RT r)
-> Parsec a
Parsec ((forall r.
State -> (c -> State -> RT r) -> (Error -> State -> RT r) -> RT r)
-> Parsec c)
-> (forall r.
State -> (c -> State -> RT r) -> (Error -> State -> RT r) -> RT r)
-> Parsec c
forall a b. (a -> b) -> a -> b
$ \State
st c -> State -> RT r
ok Error -> State -> RT r
err ->
let ok' :: Either a b -> State -> RT r
ok' Either a b
x State
st' = case Either a b
x of
Left a
a -> State
-> ((a -> c) -> State -> RT r) -> (Error -> State -> RT r) -> RT r
forall r.
State
-> ((a -> c) -> State -> RT r) -> (Error -> State -> RT r) -> RT r
q1 State
st' (c -> State -> RT r
ok (c -> State -> RT r)
-> ((a -> c) -> c) -> (a -> c) -> State -> RT r
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((a -> c) -> a -> c
forall a b. (a -> b) -> a -> b
$ a
a)) Error -> State -> RT r
err
Right b
b -> State
-> ((b -> c) -> State -> RT r) -> (Error -> State -> RT r) -> RT r
forall r.
State
-> ((b -> c) -> State -> RT r) -> (Error -> State -> RT r) -> RT r
q2 State
st' (c -> State -> RT r
ok (c -> State -> RT r)
-> ((b -> c) -> c) -> (b -> c) -> State -> RT r
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((b -> c) -> b -> c
forall a b. (a -> b) -> a -> b
$ b
b)) Error -> State -> RT r
err
in State
-> (Either a b -> State -> RT r)
-> (Error -> State -> RT r)
-> RT r
forall r.
State
-> (Either a b -> State -> RT r)
-> (Error -> State -> RT r)
-> RT r
p State
st Either a b -> State -> RT r
ok' Error -> State -> RT r
err
instance Monad Parsec where
return :: a -> Parsec a
return :: forall a. a -> Parsec a
return = a -> Parsec a
forall a. a -> Parsec a
forall (f :: * -> *) a. Applicative f => a -> f a
pure
(>>=) :: Parsec a -> (a -> Parsec b) -> Parsec b
Parsec forall r.
State -> (a -> State -> RT r) -> (Error -> State -> RT r) -> RT r
p >>= :: forall a b. Parsec a -> (a -> Parsec b) -> Parsec b
>>= a -> Parsec b
f = (forall r.
State -> (b -> State -> RT r) -> (Error -> State -> RT r) -> RT r)
-> Parsec b
forall a.
(forall r.
State -> (a -> State -> RT r) -> (Error -> State -> RT r) -> RT r)
-> Parsec a
Parsec ((forall r.
State -> (b -> State -> RT r) -> (Error -> State -> RT r) -> RT r)
-> Parsec b)
-> (forall r.
State -> (b -> State -> RT r) -> (Error -> State -> RT r) -> RT r)
-> Parsec b
forall a b. (a -> b) -> a -> b
$ \State
st b -> State -> RT r
ok Error -> State -> RT r
err ->
let ok' :: a -> State -> RT r
ok' a
x State
st' = Parsec b
-> forall r.
State -> (b -> State -> RT r) -> (Error -> State -> RT r) -> RT r
forall a.
Parsec a
-> forall r.
State -> (a -> State -> RT r) -> (Error -> State -> RT r) -> RT r
unParsec (a -> Parsec b
f a
x) State
st' b -> State -> RT r
ok Error -> State -> RT r
err
in State -> (a -> State -> RT r) -> (Error -> State -> RT r) -> RT r
forall r.
State -> (a -> State -> RT r) -> (Error -> State -> RT r) -> RT r
p State
st a -> State -> RT r
ok' Error -> State -> RT r
err
(>>) :: Parsec a -> Parsec b -> Parsec b
>> :: forall a b. Parsec a -> Parsec b -> Parsec b
(>>) = Parsec a -> Parsec b -> Parsec b
forall a b. Parsec a -> Parsec b -> Parsec b
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
(*>)
{-# INLINE return #-}
{-# INLINE (>>=) #-}
raise :: (State -> Error) -> Parsec a
raise :: forall a. (State -> Error) -> Parsec a
raise State -> Error
mkErr = (forall r.
State -> (a -> State -> RT r) -> (Error -> State -> RT r) -> RT r)
-> Parsec a
forall a.
(forall r.
State -> (a -> State -> RT r) -> (Error -> State -> RT r) -> RT r)
-> Parsec a
Parsec ((forall r.
State -> (a -> State -> RT r) -> (Error -> State -> RT r) -> RT r)
-> Parsec a)
-> (forall r.
State -> (a -> State -> RT r) -> (Error -> State -> RT r) -> RT r)
-> Parsec a
forall a b. (a -> b) -> a -> b
$ \State
st a -> State -> RT r
_ Error -> State -> RT r
bad -> (Error -> State -> RT r) -> Error -> State -> RT r
forall r. (Error -> State -> RT r) -> Error -> State -> RT r
useHints Error -> State -> RT r
bad (State -> Error
mkErr State
st) State
st
instance Alternative Parsec where
empty :: Parsec a
empty :: forall a. Parsec a
empty = (State -> Error) -> Parsec a
forall a. (State -> Error) -> Parsec a
raise (State -> Word -> Error
`emptyErr` Word
0)
(<|>) :: Parsec a -> Parsec a -> Parsec a
Parsec forall r.
State -> (a -> State -> RT r) -> (Error -> State -> RT r) -> RT r
p <|> :: forall a. Parsec a -> Parsec a -> Parsec a
<|> Parsec forall r.
State -> (a -> State -> RT r) -> (Error -> State -> RT r) -> RT r
q = (forall r.
State -> (a -> State -> RT r) -> (Error -> State -> RT r) -> RT r)
-> Parsec a
forall a.
(forall r.
State -> (a -> State -> RT r) -> (Error -> State -> RT r) -> RT r)
-> Parsec a
Parsec ((forall r.
State -> (a -> State -> RT r) -> (Error -> State -> RT r) -> RT r)
-> Parsec a)
-> (forall r.
State -> (a -> State -> RT r) -> (Error -> State -> RT r) -> RT r)
-> Parsec a
forall a b. (a -> b) -> a -> b
$ \State
st a -> State -> RT r
ok Error -> State -> RT r
bad ->
let bad' :: Error -> State -> RT r
bad' Error
err State
st'
| State -> Word
consumed State
st' Word -> Word -> Bool
forall a. Ord a => a -> a -> Bool
> State -> Word
consumed State
st = Error -> State -> RT r
bad Error
err State
st'
| Bool
otherwise = State -> (a -> State -> RT r) -> (Error -> State -> RT r) -> RT r
forall r.
State -> (a -> State -> RT r) -> (Error -> State -> RT r) -> RT r
q State
st' (\a
x State
st'' -> a -> State -> RT r
ok a
x (State -> Error -> State
errorToHints State
st'' Error
err))
(\Error
err' -> Error -> State -> RT r
bad (Error -> Error -> Error
Errors.mergeErr Error
err Error
err'))
in State -> (a -> State -> RT r) -> (Error -> State -> RT r) -> RT r
forall r.
State -> (a -> State -> RT r) -> (Error -> State -> RT r) -> RT r
p State
st a -> State -> RT r
ok Error -> State -> RT r
bad'
many :: Parsec a -> Parsec [a]
many :: forall a. Parsec a -> Parsec [a]
many = (a -> [a] -> [a]) -> [a] -> Parsec a -> Parsec [a]
forall a b. (a -> b -> b) -> b -> Parsec a -> Parsec b
manyr (:) []
some :: Parsec a -> Parsec [a]
some :: forall a. Parsec a -> Parsec [a]
some = (a -> [a] -> [a]) -> [a] -> Parsec a -> Parsec [a]
forall a b. (a -> b -> b) -> b -> Parsec a -> Parsec b
somer (:) []
{-# INLINE empty #-}
{-# INLINE (<|>) #-}
{-# INLINE many #-}
{-# INLINE some #-}
{-# INLINE manyr #-}
manyr :: (a -> b -> b)
-> b
-> Parsec a
-> Parsec b
manyr :: forall a b. (a -> b -> b) -> b -> Parsec a -> Parsec b
manyr a -> b -> b
f b
k Parsec a
p = let go :: Parsec b
go = (a -> b -> b) -> Parsec a -> Parsec b -> Parsec b
forall a b c. (a -> b -> c) -> Parsec a -> Parsec b -> Parsec c
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 a -> b -> b
f Parsec a
p Parsec b
go Parsec b -> Parsec b -> Parsec b
forall a. Parsec a -> Parsec a -> Parsec a
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> b -> Parsec b
forall a. a -> Parsec a
forall (f :: * -> *) a. Applicative f => a -> f a
pure b
k in Parsec b
go
{-# INLINE somer #-}
somer :: (a -> b -> b)
-> b
-> Parsec a
-> Parsec b
somer :: forall a b. (a -> b -> b) -> b -> Parsec a -> Parsec b
somer a -> b -> b
f b
k Parsec a
p = (a -> b -> b) -> Parsec a -> Parsec b -> Parsec b
forall a b c. (a -> b -> c) -> Parsec a -> Parsec b -> Parsec c
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 a -> b -> b
f Parsec a
p ((a -> b -> b) -> b -> Parsec a -> Parsec b
forall a b. (a -> b -> b) -> b -> Parsec a -> Parsec b
manyr a -> b -> b
f b
k Parsec a
p)
instance Semigroup m => Semigroup (Parsec m) where
(<>) :: Parsec m -> Parsec m -> Parsec m
<> :: Parsec m -> Parsec m -> Parsec m
(<>) = (m -> m -> m) -> Parsec m -> Parsec m -> Parsec m
forall a b c. (a -> b -> c) -> Parsec a -> Parsec b -> Parsec c
forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 m -> m -> m
forall a. Semigroup a => a -> a -> a
(<>)
{-# INLINE (<>) #-}
instance Monoid m => Monoid (Parsec m) where
mempty :: Parsec m
mempty :: Parsec m
mempty = m -> Parsec m
forall a. a -> Parsec a
forall (f :: * -> *) a. Applicative f => a -> f a
pure m
forall a. Monoid a => a
mempty
{-# INLINE mempty #-}
type State :: UnliftedDatatype
data State = State {
State -> String
input :: !String,
State -> Word
consumed :: {-# UNPACK #-} !Word,
State -> Word
line :: {-# UNPACK #-} !Word,
State -> Word
col :: {-# UNPACK #-} !Word,
State -> Word
hintsValidOffset :: {-# UNPACK #-} !Word,
State -> Hints
hints :: Hints,
State -> Int
debugLevel :: {-# UNPACK #-} !Int
}
emptyState :: String -> State
emptyState :: String -> State
emptyState !String
str = State { input :: String
input = String
str
, consumed :: Word
consumed = Word
0
, line :: Word
line = Word
1
, col :: Word
col = Word
1
, hintsValidOffset :: Word
hintsValidOffset = Word
0
, hints :: Hints
hints = Hints
Errors.Blank
, debugLevel :: Int
debugLevel = Int
0
}
emptyErr :: State -> Word -> Error
emptyErr :: State -> Word -> Error
emptyErr State{Int
String
Word
Hints
consumed :: State -> Word
input :: State -> String
line :: State -> Word
col :: State -> Word
hintsValidOffset :: State -> Word
hints :: State -> Hints
debugLevel :: State -> Int
input :: String
consumed :: Word
line :: Word
col :: Word
hintsValidOffset :: Word
hints :: Hints
debugLevel :: Int
..} = Word -> Word -> Word -> Word -> Error
Errors.emptyErr Word
consumed Word
line Word
col
expectedErr :: State -> Set ExpectItem -> Word -> Error
expectedErr :: State -> Set ExpectItem -> Word -> Error
expectedErr State{Int
String
Word
Hints
consumed :: State -> Word
input :: State -> String
line :: State -> Word
col :: State -> Word
hintsValidOffset :: State -> Word
hints :: State -> Hints
debugLevel :: State -> Int
input :: String
consumed :: Word
line :: Word
col :: Word
hintsValidOffset :: Word
hints :: Hints
debugLevel :: Int
..} = String -> Word -> Word -> Word -> Set ExpectItem -> Word -> Error
Errors.expectedErr String
input Word
consumed Word
line Word
col
specialisedErr :: State -> [String] -> CaretWidth -> Error
specialisedErr :: State -> [String] -> CaretWidth -> Error
specialisedErr State{Int
String
Word
Hints
consumed :: State -> Word
input :: State -> String
line :: State -> Word
col :: State -> Word
hintsValidOffset :: State -> Word
hints :: State -> Hints
debugLevel :: State -> Int
input :: String
consumed :: Word
line :: Word
col :: Word
hintsValidOffset :: Word
hints :: Hints
debugLevel :: Int
..} = Word -> Word -> Word -> [String] -> CaretWidth -> Error
Errors.specialisedErr Word
consumed Word
line Word
col
unexpectedErr :: State -> Set ExpectItem -> String -> CaretWidth -> Error
unexpectedErr :: State -> Set ExpectItem -> String -> CaretWidth -> Error
unexpectedErr State{Int
String
Word
Hints
consumed :: State -> Word
input :: State -> String
line :: State -> Word
col :: State -> Word
hintsValidOffset :: State -> Word
hints :: State -> Hints
debugLevel :: State -> Int
input :: String
consumed :: Word
line :: Word
col :: Word
hintsValidOffset :: Word
hints :: Hints
debugLevel :: Int
..} = Word
-> Word -> Word -> Set ExpectItem -> String -> CaretWidth -> Error
Errors.unexpectedErr Word
consumed Word
line Word
col
errorToHints :: State -> Error -> State
errorToHints :: State -> Error -> State
errorToHints st :: State
st@State{Int
String
Word
Hints
consumed :: State -> Word
input :: State -> String
line :: State -> Word
col :: State -> Word
hintsValidOffset :: State -> Word
hints :: State -> Hints
debugLevel :: State -> Int
input :: String
consumed :: Word
line :: Word
col :: Word
hintsValidOffset :: Word
hints :: Hints
debugLevel :: Int
..} Error
err
| Word
consumed Word -> Word -> Bool
forall a. Eq a => a -> a -> Bool
== Error -> Word
Errors.presentationOffset Error
err
, Bool -> Bool
not (Error -> Bool
Errors.isExpectedEmpty Error
err) =
if Word
hintsValidOffset Word -> Word -> Bool
forall a. Ord a => a -> a -> Bool
< Word
consumed then State
st { hints = Errors.addError (Errors.Blank) err, hintsValidOffset = consumed }
else State
st { hints = Errors.addError hints err }
errorToHints State
st Error
_ = State
st
useHints :: (Error -> State -> RT r) -> (Error -> State -> RT r)
useHints :: forall r. (Error -> State -> RT r) -> Error -> State -> RT r
useHints Error -> State -> RT r
bad Error
err st :: State
st@State{Word
hintsValidOffset :: State -> Word
hintsValidOffset :: Word
hintsValidOffset, Hints
hints :: State -> Hints
hints :: Hints
hints}
| Word
presentationOffset Word -> Word -> Bool
forall a. Eq a => a -> a -> Bool
== Word
hintsValidOffset = Error -> State -> RT r
bad (Hints -> Error -> Error
Errors.useHints Hints
hints Error
err) State
st
| Bool
otherwise = Error -> State -> RT r
bad Error
err State
st{ hintsValidOffset = presentationOffset, hints = Errors.Blank }
where !presentationOffset :: Word
presentationOffset = Error -> Word
Errors.presentationOffset Error
err
adjustErr :: (Error -> Error) -> Parsec a -> Parsec a
adjustErr :: forall a. (Error -> Error) -> Parsec a -> Parsec a
adjustErr Error -> Error
f (Parsec forall r.
State -> (a -> State -> RT r) -> (Error -> State -> RT r) -> RT r
p) = (forall r.
State -> (a -> State -> RT r) -> (Error -> State -> RT r) -> RT r)
-> Parsec a
forall a.
(forall r.
State -> (a -> State -> RT r) -> (Error -> State -> RT r) -> RT r)
-> Parsec a
Parsec ((forall r.
State -> (a -> State -> RT r) -> (Error -> State -> RT r) -> RT r)
-> Parsec a)
-> (forall r.
State -> (a -> State -> RT r) -> (Error -> State -> RT r) -> RT r)
-> Parsec a
forall a b. (a -> b) -> a -> b
$ \State
st a -> State -> RT r
good Error -> State -> RT r
bad -> State -> (a -> State -> RT r) -> (Error -> State -> RT r) -> RT r
forall r.
State -> (a -> State -> RT r) -> (Error -> State -> RT r) -> RT r
p State
st a -> State -> RT r
good ((Error -> State -> RT r) -> RT r)
-> (Error -> State -> RT r) -> RT r
forall a b. (a -> b) -> a -> b
$ \Error
err -> Error -> State -> RT r
bad (Error -> Error
f Error
err)