git.iko.soy/rowtype-yoga/purescript-native
on eada392 help sign in
80 lines 2.5 kB history raw
1-----------------------------------------------------------------------------
2--
3-- Module : Main
4-- Copyright : (c) 2013-15 Phil Freeman, (c) 2014-15 Gary Burgess
5-- License : MIT (http://opensource.org/licenses/MIT)
6--
7-- Maintainer : Phil Freeman <paf31@cantab.net>
8-- Stability : experimental
9-- Portability :
10--
11-- |
12--
13-----------------------------------------------------------------------------
15{-# LANGUAGE TemplateHaskell #-}
17module JSON where
19import Prelude ()
20import Prelude.Compat
22import qualified Data.Aeson.TH as A
24import qualified Language.PureScript as P
26data ErrorPosition = ErrorPosition
27 { startLine :: Int
28 , startColumn :: Int
29 , endLine :: Int
30 , endColumn :: Int
31 }
33data ErrorSuggestion = ErrorSuggestion { replacement :: String }
35data JSONError = JSONError
36 { position :: Maybe ErrorPosition
37 , message :: String
38 , errorCode :: String
39 , errorLink :: String
40 , filename :: Maybe String
41 , moduleName :: Maybe String
42 , suggestion :: Maybe ErrorSuggestion
43 }
45data JSONResult = JSONResult
46 { warnings :: [JSONError]
47 , errors :: [JSONError]
48 }
50$(A.deriveJSON A.defaultOptions ''ErrorPosition)
51$(A.deriveJSON A.defaultOptions ''JSONError)
52$(A.deriveJSON A.defaultOptions ''JSONResult)
53$(A.deriveJSON A.defaultOptions ''ErrorSuggestion)
56toJSONErrors :: Bool -> P.Level -> P.MultipleErrors -> [JSONError]
57toJSONErrors verbose level = map (toJSONError verbose level) . P.runMultipleErrors
59toJSONError :: Bool -> P.Level -> P.ErrorMessage -> JSONError
60toJSONError verbose level e =
61 JSONError (toErrorPosition <$> sspan)
62 (P.renderBox (P.prettyPrintSingleError verbose level False (P.stripModuleAndSpan e)))
63 (P.errorCode e)
64 (P.wikiUri e)
65 (P.spanName <$> sspan)
66 (P.runModuleName <$> P.errorModule e)
67 (toSuggestion <$> (P.errorSuggestion $ P.unwrapErrorMessage e))
68 where
69 sspan :: Maybe P.SourceSpan
70 sspan = P.errorSpan e
72 toErrorPosition :: P.SourceSpan -> ErrorPosition
73 toErrorPosition ss =
74 ErrorPosition (P.sourcePosLine (P.spanStart ss))
75 (P.sourcePosColumn (P.spanStart ss))
76 (P.sourcePosLine (P.spanEnd ss))
77 (P.sourcePosColumn (P.spanEnd ss))
78 toSuggestion :: P.ErrorSuggestion -> ErrorSuggestion
79-- TODO: Adding a newline because source spans chomp everything up to the next character
80 toSuggestion (P.ErrorSuggestion s) = ErrorSuggestion $ if null s then s else s ++ "\n"