{- |
Module      : LH.Data.SrcLoc
Description : Source positions and spans.
Copyright   : (c) Hangil Kim, 2026
                  Kairon Johnson, 2026
                  Leonidas Sallos, 2026
                  Oscar Abelanet, 2026
License     : MIT
Maintainer  : kimhang@oregonstate.edu
Stability   : experimental

This module provides definitions for locations in the source.
'LHsPos' indicates a single position, whereas 'LHsSpan' indicates a set of positions.
-}
module LH.Data.SrcLoc (
    -- * Source positions
    LHsPos (..), 
    -- * Source spans
    LHsSpan (..),
    -- ** Lexemes
    LHsLexeme (..),
    -- ** Joining spans
    Spannable (..), (><), (><*),
    -- * Located values
    L (..)
    ) where

import Data.Foldable (foldl')
import Data.Sequence (Seq)

import LH.Text.Token (LHsToken)

-------------------------------------------------------------------------------
-- Source positions

-- | A position in the source.
data LHsPos = LHsPos 
    { LHsPos -> Int
posLine :: !Int      -- ^ A line.
    , LHsPos -> Int
posCol  :: !Int      -- ^ A column.
    }
    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)

-------------------------------------------------------------------------------
-- Source spans

-- | A set of contiguous positions in the source.
--   A span can simply be described as the interval \([a,b)\), where \(a\) and \(b\) have type 'LHsPos'.
data LHsSpan = LHsSpan
    { LHsSpan -> LHsPos
spanStart :: !LHsPos    -- ^ The start, inclusive.
    , LHsSpan -> LHsPos
spanEnd   :: !LHsPos    -- ^ The end, exclusive.
    }
    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)

-- | A token paired with its span in the source.
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)

-- | A 'Spannable' type can be converted into a span via 'toSpan'.
class Spannable a where
    -- | Converts a value into a span.
    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 ><

-- | Produces a span starting at the first argument and ending at the second argument.
--
--   \([a,b)\mathbin{\texttt{><}}[c,d)=[a,d)\)
(><) :: (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 ><*

-- | Produces a span starting at the first argument all the way to the end of the last element of the second argument.
--   
--   @'(><*)'@ can be interpreted as applying @'(><)'@ zero or more times.
--
--   \([a,b)\mathbin{\texttt{><}^*}\texttt{[}[c_1,d_1)\texttt{,}\ldots\texttt{,}[c_n,d_n)\texttt{]}=[a,b)\mathbin{\texttt{><}}[c_1,d_1)\mathbin{\texttt{><}}\ldots\mathbin{\texttt{><}}[c_n,d_n)=[a,d_n)\)
(><*) :: (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)

-------------------------------------------------------------------------------
-- Located values

-- | A value paired with its span in the source.
--   'L' allows any language construct to be paired with a span, e.g., declarations, expressions, types, and patterns.
--   Error messages can then reference 'loc' for pointing to appropriate places in the source.
data L a = L 
    { forall a. L a -> LHsSpan
loc :: LHsSpan    -- ^ A span in the source.
    , forall a. L a -> a
un  :: a          -- ^ A value.
    }

-- | @x :: 'L' a == y :: 'L' a@ \(\Longleftrightarrow\) @'un' x == 'un' y@.
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

-- | @'compare' (x :: 'L' a) (y :: 'L' a)@ \(\Longleftrightarrow\) @'compare' ('un' x) ('un' 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)

-- | @'show' (x :: 'L' a) = 'show' ('un' x)@.
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