aboutsummaryrefslogtreecommitdiff
path: root/utils/CC/Types.hs
blob: b377045f33773f2260471d769bb53dfd018defb3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
module CC.Types where

import CC.Pretty


-- | Names of variables in the program
type Name = String

-- | Source metadata for compilation
data Context = Context FilePath

-- | Position in a source file; `SourcePos line column`, both zero-based
data SourcePos = SourcePos Int Int
  deriving (Show, Read, Eq, Ord)

instance Pretty SourcePos where
    pretty (SourcePos line col) = show (line + 1) ++ ":" ++ show (col + 1)

-- | A range in the original source code (for diagnostics and debug
-- information); [from, to).
data SourceRange = SourceRange SourcePos SourcePos
  deriving (Show, Read)

instance Pretty SourceRange where
    pretty (SourceRange from@(SourcePos fromLine fromCol) to@(SourcePos toLine toCol))
        | fromLine == toLine =
            show (fromLine + 1) ++ ":" ++ show (fromCol + 1) ++ "-" ++ show toCol
        | otherwise =
            show from ++ "-" ++ show to

class HasRange a where
    range :: a -> SourceRange

mergeRange :: SourceRange -> SourceRange -> SourceRange
mergeRange (SourceRange p1 p2) (SourceRange q1 q2) = SourceRange (min p1 q1) (max p2 q2)

-- | A newtype with no-op Read and Show instances
newtype RawString = RawString String

instance Read RawString where
    readsPrec _ s = [(RawString s, "")]

instance Show RawString where
    show (RawString s) = s