<commit_before>-----------------------------------------------------------------------------
-- |
-- Module      :  Language.C.Toolkit.Position
-- Copyright   :  (c) [1995..2000] <NAME>
-- License     :  BSD-style
-- Maintainer  :  <EMAIL>
-- Portability :  portable
--
-- This module provides some definitions used throughout all modules of a
--  compiler. 
--
--  * May not import anything apart from `Config'.
-----------------------------------------------------------------------------
module Language.C.Toolkit.Position (
  --
  -- source text positions
  --
  Position(Position), Pos (..), 
  posFile,posRow,posColumn,
  nopos, isNopos,
  dontCarePos,  isDontCarePos,
  builtinPos, isBuiltinPos,
  internalPos, isInternalPos,
  incPos, tabPos, retPos,
) where
import Data.Generics

-- uniform representation of source file positions; the order of the arguments
-- is important as it leads to the desired ordering of source positions
-- (EXPORTED) 
--
data Position = Position String         -- file name
        {-# UNPACK #-}   !Int           -- row
        {-# UNPACK #-}   !Int           -- column
  deriving (Eq, Ord)

instance Show Position where
  show (Position fname row col) = show (fname, row, col)
posFile :: Position -> String
posFile (Position fname _ _) = fname
posRow  :: Position -> Int
posRow (Position _ row _) = row
posColumn :: Position -> Int
posColumn (Position _ _ col) = col

-- no position (for unknown position information) (EXPORTED)
--
nopos :: Position
nopos  = Position "<no file>" (-1) (-1)

isNopos :: Position -> Bool
isNopos (Position _ (-1) (-1)) = True
isNopos _                      = False

-- don't care position (to be used for invalid position information) (EXPORTED)
--
dontCarePos :: Position
dontCarePos = Position "<invalid>" (-2) (-2)

isDontCarePos  :: Position -> Bool
isDontCarePos (Position _ (-2) (-2)) = True
isDontCarePos _                      = False

-- position attached to objects that are hard-coded into the toolkit (EXPORTED)
--
builtinPos :: Position
builtinPos  = Position "<built into the compiler>" (-3) (-3)

isBuiltinPos :: Position -> Bool
isBuiltinPos (Position _ (-3) (-3)) = True
isBuiltinPos _                      = False

-- position used for internal errors (EXPORTED)
--
internalPos :: Position
internalPos = Position "<internal error>" (-4) (-4)

isInternalPos :: Position -> Bool
isInternalPos (Position _ (-4) (-4)) = True
isInternalPos _                      = False

-- instances of the class `Pos' are associated with some source text position
-- don't care position (to be used for invalid position information) (EXPORTED)
--
class Pos a where
  posOf :: a -> Position

-- advance column
--
incPos :: Position -> Int -> Position
incPos (Position fname row col) n = Position fname row (col + n)

-- advance column to next tab positions (tabs are at every 8th column)
--
tabPos :: Position -> Position
tabPos (Position fname row col) =
        Position fname row (col + 8 - (col - 1) `mod` 8)

-- advance to next line
--
retPos :: Position -> Position
retPos (Position fname row _col) = Position fname (row + 1) 1<commit_msg>Language.C.Common.Position: cleanup<commit_after>{-# LANGUAGE DeriveDataTypeable #-}
-----------------------------------------------------------------------------
-- |
-- Module      :  Language.C.Common.Position
-- Copyright   :  (c) [1995..2000] <NAME>
-- License     :  BSD-style
-- Maintainer  :  <EMAIL>
-- Portability :  portable
--
-- Source code position
-----------------------------------------------------------------------------
module Language.C.Common.Position (
  --
  -- source text positions
  --
  Position(Position),
  posFile,posRow,posColumn,isSourcePos,
  nopos, isNopos,
  builtinPos, isBuiltinPos,
  internalPos, isInternalPos,
  incPos, tabPos, retPos,
  Pos(..),
) where
import Data.Generics

-- | uniform representation of source file positions; the order of the arguments
-- is important as it leads to the desired ordering of source positions
data Position = Position String         -- file name
        {-# UNPACK #-}   !Int           -- row
        {-# UNPACK #-}   !Int           -- column
  deriving (Eq, Ord, Typeable, Data)
    
instance Show Position where
  show pos@(Position fname row col) 
    | isNopos pos = "<no file>"
    | isBuiltinPos pos = "<builtin>"
    | isInternalPos pos = "<internal>"
    | otherwise = show (fname, row, col)

-- | get the source file of the specified position. Fails unless @isSourcePos pos@.
posFile :: Position -> String
posFile (Position fname _ _) = fname
-- | get the line number of the specified position. Fails if @isSourcePos pos@
posRow  :: Position -> Int
posRow (Position _ row _) = row
-- | get the column of the specified position. Fails if @isSourcePos pos@
posColumn :: Position -> Int
posColumn (Position _ _ col) = col

class Pos a where
    posOf :: a -> Position
      
isSourcePos :: Position -> Bool
isSourcePos (Position _ row col) = row >= 0 && col >= 0      

-- | no position (for unknown position information) 
nopos :: Position
nopos  = Position "<no file>" (-1) 0

isNopos :: Position -> Bool
isNopos (Position _ (-1) 0) = True
isNopos _                   = False

-- | position attached to built-in objects
--
builtinPos :: Position
builtinPos  = Position "<built into the parser>" (-1) 1

isBuiltinPos :: Position -> Bool
isBuiltinPos (Position _ (-1) 1) = True
isBuiltinPos _                   = False

-- | position used for internal errors
internalPos :: Position
internalPos = Position "<internal error>" (-1) 2

isInternalPos :: Position -> Bool
isInternalPos (Position _ (-1) 2) = True
isInternalPos _                      = False

-- | advance column
incPos :: Position -> Int -> Position
incPos (Position fname row col) n = Position fname row (col + n)

-- | advance column to next tab positions (tabs are considered to be at every 8th column)
tabPos :: Position -> Position
tabPos (Position fname row col) =
        Position fname row (col + 8 - (col - 1) `mod` 8)

-- | advance to next line
retPos :: Position -> Position
retPos (Position fname row _col) = Position fname (row + 1) 1
{'fraction_non_alphanumeric': 0.1614540691010316, 'fraction_numerical': 0.009497298182413624, 'mean_word_length': 2.3358820316766793, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 43, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3811911', 'n_tokens_mistral': 1791, 'n_tokens_neox': 1723, 'n_words': 642}