megaparsec/Text/Parsec/Pos.hs

131 lines
4.2 KiB
Haskell
Raw Normal View History

2008-01-13 20:53:15 +03:00
-----------------------------------------------------------------------------
-- |
-- Module : Text.Parsec.Pos
-- Copyright : (c) Daan Leijen 1999-2001, (c) Paolo Martini 2007
-- License : BSD-style (see the LICENSE file)
--
2008-01-20 07:44:41 +03:00
-- Maintainer : derek.a.elkins@gmail.com
2008-01-13 20:53:15 +03:00
-- 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
2010-09-08 09:37:47 +04:00
#ifdef BASE3 then
2008-12-26 11:29:54 +03:00
import Data.Generics
2010-09-08 09:37:47 +04:00
#else
import Data.Data (Data)
import Data.Typeable (Typeable)
#endif
2008-12-26 11:29:54 +03:00
-- < Source positions: a file name, a line and a column
2008-01-13 20:53:15 +03:00
-- 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.
2008-01-13 20:53:15 +03:00
data SourcePos = SourcePos SourceName !Line !Column
2008-12-26 11:29:54 +03:00
deriving ( Eq, Ord, Data, Typeable)
2008-01-13 20:53:15 +03:00
-- | Create a new 'SourcePos' with the given source name,
-- line number and column number.
2008-01-13 20:53:15 +03:00
newPos :: SourceName -> Line -> Column -> SourcePos
2008-02-13 07:32:24 +03:00
newPos name line column
= SourcePos name line column
2008-01-13 20:53:15 +03:00
-- | Create a new 'SourcePos' with the given source name,
-- and line number and column number set to 1, the upper left.
2008-01-13 20:53:15 +03:00
initialPos :: SourceName -> SourcePos
2008-02-13 07:32:24 +03:00
initialPos name
= newPos name 1 1
2008-01-13 20:53:15 +03:00
-- | Extracts the name of the source from a source position.
2008-01-13 20:53:15 +03:00
sourceName :: SourcePos -> SourceName
2008-02-13 07:32:24 +03:00
sourceName (SourcePos name _line _column) = name
2008-01-13 20:53:15 +03:00
-- | Extracts the line number from a source position.
2008-01-13 20:53:15 +03:00
sourceLine :: SourcePos -> Line
2008-02-13 07:32:24 +03:00
sourceLine (SourcePos _name line _column) = line
2008-01-13 20:53:15 +03:00
-- | Extracts the column number from a source position.
2008-01-13 20:53:15 +03:00
sourceColumn :: SourcePos -> Column
2008-02-13 07:32:24 +03:00
sourceColumn (SourcePos _name _line column) = column
2008-01-13 20:53:15 +03:00
-- | Increments the line number of a source position.
2008-01-13 20:53:15 +03:00
incSourceLine :: SourcePos -> Line -> SourcePos
incSourceLine (SourcePos name line column) n = SourcePos name (line+n) column
-- | Increments the column number of a source position.
2008-01-13 20:53:15 +03:00
incSourceColumn :: SourcePos -> Column -> SourcePos
incSourceColumn (SourcePos name line column) n = SourcePos name line (column+n)
-- | Set the name of the source.
2008-01-13 20:53:15 +03:00
setSourceName :: SourcePos -> SourceName -> SourcePos
2008-02-13 07:32:24 +03:00
setSourceName (SourcePos _name line column) n = SourcePos n line column
2008-01-13 20:53:15 +03:00
-- | Set the line number of a source position.
2008-01-13 20:53:15 +03:00
setSourceLine :: SourcePos -> Line -> SourcePos
2008-02-13 07:32:24 +03:00
setSourceLine (SourcePos name _line column) n = SourcePos name n column
2008-01-13 20:53:15 +03:00
-- | Set the column number of a source position.
2008-01-13 20:53:15 +03:00
setSourceColumn :: SourcePos -> Column -> SourcePos
2008-02-13 07:32:24 +03:00
setSourceColumn (SourcePos name line _column) n = SourcePos name line n
2008-01-13 20:53:15 +03:00
-- | The expression @updatePosString pos s@ updates the source position
-- @pos@ by calling 'updatePosChar' on every character in @s@, ie.
-- @foldl updatePosChar pos string@.
2008-01-13 20:53:15 +03:00
updatePosString :: SourcePos -> String -> SourcePos
updatePosString pos string
= foldl updatePosChar pos 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.
2008-01-13 20:53:15 +03:00
updatePosChar :: SourcePos -> Char -> SourcePos
2008-02-13 07:32:24 +03:00
updatePosChar (SourcePos name line column) c
= case c of
2008-01-13 20:53:15 +03:00
'\n' -> SourcePos name (line+1) 1
'\t' -> SourcePos name line (column + 8 - ((column-1) `mod` 8))
_ -> SourcePos name line (column + 1)
instance Show SourcePos where
show (SourcePos name line column)
| null name = showLineColumn
| otherwise = "\"" ++ name ++ "\" " ++ showLineColumn
where
showLineColumn = "(line " ++ show line ++
", column " ++ show column ++
")"