module LH.Data.SrcLoc (
LHsPos (..),
LHsSpan (..),
LHsLexeme (..),
Spannable (..), (><), (><*),
L (..)
) where
import Data.Foldable (foldl')
import Data.Sequence (Seq)
import LH.Text.Token (LHsToken)
data LHsPos = LHsPos
{ LHsPos -> Int
posLine :: !Int
, LHsPos -> Int
posCol :: !Int
}
deriving (LHsPos -> LHsPos -> Bool
(LHsPos -> LHsPos -> Bool)
-> (LHsPos -> LHsPos -> Bool) -> Eq LHsPos
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: LHsPos -> LHsPos -> Bool
== :: LHsPos -> LHsPos -> Bool
$c/= :: LHsPos -> LHsPos -> Bool
/= :: LHsPos -> LHsPos -> Bool
Eq, Eq LHsPos
Eq LHsPos =>
(LHsPos -> LHsPos -> Ordering)
-> (LHsPos -> LHsPos -> Bool)
-> (LHsPos -> LHsPos -> Bool)
-> (LHsPos -> LHsPos -> Bool)
-> (LHsPos -> LHsPos -> Bool)
-> (LHsPos -> LHsPos -> LHsPos)
-> (LHsPos -> LHsPos -> LHsPos)
-> Ord LHsPos
LHsPos -> LHsPos -> Bool
LHsPos -> LHsPos -> Ordering
LHsPos -> LHsPos -> LHsPos
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 :: LHsPos -> LHsPos -> Ordering
compare :: LHsPos -> LHsPos -> Ordering
$c< :: LHsPos -> LHsPos -> Bool
< :: LHsPos -> LHsPos -> Bool
$c<= :: LHsPos -> LHsPos -> Bool
<= :: LHsPos -> LHsPos -> Bool
$c> :: LHsPos -> LHsPos -> Bool
> :: LHsPos -> LHsPos -> Bool
$c>= :: LHsPos -> LHsPos -> Bool
>= :: LHsPos -> LHsPos -> Bool
$cmax :: LHsPos -> LHsPos -> LHsPos
max :: LHsPos -> LHsPos -> LHsPos
$cmin :: LHsPos -> LHsPos -> LHsPos
min :: LHsPos -> LHsPos -> LHsPos
Ord, Int -> LHsPos -> ShowS
[LHsPos] -> ShowS
LHsPos -> String
(Int -> LHsPos -> ShowS)
-> (LHsPos -> String) -> ([LHsPos] -> ShowS) -> Show LHsPos
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> LHsPos -> ShowS
showsPrec :: Int -> LHsPos -> ShowS
$cshow :: LHsPos -> String
show :: LHsPos -> String
$cshowList :: [LHsPos] -> ShowS
showList :: [LHsPos] -> ShowS
Show)
data LHsSpan = LHsSpan
{ LHsSpan -> LHsPos
spanStart :: !LHsPos
, LHsSpan -> LHsPos
spanEnd :: !LHsPos
}
deriving (LHsSpan -> LHsSpan -> Bool
(LHsSpan -> LHsSpan -> Bool)
-> (LHsSpan -> LHsSpan -> Bool) -> Eq LHsSpan
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: LHsSpan -> LHsSpan -> Bool
== :: LHsSpan -> LHsSpan -> Bool
$c/= :: LHsSpan -> LHsSpan -> Bool
/= :: LHsSpan -> LHsSpan -> Bool
Eq, Int -> LHsSpan -> ShowS
[LHsSpan] -> ShowS
LHsSpan -> String
(Int -> LHsSpan -> ShowS)
-> (LHsSpan -> String) -> ([LHsSpan] -> ShowS) -> Show LHsSpan
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> LHsSpan -> ShowS
showsPrec :: Int -> LHsSpan -> ShowS
$cshow :: LHsSpan -> String
show :: LHsSpan -> String
$cshowList :: [LHsSpan] -> ShowS
showList :: [LHsSpan] -> ShowS
Show)
data LHsLexeme = LHsLexeme !LHsSpan !LHsToken
deriving (LHsLexeme -> LHsLexeme -> Bool
(LHsLexeme -> LHsLexeme -> Bool)
-> (LHsLexeme -> LHsLexeme -> Bool) -> Eq LHsLexeme
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: LHsLexeme -> LHsLexeme -> Bool
== :: LHsLexeme -> LHsLexeme -> Bool
$c/= :: LHsLexeme -> LHsLexeme -> Bool
/= :: LHsLexeme -> LHsLexeme -> Bool
Eq, Int -> LHsLexeme -> ShowS
[LHsLexeme] -> ShowS
LHsLexeme -> String
(Int -> LHsLexeme -> ShowS)
-> (LHsLexeme -> String)
-> ([LHsLexeme] -> ShowS)
-> Show LHsLexeme
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> LHsLexeme -> ShowS
showsPrec :: Int -> LHsLexeme -> ShowS
$cshow :: LHsLexeme -> String
show :: LHsLexeme -> String
$cshowList :: [LHsLexeme] -> ShowS
showList :: [LHsLexeme] -> ShowS
Show)
class Spannable a where
toSpan :: a -> LHsSpan
instance Spannable LHsSpan where
toSpan :: LHsSpan -> LHsSpan
toSpan = LHsSpan -> LHsSpan
forall a. a -> a
id
instance Spannable (L a) where
toSpan :: L a -> LHsSpan
toSpan = L a -> LHsSpan
forall a. L a -> LHsSpan
loc
instance Spannable LHsLexeme where
toSpan :: LHsLexeme -> LHsSpan
toSpan (LHsLexeme LHsSpan
sp LHsToken
_) = LHsSpan
sp
infixl 3 ><
(><) :: (Spannable a, Spannable b) => a -> b -> LHsSpan
a
a >< :: forall a b. (Spannable a, Spannable b) => a -> b -> LHsSpan
>< b
b = LHsPos -> LHsPos -> LHsSpan
LHsSpan (LHsSpan -> LHsPos
spanStart (a -> LHsSpan
forall a. Spannable a => a -> LHsSpan
toSpan a
a)) (LHsSpan -> LHsPos
spanEnd (b -> LHsSpan
forall a. Spannable a => a -> LHsSpan
toSpan b
b))
infixl 4 ><*
(><*) :: (Spannable a, Spannable b) => a -> Seq b -> LHsSpan
><* :: forall a b. (Spannable a, Spannable b) => a -> Seq b -> LHsSpan
(><*) a
a = (LHsSpan -> b -> LHsSpan) -> LHsSpan -> Seq b -> LHsSpan
forall b a. (b -> a -> b) -> b -> Seq a -> b
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' LHsSpan -> b -> LHsSpan
forall a b. (Spannable a, Spannable b) => a -> b -> LHsSpan
(><) (a -> LHsSpan
forall a. Spannable a => a -> LHsSpan
toSpan a
a)
data L a = L
{ forall a. L a -> LHsSpan
loc :: LHsSpan
, forall a. L a -> a
un :: a
}
instance Eq a => Eq (L a) where
L a
x == :: L a -> L a -> Bool
== L a
y = L a -> a
forall a. L a -> a
un L a
x a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== L a -> a
forall a. L a -> a
un L a
y
instance Ord a => Ord (L a) where
compare :: L a -> L a -> Ordering
compare L a
x L a
y = a -> a -> Ordering
forall a. Ord a => a -> a -> Ordering
compare (L a -> a
forall a. L a -> a
un L a
x) (L a -> a
forall a. L a -> a
un L a
y)
instance Show a => Show (L a) where
showsPrec :: Int -> L a -> ShowS
showsPrec Int
d = Int -> a -> ShowS
forall a. Show a => Int -> a -> ShowS
showsPrec Int
d (a -> ShowS) -> (L a -> a) -> L a -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. L a -> a
forall a. L a -> a
un