{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE DeriveFoldable #-}
{-# LANGUAGE DeriveTraversable #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE ViewPatterns #-}

module Unison.Lexer.Pos (Pos (..), Line, Column) where

type Line = Int

type Column = Int

data Pos = Pos {Pos -> Line
line :: {-# UNPACK #-} !Line, Pos -> Line
column :: {-# UNPACK #-} !Column} deriving (Line -> Pos -> ShowS
[Pos] -> ShowS
Pos -> String
(Line -> Pos -> ShowS)
-> (Pos -> String) -> ([Pos] -> ShowS) -> Show Pos
forall a.
(Line -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Line -> Pos -> ShowS
showsPrec :: Line -> Pos -> ShowS
$cshow :: Pos -> String
show :: Pos -> String
$cshowList :: [Pos] -> ShowS
showList :: [Pos] -> ShowS
Show, Pos -> Pos -> Bool
(Pos -> Pos -> Bool) -> (Pos -> Pos -> Bool) -> Eq Pos
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Pos -> Pos -> Bool
== :: Pos -> Pos -> Bool
$c/= :: Pos -> Pos -> Bool
/= :: Pos -> Pos -> Bool
Eq, Eq Pos
Eq Pos =>
(Pos -> Pos -> Ordering)
-> (Pos -> Pos -> Bool)
-> (Pos -> Pos -> Bool)
-> (Pos -> Pos -> Bool)
-> (Pos -> Pos -> Bool)
-> (Pos -> Pos -> Pos)
-> (Pos -> Pos -> Pos)
-> Ord Pos
Pos -> Pos -> Bool
Pos -> Pos -> Ordering
Pos -> Pos -> Pos
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 :: Pos -> Pos -> Ordering
compare :: Pos -> Pos -> Ordering
$c< :: Pos -> Pos -> Bool
< :: Pos -> Pos -> Bool
$c<= :: Pos -> Pos -> Bool
<= :: Pos -> Pos -> Bool
$c> :: Pos -> Pos -> Bool
> :: Pos -> Pos -> Bool
$c>= :: Pos -> Pos -> Bool
>= :: Pos -> Pos -> Bool
$cmax :: Pos -> Pos -> Pos
max :: Pos -> Pos -> Pos
$cmin :: Pos -> Pos -> Pos
min :: Pos -> Pos -> Pos
Ord)

instance Semigroup Pos where
  Pos Line
line Line
col <> :: Pos -> Pos -> Pos
<> Pos Line
line2 Line
col2 =
    if Line
line2 Line -> Line -> Bool
forall a. Eq a => a -> a -> Bool
== Line
0
      then Line -> Line -> Pos
Pos Line
line (Line
col Line -> Line -> Line
forall a. Num a => a -> a -> a
+ Line
col2)
      else Line -> Line -> Pos
Pos (Line
line Line -> Line -> Line
forall a. Num a => a -> a -> a
+ Line
line2) Line
col2

instance Monoid Pos where
  mempty :: Pos
mempty = Line -> Line -> Pos
Pos Line
0 Line
0