{-# LANGUAGE FlexibleInstances #-} {-# LANGUAGE DefaultSignatures #-} {-# LANGUAGE DeriveGeneric #-} {-# LANGUAGE DeriveAnyClass #-} module Frontend.Parser.Posn where import Data.Typeable import Development.Shake.Classes import Frontend.Lexer.Tokens import GHC.Generics (Generic) data Posn = Posn { posnLine :: {-# UNPACK #-} !Int , posnColm :: {-# UNPACK #-} !Int } deriving (Eq, Show, Ord, Generic, Binary, Hashable, NFData) class HasPosn a where startPosn :: a -> Posn endPosn :: a -> Posn span :: (HasPosn b, HasPosn c) => b -> c -> a -> a default span :: Typeable a => b -> c -> a -> a span _ _ x = error $ "Can't span " ++ show (typeOf x) instance HasPosn Token where startPosn (Token _ l c) = Posn l c endPosn (Token t l c) = Posn l (c + tokSize t) instance HasPosn (Posn, Posn, a) where startPosn (s, _, _) = s endPosn (_, e, _) = e span start end (_, _, x) = (startPosn start, endPosn end, x) instance HasPosn Posn where startPosn = id endPosn = id span _ y _ = endPosn y