{-# LANGUAGE CPP, DeriveDataTypeable #-}
module Language.Python.Common.SrcLocation (
SrcLocation (..),
SrcSpan (..),
Span (..),
spanning,
mkSrcSpan,
combineSrcSpans,
initialSrcLocation,
spanStartPoint,
incColumn,
decColumn,
incLine,
incTab,
endCol,
endRow,
startCol,
startRow
) where
#if __GLASGOW_HASKELL__ >= 803
import Prelude hiding ((<>))
#endif
import Language.Python.Common.Pretty
import Data.Data
data SrcLocation =
Sloc { SrcLocation -> String
sloc_filename :: !String
, SrcLocation -> Int
sloc_row :: {-# UNPACK #-} !Int
, SrcLocation -> Int
sloc_column :: {-# UNPACK #-} !Int
}
| NoLocation
deriving (SrcLocation -> SrcLocation -> Bool
(SrcLocation -> SrcLocation -> Bool)
-> (SrcLocation -> SrcLocation -> Bool) -> Eq SrcLocation
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: SrcLocation -> SrcLocation -> Bool
== :: SrcLocation -> SrcLocation -> Bool
$c/= :: SrcLocation -> SrcLocation -> Bool
/= :: SrcLocation -> SrcLocation -> Bool
Eq,Eq SrcLocation
Eq SrcLocation =>
(SrcLocation -> SrcLocation -> Ordering)
-> (SrcLocation -> SrcLocation -> Bool)
-> (SrcLocation -> SrcLocation -> Bool)
-> (SrcLocation -> SrcLocation -> Bool)
-> (SrcLocation -> SrcLocation -> Bool)
-> (SrcLocation -> SrcLocation -> SrcLocation)
-> (SrcLocation -> SrcLocation -> SrcLocation)
-> Ord SrcLocation
SrcLocation -> SrcLocation -> Bool
SrcLocation -> SrcLocation -> Ordering
SrcLocation -> SrcLocation -> SrcLocation
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
$ccompare :: SrcLocation -> SrcLocation -> Ordering
compare :: SrcLocation -> SrcLocation -> Ordering
$c< :: SrcLocation -> SrcLocation -> Bool
< :: SrcLocation -> SrcLocation -> Bool
$c<= :: SrcLocation -> SrcLocation -> Bool
<= :: SrcLocation -> SrcLocation -> Bool
$c> :: SrcLocation -> SrcLocation -> Bool
> :: SrcLocation -> SrcLocation -> Bool
$c>= :: SrcLocation -> SrcLocation -> Bool
>= :: SrcLocation -> SrcLocation -> Bool
$cmax :: SrcLocation -> SrcLocation -> SrcLocation
max :: SrcLocation -> SrcLocation -> SrcLocation
$cmin :: SrcLocation -> SrcLocation -> SrcLocation
min :: SrcLocation -> SrcLocation -> SrcLocation
Ord,Int -> SrcLocation -> ShowS
[SrcLocation] -> ShowS
SrcLocation -> String
(Int -> SrcLocation -> ShowS)
-> (SrcLocation -> String)
-> ([SrcLocation] -> ShowS)
-> Show SrcLocation
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> SrcLocation -> ShowS
showsPrec :: Int -> SrcLocation -> ShowS
$cshow :: SrcLocation -> String
show :: SrcLocation -> String
$cshowList :: [SrcLocation] -> ShowS
showList :: [SrcLocation] -> ShowS
Show,Typeable,Typeable SrcLocation
Typeable SrcLocation =>
(forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> SrcLocation -> c SrcLocation)
-> (forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c SrcLocation)
-> (SrcLocation -> Constr)
-> (SrcLocation -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c SrcLocation))
-> (forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c SrcLocation))
-> ((forall b. Data b => b -> b) -> SrcLocation -> SrcLocation)
-> (forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> SrcLocation -> r)
-> (forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> SrcLocation -> r)
-> (forall u. (forall d. Data d => d -> u) -> SrcLocation -> [u])
-> (forall u.
Int -> (forall d. Data d => d -> u) -> SrcLocation -> u)
-> (forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> SrcLocation -> m SrcLocation)
-> (forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> SrcLocation -> m SrcLocation)
-> (forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> SrcLocation -> m SrcLocation)
-> Data SrcLocation
SrcLocation -> Constr
SrcLocation -> DataType
(forall b. Data b => b -> b) -> SrcLocation -> SrcLocation
forall a.
Typeable a =>
(forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> a -> c a)
-> (forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c a)
-> (a -> Constr)
-> (a -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c a))
-> (forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c a))
-> ((forall b. Data b => b -> b) -> a -> a)
-> (forall r r'.
(r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall r r'.
(r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall u. (forall d. Data d => d -> u) -> a -> [u])
-> (forall u. Int -> (forall d. Data d => d -> u) -> a -> u)
-> (forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> a -> m a)
-> Data a
forall u. Int -> (forall d. Data d => d -> u) -> SrcLocation -> u
forall u. (forall d. Data d => d -> u) -> SrcLocation -> [u]
forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> SrcLocation -> r
forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> SrcLocation -> r
forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> SrcLocation -> m SrcLocation
forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> SrcLocation -> m SrcLocation
forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c SrcLocation
forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> SrcLocation -> c SrcLocation
forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c SrcLocation)
forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c SrcLocation)
$cgfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> SrcLocation -> c SrcLocation
gfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> SrcLocation -> c SrcLocation
$cgunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c SrcLocation
gunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c SrcLocation
$ctoConstr :: SrcLocation -> Constr
toConstr :: SrcLocation -> Constr
$cdataTypeOf :: SrcLocation -> DataType
dataTypeOf :: SrcLocation -> DataType
$cdataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c SrcLocation)
dataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c SrcLocation)
$cdataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c SrcLocation)
dataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c SrcLocation)
$cgmapT :: (forall b. Data b => b -> b) -> SrcLocation -> SrcLocation
gmapT :: (forall b. Data b => b -> b) -> SrcLocation -> SrcLocation
$cgmapQl :: forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> SrcLocation -> r
gmapQl :: forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> SrcLocation -> r
$cgmapQr :: forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> SrcLocation -> r
gmapQr :: forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> SrcLocation -> r
$cgmapQ :: forall u. (forall d. Data d => d -> u) -> SrcLocation -> [u]
gmapQ :: forall u. (forall d. Data d => d -> u) -> SrcLocation -> [u]
$cgmapQi :: forall u. Int -> (forall d. Data d => d -> u) -> SrcLocation -> u
gmapQi :: forall u. Int -> (forall d. Data d => d -> u) -> SrcLocation -> u
$cgmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> SrcLocation -> m SrcLocation
gmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> SrcLocation -> m SrcLocation
$cgmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> SrcLocation -> m SrcLocation
gmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> SrcLocation -> m SrcLocation
$cgmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> SrcLocation -> m SrcLocation
gmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> SrcLocation -> m SrcLocation
Data)
instance Pretty SrcLocation where
pretty :: SrcLocation -> Doc
pretty = SrcSpan -> Doc
forall a. Pretty a => a -> Doc
pretty (SrcSpan -> Doc) -> (SrcLocation -> SrcSpan) -> SrcLocation -> Doc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SrcLocation -> SrcSpan
forall a. Span a => a -> SrcSpan
getSpan
class Span a where
getSpan :: a -> SrcSpan
getSpan a
x = SrcSpan
SpanEmpty
spanning :: (Span a, Span b) => a -> b -> SrcSpan
spanning :: forall a b. (Span a, Span b) => a -> b -> SrcSpan
spanning a
x b
y = SrcSpan -> SrcSpan -> SrcSpan
combineSrcSpans (a -> SrcSpan
forall a. Span a => a -> SrcSpan
getSpan a
x) (b -> SrcSpan
forall a. Span a => a -> SrcSpan
getSpan b
y)
instance Span a => Span [a] where
getSpan :: [a] -> SrcSpan
getSpan [] = SrcSpan
SpanEmpty
getSpan [a
x] = a -> SrcSpan
forall a. Span a => a -> SrcSpan
getSpan a
x
getSpan list :: [a]
list@(a
x:[a]
xs) = SrcSpan -> SrcSpan -> SrcSpan
combineSrcSpans (a -> SrcSpan
forall a. Span a => a -> SrcSpan
getSpan a
x) (a -> SrcSpan
forall a. Span a => a -> SrcSpan
getSpan ([a] -> a
forall a. HasCallStack => [a] -> a
last [a]
list))
instance Span a => Span (Maybe a) where
getSpan :: Maybe a -> SrcSpan
getSpan Maybe a
Nothing = SrcSpan
SpanEmpty
getSpan (Just a
x) = a -> SrcSpan
forall a. Span a => a -> SrcSpan
getSpan a
x
instance (Span a, Span b) => Span (Either a b) where
getSpan :: Either a b -> SrcSpan
getSpan (Left a
x) = a -> SrcSpan
forall a. Span a => a -> SrcSpan
getSpan a
x
getSpan (Right b
x) = b -> SrcSpan
forall a. Span a => a -> SrcSpan
getSpan b
x
instance (Span a, Span b) => Span (a, b) where
getSpan :: (a, b) -> SrcSpan
getSpan (a
x,b
y) = a -> b -> SrcSpan
forall a b. (Span a, Span b) => a -> b -> SrcSpan
spanning a
x b
y
instance Span SrcSpan where
getSpan :: SrcSpan -> SrcSpan
getSpan = SrcSpan -> SrcSpan
forall a. a -> a
id
initialSrcLocation :: String -> SrcLocation
initialSrcLocation :: String -> SrcLocation
initialSrcLocation String
filename
= Sloc
{ sloc_filename :: String
sloc_filename = String
filename
, sloc_row :: Int
sloc_row = Int
1
, sloc_column :: Int
sloc_column = Int
1
}
decColumn :: Int -> SrcLocation -> SrcLocation
decColumn :: Int -> SrcLocation -> SrcLocation
decColumn Int
n SrcLocation
loc
| Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
col = SrcLocation
loc { sloc_column = col - n }
| Bool
otherwise = SrcLocation
loc
where
col :: Int
col = SrcLocation -> Int
sloc_column SrcLocation
loc
incColumn :: Int -> SrcLocation -> SrcLocation
incColumn :: Int -> SrcLocation -> SrcLocation
incColumn Int
n loc :: SrcLocation
loc@(Sloc { sloc_column :: SrcLocation -> Int
sloc_column = Int
col })
= SrcLocation
loc { sloc_column = col + n }
incColumn Int
_ SrcLocation
NoLocation = SrcLocation
NoLocation
incTab :: SrcLocation -> SrcLocation
incTab :: SrcLocation -> SrcLocation
incTab loc :: SrcLocation
loc@(Sloc { sloc_column :: SrcLocation -> Int
sloc_column = Int
col })
= SrcLocation
loc { sloc_column = newCol }
where
newCol :: Int
newCol = Int
col Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
8 Int -> Int -> Int
forall a. Num a => a -> a -> a
- (Int
col Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) Int -> Int -> Int
forall a. Integral a => a -> a -> a
`mod` Int
8
incTab SrcLocation
NoLocation = SrcLocation
NoLocation
incLine :: Int -> SrcLocation -> SrcLocation
incLine :: Int -> SrcLocation -> SrcLocation
incLine Int
n loc :: SrcLocation
loc@(Sloc { sloc_row :: SrcLocation -> Int
sloc_row = Int
row })
= SrcLocation
loc { sloc_column = 1, sloc_row = row + n }
incLine Int
_ SrcLocation
NoLocation = SrcLocation
NoLocation
data SrcSpan
= SpanCoLinear
{ SrcSpan -> String
span_filename :: !String
, SrcSpan -> Int
span_row :: {-# UNPACK #-} !Int
, SrcSpan -> Int
span_start_column :: {-# UNPACK #-} !Int
, SrcSpan -> Int
span_end_column :: {-# UNPACK #-} !Int
}
| SpanMultiLine
{ span_filename :: !String
, SrcSpan -> Int
span_start_row :: {-# UNPACK #-} !Int
, span_start_column :: {-# UNPACK #-} !Int
, SrcSpan -> Int
span_end_row :: {-# UNPACK #-} !Int
, span_end_column :: {-# UNPACK #-} !Int
}
| SpanPoint
{ span_filename :: !String
, span_row :: {-# UNPACK #-} !Int
, SrcSpan -> Int
span_column :: {-# UNPACK #-} !Int
}
| SpanEmpty
deriving (SrcSpan -> SrcSpan -> Bool
(SrcSpan -> SrcSpan -> Bool)
-> (SrcSpan -> SrcSpan -> Bool) -> Eq SrcSpan
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: SrcSpan -> SrcSpan -> Bool
== :: SrcSpan -> SrcSpan -> Bool
$c/= :: SrcSpan -> SrcSpan -> Bool
/= :: SrcSpan -> SrcSpan -> Bool
Eq,Eq SrcSpan
Eq SrcSpan =>
(SrcSpan -> SrcSpan -> Ordering)
-> (SrcSpan -> SrcSpan -> Bool)
-> (SrcSpan -> SrcSpan -> Bool)
-> (SrcSpan -> SrcSpan -> Bool)
-> (SrcSpan -> SrcSpan -> Bool)
-> (SrcSpan -> SrcSpan -> SrcSpan)
-> (SrcSpan -> SrcSpan -> SrcSpan)
-> Ord SrcSpan
SrcSpan -> SrcSpan -> Bool
SrcSpan -> SrcSpan -> Ordering
SrcSpan -> SrcSpan -> SrcSpan
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
$ccompare :: SrcSpan -> SrcSpan -> Ordering
compare :: SrcSpan -> SrcSpan -> Ordering
$c< :: SrcSpan -> SrcSpan -> Bool
< :: SrcSpan -> SrcSpan -> Bool
$c<= :: SrcSpan -> SrcSpan -> Bool
<= :: SrcSpan -> SrcSpan -> Bool
$c> :: SrcSpan -> SrcSpan -> Bool
> :: SrcSpan -> SrcSpan -> Bool
$c>= :: SrcSpan -> SrcSpan -> Bool
>= :: SrcSpan -> SrcSpan -> Bool
$cmax :: SrcSpan -> SrcSpan -> SrcSpan
max :: SrcSpan -> SrcSpan -> SrcSpan
$cmin :: SrcSpan -> SrcSpan -> SrcSpan
min :: SrcSpan -> SrcSpan -> SrcSpan
Ord,Int -> SrcSpan -> ShowS
[SrcSpan] -> ShowS
SrcSpan -> String
(Int -> SrcSpan -> ShowS)
-> (SrcSpan -> String) -> ([SrcSpan] -> ShowS) -> Show SrcSpan
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> SrcSpan -> ShowS
showsPrec :: Int -> SrcSpan -> ShowS
$cshow :: SrcSpan -> String
show :: SrcSpan -> String
$cshowList :: [SrcSpan] -> ShowS
showList :: [SrcSpan] -> ShowS
Show,Typeable,Typeable SrcSpan
Typeable SrcSpan =>
(forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> SrcSpan -> c SrcSpan)
-> (forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c SrcSpan)
-> (SrcSpan -> Constr)
-> (SrcSpan -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c SrcSpan))
-> (forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c SrcSpan))
-> ((forall b. Data b => b -> b) -> SrcSpan -> SrcSpan)
-> (forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> SrcSpan -> r)
-> (forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> SrcSpan -> r)
-> (forall u. (forall d. Data d => d -> u) -> SrcSpan -> [u])
-> (forall u. Int -> (forall d. Data d => d -> u) -> SrcSpan -> u)
-> (forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> SrcSpan -> m SrcSpan)
-> (forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> SrcSpan -> m SrcSpan)
-> (forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> SrcSpan -> m SrcSpan)
-> Data SrcSpan
SrcSpan -> Constr
SrcSpan -> DataType
(forall b. Data b => b -> b) -> SrcSpan -> SrcSpan
forall a.
Typeable a =>
(forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> a -> c a)
-> (forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c a)
-> (a -> Constr)
-> (a -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c a))
-> (forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c a))
-> ((forall b. Data b => b -> b) -> a -> a)
-> (forall r r'.
(r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall r r'.
(r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall u. (forall d. Data d => d -> u) -> a -> [u])
-> (forall u. Int -> (forall d. Data d => d -> u) -> a -> u)
-> (forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> a -> m a)
-> Data a
forall u. Int -> (forall d. Data d => d -> u) -> SrcSpan -> u
forall u. (forall d. Data d => d -> u) -> SrcSpan -> [u]
forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> SrcSpan -> r
forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> SrcSpan -> r
forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> SrcSpan -> m SrcSpan
forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> SrcSpan -> m SrcSpan
forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c SrcSpan
forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> SrcSpan -> c SrcSpan
forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c SrcSpan)
forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c SrcSpan)
$cgfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> SrcSpan -> c SrcSpan
gfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> SrcSpan -> c SrcSpan
$cgunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c SrcSpan
gunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c SrcSpan
$ctoConstr :: SrcSpan -> Constr
toConstr :: SrcSpan -> Constr
$cdataTypeOf :: SrcSpan -> DataType
dataTypeOf :: SrcSpan -> DataType
$cdataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c SrcSpan)
dataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c SrcSpan)
$cdataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c SrcSpan)
dataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c SrcSpan)
$cgmapT :: (forall b. Data b => b -> b) -> SrcSpan -> SrcSpan
gmapT :: (forall b. Data b => b -> b) -> SrcSpan -> SrcSpan
$cgmapQl :: forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> SrcSpan -> r
gmapQl :: forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> SrcSpan -> r
$cgmapQr :: forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> SrcSpan -> r
gmapQr :: forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> SrcSpan -> r
$cgmapQ :: forall u. (forall d. Data d => d -> u) -> SrcSpan -> [u]
gmapQ :: forall u. (forall d. Data d => d -> u) -> SrcSpan -> [u]
$cgmapQi :: forall u. Int -> (forall d. Data d => d -> u) -> SrcSpan -> u
gmapQi :: forall u. Int -> (forall d. Data d => d -> u) -> SrcSpan -> u
$cgmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> SrcSpan -> m SrcSpan
gmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> SrcSpan -> m SrcSpan
$cgmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> SrcSpan -> m SrcSpan
gmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> SrcSpan -> m SrcSpan
$cgmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> SrcSpan -> m SrcSpan
gmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> SrcSpan -> m SrcSpan
Data)
instance Pretty SrcSpan where
pretty :: SrcSpan -> Doc
pretty span :: SrcSpan
span@(SpanCoLinear {}) = SrcSpan -> Doc
prettyMultiSpan SrcSpan
span
pretty span :: SrcSpan
span@(SpanMultiLine {}) = SrcSpan -> Doc
prettyMultiSpan SrcSpan
span
pretty span :: SrcSpan
span@(SpanPoint {})
= String -> Doc
text (SrcSpan -> String
span_filename SrcSpan
span) Doc -> Doc -> Doc
<> Doc
colon Doc -> Doc -> Doc
<+>
Doc -> Doc
parens (Int -> Doc
forall a. Pretty a => a -> Doc
pretty (SrcSpan -> Int
span_row SrcSpan
span) Doc -> Doc -> Doc
<> Doc
comma Doc -> Doc -> Doc
<> Int -> Doc
forall a. Pretty a => a -> Doc
pretty (SrcSpan -> Int
span_column SrcSpan
span))
pretty SrcSpan
SpanEmpty = Doc
empty
prettyMultiSpan :: SrcSpan -> Doc
prettyMultiSpan :: SrcSpan -> Doc
prettyMultiSpan SrcSpan
span
= String -> Doc
text (SrcSpan -> String
span_filename SrcSpan
span) Doc -> Doc -> Doc
<> Doc
colon Doc -> Doc -> Doc
<+>
Doc -> Doc
parens (Int -> Doc
forall a. Pretty a => a -> Doc
pretty (SrcSpan -> Int
startRow SrcSpan
span) Doc -> Doc -> Doc
<> Doc
comma Doc -> Doc -> Doc
<> Int -> Doc
forall a. Pretty a => a -> Doc
pretty (SrcSpan -> Int
startCol SrcSpan
span)) Doc -> Doc -> Doc
<> Char -> Doc
char Char
'-' Doc -> Doc -> Doc
<>
Doc -> Doc
parens (Int -> Doc
forall a. Pretty a => a -> Doc
pretty (SrcSpan -> Int
endRow SrcSpan
span) Doc -> Doc -> Doc
<> Doc
comma Doc -> Doc -> Doc
<> Int -> Doc
forall a. Pretty a => a -> Doc
pretty (SrcSpan -> Int
endCol SrcSpan
span))
instance Span SrcLocation where
getSpan :: SrcLocation -> SrcSpan
getSpan loc :: SrcLocation
loc@(Sloc {})
= SpanPoint
{ span_filename :: String
span_filename = SrcLocation -> String
sloc_filename SrcLocation
loc
, span_row :: Int
span_row = SrcLocation -> Int
sloc_row SrcLocation
loc
, span_column :: Int
span_column = SrcLocation -> Int
sloc_column SrcLocation
loc
}
getSpan SrcLocation
NoLocation = SrcSpan
SpanEmpty
spanStartPoint :: SrcSpan -> SrcSpan
spanStartPoint :: SrcSpan -> SrcSpan
spanStartPoint SrcSpan
SpanEmpty = SrcSpan
SpanEmpty
spanStartPoint SrcSpan
span =
SpanPoint
{ span_filename :: String
span_filename = SrcSpan -> String
span_filename SrcSpan
span
, span_row :: Int
span_row = SrcSpan -> Int
startRow SrcSpan
span
, span_column :: Int
span_column = SrcSpan -> Int
startCol SrcSpan
span
}
mkSrcSpan :: SrcLocation -> SrcLocation -> SrcSpan
mkSrcSpan :: SrcLocation -> SrcLocation -> SrcSpan
mkSrcSpan SrcLocation
NoLocation SrcLocation
_ = SrcSpan
SpanEmpty
mkSrcSpan SrcLocation
_ SrcLocation
NoLocation = SrcSpan
SpanEmpty
mkSrcSpan SrcLocation
loc1 SrcLocation
loc2
| Int
line1 Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
line2 =
if Int
col2 Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
col1
then String -> Int -> Int -> SrcSpan
SpanPoint String
file Int
line1 Int
col1
else String -> Int -> Int -> Int -> SrcSpan
SpanCoLinear String
file Int
line1 Int
col1 Int
col2
| Bool
otherwise =
String -> Int -> Int -> Int -> Int -> SrcSpan
SpanMultiLine String
file Int
line1 Int
col1 Int
line2 Int
col2
where
line1 :: Int
line1 = SrcLocation -> Int
sloc_row SrcLocation
loc1
line2 :: Int
line2 = SrcLocation -> Int
sloc_row SrcLocation
loc2
col1 :: Int
col1 = SrcLocation -> Int
sloc_column SrcLocation
loc1
col2 :: Int
col2 = SrcLocation -> Int
sloc_column SrcLocation
loc2
file :: String
file = SrcLocation -> String
sloc_filename SrcLocation
loc1
combineSrcSpans :: SrcSpan -> SrcSpan -> SrcSpan
combineSrcSpans :: SrcSpan -> SrcSpan -> SrcSpan
combineSrcSpans SrcSpan
SpanEmpty SrcSpan
r = SrcSpan
r
combineSrcSpans SrcSpan
l SrcSpan
SpanEmpty = SrcSpan
l
combineSrcSpans SrcSpan
start SrcSpan
end
= case Int
row1 Int -> Int -> Ordering
forall a. Ord a => a -> a -> Ordering
`compare` Int
row2 of
Ordering
EQ -> case Int
col1 Int -> Int -> Ordering
forall a. Ord a => a -> a -> Ordering
`compare` Int
col2 of
Ordering
EQ -> String -> Int -> Int -> SrcSpan
SpanPoint String
file Int
row1 Int
col1
Ordering
LT -> String -> Int -> Int -> Int -> SrcSpan
SpanCoLinear String
file Int
row1 Int
col1 Int
col2
Ordering
GT -> String -> Int -> Int -> Int -> SrcSpan
SpanCoLinear String
file Int
row1 Int
col2 Int
col1
Ordering
LT -> String -> Int -> Int -> Int -> Int -> SrcSpan
SpanMultiLine String
file Int
row1 Int
col1 Int
row2 Int
col2
Ordering
GT -> String -> Int -> Int -> Int -> Int -> SrcSpan
SpanMultiLine String
file Int
row2 Int
col2 Int
row1 Int
col1
where
row1 :: Int
row1 = SrcSpan -> Int
startRow SrcSpan
start
col1 :: Int
col1 = SrcSpan -> Int
startCol SrcSpan
start
row2 :: Int
row2 = SrcSpan -> Int
endRow SrcSpan
end
col2 :: Int
col2 = SrcSpan -> Int
endCol SrcSpan
end
file :: String
file = SrcSpan -> String
span_filename SrcSpan
start
startRow :: SrcSpan -> Int
startRow :: SrcSpan -> Int
startRow (SpanCoLinear { span_row :: SrcSpan -> Int
span_row = Int
row }) = Int
row
startRow (SpanMultiLine { span_start_row :: SrcSpan -> Int
span_start_row = Int
row }) = Int
row
startRow (SpanPoint { span_row :: SrcSpan -> Int
span_row = Int
row }) = Int
row
startRow SrcSpan
SpanEmpty = String -> Int
forall a. HasCallStack => String -> a
error String
"startRow called on empty span"
endRow :: SrcSpan -> Int
endRow :: SrcSpan -> Int
endRow (SpanCoLinear { span_row :: SrcSpan -> Int
span_row = Int
row }) = Int
row
endRow (SpanMultiLine { span_end_row :: SrcSpan -> Int
span_end_row = Int
row }) = Int
row
endRow (SpanPoint { span_row :: SrcSpan -> Int
span_row = Int
row }) = Int
row
endRow SrcSpan
SpanEmpty = String -> Int
forall a. HasCallStack => String -> a
error String
"endRow called on empty span"
startCol :: SrcSpan -> Int
startCol :: SrcSpan -> Int
startCol (SpanCoLinear { span_start_column :: SrcSpan -> Int
span_start_column = Int
col }) = Int
col
startCol (SpanMultiLine { span_start_column :: SrcSpan -> Int
span_start_column = Int
col }) = Int
col
startCol (SpanPoint { span_column :: SrcSpan -> Int
span_column = Int
col }) = Int
col
startCol SrcSpan
SpanEmpty = String -> Int
forall a. HasCallStack => String -> a
error String
"startCol called on empty span"
endCol :: SrcSpan -> Int
endCol :: SrcSpan -> Int
endCol (SpanCoLinear { span_end_column :: SrcSpan -> Int
span_end_column = Int
col }) = Int
col
endCol (SpanMultiLine { span_end_column :: SrcSpan -> Int
span_end_column = Int
col }) = Int
col
endCol (SpanPoint { span_column :: SrcSpan -> Int
span_column = Int
col }) = Int
col
endCol SrcSpan
SpanEmpty = String -> Int
forall a. HasCallStack => String -> a
error String
"endCol called on empty span"