never executed always true always false
1 -- Copyright (C) 2018-2024 Jun Zhang <zhangjunphy[at]gmail[dot]com>
2 --
3 -- This file is a part of decafc.
4 --
5 -- decafc is free software: you can redistribute it and/or modify it under the
6 -- terms of the MIT (X11) License as described in the LICENSE file.
7 --
8 -- decafc is distributed in the hope that it will be useful, but WITHOUT ANY
9 -- WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
10 -- FOR A PARTICULAR PURPOSE. See the X11 license for more details.
11
12 module Util.SourceLoc (Posn (..), Range (..), Located (..), unLoc, getLoc) where
13
14 import Formatting (formatToString, int, shown, (%))
15
16 data Posn = Posn
17 { offset :: !Int,
18 row :: !Int,
19 col :: !Int
20 }
21 deriving (Eq, Ord)
22
23 posn0 :: Posn
24 posn0 = Posn 0 0 0
25
26 instance Show Posn where
27 show (Posn _ row col) = formatToString ("(" % int % ":" % int % ")") row col
28
29 data Range = Range
30 { start :: Posn,
31 stop :: Posn
32 }
33 deriving (Eq, Ord)
34
35 range0 :: Range
36 range0 = Range posn0 posn0
37
38 instance Show Range where
39 show (Range start stop) = formatToString ("[" % shown % "-" % shown % "]") start stop
40
41
42 data Located a = LocatedAt Range a
43 deriving (Show, Functor)
44
45 unLoc :: Located a -> a
46 unLoc (LocatedAt _ a) = a
47
48 getLoc :: Located a -> Range
49 getLoc (LocatedAt r _) = r