forked from purescript/purescript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSourcePos.hs
More file actions
81 lines (69 loc) · 1.81 KB
/
SourcePos.hs
File metadata and controls
81 lines (69 loc) · 1.81 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
-- |
-- Source position information
--
module Language.PureScript.AST.SourcePos where
import Prelude.Compat
import Data.Aeson ((.=), (.:))
import qualified Data.Aeson as A
import Data.Monoid
import qualified Data.Text as T
import Data.Text (Text)
-- |
-- Source position information
--
data SourcePos = SourcePos
{ -- |
-- Line number
--
sourcePosLine :: Int
-- |
-- Column number
--
, sourcePosColumn :: Int
} deriving (Show, Eq, Ord)
displaySourcePos :: SourcePos -> Text
displaySourcePos sp =
"line " <> T.pack (show (sourcePosLine sp)) <>
", column " <> T.pack (show (sourcePosColumn sp))
instance A.ToJSON SourcePos where
toJSON SourcePos{..} =
A.toJSON [sourcePosLine, sourcePosColumn]
instance A.FromJSON SourcePos where
parseJSON arr = do
[line, col] <- A.parseJSON arr
return $ SourcePos line col
data SourceSpan = SourceSpan
{ -- |
-- Source name
--
spanName :: String
-- |
-- Start of the span
--
, spanStart :: SourcePos
-- End of the span
--
, spanEnd :: SourcePos
} deriving (Show, Eq, Ord)
displayStartEndPos :: SourceSpan -> Text
displayStartEndPos sp =
displaySourcePos (spanStart sp) <> " - " <>
displaySourcePos (spanEnd sp)
displaySourceSpan :: SourceSpan -> Text
displaySourceSpan sp =
T.pack (spanName sp) <> " " <>
displayStartEndPos sp
instance A.ToJSON SourceSpan where
toJSON SourceSpan{..} =
A.object [ "name" .= spanName
, "start" .= spanStart
, "end" .= spanEnd
]
instance A.FromJSON SourceSpan where
parseJSON = A.withObject "SourceSpan" $ \o ->
SourceSpan <$>
o .: "name" <*>
o .: "start" <*>
o .: "end"
internalModuleSourceSpan :: String -> SourceSpan
internalModuleSourceSpan name = SourceSpan name (SourcePos 0 0) (SourcePos 0 0)