{-# LANGUAGE DeriveGeneric #-}
module Distribution.Parsec.Common (
PError (..),
showPError,
PWarning (..),
PWarnType (..),
showPWarning,
Position (..),
incPos,
retPos,
showPos,
zeroPos,
) where
import Distribution.Compat.Prelude
import Prelude ()
import System.FilePath (normalise)
data PError = PError Position String
deriving (Show, Generic)
instance Binary PError
instance NFData PError where rnf = genericRnf
data PWarnType
= PWTOther
| PWTUTF
| PWTBoolCase
| PWTVersionTag
| PWTNewSyntax
| PWTOldSyntax
| PWTDeprecatedField
| PWTInvalidSubsection
| PWTUnknownField
| PWTUnknownSection
| PWTTrailingFields
| PWTExtraMainIs
| PWTExtraTestModule
| PWTExtraBenchmarkModule
| PWTLexNBSP
| PWTLexBOM
| PWTLexTab
| PWTQuirkyCabalFile
| PWTDoubleDash
| PWTMultipleSingularField
| PWTBuildTypeDefault
| PWTVersionLeadingZeros
deriving (Eq, Ord, Show, Enum, Bounded, Generic)
instance Binary PWarnType
instance NFData PWarnType where rnf = genericRnf
data PWarning = PWarning !PWarnType !Position String
deriving (Show, Generic)
instance Binary PWarning
instance NFData PWarning where rnf = genericRnf
showPWarning :: FilePath -> PWarning -> String
showPWarning fpath (PWarning _ pos msg) =
normalise fpath ++ ":" ++ showPos pos ++ ": " ++ msg
showPError :: FilePath -> PError -> String
showPError fpath (PError pos msg) =
normalise fpath ++ ":" ++ showPos pos ++ ": " ++ msg
data Position = Position
{-# UNPACK #-} !Int
{-# UNPACK #-} !Int
deriving (Eq, Ord, Show, Generic)
instance Binary Position
instance NFData Position where rnf = genericRnf
incPos :: Int -> Position -> Position
incPos n (Position row col) = Position row (col + n)
retPos :: Position -> Position
retPos (Position row _col) = Position (row + 1) 1
showPos :: Position -> String
showPos (Position row col) = show row ++ ":" ++ show col
zeroPos :: Position
zeroPos = Position 0 0