{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE TypeSynonymInstances #-}
{-# LANGUAGE Unsafe #-}
module Parser.TextParser (
module Text.Megaparsec,
module Text.Megaparsec.Char,
SourceContext,
TextParser,
getSourceContext,
runTextParser,
) where
import Data.Char (isSpace)
import Data.List (dropWhileEnd)
import Text.Megaparsec
import Text.Megaparsec.Char
import qualified Data.Set as Set
import Base.CompilerError
import Base.CompilerMessage
type TextParser = Parsec CompilerMessage String
instance ErrorContextM TextParser where
compilerErrorM :: forall a. String -> TextParser a
compilerErrorM = forall e s (m :: * -> *) a. MonadParsec e s m => e -> m a
customFailure forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> CompilerMessage
compilerMessage
withContextM :: forall a. TextParser a -> String -> TextParser a
withContextM TextParser a
x String
e = forall e s (m :: * -> *) a.
MonadParsec e s m =>
(ParseError s e -> ParseError s e) -> m a -> m a
region (forall e' e s.
Ord e' =>
(e -> e') -> ParseError s e -> ParseError s e'
mapParseError (String -> CompilerMessage -> CompilerMessage
pushErrorScope String
e) forall b c a. (b -> c) -> (a -> b) -> a -> c
. ParseError String CompilerMessage
-> ParseError String CompilerMessage
promoteError) TextParser a
x
summarizeErrorsM :: forall a. TextParser a -> String -> TextParser a
summarizeErrorsM TextParser a
x String
e = forall e s (m :: * -> *) a.
MonadParsec e s m =>
(ParseError s e -> ParseError s e) -> m a -> m a
region (forall e' e s.
Ord e' =>
(e -> e') -> ParseError s e -> ParseError s e'
mapParseError (String -> CompilerMessage -> CompilerMessage
pushErrorScope String
e) forall b c a. (b -> c) -> (a -> b) -> a -> c
. ParseError String CompilerMessage
-> ParseError String CompilerMessage
promoteError) TextParser a
x
instance ShowErrorComponent CompilerMessage where
showErrorComponent :: CompilerMessage -> String
showErrorComponent = forall a. Show a => a -> String
show
runTextParser :: ErrorContextM m => TextParser a -> String -> String -> m a
runTextParser :: forall (m :: * -> *) a.
ErrorContextM m =>
TextParser a -> String -> String -> m a
runTextParser TextParser a
p String
n String
s = case forall e s a.
Parsec e s a -> String -> s -> Either (ParseErrorBundle s e) a
parse TextParser a
p String
n String
s of
Left ParseErrorBundle String CompilerMessage
e -> forall (m :: * -> *) a. ErrorContextM m => String -> m a
compilerErrorM forall a b. (a -> b) -> a -> b
$ forall a. (a -> Bool) -> [a] -> [a]
dropWhileEnd Char -> Bool
isSpace forall a b. (a -> b) -> a -> b
$ forall s e.
(VisualStream s, TraversableStream s, ShowErrorComponent e) =>
ParseErrorBundle s e -> String
errorBundlePretty ParseErrorBundle String CompilerMessage
e
Right a
x -> forall (m :: * -> *) a. Monad m => a -> m a
return a
x
newtype SourceContext = SourceContext SourcePos deriving (SourceContext -> SourceContext -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: SourceContext -> SourceContext -> Bool
$c/= :: SourceContext -> SourceContext -> Bool
== :: SourceContext -> SourceContext -> Bool
$c== :: SourceContext -> SourceContext -> Bool
Eq,Eq SourceContext
SourceContext -> SourceContext -> Bool
SourceContext -> SourceContext -> Ordering
SourceContext -> SourceContext -> SourceContext
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 :: SourceContext -> SourceContext -> SourceContext
$cmin :: SourceContext -> SourceContext -> SourceContext
max :: SourceContext -> SourceContext -> SourceContext
$cmax :: SourceContext -> SourceContext -> SourceContext
>= :: SourceContext -> SourceContext -> Bool
$c>= :: SourceContext -> SourceContext -> Bool
> :: SourceContext -> SourceContext -> Bool
$c> :: SourceContext -> SourceContext -> Bool
<= :: SourceContext -> SourceContext -> Bool
$c<= :: SourceContext -> SourceContext -> Bool
< :: SourceContext -> SourceContext -> Bool
$c< :: SourceContext -> SourceContext -> Bool
compare :: SourceContext -> SourceContext -> Ordering
$ccompare :: SourceContext -> SourceContext -> Ordering
Ord)
instance Show SourceContext where
show :: SourceContext -> String
show (SourceContext (SourcePos String
f Pos
l Pos
c)) =
String
"line " forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show (Pos -> Int
unPos Pos
l) forall a. [a] -> [a] -> [a]
++ String
" column " forall a. [a] -> [a] -> [a]
++ forall a. Show a => a -> String
show (Pos -> Int
unPos Pos
c) forall a. [a] -> [a] -> [a]
++ String
" of " forall a. [a] -> [a] -> [a]
++ String
f
getSourceContext :: TextParser SourceContext
getSourceContext :: TextParser SourceContext
getSourceContext = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap SourcePos -> SourceContext
SourceContext forall s e (m :: * -> *).
(TraversableStream s, MonadParsec e s m) =>
m SourcePos
getSourcePos
promoteError :: ParseError String CompilerMessage -> ParseError String CompilerMessage
promoteError :: ParseError String CompilerMessage
-> ParseError String CompilerMessage
promoteError e :: ParseError String CompilerMessage
e@(TrivialError Int
i Maybe (ErrorItem (Token String))
_ Set (ErrorItem (Token String))
_) = forall s e. Int -> Set (ErrorFancy e) -> ParseError s e
FancyError Int
i forall a b. (a -> b) -> a -> b
$ forall a. Ord a => [a] -> Set a
Set.fromList [forall e. e -> ErrorFancy e
ErrorCustom forall a b. (a -> b) -> a -> b
$ String -> CompilerMessage
compilerMessage forall a b. (a -> b) -> a -> b
$ forall s e.
(VisualStream s, ShowErrorComponent e) =>
ParseError s e -> String
parseErrorTextPretty ParseError String CompilerMessage
e]
promoteError ParseError String CompilerMessage
e = ParseError String CompilerMessage
e