@@ -1,14 +1,28 @@
1{-# OPTIONS_GHC -Wall #-}
1{-# OPTIONS_GHC -Wall #-}
2{-# LANGUAGE OverloadedStrings #-}
2{-# LANGUAGE OverloadedStrings #-}
3module Reporting.Error
3module Reporting.Error
4− ( Error(..)
5+ , Error(..)
12+import qualified Data.ByteString as B
13+import qualified Data.Name as Name
14+import qualified Data.NonEmptyList as NE
15+import qualified Data.OneOrMore as OneOrMore
16+import qualified Data.Utf8 as Utf8
18+import qualified Elm.ModuleName as ModuleName
20+import qualified Json.Encode as E
21+import Json.Encode ((==>))
22+import qualified Reporting.Annotation as A
23+import qualified Reporting.Doc as D
10import qualified Reporting.Error.Canonicalize as Canonicalize
24import qualified Reporting.Error.Canonicalize as Canonicalize
11−import qualified Reporting.Error.Docs as Docs
25+import qualified Reporting.Error.Import as Import
12import qualified Reporting.Error.Main as Main
26import qualified Reporting.Error.Main as Main
13import qualified Reporting.Error.Pattern as Pattern
27import qualified Reporting.Error.Pattern as Pattern
14import qualified Reporting.Error.Syntax as Syntax
28import qualified Reporting.Error.Syntax as Syntax
@@ -19,39 +33,176 @@ import qualified Reporting.Report as Report
41+ { _name :: ModuleName.Raw
44+ , _source :: B.ByteString
46+ , _localizer :: L.Localizer
22−-- ALL POSSIBLE ERRORS
51+-- ERRORS
26− = Syntax Syntax.Error
55+ = BadSyntax Syntax.Error
27− | Canonicalize Canonicalize.Error
56+ | BadImports (NE.List Import.Error)
28− | Type L.Localizer [Type.Error]
57+ | BadNames (OneOrMore.OneOrMore Canonicalize.Error)
29− | Main L.Localizer Main.Error
58+ | BadTypes (NE.List Type.Error)
30− | Pattern [Pattern.Error]
59+ | BadPatterns (NE.List Pattern.Error)
31− | Docs Docs.Error
60+ | BadMains (OneOrMore.OneOrMore Main.Error)
35-- TO REPORT
64-- TO REPORT
38−toReports :: Code.Source -> Error -> [Report.Report]
67+toReports :: L.Localizer -> Code.Source -> Error -> NE.List Report.Report
39−toReports source err =
68+toReports localizer source err =
40 case err of
69 case err of
41− Syntax syntaxError ->
70+ BadSyntax syntaxError ->
42− [Syntax.toReport source syntaxError]
71+ NE.List (Syntax.toReport source syntaxError) []
74+ fmap Import.toReport errs
77+ fmap (Canonicalize.toReport source) (OneOrMore.destruct NE.List errs)
80+ fmap (Type.toReport source localizer) errs
83+ fmap (Pattern.toReport source) errs
86+ fmap (Main.toReport localizer source) (OneOrMore.destruct NE.List errs)
93+toDoc :: NE.List Module -> D.Doc
96+ timeCompare m1 m2 = compare (_time m1) (_time m2)
97+ (NE.List m ms) = NE.sortBy timeCompare modules
99+ D.vcat (toDocHelp m ms)
102+toDocHelp :: Module -> [Module] -> [D.Doc]
103+toDocHelp module1 modules =
106+ [moduleToDoc module1]
108+ module2 : otherModules ->
110+ : toSeparator module1 module2
111+ : toDocHelp module2 otherModules
114+toSeparator :: Module -> Module -> D.Doc
115+toSeparator beforeModule afterModule =
117+ before = ModuleName.toChars (_name beforeModule) ++ " ↑ "
118+ after = " ↓ " ++ ModuleName.toChars (_name afterModule)
120+ D.dullred $ D.vcat $
121+ [ D.indent (80 - length before) (D.fromChars before)
122+ , "====o======================================================================o===="
133+moduleToDoc :: Module -> D.Doc
134+moduleToDoc (Module _ path _ source err localizer) =
137+ toReports localizer (Code.toSource source) err
139+ D.vcat $ map (reportToDoc path) (NE.toList reports)
142+reportToDoc :: FilePath -> Report.Report -> D.Doc
143+reportToDoc path (Report.Report title _ _ message) =
145+ [ toMessageBar title path
152+toMessageBar :: String -> FilePath -> D.Doc
153+toMessageBar title filePath =
156+ 4 + length title + 1 + length filePath
158+ D.dullcyan $ D.fromChars $
160+ ++ " " ++ replicate (max 1 (80 - usedSpace)) '-'
44− Canonicalize canonicalizeError ->
45− [Canonicalize.toReport source canonicalizeError]
47− Type localizer typeErrors ->
168+toJson :: NE.List Module -> E.Value
48− map (Type.toReport source localizer) typeErrors
169+toJson modules =
170+ E.array (map moduleToJson (NE.toList modules))
50− Main localizer mainError ->
51− [Main.toReport localizer source mainError]
53− Pattern patternErrors ->
173+moduleToJson :: Module -> E.Value
54− map (Pattern.toReport source) patternErrors
174+moduleToJson (Module name path _ source err localizer) =
177+ toReports localizer (Code.toSource source) err
180+ [ "path" ==> E.string (Utf8.fromChars path)
181+ , "name" ==> E.string (Name.toUtf8 name)
182+ , "problems" ==> E.array (map reportToJson (NE.toList reports))
56− Docs docsError ->
185+
57− [Docs.toReport source docsError]
186+reportToJson :: Report.Report -> E.Value
187+reportToJson (Report.Report title region _sgstns message) =
189+ [ "title" ==> E.string (Utf8.fromChars title)
190+ , "region" ==> encodeRegion region
191+ , "message" ==> D.encode message
195+encodeRegion :: A.Region -> E.Value
196+encodeRegion (A.Region (A.Position sr sc) (A.Position er ec)) =
200+ [ ("line", E.int (fromIntegral sr))
201+ , ("column", E.int (fromIntegral sc))
205+ [ ("line", E.int (fromIntegral er))
206+ , ("column", E.int (fromIntegral ec))