module Language.Egison.Parser.Pattern.Prim.Location
( Location(..)
, Position(..)
, Locate(..)
, fromSourcePos
)
where
import GHC.Generics ( Generic )
import Data.Data ( Data
, Typeable
)
import qualified Text.Megaparsec as Parsec
( SourcePos(..)
, unPos
)
data Position
= Position { line :: Int
, column :: Int
}
deriving (Show, Eq, Generic, Data, Typeable)
data Location
= Location { begin :: Position
, end :: Position
}
deriving (Show, Eq, Generic, Data, Typeable)
class Monad m => Locate m where
getPosition :: m Position
getLocation :: m a -> m (a, Location)
getLocation m = do
begin <- getPosition
x <- m
end <- getPosition
let location = Location { begin, end }
pure (x, location)
fromSourcePos :: Parsec.SourcePos -> Position
fromSourcePos Parsec.SourcePos { Parsec.sourceLine, Parsec.sourceColumn } =
Position { line, column }
where
line = Parsec.unPos sourceLine
column = Parsec.unPos sourceColumn