{-# language BangPatterns #-}
{-# language CPP #-}
{-# language DeriveFoldable #-}
{-# language DeriveFunctor #-}
{-# language DeriveTraversable #-}
{-# language FlexibleContexts #-}
{-# language FlexibleInstances #-}
{-# language FunctionalDependencies #-}
{-# language MultiParamTypeClasses #-}
{-# language Rank2Types #-}
{-# language TemplateHaskell #-}
module Text.Trifecta.Parser
( Parser(..)
, manyAccum
, Step(..)
, feed
, starve
, stepParser
, stepResult
, stepIt
, runParser
, parseFromFile
, parseFromFileEx
, parseString
, parseByteString
, parseTest
) where
import Control.Applicative as Alternative
import Control.Monad (MonadPlus(..), ap, join)
import Control.Monad.IO.Class
import qualified Control.Monad.Fail as Fail
import Data.ByteString as Strict hiding (empty, snoc)
import Data.ByteString.UTF8 as UTF8
import Data.Maybe (fromMaybe, isJust)
#if !(MIN_VERSION_base(4,11,0))
import Data.Semigroup
#endif
import Data.Semigroup.Reducer
import Data.Set as Set hiding (empty, toList)
import Prettyprinter as Pretty hiding (line)
import System.IO
import Text.Parser.Combinators
import Text.Parser.Char
import Text.Parser.LookAhead
import Text.Parser.Token
import Text.Trifecta.Combinators
import Text.Trifecta.Delta as Delta
import Text.Trifecta.Rendering
import Text.Trifecta.Result
import Text.Trifecta.Rope
import Text.Trifecta.Util.It
import Text.Trifecta.Util.Pretty
newtype Parser a = Parser
{ forall a.
Parser a
-> forall r.
(a -> Err -> It Rope r)
-> (Err -> It Rope r)
-> (a -> Set String -> Delta -> ByteString -> It Rope r)
-> (ErrInfo -> It Rope r)
-> Delta
-> ByteString
-> It Rope r
unparser :: forall r.
(a -> Err -> It Rope r)
-> (Err -> It Rope r)
-> (a -> Set String -> Delta -> ByteString -> It Rope r)
-> (ErrInfo -> It Rope r)
-> Delta
-> ByteString
-> It Rope r
}
instance Functor Parser where
fmap :: forall a b. (a -> b) -> Parser a -> Parser b
fmap a -> b
f (Parser forall r.
(a -> Err -> It Rope r)
-> (Err -> It Rope r)
-> (a -> Set String -> Delta -> ByteString -> It Rope r)
-> (ErrInfo -> It Rope r)
-> Delta
-> ByteString
-> It Rope r
m) = forall a.
(forall r.
(a -> Err -> It Rope r)
-> (Err -> It Rope r)
-> (a -> Set String -> Delta -> ByteString -> It Rope r)
-> (ErrInfo -> It Rope r)
-> Delta
-> ByteString
-> It Rope r)
-> Parser a
Parser forall a b. (a -> b) -> a -> b
$ \ b -> Err -> It Rope r
eo Err -> It Rope r
ee b -> Set String -> Delta -> ByteString -> It Rope r
co -> forall r.
(a -> Err -> It Rope r)
-> (Err -> It Rope r)
-> (a -> Set String -> Delta -> ByteString -> It Rope r)
-> (ErrInfo -> It Rope r)
-> Delta
-> ByteString
-> It Rope r
m (b -> Err -> It Rope r
eo forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> b
f) Err -> It Rope r
ee (b -> Set String -> Delta -> ByteString -> It Rope r
co forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> b
f)
{-# inlinable fmap #-}
a
a <$ :: forall a b. a -> Parser b -> Parser a
<$ Parser forall r.
(b -> Err -> It Rope r)
-> (Err -> It Rope r)
-> (b -> Set String -> Delta -> ByteString -> It Rope r)
-> (ErrInfo -> It Rope r)
-> Delta
-> ByteString
-> It Rope r
m = forall a.
(forall r.
(a -> Err -> It Rope r)
-> (Err -> It Rope r)
-> (a -> Set String -> Delta -> ByteString -> It Rope r)
-> (ErrInfo -> It Rope r)
-> Delta
-> ByteString
-> It Rope r)
-> Parser a
Parser forall a b. (a -> b) -> a -> b
$ \ a -> Err -> It Rope r
eo Err -> It Rope r
ee a -> Set String -> Delta -> ByteString -> It Rope r
co -> forall r.
(b -> Err -> It Rope r)
-> (Err -> It Rope r)
-> (b -> Set String -> Delta -> ByteString -> It Rope r)
-> (ErrInfo -> It Rope r)
-> Delta
-> ByteString
-> It Rope r
m (\b
_ -> a -> Err -> It Rope r
eo a
a) Err -> It Rope r
ee (\b
_ -> a -> Set String -> Delta -> ByteString -> It Rope r
co a
a)
{-# inlinable (<$) #-}
instance Applicative Parser where
pure :: forall a. a -> Parser a
pure a
a = forall a.
(forall r.
(a -> Err -> It Rope r)
-> (Err -> It Rope r)
-> (a -> Set String -> Delta -> ByteString -> It Rope r)
-> (ErrInfo -> It Rope r)
-> Delta
-> ByteString
-> It Rope r)
-> Parser a
Parser forall a b. (a -> b) -> a -> b
$ \ a -> Err -> It Rope r
eo Err -> It Rope r
_ a -> Set String -> Delta -> ByteString -> It Rope r
_ ErrInfo -> It Rope r
_ Delta
_ ByteString
_ -> a -> Err -> It Rope r
eo a
a forall a. Monoid a => a
mempty
{-# inlinable pure #-}
<*> :: forall a b. Parser (a -> b) -> Parser a -> Parser b
(<*>) = forall (m :: * -> *) a b. Monad m => m (a -> b) -> m a -> m b
ap
{-# inlinable (<*>) #-}
instance Alternative Parser where
empty :: forall a. Parser a
empty = forall a.
(forall r.
(a -> Err -> It Rope r)
-> (Err -> It Rope r)
-> (a -> Set String -> Delta -> ByteString -> It Rope r)
-> (ErrInfo -> It Rope r)
-> Delta
-> ByteString
-> It Rope r)
-> Parser a
Parser forall a b. (a -> b) -> a -> b
$ \a -> Err -> It Rope r
_ Err -> It Rope r
ee a -> Set String -> Delta -> ByteString -> It Rope r
_ ErrInfo -> It Rope r
_ Delta
_ ByteString
_ -> Err -> It Rope r
ee forall a. Monoid a => a
mempty
{-# inlinable empty #-}
Parser forall r.
(a -> Err -> It Rope r)
-> (Err -> It Rope r)
-> (a -> Set String -> Delta -> ByteString -> It Rope r)
-> (ErrInfo -> It Rope r)
-> Delta
-> ByteString
-> It Rope r
m <|> :: forall a. Parser a -> Parser a -> Parser a
<|> Parser forall r.
(a -> Err -> It Rope r)
-> (Err -> It Rope r)
-> (a -> Set String -> Delta -> ByteString -> It Rope r)
-> (ErrInfo -> It Rope r)
-> Delta
-> ByteString
-> It Rope r
n = forall a.
(forall r.
(a -> Err -> It Rope r)
-> (Err -> It Rope r)
-> (a -> Set String -> Delta -> ByteString -> It Rope r)
-> (ErrInfo -> It Rope r)
-> Delta
-> ByteString
-> It Rope r)
-> Parser a
Parser forall a b. (a -> b) -> a -> b
$ \ a -> Err -> It Rope r
eo Err -> It Rope r
ee a -> Set String -> Delta -> ByteString -> It Rope r
co ErrInfo -> It Rope r
ce Delta
d ByteString
bs ->
forall r.
(a -> Err -> It Rope r)
-> (Err -> It Rope r)
-> (a -> Set String -> Delta -> ByteString -> It Rope r)
-> (ErrInfo -> It Rope r)
-> Delta
-> ByteString
-> It Rope r
m a -> Err -> It Rope r
eo (\Err
e -> forall r.
(a -> Err -> It Rope r)
-> (Err -> It Rope r)
-> (a -> Set String -> Delta -> ByteString -> It Rope r)
-> (ErrInfo -> It Rope r)
-> Delta
-> ByteString
-> It Rope r
n (\a
a Err
e' -> a -> Err -> It Rope r
eo a
a (Err
e forall a. Semigroup a => a -> a -> a
<> Err
e')) (\Err
e' -> Err -> It Rope r
ee (Err
e forall a. Semigroup a => a -> a -> a
<> Err
e')) a -> Set String -> Delta -> ByteString -> It Rope r
co ErrInfo -> It Rope r
ce Delta
d ByteString
bs) a -> Set String -> Delta -> ByteString -> It Rope r
co ErrInfo -> It Rope r
ce Delta
d ByteString
bs
{-# inlinable (<|>) #-}
many :: forall a. Parser a -> Parser [a]
many Parser a
p = forall a. [a] -> [a]
Prelude.reverse forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a. (a -> [a] -> [a]) -> Parser a -> Parser [a]
manyAccum (:) Parser a
p
{-# inlinable many #-}
some :: forall a. Parser a -> Parser [a]
some Parser a
p = (:) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser a
p forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall (f :: * -> *) a. Alternative f => f a -> f [a]
Alternative.many Parser a
p
instance Semigroup a => Semigroup (Parser a) where
<> :: Parser a -> Parser a -> Parser a
(<>) = forall (f :: * -> *) a b c.
Applicative f =>
(a -> b -> c) -> f a -> f b -> f c
liftA2 forall a. Semigroup a => a -> a -> a
(<>)
{-# inlinable (<>) #-}
instance (Semigroup a, Monoid a) => Monoid (Parser a) where
mappend :: Parser a -> Parser a -> Parser a
mappend = forall a. Semigroup a => a -> a -> a
(<>)
{-# inlinable mappend #-}
mempty :: Parser a
mempty = forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a. Monoid a => a
mempty
{-# inlinable mempty #-}
instance Monad Parser where
return :: forall a. a -> Parser a
return = forall (f :: * -> *) a. Applicative f => a -> f a
pure
{-# inlinable return #-}
Parser forall r.
(a -> Err -> It Rope r)
-> (Err -> It Rope r)
-> (a -> Set String -> Delta -> ByteString -> It Rope r)
-> (ErrInfo -> It Rope r)
-> Delta
-> ByteString
-> It Rope r
m >>= :: forall a b. Parser a -> (a -> Parser b) -> Parser b
>>= a -> Parser b
k = forall a.
(forall r.
(a -> Err -> It Rope r)
-> (Err -> It Rope r)
-> (a -> Set String -> Delta -> ByteString -> It Rope r)
-> (ErrInfo -> It Rope r)
-> Delta
-> ByteString
-> It Rope r)
-> Parser a
Parser forall a b. (a -> b) -> a -> b
$ \ b -> Err -> It Rope r
eo Err -> It Rope r
ee b -> Set String -> Delta -> ByteString -> It Rope r
co ErrInfo -> It Rope r
ce Delta
d ByteString
bs ->
forall r.
(a -> Err -> It Rope r)
-> (Err -> It Rope r)
-> (a -> Set String -> Delta -> ByteString -> It Rope r)
-> (ErrInfo -> It Rope r)
-> Delta
-> ByteString
-> It Rope r
m
(\a
a Err
e -> forall a.
Parser a
-> forall r.
(a -> Err -> It Rope r)
-> (Err -> It Rope r)
-> (a -> Set String -> Delta -> ByteString -> It Rope r)
-> (ErrInfo -> It Rope r)
-> Delta
-> ByteString
-> It Rope r
unparser (a -> Parser b
k a
a) (\b
b Err
e' -> b -> Err -> It Rope r
eo b
b (Err
e forall a. Semigroup a => a -> a -> a
<> Err
e')) (\Err
e' -> Err -> It Rope r
ee (Err
e forall a. Semigroup a => a -> a -> a
<> Err
e')) b -> Set String -> Delta -> ByteString -> It Rope r
co ErrInfo -> It Rope r
ce Delta
d ByteString
bs)
Err -> It Rope r
ee
(\a
a Set String
es Delta
d' ByteString
bs' -> forall a.
Parser a
-> forall r.
(a -> Err -> It Rope r)
-> (Err -> It Rope r)
-> (a -> Set String -> Delta -> ByteString -> It Rope r)
-> (ErrInfo -> It Rope r)
-> Delta
-> ByteString
-> It Rope r
unparser (a -> Parser b
k a
a)
(\b
b Err
e' -> b -> Set String -> Delta -> ByteString -> It Rope r
co b
b (Set String
es forall a. Semigroup a => a -> a -> a
<> Err -> Set String
_expected Err
e') Delta
d' ByteString
bs')
(\Err
e ->
let errDoc :: Doc AnsiStyle
errDoc = Rendering -> Err -> Doc AnsiStyle
explain (Delta -> ByteString -> Rendering
renderingCaret Delta
d' ByteString
bs') Err
e { _expected :: Set String
_expected = Err -> Set String
_expected Err
e forall a. Semigroup a => a -> a -> a
<> Set String
es }
errDelta :: [Delta]
errDelta = Err -> [Delta]
_finalDeltas Err
e
in ErrInfo -> It Rope r
ce forall a b. (a -> b) -> a -> b
$ Doc AnsiStyle -> [Delta] -> ErrInfo
ErrInfo Doc AnsiStyle
errDoc (Delta
d' forall a. a -> [a] -> [a]
: [Delta]
errDelta)
)
b -> Set String -> Delta -> ByteString -> It Rope r
co ErrInfo -> It Rope r
ce
Delta
d' ByteString
bs')
ErrInfo -> It Rope r
ce Delta
d ByteString
bs
{-# inlinable (>>=) #-}
>> :: forall a b. Parser a -> Parser b -> Parser b
(>>) = forall (f :: * -> *) a b. Applicative f => f a -> f b -> f b
(*>)
{-# inlinable (>>) #-}
#if !(MIN_VERSION_base(4,13,0))
fail = Fail.fail
{-# inlinable fail #-}
#endif
instance Fail.MonadFail Parser where
fail :: forall a. String -> Parser a
fail String
s = forall a.
(forall r.
(a -> Err -> It Rope r)
-> (Err -> It Rope r)
-> (a -> Set String -> Delta -> ByteString -> It Rope r)
-> (ErrInfo -> It Rope r)
-> Delta
-> ByteString
-> It Rope r)
-> Parser a
Parser forall a b. (a -> b) -> a -> b
$ \ a -> Err -> It Rope r
_ Err -> It Rope r
ee a -> Set String -> Delta -> ByteString -> It Rope r
_ ErrInfo -> It Rope r
_ Delta
_ ByteString
_ -> Err -> It Rope r
ee (String -> Err
failed String
s)
{-# inlinable fail #-}
instance MonadPlus Parser where
mzero :: forall a. Parser a
mzero = forall (f :: * -> *) a. Alternative f => f a
empty
{-# inlinable mzero #-}
mplus :: forall a. Parser a -> Parser a -> Parser a
mplus = forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
(<|>)
{-# inlinable mplus #-}
manyAccum :: (a -> [a] -> [a]) -> Parser a -> Parser [a]
manyAccum :: forall a. (a -> [a] -> [a]) -> Parser a -> Parser [a]
manyAccum a -> [a] -> [a]
f (Parser forall r.
(a -> Err -> It Rope r)
-> (Err -> It Rope r)
-> (a -> Set String -> Delta -> ByteString -> It Rope r)
-> (ErrInfo -> It Rope r)
-> Delta
-> ByteString
-> It Rope r
p) = forall a.
(forall r.
(a -> Err -> It Rope r)
-> (Err -> It Rope r)
-> (a -> Set String -> Delta -> ByteString -> It Rope r)
-> (ErrInfo -> It Rope r)
-> Delta
-> ByteString
-> It Rope r)
-> Parser a
Parser forall a b. (a -> b) -> a -> b
$ \[a] -> Err -> It Rope r
eo Err -> It Rope r
_ [a] -> Set String -> Delta -> ByteString -> It Rope r
co ErrInfo -> It Rope r
ce Delta
d ByteString
bs ->
let walk :: [a] -> a -> Set String -> Delta -> ByteString -> It Rope r
walk [a]
xs a
x Set String
es Delta
d' ByteString
bs' = forall r.
(a -> Err -> It Rope r)
-> (Err -> It Rope r)
-> (a -> Set String -> Delta -> ByteString -> It Rope r)
-> (ErrInfo -> It Rope r)
-> Delta
-> ByteString
-> It Rope r
p (forall {p}. Delta -> ByteString -> p -> Err -> It Rope r
manyErr Delta
d' ByteString
bs') (\Err
e -> [a] -> Set String -> Delta -> ByteString -> It Rope r
co (a -> [a] -> [a]
f a
x [a]
xs) (Err -> Set String
_expected Err
e forall a. Semigroup a => a -> a -> a
<> Set String
es) Delta
d' ByteString
bs') ([a] -> a -> Set String -> Delta -> ByteString -> It Rope r
walk (a -> [a] -> [a]
f a
x [a]
xs)) ErrInfo -> It Rope r
ce Delta
d' ByteString
bs'
manyErr :: Delta -> ByteString -> p -> Err -> It Rope r
manyErr Delta
d' ByteString
bs' p
_ Err
e = ErrInfo -> It Rope r
ce (Doc AnsiStyle -> [Delta] -> ErrInfo
ErrInfo Doc AnsiStyle
errDoc [Delta
d'])
where errDoc :: Doc AnsiStyle
errDoc = Rendering -> Err -> Doc AnsiStyle
explain (Delta -> ByteString -> Rendering
renderingCaret Delta
d' ByteString
bs') (Err
e forall a. Semigroup a => a -> a -> a
<> String -> Err
failed String
"'many' applied to a parser that accepted an empty string")
in forall r.
(a -> Err -> It Rope r)
-> (Err -> It Rope r)
-> (a -> Set String -> Delta -> ByteString -> It Rope r)
-> (ErrInfo -> It Rope r)
-> Delta
-> ByteString
-> It Rope r
p (forall {p}. Delta -> ByteString -> p -> Err -> It Rope r
manyErr Delta
d ByteString
bs) ([a] -> Err -> It Rope r
eo []) ([a] -> a -> Set String -> Delta -> ByteString -> It Rope r
walk []) ErrInfo -> It Rope r
ce Delta
d ByteString
bs
liftIt :: It Rope a -> Parser a
liftIt :: forall a. It Rope a -> Parser a
liftIt It Rope a
m = forall a.
(forall r.
(a -> Err -> It Rope r)
-> (Err -> It Rope r)
-> (a -> Set String -> Delta -> ByteString -> It Rope r)
-> (ErrInfo -> It Rope r)
-> Delta
-> ByteString
-> It Rope r)
-> Parser a
Parser forall a b. (a -> b) -> a -> b
$ \ a -> Err -> It Rope r
eo Err -> It Rope r
_ a -> Set String -> Delta -> ByteString -> It Rope r
_ ErrInfo -> It Rope r
_ Delta
_ ByteString
_ -> do
a
a <- It Rope a
m
a -> Err -> It Rope r
eo a
a forall a. Monoid a => a
mempty
{-# inlinable liftIt #-}
instance Parsing Parser where
try :: forall a. Parser a -> Parser a
try (Parser forall r.
(a -> Err -> It Rope r)
-> (Err -> It Rope r)
-> (a -> Set String -> Delta -> ByteString -> It Rope r)
-> (ErrInfo -> It Rope r)
-> Delta
-> ByteString
-> It Rope r
m) = forall a.
(forall r.
(a -> Err -> It Rope r)
-> (Err -> It Rope r)
-> (a -> Set String -> Delta -> ByteString -> It Rope r)
-> (ErrInfo -> It Rope r)
-> Delta
-> ByteString
-> It Rope r)
-> Parser a
Parser forall a b. (a -> b) -> a -> b
$ \ a -> Err -> It Rope r
eo Err -> It Rope r
ee a -> Set String -> Delta -> ByteString -> It Rope r
co ErrInfo -> It Rope r
_ -> forall r.
(a -> Err -> It Rope r)
-> (Err -> It Rope r)
-> (a -> Set String -> Delta -> ByteString -> It Rope r)
-> (ErrInfo -> It Rope r)
-> Delta
-> ByteString
-> It Rope r
m a -> Err -> It Rope r
eo Err -> It Rope r
ee a -> Set String -> Delta -> ByteString -> It Rope r
co (\ErrInfo
_ -> Err -> It Rope r
ee forall a. Monoid a => a
mempty)
{-# inlinable try #-}
Parser forall r.
(a -> Err -> It Rope r)
-> (Err -> It Rope r)
-> (a -> Set String -> Delta -> ByteString -> It Rope r)
-> (ErrInfo -> It Rope r)
-> Delta
-> ByteString
-> It Rope r
m <?> :: forall a. Parser a -> String -> Parser a
<?> String
nm = forall a.
(forall r.
(a -> Err -> It Rope r)
-> (Err -> It Rope r)
-> (a -> Set String -> Delta -> ByteString -> It Rope r)
-> (ErrInfo -> It Rope r)
-> Delta
-> ByteString
-> It Rope r)
-> Parser a
Parser forall a b. (a -> b) -> a -> b
$ \ a -> Err -> It Rope r
eo Err -> It Rope r
ee -> forall r.
(a -> Err -> It Rope r)
-> (Err -> It Rope r)
-> (a -> Set String -> Delta -> ByteString -> It Rope r)
-> (ErrInfo -> It Rope r)
-> Delta
-> ByteString
-> It Rope r
m
(\a
a Err
e -> a -> Err -> It Rope r
eo a
a (if forall a. Maybe a -> Bool
isJust (Err -> Maybe (Doc AnsiStyle)
_reason Err
e) then Err
e { _expected :: Set String
_expected = forall a. a -> Set a
Set.singleton String
nm } else Err
e))
(\Err
e -> Err -> It Rope r
ee Err
e { _expected :: Set String
_expected = forall a. a -> Set a
Set.singleton String
nm })
{-# inlinable (<?>) #-}
skipMany :: forall a. Parser a -> Parser ()
skipMany Parser a
p = () forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ forall a. (a -> [a] -> [a]) -> Parser a -> Parser [a]
manyAccum (\a
_ [a]
_ -> []) Parser a
p
{-# inlinable skipMany #-}
unexpected :: forall a. String -> Parser a
unexpected String
s = forall a.
(forall r.
(a -> Err -> It Rope r)
-> (Err -> It Rope r)
-> (a -> Set String -> Delta -> ByteString -> It Rope r)
-> (ErrInfo -> It Rope r)
-> Delta
-> ByteString
-> It Rope r)
-> Parser a
Parser forall a b. (a -> b) -> a -> b
$ \ a -> Err -> It Rope r
_ Err -> It Rope r
ee a -> Set String -> Delta -> ByteString -> It Rope r
_ ErrInfo -> It Rope r
_ Delta
_ ByteString
_ -> Err -> It Rope r
ee forall a b. (a -> b) -> a -> b
$ String -> Err
failed forall a b. (a -> b) -> a -> b
$ String
"unexpected " forall a. [a] -> [a] -> [a]
++ String
s
{-# inlinable unexpected #-}
eof :: Parser ()
eof = forall (m :: * -> *) a. (Parsing m, Show a) => m a -> m ()
notFollowedBy forall (m :: * -> *). CharParsing m => m Char
anyChar forall (m :: * -> *) a. Parsing m => m a -> String -> m a
<?> String
"end of input"
{-# inlinable eof #-}
notFollowedBy :: forall a. Show a => Parser a -> Parser ()
notFollowedBy Parser a
p = forall (m :: * -> *) a. Parsing m => m a -> m a
try (forall (f :: * -> *) a. Alternative f => f a -> f (Maybe a)
optional Parser a
p forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= forall b a. b -> (a -> b) -> Maybe a -> b
maybe (forall (f :: * -> *) a. Applicative f => a -> f a
pure ()) (forall (m :: * -> *) a. Parsing m => String -> m a
unexpected forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Show a => a -> String
show))
{-# inlinable notFollowedBy #-}
instance Errable Parser where
raiseErr :: forall a. Err -> Parser a
raiseErr Err
e = forall a.
(forall r.
(a -> Err -> It Rope r)
-> (Err -> It Rope r)
-> (a -> Set String -> Delta -> ByteString -> It Rope r)
-> (ErrInfo -> It Rope r)
-> Delta
-> ByteString
-> It Rope r)
-> Parser a
Parser forall a b. (a -> b) -> a -> b
$ \ a -> Err -> It Rope r
_ Err -> It Rope r
ee a -> Set String -> Delta -> ByteString -> It Rope r
_ ErrInfo -> It Rope r
_ Delta
_ ByteString
_ -> Err -> It Rope r
ee Err
e
{-# inlinable raiseErr #-}
instance LookAheadParsing Parser where
lookAhead :: forall a. Parser a -> Parser a
lookAhead (Parser forall r.
(a -> Err -> It Rope r)
-> (Err -> It Rope r)
-> (a -> Set String -> Delta -> ByteString -> It Rope r)
-> (ErrInfo -> It Rope r)
-> Delta
-> ByteString
-> It Rope r
m) = forall a.
(forall r.
(a -> Err -> It Rope r)
-> (Err -> It Rope r)
-> (a -> Set String -> Delta -> ByteString -> It Rope r)
-> (ErrInfo -> It Rope r)
-> Delta
-> ByteString
-> It Rope r)
-> Parser a
Parser forall a b. (a -> b) -> a -> b
$ \a -> Err -> It Rope r
eo Err -> It Rope r
ee a -> Set String -> Delta -> ByteString -> It Rope r
_ -> forall r.
(a -> Err -> It Rope r)
-> (Err -> It Rope r)
-> (a -> Set String -> Delta -> ByteString -> It Rope r)
-> (ErrInfo -> It Rope r)
-> Delta
-> ByteString
-> It Rope r
m a -> Err -> It Rope r
eo Err -> It Rope r
ee (\a
a Set String
_ Delta
_ ByteString
_ -> a -> Err -> It Rope r
eo a
a forall a. Monoid a => a
mempty)
{-# inlinable lookAhead #-}
instance CharParsing Parser where
satisfy :: (Char -> Bool) -> Parser Char
satisfy Char -> Bool
f = forall a.
(forall r.
(a -> Err -> It Rope r)
-> (Err -> It Rope r)
-> (a -> Set String -> Delta -> ByteString -> It Rope r)
-> (ErrInfo -> It Rope r)
-> Delta
-> ByteString
-> It Rope r)
-> Parser a
Parser forall a b. (a -> b) -> a -> b
$ \ Char -> Err -> It Rope r
_ Err -> It Rope r
ee Char -> Set String -> Delta -> ByteString -> It Rope r
co ErrInfo -> It Rope r
_ Delta
d ByteString
bs ->
case ByteString -> Maybe (Char, ByteString)
UTF8.uncons forall a b. (a -> b) -> a -> b
$ Int -> ByteString -> ByteString
Strict.drop (forall a b. (Integral a, Num b) => a -> b
fromIntegral (Delta -> Int64
columnByte Delta
d)) ByteString
bs of
Maybe (Char, ByteString)
Nothing -> Err -> It Rope r
ee (String -> Err
failed String
"unexpected EOF")
Just (Char
c, ByteString
xs)
| Bool -> Bool
not (Char -> Bool
f Char
c) -> Err -> It Rope r
ee forall a. Monoid a => a
mempty
| ByteString -> Bool
Strict.null ByteString
xs -> let !ddc :: Delta
ddc = Delta
d forall a. Semigroup a => a -> a -> a
<> forall t. HasDelta t => t -> Delta
delta Char
c
in forall (m :: * -> *) a. Monad m => m (m a) -> m a
join forall a b. (a -> b) -> a -> b
$ forall r. r -> (Delta -> ByteString -> r) -> Delta -> It Rope r
fillIt (Char -> Set String -> Delta -> ByteString -> It Rope r
co Char
c forall a. Monoid a => a
mempty Delta
ddc (if Char
c forall a. Eq a => a -> a -> Bool
== Char
'\n' then forall a. Monoid a => a
mempty else ByteString
bs))
(Char -> Set String -> Delta -> ByteString -> It Rope r
co Char
c forall a. Monoid a => a
mempty)
Delta
ddc
| Bool
otherwise -> Char -> Set String -> Delta -> ByteString -> It Rope r
co Char
c forall a. Monoid a => a
mempty (Delta
d forall a. Semigroup a => a -> a -> a
<> forall t. HasDelta t => t -> Delta
delta Char
c) ByteString
bs
{-# inlinable satisfy #-}
instance TokenParsing Parser
instance DeltaParsing Parser where
line :: Parser ByteString
line = forall a.
(forall r.
(a -> Err -> It Rope r)
-> (Err -> It Rope r)
-> (a -> Set String -> Delta -> ByteString -> It Rope r)
-> (ErrInfo -> It Rope r)
-> Delta
-> ByteString
-> It Rope r)
-> Parser a
Parser forall a b. (a -> b) -> a -> b
$ \ByteString -> Err -> It Rope r
eo Err -> It Rope r
_ ByteString -> Set String -> Delta -> ByteString -> It Rope r
_ ErrInfo -> It Rope r
_ Delta
_ ByteString
bs -> ByteString -> Err -> It Rope r
eo ByteString
bs forall a. Monoid a => a
mempty
{-# inlinable line #-}
position :: Parser Delta
position = forall a.
(forall r.
(a -> Err -> It Rope r)
-> (Err -> It Rope r)
-> (a -> Set String -> Delta -> ByteString -> It Rope r)
-> (ErrInfo -> It Rope r)
-> Delta
-> ByteString
-> It Rope r)
-> Parser a
Parser forall a b. (a -> b) -> a -> b
$ \Delta -> Err -> It Rope r
eo Err -> It Rope r
_ Delta -> Set String -> Delta -> ByteString -> It Rope r
_ ErrInfo -> It Rope r
_ Delta
d ByteString
_ -> Delta -> Err -> It Rope r
eo Delta
d forall a. Monoid a => a
mempty
{-# inlinable position #-}
rend :: Parser Rendering
rend = forall a.
(forall r.
(a -> Err -> It Rope r)
-> (Err -> It Rope r)
-> (a -> Set String -> Delta -> ByteString -> It Rope r)
-> (ErrInfo -> It Rope r)
-> Delta
-> ByteString
-> It Rope r)
-> Parser a
Parser forall a b. (a -> b) -> a -> b
$ \Rendering -> Err -> It Rope r
eo Err -> It Rope r
_ Rendering -> Set String -> Delta -> ByteString -> It Rope r
_ ErrInfo -> It Rope r
_ Delta
d ByteString
bs -> Rendering -> Err -> It Rope r
eo (forall s. Source s => Delta -> s -> Rendering
rendered Delta
d ByteString
bs) forall a. Monoid a => a
mempty
{-# inlinable rend #-}
slicedWith :: forall a r. (a -> ByteString -> r) -> Parser a -> Parser r
slicedWith a -> ByteString -> r
f Parser a
p = do
Delta
m <- forall (m :: * -> *). DeltaParsing m => m Delta
position
a
a <- Parser a
p
Delta
r <- forall (m :: * -> *). DeltaParsing m => m Delta
position
a -> ByteString -> r
f a
a forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a. It Rope a -> Parser a
liftIt (Delta -> Delta -> It Rope ByteString
sliceIt Delta
m Delta
r)
{-# inlinable slicedWith #-}
instance MarkParsing Delta Parser where
mark :: Parser Delta
mark = forall (m :: * -> *). DeltaParsing m => m Delta
position
{-# inlinable mark #-}
release :: Delta -> Parser ()
release Delta
d' = forall a.
(forall r.
(a -> Err -> It Rope r)
-> (Err -> It Rope r)
-> (a -> Set String -> Delta -> ByteString -> It Rope r)
-> (ErrInfo -> It Rope r)
-> Delta
-> ByteString
-> It Rope r)
-> Parser a
Parser forall a b. (a -> b) -> a -> b
$ \() -> Err -> It Rope r
_ Err -> It Rope r
ee () -> Set String -> Delta -> ByteString -> It Rope r
co ErrInfo -> It Rope r
_ Delta
d ByteString
bs -> do
Maybe ByteString
mbs <- Delta -> It Rope (Maybe ByteString)
rewindIt Delta
d'
case Maybe ByteString
mbs of
Just ByteString
bs' -> () -> Set String -> Delta -> ByteString -> It Rope r
co () forall a. Monoid a => a
mempty Delta
d' ByteString
bs'
Maybe ByteString
Nothing
| forall t. HasBytes t => t -> Int64
bytes Delta
d' forall a. Eq a => a -> a -> Bool
== forall t. HasBytes t => t -> Int64
bytes (Delta -> Delta
rewind Delta
d) forall a. Num a => a -> a -> a
+ forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString -> Int
Strict.length ByteString
bs) -> if forall s t. (HasDelta s, HasDelta t) => s -> t -> Bool
near Delta
d Delta
d'
then () -> Set String -> Delta -> ByteString -> It Rope r
co () forall a. Monoid a => a
mempty Delta
d' ByteString
bs
else () -> Set String -> Delta -> ByteString -> It Rope r
co () forall a. Monoid a => a
mempty Delta
d' forall a. Monoid a => a
mempty
| Bool
otherwise -> Err -> It Rope r
ee forall a. Monoid a => a
mempty
data Step a
= StepDone !Rope a
| StepFail !Rope ErrInfo
| StepCont !Rope (Result a) (Rope -> Step a)
instance Show a => Show (Step a) where
showsPrec :: Int -> Step a -> ShowS
showsPrec Int
d (StepDone Rope
r a
a) = Bool -> ShowS -> ShowS
showParen (Int
d forall a. Ord a => a -> a -> Bool
> Int
10) forall a b. (a -> b) -> a -> b
$
String -> ShowS
showString String
"StepDone " forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Show a => Int -> a -> ShowS
showsPrec Int
11 Rope
r forall b c a. (b -> c) -> (a -> b) -> a -> c
. Char -> ShowS
showChar Char
' ' forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Show a => Int -> a -> ShowS
showsPrec Int
11 a
a
showsPrec Int
d (StepFail Rope
r ErrInfo
xs) = Bool -> ShowS -> ShowS
showParen (Int
d forall a. Ord a => a -> a -> Bool
> Int
10) forall a b. (a -> b) -> a -> b
$
String -> ShowS
showString String
"StepFail " forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Show a => Int -> a -> ShowS
showsPrec Int
11 Rope
r forall b c a. (b -> c) -> (a -> b) -> a -> c
. Char -> ShowS
showChar Char
' ' forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Show a => Int -> a -> ShowS
showsPrec Int
11 ErrInfo
xs
showsPrec Int
d (StepCont Rope
r Result a
fin Rope -> Step a
_) = Bool -> ShowS -> ShowS
showParen (Int
d forall a. Ord a => a -> a -> Bool
> Int
10) forall a b. (a -> b) -> a -> b
$
String -> ShowS
showString String
"StepCont " forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Show a => Int -> a -> ShowS
showsPrec Int
11 Rope
r forall b c a. (b -> c) -> (a -> b) -> a -> c
. Char -> ShowS
showChar Char
' ' forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Show a => Int -> a -> ShowS
showsPrec Int
11 Result a
fin forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> ShowS
showString String
" ..."
instance Functor Step where
fmap :: forall a b. (a -> b) -> Step a -> Step b
fmap a -> b
f (StepDone Rope
r a
a) = forall a. Rope -> a -> Step a
StepDone Rope
r (a -> b
f a
a)
fmap a -> b
_ (StepFail Rope
r ErrInfo
xs) = forall a. Rope -> ErrInfo -> Step a
StepFail Rope
r ErrInfo
xs
fmap a -> b
f (StepCont Rope
r Result a
z Rope -> Step a
k) = forall a. Rope -> Result a -> (Rope -> Step a) -> Step a
StepCont Rope
r (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
f Result a
z) (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
f forall b c a. (b -> c) -> (a -> b) -> a -> c
. Rope -> Step a
k)
feed :: Reducer t Rope => t -> Step r -> Step r
feed :: forall t r. Reducer t Rope => t -> Step r -> Step r
feed t
t (StepDone Rope
r r
a) = forall a. Rope -> a -> Step a
StepDone (forall c m. Reducer c m => m -> c -> m
snoc Rope
r t
t) r
a
feed t
t (StepFail Rope
r ErrInfo
xs) = forall a. Rope -> ErrInfo -> Step a
StepFail (forall c m. Reducer c m => m -> c -> m
snoc Rope
r t
t) ErrInfo
xs
feed t
t (StepCont Rope
r Result r
_ Rope -> Step r
k) = Rope -> Step r
k (forall c m. Reducer c m => m -> c -> m
snoc Rope
r t
t)
{-# inlinable feed #-}
starve :: Step a -> Result a
starve :: forall a. Step a -> Result a
starve (StepDone Rope
_ a
a) = forall a. a -> Result a
Success a
a
starve (StepFail Rope
_ ErrInfo
xs) = forall a. ErrInfo -> Result a
Failure ErrInfo
xs
starve (StepCont Rope
_ Result a
z Rope -> Step a
_) = Result a
z
{-# inlinable starve #-}
stepResult :: Rope -> Result a -> Step a
stepResult :: forall a. Rope -> Result a -> Step a
stepResult Rope
r (Success a
a) = forall a. Rope -> a -> Step a
StepDone Rope
r a
a
stepResult Rope
r (Failure ErrInfo
xs) = forall a. Rope -> ErrInfo -> Step a
StepFail Rope
r ErrInfo
xs
{-# inlinable stepResult #-}
stepIt :: It Rope a -> Step a
stepIt :: forall a. It Rope a -> Step a
stepIt = forall {a}. Rope -> It Rope a -> Step a
go forall a. Monoid a => a
mempty where
go :: Rope -> It Rope a -> Step a
go Rope
r It Rope a
m = case forall r a. It r a -> r -> It r a
simplifyIt It Rope a
m Rope
r of
Pure a
a -> forall a. Rope -> a -> Step a
StepDone Rope
r a
a
It a
a Rope -> It Rope a
k -> forall a. Rope -> Result a -> (Rope -> Step a) -> Step a
StepCont Rope
r (forall (f :: * -> *) a. Applicative f => a -> f a
pure a
a) forall a b. (a -> b) -> a -> b
$ \Rope
r' -> Rope -> It Rope a -> Step a
go Rope
r' (Rope -> It Rope a
k Rope
r')
{-# inlinable stepIt #-}
data Stepping a
= EO a Err
| EE Err
| CO a (Set String) Delta ByteString
| CE ErrInfo
stepParser
:: Parser a
-> Delta
-> Step a
stepParser :: forall a. Parser a -> Delta -> Step a
stepParser (Parser forall r.
(a -> Err -> It Rope r)
-> (Err -> It Rope r)
-> (a -> Set String -> Delta -> ByteString -> It Rope r)
-> (ErrInfo -> It Rope r)
-> Delta
-> ByteString
-> It Rope r
p) Delta
d0 = forall a. Step (Result a) -> Step a
joinStep forall a b. (a -> b) -> a -> b
$ forall a. It Rope a -> Step a
stepIt forall a b. (a -> b) -> a -> b
$ do
ByteString
bs0 <- forall a. a -> Maybe a -> a
fromMaybe forall a. Monoid a => a
mempty forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Delta -> It Rope (Maybe ByteString)
rewindIt Delta
d0
forall a. ByteString -> Stepping a -> Result a
go ByteString
bs0 forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall r.
(a -> Err -> It Rope r)
-> (Err -> It Rope r)
-> (a -> Set String -> Delta -> ByteString -> It Rope r)
-> (ErrInfo -> It Rope r)
-> Delta
-> ByteString
-> It Rope r
p forall {a} {r}. a -> Err -> It r (Stepping a)
eo forall {r} {a}. Err -> It r (Stepping a)
ee forall {a} {r}.
a -> Set String -> Delta -> ByteString -> It r (Stepping a)
co forall {r} {a}. ErrInfo -> It r (Stepping a)
ce Delta
d0 ByteString
bs0
where
eo :: a -> Err -> It r (Stepping a)
eo a
a Err
e = forall r a. a -> It r a
Pure (forall a. a -> Err -> Stepping a
EO a
a Err
e)
ee :: Err -> It r (Stepping a)
ee Err
e = forall r a. a -> It r a
Pure (forall a. Err -> Stepping a
EE Err
e)
co :: a -> Set String -> Delta -> ByteString -> It r (Stepping a)
co a
a Set String
es Delta
d' ByteString
bs = forall r a. a -> It r a
Pure (forall a. a -> Set String -> Delta -> ByteString -> Stepping a
CO a
a Set String
es Delta
d' ByteString
bs)
ce :: ErrInfo -> It r (Stepping a)
ce ErrInfo
errInf = forall r a. a -> It r a
Pure (forall a. ErrInfo -> Stepping a
CE ErrInfo
errInf)
go :: ByteString -> Stepping a -> Result a
go :: forall a. ByteString -> Stepping a -> Result a
go ByteString
_ (EO a
a Err
_) = forall a. a -> Result a
Success a
a
go ByteString
bs0 (EE Err
e) = forall a. ErrInfo -> Result a
Failure forall a b. (a -> b) -> a -> b
$
let errDoc :: Doc AnsiStyle
errDoc = Rendering -> Err -> Doc AnsiStyle
explain (Delta -> ByteString -> Rendering
renderingCaret Delta
d0 ByteString
bs0) Err
e
in Doc AnsiStyle -> [Delta] -> ErrInfo
ErrInfo Doc AnsiStyle
errDoc (Delta
d0 forall a. a -> [a] -> [a]
: Err -> [Delta]
_finalDeltas Err
e)
go ByteString
_ (CO a
a Set String
_ Delta
_ ByteString
_) = forall a. a -> Result a
Success a
a
go ByteString
_ (CE ErrInfo
e) = forall a. ErrInfo -> Result a
Failure ErrInfo
e
joinStep :: Step (Result a) -> Step a
joinStep :: forall a. Step (Result a) -> Step a
joinStep (StepDone Rope
r (Success a
a)) = forall a. Rope -> a -> Step a
StepDone Rope
r a
a
joinStep (StepDone Rope
r (Failure ErrInfo
e)) = forall a. Rope -> ErrInfo -> Step a
StepFail Rope
r ErrInfo
e
joinStep (StepFail Rope
r ErrInfo
e) = forall a. Rope -> ErrInfo -> Step a
StepFail Rope
r ErrInfo
e
joinStep (StepCont Rope
r Result (Result a)
a Rope -> Step (Result a)
k) = forall a. Rope -> Result a -> (Rope -> Step a) -> Step a
StepCont Rope
r (forall (m :: * -> *) a. Monad m => m (m a) -> m a
join Result (Result a)
a) (forall a. Step (Result a) -> Step a
joinStep forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Rope -> Step (Result a)
k)
{-# inlinable joinStep #-}
runParser
:: Reducer t Rope
=> Parser a
-> Delta
-> t
-> Result a
runParser :: forall t a. Reducer t Rope => Parser a -> Delta -> t -> Result a
runParser Parser a
p Delta
d t
bs = forall a. Step a -> Result a
starve forall a b. (a -> b) -> a -> b
$ forall t r. Reducer t Rope => t -> Step r -> Step r
feed t
bs forall a b. (a -> b) -> a -> b
$ forall a. Parser a -> Delta -> Step a
stepParser Parser a
p Delta
d
{-# inlinable runParser #-}
parseFromFile :: MonadIO m => Parser a -> String -> m (Maybe a)
parseFromFile :: forall (m :: * -> *) a.
MonadIO m =>
Parser a -> String -> m (Maybe a)
parseFromFile Parser a
p String
fn = do
Result a
result <- forall (m :: * -> *) a.
MonadIO m =>
Parser a -> String -> m (Result a)
parseFromFileEx Parser a
p String
fn
case Result a
result of
Success a
a -> forall (m :: * -> *) a. Monad m => a -> m a
return (forall a. a -> Maybe a
Just a
a)
Failure ErrInfo
xs -> do
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ Handle -> SimpleDocStream AnsiStyle -> IO ()
renderIO Handle
stdout forall a b. (a -> b) -> a -> b
$ Double -> Int -> Doc AnsiStyle -> SimpleDocStream AnsiStyle
renderPretty Double
0.8 Int
80 forall a b. (a -> b) -> a -> b
$ (ErrInfo -> Doc AnsiStyle
_errDoc ErrInfo
xs) forall a. Semigroup a => a -> a -> a
<> forall ann. Doc ann
line'
forall (m :: * -> *) a. Monad m => a -> m a
return forall a. Maybe a
Nothing
parseFromFileEx :: MonadIO m => Parser a -> String -> m (Result a)
parseFromFileEx :: forall (m :: * -> *) a.
MonadIO m =>
Parser a -> String -> m (Result a)
parseFromFileEx Parser a
p String
fn = do
ByteString
s <- forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ String -> IO ByteString
Strict.readFile String
fn
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a. Parser a -> Delta -> ByteString -> Result a
parseByteString Parser a
p (ByteString -> Int64 -> Int64 -> Int64 -> Int64 -> Delta
Directed (String -> ByteString
UTF8.fromString String
fn) Int64
0 Int64
0 Int64
0 Int64
0) ByteString
s
parseByteString
:: Parser a
-> Delta
-> UTF8.ByteString
-> Result a
parseByteString :: forall a. Parser a -> Delta -> ByteString -> Result a
parseByteString = forall t a. Reducer t Rope => Parser a -> Delta -> t -> Result a
runParser
parseString
:: Parser a
-> Delta
-> String
-> Result a
parseString :: forall a. Parser a -> Delta -> String -> Result a
parseString = forall t a. Reducer t Rope => Parser a -> Delta -> t -> Result a
runParser
parseTest :: (MonadIO m, Show a) => Parser a -> String -> m ()
parseTest :: forall (m :: * -> *) a.
(MonadIO m, Show a) =>
Parser a -> String -> m ()
parseTest Parser a
p String
s = case forall a. Parser a -> Delta -> ByteString -> Result a
parseByteString Parser a
p forall a. Monoid a => a
mempty (String -> ByteString
UTF8.fromString String
s) of
Failure ErrInfo
xs -> forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ Handle -> SimpleDocStream AnsiStyle -> IO ()
renderIO Handle
stdout forall a b. (a -> b) -> a -> b
$ Double -> Int -> Doc AnsiStyle -> SimpleDocStream AnsiStyle
renderPretty Double
0.8 Int
80 forall a b. (a -> b) -> a -> b
$ (ErrInfo -> Doc AnsiStyle
_errDoc ErrInfo
xs) forall a. Semigroup a => a -> a -> a
<> forall ann. Doc ann
line'
Success a
a -> forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (forall a. Show a => a -> IO ()
print a
a)