{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE CPP #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE PackageImports #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE StandaloneDeriving #-}
module Text.Megaparsec.Custom (
CustomErr,
parseErrorAt,
parseErrorAtRegion,
SourceExcerpt,
getExcerptText,
excerpt_,
reparseExcerpt,
customErrorBundlePretty,
FinalParseError,
FinalParseError',
FinalParseErrorBundle,
FinalParseErrorBundle',
finalError,
finalFancyFailure,
finalFail,
finalCustomFailure,
finalErrorBundlePretty,
attachSource,
parseIncludeFile,
)
where
import Prelude ()
import "base-compat-batteries" Prelude.Compat hiding (readFile)
import Control.Monad.Except
import Control.Monad.State.Strict (StateT, evalStateT)
import Data.Foldable (asum, toList)
import qualified Data.List.NonEmpty as NE
import qualified Data.Set as S
import Data.Text (Text)
import Text.Megaparsec
data CustomErr
= ErrorFailAt Int
Int
String
| ErrorReparsing
(NE.NonEmpty (ParseError Text CustomErr))
deriving (Int -> CustomErr -> ShowS
[CustomErr] -> ShowS
CustomErr -> String
(Int -> CustomErr -> ShowS)
-> (CustomErr -> String)
-> ([CustomErr] -> ShowS)
-> Show CustomErr
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [CustomErr] -> ShowS
$cshowList :: [CustomErr] -> ShowS
show :: CustomErr -> String
$cshow :: CustomErr -> String
showsPrec :: Int -> CustomErr -> ShowS
$cshowsPrec :: Int -> CustomErr -> ShowS
Show, CustomErr -> CustomErr -> Bool
(CustomErr -> CustomErr -> Bool)
-> (CustomErr -> CustomErr -> Bool) -> Eq CustomErr
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: CustomErr -> CustomErr -> Bool
$c/= :: CustomErr -> CustomErr -> Bool
== :: CustomErr -> CustomErr -> Bool
$c== :: CustomErr -> CustomErr -> Bool
Eq, Eq CustomErr
Eq CustomErr
-> (CustomErr -> CustomErr -> Ordering)
-> (CustomErr -> CustomErr -> Bool)
-> (CustomErr -> CustomErr -> Bool)
-> (CustomErr -> CustomErr -> Bool)
-> (CustomErr -> CustomErr -> Bool)
-> (CustomErr -> CustomErr -> CustomErr)
-> (CustomErr -> CustomErr -> CustomErr)
-> Ord CustomErr
CustomErr -> CustomErr -> Bool
CustomErr -> CustomErr -> Ordering
CustomErr -> CustomErr -> CustomErr
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: CustomErr -> CustomErr -> CustomErr
$cmin :: CustomErr -> CustomErr -> CustomErr
max :: CustomErr -> CustomErr -> CustomErr
$cmax :: CustomErr -> CustomErr -> CustomErr
>= :: CustomErr -> CustomErr -> Bool
$c>= :: CustomErr -> CustomErr -> Bool
> :: CustomErr -> CustomErr -> Bool
$c> :: CustomErr -> CustomErr -> Bool
<= :: CustomErr -> CustomErr -> Bool
$c<= :: CustomErr -> CustomErr -> Bool
< :: CustomErr -> CustomErr -> Bool
$c< :: CustomErr -> CustomErr -> Bool
compare :: CustomErr -> CustomErr -> Ordering
$ccompare :: CustomErr -> CustomErr -> Ordering
$cp1Ord :: Eq CustomErr
Ord)
deriving instance Ord (ParseError Text CustomErr)
instance ShowErrorComponent CustomErr where
showErrorComponent :: CustomErr -> String
showErrorComponent (ErrorFailAt Int
_ Int
_ String
errMsg) = String
errMsg
showErrorComponent (ErrorReparsing NonEmpty (ParseError Text CustomErr)
_) = String
""
errorComponentLen :: CustomErr -> Int
errorComponentLen (ErrorFailAt Int
startOffset Int
endOffset String
_) =
Int
endOffset Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
startOffset
errorComponentLen (ErrorReparsing NonEmpty (ParseError Text CustomErr)
_) = Int
1
parseErrorAt :: Int -> String -> CustomErr
parseErrorAt :: Int -> String -> CustomErr
parseErrorAt Int
offset String
msg = Int -> Int -> String -> CustomErr
ErrorFailAt Int
offset (Int
offsetInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1) String
msg
parseErrorAtRegion
:: Int
-> Int
-> String
-> CustomErr
parseErrorAtRegion :: Int -> Int -> String -> CustomErr
parseErrorAtRegion Int
startOffset Int
endOffset String
msg =
if Int
startOffset Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
endOffset
then Int -> Int -> String -> CustomErr
ErrorFailAt Int
startOffset Int
endOffset String
msg
else Int -> Int -> String -> CustomErr
ErrorFailAt Int
startOffset (Int
startOffsetInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1) String
msg
data SourceExcerpt = SourceExcerpt Int
Text
getExcerptText :: SourceExcerpt -> Text
getExcerptText :: SourceExcerpt -> Text
getExcerptText (SourceExcerpt Int
_ Text
txt) = Text
txt
excerpt_ :: MonadParsec CustomErr Text m => m a -> m SourceExcerpt
excerpt_ :: m a -> m SourceExcerpt
excerpt_ m a
p = do
Int
offset <- m Int
forall e s (m :: * -> *). MonadParsec e s m => m Int
getOffset
(!Text
txt, a
_) <- m a -> m (Tokens Text, a)
forall e s (m :: * -> *) a.
MonadParsec e s m =>
m a -> m (Tokens s, a)
match m a
p
SourceExcerpt -> m SourceExcerpt
forall (f :: * -> *) a. Applicative f => a -> f a
pure (SourceExcerpt -> m SourceExcerpt)
-> SourceExcerpt -> m SourceExcerpt
forall a b. (a -> b) -> a -> b
$ Int -> Text -> SourceExcerpt
SourceExcerpt Int
offset Text
txt
reparseExcerpt
:: Monad m
=> SourceExcerpt
-> ParsecT CustomErr Text m a
-> ParsecT CustomErr Text m a
reparseExcerpt :: SourceExcerpt
-> ParsecT CustomErr Text m a -> ParsecT CustomErr Text m a
reparseExcerpt (SourceExcerpt Int
offset Text
txt) ParsecT CustomErr Text m a
p = do
(State Text CustomErr
_, Either (ParseErrorBundle Text CustomErr) a
res) <- m (State Text CustomErr,
Either (ParseErrorBundle Text CustomErr) a)
-> ParsecT
CustomErr
Text
m
(State Text CustomErr, Either (ParseErrorBundle Text CustomErr) a)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (m (State Text CustomErr,
Either (ParseErrorBundle Text CustomErr) a)
-> ParsecT
CustomErr
Text
m
(State Text CustomErr, Either (ParseErrorBundle Text CustomErr) a))
-> m (State Text CustomErr,
Either (ParseErrorBundle Text CustomErr) a)
-> ParsecT
CustomErr
Text
m
(State Text CustomErr, Either (ParseErrorBundle Text CustomErr) a)
forall a b. (a -> b) -> a -> b
$ ParsecT CustomErr Text m a
-> State Text CustomErr
-> m (State Text CustomErr,
Either (ParseErrorBundle Text CustomErr) a)
forall (m :: * -> *) e s a.
Monad m =>
ParsecT e s m a
-> State s e -> m (State s e, Either (ParseErrorBundle s e) a)
runParserT' ParsecT CustomErr Text m a
p (Int -> Text -> State Text CustomErr
forall s e. Int -> s -> State s e
offsetInitialState Int
offset Text
txt)
case Either (ParseErrorBundle Text CustomErr) a
res of
Right a
result -> a -> ParsecT CustomErr Text m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure a
result
Left ParseErrorBundle Text CustomErr
errBundle -> CustomErr -> ParsecT CustomErr Text m a
forall e s (m :: * -> *) a. MonadParsec e s m => e -> m a
customFailure (CustomErr -> ParsecT CustomErr Text m a)
-> CustomErr -> ParsecT CustomErr Text m a
forall a b. (a -> b) -> a -> b
$ NonEmpty (ParseError Text CustomErr) -> CustomErr
ErrorReparsing (NonEmpty (ParseError Text CustomErr) -> CustomErr)
-> NonEmpty (ParseError Text CustomErr) -> CustomErr
forall a b. (a -> b) -> a -> b
$ ParseErrorBundle Text CustomErr
-> NonEmpty (ParseError Text CustomErr)
forall s e. ParseErrorBundle s e -> NonEmpty (ParseError s e)
bundleErrors ParseErrorBundle Text CustomErr
errBundle
where
offsetInitialState :: Int -> s ->
#if MIN_VERSION_megaparsec(8,0,0)
State s e
#else
State s
#endif
offsetInitialState :: Int -> s -> State s e
offsetInitialState Int
initialOffset s
s = State :: forall s e. s -> Int -> PosState s -> [ParseError s e] -> State s e
State
{ stateInput :: s
stateInput = s
s
, stateOffset :: Int
stateOffset = Int
initialOffset
, statePosState :: PosState s
statePosState = PosState :: forall s. s -> Int -> SourcePos -> Pos -> String -> PosState s
PosState
{ pstateInput :: s
pstateInput = s
s
, pstateOffset :: Int
pstateOffset = Int
initialOffset
, pstateSourcePos :: SourcePos
pstateSourcePos = String -> SourcePos
initialPos String
""
, pstateTabWidth :: Pos
pstateTabWidth = Pos
defaultTabWidth
, pstateLinePrefix :: String
pstateLinePrefix = String
""
}
#if MIN_VERSION_megaparsec(8,0,0)
, stateParseErrors :: [ParseError s e]
stateParseErrors = []
#endif
}
customErrorBundlePretty :: ParseErrorBundle Text CustomErr -> String
customErrorBundlePretty :: ParseErrorBundle Text CustomErr -> String
customErrorBundlePretty ParseErrorBundle Text CustomErr
errBundle =
let errBundle' :: ParseErrorBundle Text CustomErr
errBundle' = ParseErrorBundle Text CustomErr
errBundle { bundleErrors :: NonEmpty (ParseError Text CustomErr)
bundleErrors =
(ParseError Text CustomErr -> Int)
-> NonEmpty (ParseError Text CustomErr)
-> NonEmpty (ParseError Text CustomErr)
forall o a. Ord o => (a -> o) -> NonEmpty a -> NonEmpty a
NE.sortWith ParseError Text CustomErr -> Int
forall s e. ParseError s e -> Int
errorOffset (NonEmpty (ParseError Text CustomErr)
-> NonEmpty (ParseError Text CustomErr))
-> NonEmpty (ParseError Text CustomErr)
-> NonEmpty (ParseError Text CustomErr)
forall a b. (a -> b) -> a -> b
$
ParseErrorBundle Text CustomErr
-> NonEmpty (ParseError Text CustomErr)
forall s e. ParseErrorBundle s e -> NonEmpty (ParseError s e)
bundleErrors ParseErrorBundle Text CustomErr
errBundle NonEmpty (ParseError Text CustomErr)
-> (ParseError Text CustomErr
-> NonEmpty (ParseError Text CustomErr))
-> NonEmpty (ParseError Text CustomErr)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ParseError Text CustomErr -> NonEmpty (ParseError Text CustomErr)
finalizeCustomError }
in ParseErrorBundle Text CustomErr -> String
forall s e.
(VisualStream s, TraversableStream s, ShowErrorComponent e) =>
ParseErrorBundle s e -> String
errorBundlePretty ParseErrorBundle Text CustomErr
errBundle'
where
finalizeCustomError
:: ParseError Text CustomErr -> NE.NonEmpty (ParseError Text CustomErr)
finalizeCustomError :: ParseError Text CustomErr -> NonEmpty (ParseError Text CustomErr)
finalizeCustomError ParseError Text CustomErr
err = case ParseError Text CustomErr -> Maybe CustomErr
findCustomError ParseError Text CustomErr
err of
Maybe CustomErr
Nothing -> ParseError Text CustomErr -> NonEmpty (ParseError Text CustomErr)
forall (f :: * -> *) a. Applicative f => a -> f a
pure ParseError Text CustomErr
err
Just errFailAt :: CustomErr
errFailAt@(ErrorFailAt Int
startOffset Int
_ String
_) ->
ParseError Text CustomErr -> NonEmpty (ParseError Text CustomErr)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (ParseError Text CustomErr -> NonEmpty (ParseError Text CustomErr))
-> ParseError Text CustomErr
-> NonEmpty (ParseError Text CustomErr)
forall a b. (a -> b) -> a -> b
$ Int -> Set (ErrorFancy CustomErr) -> ParseError Text CustomErr
forall s e. Int -> Set (ErrorFancy e) -> ParseError s e
FancyError Int
startOffset (Set (ErrorFancy CustomErr) -> ParseError Text CustomErr)
-> Set (ErrorFancy CustomErr) -> ParseError Text CustomErr
forall a b. (a -> b) -> a -> b
$ ErrorFancy CustomErr -> Set (ErrorFancy CustomErr)
forall a. a -> Set a
S.singleton (ErrorFancy CustomErr -> Set (ErrorFancy CustomErr))
-> ErrorFancy CustomErr -> Set (ErrorFancy CustomErr)
forall a b. (a -> b) -> a -> b
$ CustomErr -> ErrorFancy CustomErr
forall e. e -> ErrorFancy e
ErrorCustom CustomErr
errFailAt
Just (ErrorReparsing NonEmpty (ParseError Text CustomErr)
errs) ->
NonEmpty (ParseError Text CustomErr)
errs NonEmpty (ParseError Text CustomErr)
-> (ParseError Text CustomErr
-> NonEmpty (ParseError Text CustomErr))
-> NonEmpty (ParseError Text CustomErr)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ParseError Text CustomErr -> NonEmpty (ParseError Text CustomErr)
finalizeCustomError
findCustomError :: ParseError Text CustomErr -> Maybe CustomErr
findCustomError :: ParseError Text CustomErr -> Maybe CustomErr
findCustomError ParseError Text CustomErr
err = case ParseError Text CustomErr
err of
FancyError Int
_ Set (ErrorFancy CustomErr)
errSet ->
(ErrorFancy CustomErr -> Maybe CustomErr)
-> Set (ErrorFancy CustomErr) -> Maybe CustomErr
forall (t :: * -> *) a b.
Foldable t =>
(a -> Maybe b) -> t a -> Maybe b
finds (\case {ErrorCustom CustomErr
e -> CustomErr -> Maybe CustomErr
forall a. a -> Maybe a
Just CustomErr
e; ErrorFancy CustomErr
_ -> Maybe CustomErr
forall a. Maybe a
Nothing}) Set (ErrorFancy CustomErr)
errSet
ParseError Text CustomErr
_ -> Maybe CustomErr
forall a. Maybe a
Nothing
finds :: (Foldable t) => (a -> Maybe b) -> t a -> Maybe b
finds :: (a -> Maybe b) -> t a -> Maybe b
finds a -> Maybe b
f = [Maybe b] -> Maybe b
forall (t :: * -> *) (f :: * -> *) a.
(Foldable t, Alternative f) =>
t (f a) -> f a
asum ([Maybe b] -> Maybe b) -> (t a -> [Maybe b]) -> t a -> Maybe b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> Maybe b) -> [a] -> [Maybe b]
forall a b. (a -> b) -> [a] -> [b]
map a -> Maybe b
f ([a] -> [Maybe b]) -> (t a -> [a]) -> t a -> [Maybe b]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. t a -> [a]
forall (t :: * -> *) a. Foldable t => t a -> [a]
toList
data FinalParseError' e
= FinalError (ParseError Text e)
| FinalBundle (ParseErrorBundle Text e)
| FinalBundleWithStack (FinalParseErrorBundle' e)
deriving (Int -> FinalParseError' e -> ShowS
[FinalParseError' e] -> ShowS
FinalParseError' e -> String
(Int -> FinalParseError' e -> ShowS)
-> (FinalParseError' e -> String)
-> ([FinalParseError' e] -> ShowS)
-> Show (FinalParseError' e)
forall e. Show e => Int -> FinalParseError' e -> ShowS
forall e. Show e => [FinalParseError' e] -> ShowS
forall e. Show e => FinalParseError' e -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [FinalParseError' e] -> ShowS
$cshowList :: forall e. Show e => [FinalParseError' e] -> ShowS
show :: FinalParseError' e -> String
$cshow :: forall e. Show e => FinalParseError' e -> String
showsPrec :: Int -> FinalParseError' e -> ShowS
$cshowsPrec :: forall e. Show e => Int -> FinalParseError' e -> ShowS
Show)
type FinalParseError = FinalParseError' CustomErr
instance Semigroup (FinalParseError' e) where
FinalParseError' e
e <> :: FinalParseError' e -> FinalParseError' e -> FinalParseError' e
<> FinalParseError' e
_ = FinalParseError' e
e
instance Monoid (FinalParseError' e) where
mempty :: FinalParseError' e
mempty = ParseError Text e -> FinalParseError' e
forall e. ParseError Text e -> FinalParseError' e
FinalError (ParseError Text e -> FinalParseError' e)
-> ParseError Text e -> FinalParseError' e
forall a b. (a -> b) -> a -> b
$ Int -> Set (ErrorFancy e) -> ParseError Text e
forall s e. Int -> Set (ErrorFancy e) -> ParseError s e
FancyError Int
0 (Set (ErrorFancy e) -> ParseError Text e)
-> Set (ErrorFancy e) -> ParseError Text e
forall a b. (a -> b) -> a -> b
$
ErrorFancy e -> Set (ErrorFancy e)
forall a. a -> Set a
S.singleton (String -> ErrorFancy e
forall e. String -> ErrorFancy e
ErrorFail String
"default parse error")
mappend :: FinalParseError' e -> FinalParseError' e -> FinalParseError' e
mappend = FinalParseError' e -> FinalParseError' e -> FinalParseError' e
forall a. Semigroup a => a -> a -> a
(<>)
data FinalParseErrorBundle' e = FinalParseErrorBundle'
{ FinalParseErrorBundle' e -> ParseErrorBundle Text e
finalErrorBundle :: ParseErrorBundle Text e
, FinalParseErrorBundle' e -> [String]
includeFileStack :: [FilePath]
} deriving (Int -> FinalParseErrorBundle' e -> ShowS
[FinalParseErrorBundle' e] -> ShowS
FinalParseErrorBundle' e -> String
(Int -> FinalParseErrorBundle' e -> ShowS)
-> (FinalParseErrorBundle' e -> String)
-> ([FinalParseErrorBundle' e] -> ShowS)
-> Show (FinalParseErrorBundle' e)
forall e. Show e => Int -> FinalParseErrorBundle' e -> ShowS
forall e. Show e => [FinalParseErrorBundle' e] -> ShowS
forall e. Show e => FinalParseErrorBundle' e -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [FinalParseErrorBundle' e] -> ShowS
$cshowList :: forall e. Show e => [FinalParseErrorBundle' e] -> ShowS
show :: FinalParseErrorBundle' e -> String
$cshow :: forall e. Show e => FinalParseErrorBundle' e -> String
showsPrec :: Int -> FinalParseErrorBundle' e -> ShowS
$cshowsPrec :: forall e. Show e => Int -> FinalParseErrorBundle' e -> ShowS
Show)
type FinalParseErrorBundle = FinalParseErrorBundle' CustomErr
finalError :: ParseError Text e -> FinalParseError' e
finalError :: ParseError Text e -> FinalParseError' e
finalError = ParseError Text e -> FinalParseError' e
forall e. ParseError Text e -> FinalParseError' e
FinalError
finalFancyFailure
:: (MonadParsec e s m, MonadError (FinalParseError' e) m)
=> S.Set (ErrorFancy e) -> m a
finalFancyFailure :: Set (ErrorFancy e) -> m a
finalFancyFailure Set (ErrorFancy e)
errSet = do
Int
offset <- m Int
forall e s (m :: * -> *). MonadParsec e s m => m Int
getOffset
FinalParseError' e -> m a
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (FinalParseError' e -> m a) -> FinalParseError' e -> m a
forall a b. (a -> b) -> a -> b
$ ParseError Text e -> FinalParseError' e
forall e. ParseError Text e -> FinalParseError' e
FinalError (ParseError Text e -> FinalParseError' e)
-> ParseError Text e -> FinalParseError' e
forall a b. (a -> b) -> a -> b
$ Int -> Set (ErrorFancy e) -> ParseError Text e
forall s e. Int -> Set (ErrorFancy e) -> ParseError s e
FancyError Int
offset Set (ErrorFancy e)
errSet
finalFail
:: (MonadParsec e s m, MonadError (FinalParseError' e) m) => String -> m a
finalFail :: String -> m a
finalFail = Set (ErrorFancy e) -> m a
forall e s (m :: * -> *) a.
(MonadParsec e s m, MonadError (FinalParseError' e) m) =>
Set (ErrorFancy e) -> m a
finalFancyFailure (Set (ErrorFancy e) -> m a)
-> (String -> Set (ErrorFancy e)) -> String -> m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ErrorFancy e -> Set (ErrorFancy e)
forall a. a -> Set a
S.singleton (ErrorFancy e -> Set (ErrorFancy e))
-> (String -> ErrorFancy e) -> String -> Set (ErrorFancy e)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> ErrorFancy e
forall e. String -> ErrorFancy e
ErrorFail
finalCustomFailure
:: (MonadParsec e s m, MonadError (FinalParseError' e) m) => e -> m a
finalCustomFailure :: e -> m a
finalCustomFailure = Set (ErrorFancy e) -> m a
forall e s (m :: * -> *) a.
(MonadParsec e s m, MonadError (FinalParseError' e) m) =>
Set (ErrorFancy e) -> m a
finalFancyFailure (Set (ErrorFancy e) -> m a)
-> (e -> Set (ErrorFancy e)) -> e -> m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ErrorFancy e -> Set (ErrorFancy e)
forall a. a -> Set a
S.singleton (ErrorFancy e -> Set (ErrorFancy e))
-> (e -> ErrorFancy e) -> e -> Set (ErrorFancy e)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. e -> ErrorFancy e
forall e. e -> ErrorFancy e
ErrorCustom
finalErrorBundlePretty :: FinalParseErrorBundle' CustomErr -> String
finalErrorBundlePretty :: FinalParseErrorBundle' CustomErr -> String
finalErrorBundlePretty FinalParseErrorBundle' CustomErr
bundle =
ShowS -> [String] -> String
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap ShowS
showIncludeFilepath (FinalParseErrorBundle' CustomErr -> [String]
forall e. FinalParseErrorBundle' e -> [String]
includeFileStack FinalParseErrorBundle' CustomErr
bundle)
String -> ShowS
forall a. Semigroup a => a -> a -> a
<> ParseErrorBundle Text CustomErr -> String
customErrorBundlePretty (FinalParseErrorBundle' CustomErr -> ParseErrorBundle Text CustomErr
forall e. FinalParseErrorBundle' e -> ParseErrorBundle Text e
finalErrorBundle FinalParseErrorBundle' CustomErr
bundle)
where
showIncludeFilepath :: ShowS
showIncludeFilepath String
path = String
"in file included from " String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
path String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
",\n"
attachSource
:: FilePath -> Text -> FinalParseError' e -> FinalParseErrorBundle' e
attachSource :: String -> Text -> FinalParseError' e -> FinalParseErrorBundle' e
attachSource String
filePath Text
sourceText FinalParseError' e
finalParseError = case FinalParseError' e
finalParseError of
FinalError ParseError Text e
parseError ->
let bundle :: ParseErrorBundle Text e
bundle = ParseErrorBundle :: forall s e.
NonEmpty (ParseError s e) -> PosState s -> ParseErrorBundle s e
ParseErrorBundle
{ bundleErrors :: NonEmpty (ParseError Text e)
bundleErrors = ParseError Text e
parseError ParseError Text e
-> [ParseError Text e] -> NonEmpty (ParseError Text e)
forall a. a -> [a] -> NonEmpty a
NE.:| []
, bundlePosState :: PosState Text
bundlePosState = String -> Text -> PosState Text
initialPosState String
filePath Text
sourceText }
in FinalParseErrorBundle' :: forall e.
ParseErrorBundle Text e -> [String] -> FinalParseErrorBundle' e
FinalParseErrorBundle'
{ finalErrorBundle :: ParseErrorBundle Text e
finalErrorBundle = ParseErrorBundle Text e
bundle
, includeFileStack :: [String]
includeFileStack = [] }
FinalBundle ParseErrorBundle Text e
peBundle -> FinalParseErrorBundle' :: forall e.
ParseErrorBundle Text e -> [String] -> FinalParseErrorBundle' e
FinalParseErrorBundle'
{ finalErrorBundle :: ParseErrorBundle Text e
finalErrorBundle = ParseErrorBundle Text e
peBundle
, includeFileStack :: [String]
includeFileStack = [] }
FinalBundleWithStack FinalParseErrorBundle' e
fpeBundle -> FinalParseErrorBundle' e
fpeBundle
{ includeFileStack :: [String]
includeFileStack = String
filePath String -> [String] -> [String]
forall a. a -> [a] -> [a]
: FinalParseErrorBundle' e -> [String]
forall e. FinalParseErrorBundle' e -> [String]
includeFileStack FinalParseErrorBundle' e
fpeBundle }
parseIncludeFile
:: Monad m
=> StateT st (ParsecT CustomErr Text (ExceptT FinalParseError m)) a
-> st
-> FilePath
-> Text
-> StateT st (ParsecT CustomErr Text (ExceptT FinalParseError m)) a
parseIncludeFile :: StateT st (ParsecT CustomErr Text (ExceptT FinalParseError m)) a
-> st
-> String
-> Text
-> StateT st (ParsecT CustomErr Text (ExceptT FinalParseError m)) a
parseIncludeFile StateT st (ParsecT CustomErr Text (ExceptT FinalParseError m)) a
parser st
initialState String
filepath Text
text =
StateT st (ParsecT CustomErr Text (ExceptT FinalParseError m)) a
-> (FinalParseError
-> StateT
st (ParsecT CustomErr Text (ExceptT FinalParseError m)) a)
-> StateT st (ParsecT CustomErr Text (ExceptT FinalParseError m)) a
forall e (m :: * -> *) a.
MonadError e m =>
m a -> (e -> m a) -> m a
catchError StateT st (ParsecT CustomErr Text (ExceptT FinalParseError m)) a
parser' FinalParseError
-> StateT st (ParsecT CustomErr Text (ExceptT FinalParseError m)) a
forall e (m :: * -> *) a.
MonadError (FinalParseError' e) m =>
FinalParseError' e -> m a
handler
where
parser' :: StateT st (ParsecT CustomErr Text (ExceptT FinalParseError m)) a
parser' = do
Either (ParseErrorBundle Text CustomErr) a
eResult <- ParsecT
CustomErr
Text
(ExceptT FinalParseError m)
(Either (ParseErrorBundle Text CustomErr) a)
-> StateT
st
(ParsecT CustomErr Text (ExceptT FinalParseError m))
(Either (ParseErrorBundle Text CustomErr) a)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (ParsecT
CustomErr
Text
(ExceptT FinalParseError m)
(Either (ParseErrorBundle Text CustomErr) a)
-> StateT
st
(ParsecT CustomErr Text (ExceptT FinalParseError m))
(Either (ParseErrorBundle Text CustomErr) a))
-> ParsecT
CustomErr
Text
(ExceptT FinalParseError m)
(Either (ParseErrorBundle Text CustomErr) a)
-> StateT
st
(ParsecT CustomErr Text (ExceptT FinalParseError m))
(Either (ParseErrorBundle Text CustomErr) a)
forall a b. (a -> b) -> a -> b
$ ExceptT
FinalParseError m (Either (ParseErrorBundle Text CustomErr) a)
-> ParsecT
CustomErr
Text
(ExceptT FinalParseError m)
(Either (ParseErrorBundle Text CustomErr) a)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (ExceptT
FinalParseError m (Either (ParseErrorBundle Text CustomErr) a)
-> ParsecT
CustomErr
Text
(ExceptT FinalParseError m)
(Either (ParseErrorBundle Text CustomErr) a))
-> ExceptT
FinalParseError m (Either (ParseErrorBundle Text CustomErr) a)
-> ParsecT
CustomErr
Text
(ExceptT FinalParseError m)
(Either (ParseErrorBundle Text CustomErr) a)
forall a b. (a -> b) -> a -> b
$
ParsecT CustomErr Text (ExceptT FinalParseError m) a
-> String
-> Text
-> ExceptT
FinalParseError m (Either (ParseErrorBundle Text CustomErr) a)
forall (m :: * -> *) e s a.
Monad m =>
ParsecT e s m a
-> String -> s -> m (Either (ParseErrorBundle s e) a)
runParserT (StateT st (ParsecT CustomErr Text (ExceptT FinalParseError m)) a
-> st -> ParsecT CustomErr Text (ExceptT FinalParseError m) a
forall (m :: * -> *) s a. Monad m => StateT s m a -> s -> m a
evalStateT StateT st (ParsecT CustomErr Text (ExceptT FinalParseError m)) a
parser st
initialState) String
filepath Text
text
case Either (ParseErrorBundle Text CustomErr) a
eResult of
Left ParseErrorBundle Text CustomErr
parseErrorBundle -> FinalParseError
-> StateT st (ParsecT CustomErr Text (ExceptT FinalParseError m)) a
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (FinalParseError
-> StateT
st (ParsecT CustomErr Text (ExceptT FinalParseError m)) a)
-> FinalParseError
-> StateT st (ParsecT CustomErr Text (ExceptT FinalParseError m)) a
forall a b. (a -> b) -> a -> b
$ ParseErrorBundle Text CustomErr -> FinalParseError
forall e. ParseErrorBundle Text e -> FinalParseError' e
FinalBundle ParseErrorBundle Text CustomErr
parseErrorBundle
Right a
result -> a
-> StateT st (ParsecT CustomErr Text (ExceptT FinalParseError m)) a
forall (f :: * -> *) a. Applicative f => a -> f a
pure a
result
handler :: FinalParseError' e -> m a
handler FinalParseError' e
e = FinalParseError' e -> m a
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (FinalParseError' e -> m a) -> FinalParseError' e -> m a
forall a b. (a -> b) -> a -> b
$ FinalParseErrorBundle' e -> FinalParseError' e
forall e. FinalParseErrorBundle' e -> FinalParseError' e
FinalBundleWithStack (FinalParseErrorBundle' e -> FinalParseError' e)
-> FinalParseErrorBundle' e -> FinalParseError' e
forall a b. (a -> b) -> a -> b
$ String -> Text -> FinalParseError' e -> FinalParseErrorBundle' e
forall e.
String -> Text -> FinalParseError' e -> FinalParseErrorBundle' e
attachSource String
filepath Text
text FinalParseError' e
e
initialPosState :: FilePath -> Text -> PosState Text
initialPosState :: String -> Text -> PosState Text
initialPosState String
filePath Text
sourceText = PosState :: forall s. s -> Int -> SourcePos -> Pos -> String -> PosState s
PosState
{ pstateInput :: Text
pstateInput = Text
sourceText
, pstateOffset :: Int
pstateOffset = Int
0
, pstateSourcePos :: SourcePos
pstateSourcePos = String -> SourcePos
initialPos String
filePath
, pstateTabWidth :: Pos
pstateTabWidth = Pos
defaultTabWidth
, pstateLinePrefix :: String
pstateLinePrefix = String
"" }