forked from purescript/purescript
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathJSON.hs
More file actions
69 lines (57 loc) · 1.92 KB
/
JSON.hs
File metadata and controls
69 lines (57 loc) · 1.92 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
-----------------------------------------------------------------------------
--
-- Module : Main
-- Copyright : (c) 2013-15 Phil Freeman, (c) 2014-15 Gary Burgess
-- License : MIT (http://opensource.org/licenses/MIT)
--
-- Maintainer : Phil Freeman <paf31@cantab.net>
-- Stability : experimental
-- Portability :
--
-- |
--
-----------------------------------------------------------------------------
{-# LANGUAGE TemplateHaskell #-}
module JSON where
import Prelude ()
import Prelude.Compat
import qualified Data.Aeson.TH as A
import qualified Language.PureScript as P
data ErrorPosition = ErrorPosition
{ startLine :: Int
, startColumn :: Int
, endLine :: Int
, endColumn :: Int
}
data JSONError = JSONError
{ position :: Maybe ErrorPosition
, message :: String
, errorCode :: String
, filename :: Maybe String
, moduleName :: Maybe String
}
data JSONResult = JSONResult
{ warnings :: [JSONError]
, errors :: [JSONError]
}
$(A.deriveJSON A.defaultOptions ''ErrorPosition)
$(A.deriveJSON A.defaultOptions ''JSONError)
$(A.deriveJSON A.defaultOptions ''JSONResult)
toJSONErrors :: Bool -> P.Level -> P.MultipleErrors -> [JSONError]
toJSONErrors verbose level = map (toJSONError verbose level) . P.runMultipleErrors
toJSONError :: Bool -> P.Level -> P.ErrorMessage -> JSONError
toJSONError verbose level e =
JSONError (toErrorPosition <$> sspan)
(P.renderBox (P.prettyPrintSingleError verbose level (P.stripModuleAndSpan e)))
(P.errorCode e)
(P.spanName <$> sspan)
(P.runModuleName <$> P.errorModule e)
where
sspan :: Maybe P.SourceSpan
sspan = P.errorSpan e
toErrorPosition :: P.SourceSpan -> ErrorPosition
toErrorPosition ss =
ErrorPosition (P.sourcePosLine (P.spanStart ss))
(P.sourcePosColumn (P.spanStart ss))
(P.sourcePosLine (P.spanEnd ss))
(P.sourcePosColumn (P.spanEnd ss))