Abbie's Haskell compiler
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

32 lines
836 B

  1. {-# LANGUAGE FlexibleInstances #-}
  2. {-# LANGUAGE DefaultSignatures #-}
  3. module Frontend.Parser.Posn where
  4. import Frontend.Lexer.Tokens
  5. import Data.Typeable
  6. data Posn
  7. = Posn { posnLine :: {-# UNPACK #-} !Int
  8. , posnColm :: {-# UNPACK #-} !Int
  9. }
  10. deriving (Eq, Show, Ord)
  11. class HasPosn a where
  12. startPosn :: a -> Posn
  13. endPosn :: a -> Posn
  14. span :: (HasPosn b, HasPosn c) => b -> c -> a -> a
  15. default span :: Typeable a => b -> c -> a -> a
  16. span _ _ x = error $ "Can't span " ++ show (typeOf x)
  17. instance HasPosn Token where
  18. startPosn (Token _ l c) = Posn l c
  19. endPosn (Token t l c) = Posn l (c + tokSize t)
  20. instance HasPosn (Posn, Posn, a) where
  21. startPosn (s, _, _) = s
  22. endPosn (_, e, _) = e
  23. span start end (_, _, x) = (startPosn start, endPosn end, x)
  24. thd :: (a, b, c) -> c
  25. thd (_, _, z) = z