module Text.XML.HaXml.Posn
(
Posn()
, posInNewCxt
, noPos
, forcep
, addcol, newline, tab, white
, posnFilename, posnLine, posnColumn
) where
import Data.Char
data Posn = Pn String !Int !Int (Maybe Posn)
deriving (Posn -> Posn -> Bool
(Posn -> Posn -> Bool) -> (Posn -> Posn -> Bool) -> Eq Posn
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Posn -> Posn -> Bool
$c/= :: Posn -> Posn -> Bool
== :: Posn -> Posn -> Bool
$c== :: Posn -> Posn -> Bool
Eq)
posnFilename :: Posn -> FilePath
posnFilename :: Posn -> FilePath
posnFilename (Pn f :: FilePath
f _ _ _) = FilePath
f
posnLine, posnColumn :: Posn -> Int
posnLine :: Posn -> Int
posnLine (Pn _ x :: Int
x _ _) = Int
x
posnColumn :: Posn -> Int
posnColumn (Pn _ _ x :: Int
x _) = Int
x
noPos :: Posn
noPos :: Posn
noPos = FilePath -> Int -> Int -> Maybe Posn -> Posn
Pn "no recorded position" 0 0 Maybe Posn
forall a. Maybe a
Nothing
posInNewCxt :: String -> Maybe Posn -> Posn
posInNewCxt :: FilePath -> Maybe Posn -> Posn
posInNewCxt name :: FilePath
name pos :: Maybe Posn
pos = FilePath -> Int -> Int -> Maybe Posn -> Posn
Pn FilePath
name 1 1 Maybe Posn
pos
instance Show Posn where
showsPrec :: Int -> Posn -> ShowS
showsPrec _ (Pn f :: FilePath
f l :: Int
l c :: Int
c i :: Maybe Posn
i) = FilePath -> ShowS
showString "file " ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
FilePath -> ShowS
showString FilePath
f ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
FilePath -> ShowS
showString " at line " ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> ShowS
forall a. Show a => a -> ShowS
shows Int
l ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
FilePath -> ShowS
showString " col " ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> ShowS
forall a. Show a => a -> ShowS
shows Int
c ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
( case Maybe Posn
i of
Nothing -> ShowS
forall a. a -> a
id
Just p :: Posn
p -> FilePath -> ShowS
showString "\n used by " ShowS -> ShowS -> ShowS
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
Posn -> ShowS
forall a. Show a => a -> ShowS
shows Posn
p )
forcep :: Posn -> Int
forcep :: Posn -> Int
forcep (Pn _ n :: Int
n m :: Int
m _) = Int
m Int -> Int -> Int
forall a b. a -> b -> b
`seq` Int
n
addcol :: Int -> Posn -> Posn
addcol :: Int -> Posn -> Posn
addcol n :: Int
n (Pn f :: FilePath
f r :: Int
r c :: Int
c i :: Maybe Posn
i) = FilePath -> Int -> Int -> Maybe Posn -> Posn
Pn FilePath
f Int
r (Int
cInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
n) Maybe Posn
i
newline, tab :: Posn -> Posn
newline :: Posn -> Posn
newline (Pn f :: FilePath
f r :: Int
r _ i :: Maybe Posn
i) = FilePath -> Int -> Int -> Maybe Posn -> Posn
Pn FilePath
f (Int
rInt -> Int -> Int
forall a. Num a => a -> a -> a
+1) 1 Maybe Posn
i
tab :: Posn -> Posn
tab (Pn f :: FilePath
f r :: Int
r c :: Int
c i :: Maybe Posn
i) = FilePath -> Int -> Int -> Maybe Posn -> Posn
Pn FilePath
f Int
r (((Int
cInt -> Int -> Int
forall a. Integral a => a -> a -> a
`div`8)Int -> Int -> Int
forall a. Num a => a -> a -> a
+1)Int -> Int -> Int
forall a. Num a => a -> a -> a
*8) Maybe Posn
i
white :: Char -> Posn -> Posn
white :: Char -> Posn -> Posn
white ' ' = Int -> Posn -> Posn
addcol 1
white '\n' = Posn -> Posn
newline
white '\r' = Posn -> Posn
forall a. a -> a
id
white '\t' = Posn -> Posn
tab
white '\xa0' = Int -> Posn -> Posn
addcol 1
white x :: Char
x | Char -> Bool
isSpace Char
x = Int -> Posn -> Posn
addcol 1
white _ = FilePath -> Posn -> Posn
forall a. HasCallStack => FilePath -> a
error "precondition not satisfied: Posn.white c | isSpace c"