    {-# LANGUAGE DeriveDataTypeable #-}
    {-# LANGUAGE Safe #-}

    -----------------------------------------------------------------------------
    -- |
    -- Module      :  Text.Parsec.Pos
    -- Copyright   :  (c) Daan Leijen 1999-2001, (c) Paolo Martini 2007
    -- License     :  BSD-style (see the LICENSE file)
    --
    -- Maintainer  :  derek.a.elkins@gmail.com
    -- Stability   :  provisional
    -- Portability :  portable
    --
    -- Textual source positions.
    --
    -----------------------------------------------------------------------------

    module Text.Parsec.Pos
        ( SourceName, Line, Column
        , SourcePos
        , sourceLine, sourceColumn, sourceName
        , incSourceLine, incSourceColumn
        , setSourceLine, setSourceColumn, setSourceName
        , newPos, initialPos
        , updatePosChar, updatePosString
        ) where

    import Data.Data (Data)
    import Data.Typeable (Typeable)

    -- < Source positions: a file name, a line and a column
    -- upper left is (1,1)

    type SourceName = String
    type Line       = Int
    type Column     = Int

    -- | The abstract data type @SourcePos@ represents source positions. It
    -- contains the name of the source (i.e. file name), a line number and
    -- a column number. @SourcePos@ is an instance of the 'Show', 'Eq' and
    -- 'Ord' class.

    data SourcePos  = SourcePos SourceName !Line !Column
        deriving ( SourcePos -> SourcePos -> Bool
    (SourcePos -> SourcePos -> Bool)
    -> (SourcePos -> SourcePos -> Bool) -> Eq SourcePos
    forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
    $c== :: SourcePos -> SourcePos -> Bool
    == :: SourcePos -> SourcePos -> Bool
    $c/= :: SourcePos -> SourcePos -> Bool
    /= :: SourcePos -> SourcePos -> Bool
    Eq, Eq SourcePos
    Eq SourcePos =>
    (SourcePos -> SourcePos -> Ordering)
    -> (SourcePos -> SourcePos -> Bool)
    -> (SourcePos -> SourcePos -> Bool)
    -> (SourcePos -> SourcePos -> Bool)
    -> (SourcePos -> SourcePos -> Bool)
    -> (SourcePos -> SourcePos -> SourcePos)
    -> (SourcePos -> SourcePos -> SourcePos)
    -> Ord SourcePos
    SourcePos -> SourcePos -> Bool
    SourcePos -> SourcePos -> Ordering
    SourcePos -> SourcePos -> SourcePos
    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 :: SourcePos -> SourcePos -> Ordering
    compare :: SourcePos -> SourcePos -> Ordering
    $c< :: SourcePos -> SourcePos -> Bool
    < :: SourcePos -> SourcePos -> Bool
    $c<= :: SourcePos -> SourcePos -> Bool
    <= :: SourcePos -> SourcePos -> Bool
    $c> :: SourcePos -> SourcePos -> Bool
    > :: SourcePos -> SourcePos -> Bool
    $c>= :: SourcePos -> SourcePos -> Bool
    >= :: SourcePos -> SourcePos -> Bool
    $cmax :: SourcePos -> SourcePos -> SourcePos
    max :: SourcePos -> SourcePos -> SourcePos
    $cmin :: SourcePos -> SourcePos -> SourcePos
    min :: SourcePos -> SourcePos -> SourcePos
    Ord, Typeable SourcePos
    Typeable SourcePos =>
    (forall (c :: * -> *).
     (forall d b. Data d => c (d -> b) -> d -> c b)
     -> (forall g. g -> c g) -> SourcePos -> c SourcePos)
    -> (forall (c :: * -> *).
        (forall b r. Data b => c (b -> r) -> c r)
        -> (forall r. r -> c r) -> Constr -> c SourcePos)
    -> (SourcePos -> Constr)
    -> (SourcePos -> DataType)
    -> (forall (t :: * -> *) (c :: * -> *).
        Typeable t =>
        (forall d. Data d => c (t d)) -> Maybe (c SourcePos))
    -> (forall (t :: * -> * -> *) (c :: * -> *).
        Typeable t =>
        (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c SourcePos))
    -> ((forall b. Data b => b -> b) -> SourcePos -> SourcePos)
    -> (forall r r'.
        (r -> r' -> r)
        -> r -> (forall d. Data d => d -> r') -> SourcePos -> r)
    -> (forall r r'.
        (r' -> r -> r)
        -> r -> (forall d. Data d => d -> r') -> SourcePos -> r)
    -> (forall u. (forall d. Data d => d -> u) -> SourcePos -> [u])
    -> (forall u.
        Int -> (forall d. Data d => d -> u) -> SourcePos -> u)
    -> (forall (m :: * -> *).
        Monad m =>
        (forall d. Data d => d -> m d) -> SourcePos -> m SourcePos)
    -> (forall (m :: * -> *).
        MonadPlus m =>
        (forall d. Data d => d -> m d) -> SourcePos -> m SourcePos)
    -> (forall (m :: * -> *).
        MonadPlus m =>
        (forall d. Data d => d -> m d) -> SourcePos -> m SourcePos)
    -> Data SourcePos
    SourcePos -> Constr
    SourcePos -> DataType
    (forall b. Data b => b -> b) -> SourcePos -> SourcePos
    forall a.
    Typeable a =>
    (forall (c :: * -> *).
     (forall d b. Data d => c (d -> b) -> d -> c b)
     -> (forall g. g -> c g) -> a -> c a)
    -> (forall (c :: * -> *).
        (forall b r. Data b => c (b -> r) -> c r)
        -> (forall r. r -> c r) -> Constr -> c a)
    -> (a -> Constr)
    -> (a -> DataType)
    -> (forall (t :: * -> *) (c :: * -> *).
        Typeable t =>
        (forall d. Data d => c (t d)) -> Maybe (c a))
    -> (forall (t :: * -> * -> *) (c :: * -> *).
        Typeable t =>
        (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c a))
    -> ((forall b. Data b => b -> b) -> a -> a)
    -> (forall r r'.
        (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
    -> (forall r r'.
        (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
    -> (forall u. (forall d. Data d => d -> u) -> a -> [u])
    -> (forall u. Int -> (forall d. Data d => d -> u) -> a -> u)
    -> (forall (m :: * -> *).
        Monad m =>
        (forall d. Data d => d -> m d) -> a -> m a)
    -> (forall (m :: * -> *).
        MonadPlus m =>
        (forall d. Data d => d -> m d) -> a -> m a)
    -> (forall (m :: * -> *).
        MonadPlus m =>
        (forall d. Data d => d -> m d) -> a -> m a)
    -> Data a
    forall u. Int -> (forall d. Data d => d -> u) -> SourcePos -> u
    forall u. (forall d. Data d => d -> u) -> SourcePos -> [u]
    forall r r'.
    (r -> r' -> r)
    -> r -> (forall d. Data d => d -> r') -> SourcePos -> r
    forall r r'.
    (r' -> r -> r)
    -> r -> (forall d. Data d => d -> r') -> SourcePos -> r
    forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d) -> SourcePos -> m SourcePos
    forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> SourcePos -> m SourcePos
    forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c SourcePos
    forall (c :: * -> *).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g) -> SourcePos -> c SourcePos
    forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c SourcePos)
    forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c SourcePos)
    $cgfoldl :: forall (c :: * -> *).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g) -> SourcePos -> c SourcePos
    gfoldl :: forall (c :: * -> *).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g) -> SourcePos -> c SourcePos
    $cgunfold :: forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c SourcePos
    gunfold :: forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c SourcePos
    $ctoConstr :: SourcePos -> Constr
    toConstr :: SourcePos -> Constr
    $cdataTypeOf :: SourcePos -> DataType
    dataTypeOf :: SourcePos -> DataType
    $cdataCast1 :: forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c SourcePos)
    dataCast1 :: forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c SourcePos)
    $cdataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c SourcePos)
    dataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c SourcePos)
    $cgmapT :: (forall b. Data b => b -> b) -> SourcePos -> SourcePos
    gmapT :: (forall b. Data b => b -> b) -> SourcePos -> SourcePos
    $cgmapQl :: forall r r'.
    (r -> r' -> r)
    -> r -> (forall d. Data d => d -> r') -> SourcePos -> r
    gmapQl :: forall r r'.
    (r -> r' -> r)
    -> r -> (forall d. Data d => d -> r') -> SourcePos -> r
    $cgmapQr :: forall r r'.
    (r' -> r -> r)
    -> r -> (forall d. Data d => d -> r') -> SourcePos -> r
    gmapQr :: forall r r'.
    (r' -> r -> r)
    -> r -> (forall d. Data d => d -> r') -> SourcePos -> r
    $cgmapQ :: forall u. (forall d. Data d => d -> u) -> SourcePos -> [u]
    gmapQ :: forall u. (forall d. Data d => d -> u) -> SourcePos -> [u]
    $cgmapQi :: forall u. Int -> (forall d. Data d => d -> u) -> SourcePos -> u
    gmapQi :: forall u. Int -> (forall d. Data d => d -> u) -> SourcePos -> u
    $cgmapM :: forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d) -> SourcePos -> m SourcePos
    gmapM :: forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d) -> SourcePos -> m SourcePos
    $cgmapMp :: forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> SourcePos -> m SourcePos
    gmapMp :: forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> SourcePos -> m SourcePos
    $cgmapMo :: forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> SourcePos -> m SourcePos
    gmapMo :: forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> SourcePos -> m SourcePos
    Data, Typeable)

    -- | Create a new 'SourcePos' with the given source name,
    -- line number and column number.

    newPos :: SourceName -> Line -> Column -> SourcePos
    newPos :: [Char] -> Int -> Int -> SourcePos
    newPos [Char]
    name Int
    line Int
    column
        = [Char] -> Int -> Int -> SourcePos
    SourcePos [Char]
    name Int
    line Int
    column

    -- | Create a new 'SourcePos' with the given source name,
    -- and line number and column number set to 1, the upper left.

    initialPos :: SourceName -> SourcePos
    initialPos :: [Char] -> SourcePos
    initialPos [Char]
    name
        = [Char] -> Int -> Int -> SourcePos
    newPos [Char]
    name Int
    1 Int
    1

    -- | Extracts the name of the source from a source position.

    sourceName :: SourcePos -> SourceName
    sourceName :: SourcePos -> [Char]
    sourceName (SourcePos [Char]
    name Int
    _line Int
    _column) = [Char]
    name

    -- | Extracts the line number from a source position.

    sourceLine :: SourcePos -> Line
    sourceLine :: SourcePos -> Int
    sourceLine (SourcePos [Char]
    _name Int
    line Int
    _column) = Int
    line

    -- | Extracts the column number from a source position.

    sourceColumn :: SourcePos -> Column
    sourceColumn :: SourcePos -> Int
    sourceColumn (SourcePos [Char]
    _name Int
    _line Int
    column) = Int
    column

    -- | Increments the line number of a source position.

    incSourceLine :: SourcePos -> Line -> SourcePos
    incSourceLine :: SourcePos -> Int -> SourcePos
    incSourceLine (SourcePos [Char]
    name Int
    line Int
    column) Int
    n = [Char] -> Int -> Int -> SourcePos
    SourcePos [Char]
    name (Int
    lineInt -> Int -> Int
    forall a. Num a => a -> a -> a
    +Int
    n) Int
    column

    -- | Increments the column number of a source position.

    incSourceColumn :: SourcePos -> Column -> SourcePos
    incSourceColumn :: SourcePos -> Int -> SourcePos
    incSourceColumn (SourcePos [Char]
    name Int
    line Int
    column) Int
    n = [Char] -> Int -> Int -> SourcePos
    SourcePos [Char]
    name Int
    line (Int
    columnInt -> Int -> Int
    forall a. Num a => a -> a -> a
    +Int
    n)

    -- | Set the name of the source.

    setSourceName :: SourcePos -> SourceName -> SourcePos
    setSourceName :: SourcePos -> [Char] -> SourcePos
    setSourceName (SourcePos [Char]
    _name Int
    line Int
    column) [Char]
    n = [Char] -> Int -> Int -> SourcePos
    SourcePos [Char]
    n Int
    line Int
    column

    -- | Set the line number of a source position.

    setSourceLine :: SourcePos -> Line -> SourcePos
    setSourceLine :: SourcePos -> Int -> SourcePos
    setSourceLine (SourcePos [Char]
    name Int
    _line Int
    column) Int
    n = [Char] -> Int -> Int -> SourcePos
    SourcePos [Char]
    name Int
    n Int
    column

    -- | Set the column number of a source position.

    setSourceColumn :: SourcePos -> Column -> SourcePos
    setSourceColumn :: SourcePos -> Int -> SourcePos
    setSourceColumn (SourcePos [Char]
    name Int
    line Int
    _column) Int
    n = [Char] -> Int -> Int -> SourcePos
    SourcePos [Char]
    name Int
    line Int
    n

    -- | The expression @updatePosString pos s@ updates the source position
    -- @pos@ by calling 'updatePosChar' on every character in @s@, ie.
    -- @foldl updatePosChar pos string@.

    updatePosString :: SourcePos -> String -> SourcePos
    updatePosString :: SourcePos -> [Char] -> SourcePos
    updatePosString SourcePos
    pos [Char]
    string
        = (SourcePos -> Char -> SourcePos)
    -> SourcePos -> [Char] -> SourcePos
    forall b a. (b -> a -> b) -> b -> [a] -> b
    forall (t :: * -> *) b a.
    Foldable t =>
    (b -> a -> b) -> b -> t a -> b
    foldl SourcePos -> Char -> SourcePos
    updatePosChar SourcePos
    pos [Char]
    string

    -- | Update a source position given a character. If the character is a
    -- newline (\'\\n\') or carriage return (\'\\r\') the line number is
    -- incremented by 1. If the character is a tab (\'\t\') the column
    -- number is incremented to the nearest 8'th column, ie. @column + 8 -
    -- ((column-1) \`mod\` 8)@. In all other cases, the column is
    -- incremented by 1.

    updatePosChar   :: SourcePos -> Char -> SourcePos
    updatePosChar :: SourcePos -> Char -> SourcePos
    updatePosChar (SourcePos [Char]
    name Int
    line Int
    column) Char
    c
        = case Char
    c of
            Char
    '\n' -> [Char] -> Int -> Int -> SourcePos
    SourcePos [Char]
    name (Int
    lineInt -> Int -> Int
    forall a. Num a => a -> a -> a
    +Int
    1) Int
    1
            Char
    '\t' -> [Char] -> Int -> Int -> SourcePos
    SourcePos [Char]
    name Int
    line (Int
    column Int -> Int -> Int
    forall a. Num a => a -> a -> a
    + Int
    8 Int -> Int -> Int
    forall a. Num a => a -> a -> a
    - ((Int
    columnInt -> Int -> Int
    forall a. Num a => a -> a -> a
    -Int
    1) Int -> Int -> Int
    forall a. Integral a => a -> a -> a
    `mod` Int
    8))
            Char
    _    -> [Char] -> Int -> Int -> SourcePos
    SourcePos [Char]
    name Int
    line (Int
    column Int -> Int -> Int
    forall a. Num a => a -> a -> a
    + Int
    1)

    instance Show SourcePos where
      show :: SourcePos -> [Char]
    show (SourcePos [Char]
    name Int
    line Int
    column)
        | [Char] -> Bool
    forall a. [a] -> Bool
    forall (t :: * -> *) a. Foldable t => t a -> Bool
    null [Char]
    name = [Char]
    showLineColumn
        | Bool
    otherwise = [Char]
    "\"" [Char] -> ShowS
    forall a. [a] -> [a] -> [a]
    ++ [Char]
    name [Char] -> ShowS
    forall a. [a] -> [a] -> [a]
    ++ [Char]
    "\" " [Char] -> ShowS
    forall a. [a] -> [a] -> [a]
    ++ [Char]
    showLineColumn
        where
          showLineColumn :: [Char]
    showLineColumn    = [Char]
    "(line " [Char] -> ShowS
    forall a. [a] -> [a] -> [a]
    ++ Int -> [Char]
    forall a. Show a => a -> [Char]
    show Int
    line [Char] -> ShowS
    forall a. [a] -> [a] -> [a]
    ++
                              [Char]
    ", column " [Char] -> ShowS
    forall a. [a] -> [a] -> [a]
    ++ Int -> [Char]
    forall a. Show a => a -> [Char]
    show Int
    column [Char] -> ShowS
    forall a. [a] -> [a] -> [a]
    ++
                              [Char]
    ")"