module Language.Haskell.Lexer.Position where
data Pos = Pos { char, line, column :: !Int } deriving (Show)
simpPos :: Pos -> (Int,Int)
simpPos (Pos _ l c) = (l,c)
instance Eq Pos where p1 == p2 = simpPos p1 == simpPos p2
instance Ord Pos where compare p1 p2 = compare (simpPos p1) (simpPos p2)
startPos :: Pos
startPos = Pos { char = 0, line = 1, column = 1 }
nextPos :: Pos -> String -> Pos
nextPos = foldl nextPos1
nextPos1 :: Pos -> Char -> Pos
nextPos1 (Pos n y x) c =
case c of
'\CR' -> Pos (n+1) (y+1) 1
'\LF' -> Pos (n+1) (y+1) 1
'\FF' -> Pos (n+1) (y+1) 1
'\t' -> Pos (n+1) y (x+8-(x-1) `mod` 8)
_ -> Pos (n+1) y (x+1)