{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE DeriveGeneric #-}

module Distribution.Parsec.Error
  ( PError (..)
  , PErrorWithSource (..)
  , showPError
  , showPErrorWithSource
  ) where

import Distribution.Compat.Prelude
import Distribution.Parsec.Position
import Distribution.Parsec.Source
import Distribution.Parsec.Warning -- TODO: Move PSource into own module
import System.FilePath (normalise)
import Prelude ()

-- | Parser error.
data PError = PError {PError -> Position
perrorPosition :: Position, PError -> String
perrorMessage :: String}
  deriving (Int -> PError -> ShowS
[PError] -> ShowS
PError -> String
(Int -> PError -> ShowS)
-> (PError -> String) -> ([PError] -> ShowS) -> Show PError
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> PError -> ShowS
showsPrec :: Int -> PError -> ShowS
$cshow :: PError -> String
show :: PError -> String
$cshowList :: [PError] -> ShowS
showList :: [PError] -> ShowS
Show, (forall x. PError -> Rep PError x)
-> (forall x. Rep PError x -> PError) -> Generic PError
forall x. Rep PError x -> PError
forall x. PError -> Rep PError x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. PError -> Rep PError x
from :: forall x. PError -> Rep PError x
$cto :: forall x. Rep PError x -> PError
to :: forall x. Rep PError x -> PError
Generic)

data PErrorWithSource src = PErrorWithSource {forall src. PErrorWithSource src -> PSource src
perrorSource :: !(PSource src), forall src. PErrorWithSource src -> PError
perror :: !PError}
  deriving (Int -> PErrorWithSource src -> ShowS
[PErrorWithSource src] -> ShowS
PErrorWithSource src -> String
(Int -> PErrorWithSource src -> ShowS)
-> (PErrorWithSource src -> String)
-> ([PErrorWithSource src] -> ShowS)
-> Show (PErrorWithSource src)
forall src. Show src => Int -> PErrorWithSource src -> ShowS
forall src. Show src => [PErrorWithSource src] -> ShowS
forall src. Show src => PErrorWithSource src -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: forall src. Show src => Int -> PErrorWithSource src -> ShowS
showsPrec :: Int -> PErrorWithSource src -> ShowS
$cshow :: forall src. Show src => PErrorWithSource src -> String
show :: PErrorWithSource src -> String
$cshowList :: forall src. Show src => [PErrorWithSource src] -> ShowS
showList :: [PErrorWithSource src] -> ShowS
Show, (forall x. PErrorWithSource src -> Rep (PErrorWithSource src) x)
-> (forall x. Rep (PErrorWithSource src) x -> PErrorWithSource src)
-> Generic (PErrorWithSource src)
forall x. Rep (PErrorWithSource src) x -> PErrorWithSource src
forall x. PErrorWithSource src -> Rep (PErrorWithSource src) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall src x. Rep (PErrorWithSource src) x -> PErrorWithSource src
forall src x. PErrorWithSource src -> Rep (PErrorWithSource src) x
$cfrom :: forall src x. PErrorWithSource src -> Rep (PErrorWithSource src) x
from :: forall x. PErrorWithSource src -> Rep (PErrorWithSource src) x
$cto :: forall src x. Rep (PErrorWithSource src) x -> PErrorWithSource src
to :: forall x. Rep (PErrorWithSource src) x -> PErrorWithSource src
Generic, (forall a b. (a -> b) -> PErrorWithSource a -> PErrorWithSource b)
-> (forall a b. a -> PErrorWithSource b -> PErrorWithSource a)
-> Functor PErrorWithSource
forall a b. a -> PErrorWithSource b -> PErrorWithSource a
forall a b. (a -> b) -> PErrorWithSource a -> PErrorWithSource b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
$cfmap :: forall a b. (a -> b) -> PErrorWithSource a -> PErrorWithSource b
fmap :: forall a b. (a -> b) -> PErrorWithSource a -> PErrorWithSource b
$c<$ :: forall a b. a -> PErrorWithSource b -> PErrorWithSource a
<$ :: forall a b. a -> PErrorWithSource b -> PErrorWithSource a
Functor)

instance Binary PError
instance NFData PError

showPError :: FilePath -> PError -> String
showPError :: String -> PError -> String
showPError String
fpath (PError Position
pos String
msg) =
  ShowS
normalise String
fpath String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
":" String -> ShowS
forall a. [a] -> [a] -> [a]
++ Position -> String
showPos Position
pos String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
": " String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
msg

showPErrorWithSource :: PErrorWithSource String -> String
showPErrorWithSource :: PErrorWithSource String -> String
showPErrorWithSource (PErrorWithSource PSource String
source (PError Position
pos String
msg)) =
  String -> PError -> String
showPError (PSource String -> String
showPSourceAsFilePath PSource String
source) (Position -> String -> PError
PError Position
pos String
msg)