on ebde691 help sign in
elk/src/Reporting/Error/Syntax.hs
326 lines 11 kB history raw
1{-# OPTIONS_GHC -Wall #-}
2module Reporting.Error.Syntax where
4import qualified Data.List as List
5import qualified Data.Set as Set
6import qualified Text.Parsec.Error as Parsec
7import Text.PrettyPrint.ANSI.Leijen (dullyellow, hsep, text)
9import qualified Reporting.Error.Helpers as Help
10import qualified Reporting.Render.Type as RenderType
11import qualified Reporting.Report as Report
14data Error
15 = Parse [Parsec.Message]
16 | BadFunctionName Int
17 | BadPattern String
18 | InfixDuplicate String
19 | TypeWithoutDefinition String
20 | PortWithoutAnnotation String
21 | UnexpectedPort
22 | DuplicateFieldName String
23 | DuplicateValueDeclaration String
24 | DuplicateTypeDeclaration String
25 | DuplicateDefinition String
26 | UnboundTypeVarsInUnion String [String] [String]
27 | UnboundTypeVarsInAlias String [String] [String]
28 | UnusedTypeVarsInAlias String [String] [String]
29 | MessyTypeVarsInAlias String [String] [String] [String]
32-- TO REPORT
34toReport :: RenderType.Localizer -> Error -> Report.Report
35toReport _localizer err =
36 case err of
37 Parse messages ->
38 parseErrorReport messages
40 BadFunctionName arity ->
41 Report.report
42 "BAD FUNCTION DEFINITION"
43 Nothing
44 "Complex patterns cannot be used as function names."
45 ( Help.reflowParagraph $
46 "This function definition has " ++ show arity ++ " arguments, but instead\
47 \ of a normal name like `add` or `reverse` it has a pattern. There is no\
48 \ way to \"deconstruct\" a function with pattern matching, so this needs to\
49 \ be changed to a normal name."
50 )
52 BadPattern name ->
53 Report.report
54 "BAD PATTERN"
55 Nothing
56 ("The free variable `" ++ name ++ "` appears more than once in this pattern.")
57 ( Help.stack
58 [ text "Rename the variables so there are no duplicates."
59 , Help.reflowParagraph $
60 "In Elm, pattern matching works by binding these free variables to subsections\
61 \ of the matching value. It does not make sense to have the same name for two\
62 \ different subsections though! When you say `" ++ name ++ "` in some code, there\
63 \ is no way for me to know which subsection you mean!"
64 ]
65 )
67 InfixDuplicate opName ->
68 Report.report
69 "INFIX OVERLAP"
70 Nothing
71 ("The infix declarations for " ++ Help.functionName opName ++ " must be removed.")
72 ( Help.reflowParagraph $
73 "The precedence and associativity of a particular operator can only be set in\
74 \ one place in a code base, and " ++ Help.functionName opName
75 ++ " has already been set somewhere else."
76 )
78 TypeWithoutDefinition valueName ->
79 Report.report
80 "MISSING DEFINITION"
81 Nothing
82 ("There is a type annotation for `" ++ valueName ++ "` but there"
83 ++ " is no corresponding definition!"
84 )
85 ( text $
86 "Directly below the type annotation, put a definition like:\n\n"
87 ++ " " ++ valueName ++ " = 42"
88 )
90 PortWithoutAnnotation portName ->
91 Report.report
92 "PORT ERROR"
93 Nothing
94 ("Port `" ++ portName ++ "` does not have a type annotation!")
95 ( text $
96 "Directly above the port definition, I need something like this:\n\n"
97 ++ " port " ++ portName ++ " : Signal Int"
98 )
101 UnexpectedPort ->
102 Report.report
103 "PORT ERROR"
104 Nothing
105 "This module has ports, but ports can only appear in the main module."
106 ( Help.reflowParagraph $
107 "Ports in library code would create hidden dependencies where importing a\
108 \ module could bring in constraints not captured in the public API. Furthermore,\
109 \ if the module is imported twice, do we send values out the port twice?"
110 )
112 DuplicateFieldName name ->
113 Report.report
114 "DUPLICATE FIELD"
115 Nothing
116 ("This record has more than one field named `" ++ name ++ "`.")
117 (text "There can only be one. Do some renaming to make sure the names are distinct!")
119 DuplicateValueDeclaration name ->
120 Report.report
121 "DUPLICATE DEFINITION"
122 Nothing
123 ( "Naming multiple top-level values `" ++ name ++ "` makes things\n"
124 ++ "ambiguous. When you say `" ++ name ++ "` which one do you want?"
125 )
126 ( Help.reflowParagraph $
127 "Find all the top-level values named " ++ Help.functionName name
128 ++ " and do some renaming. Make sure the names are distinct!"
129 )
131 DuplicateTypeDeclaration name ->
132 Report.report
133 "DUPLICATE DEFINITION"
134 Nothing
135 ( "There are multiple types named `" ++ name ++ "` in this module."
136 )
137 ( Help.reflowParagraph $
138 "Search through this module, find all the types named `" ++ name
139 ++ "`, and give each of them a unique name."
140 )
142 DuplicateDefinition name ->
143 Report.report
144 "DUPLICATE DEFINITION"
145 Nothing
146 ("There are multiple values named `" ++ name ++ "` in this let-expression."
147 )
148 ( Help.reflowParagraph $
149 "Search through this let-expression, find all the values named `" ++ name
150 ++ "`, and give each of them a unique name."
151 )
153 UnboundTypeVarsInUnion typeName givenVars unbound ->
154 unboundTypeVars "type" typeName givenVars unbound
156 UnboundTypeVarsInAlias typeName givenVars unbound ->
157 unboundTypeVars "type alias" typeName givenVars unbound
159 UnusedTypeVarsInAlias typeName givenVars unused ->
160 Report.report
161 "UNUSED TYPE VARIABLES"
162 Nothing
163 ( "Type alias `" ++ typeName ++ "` cannot have unused type variables: "
164 ++ Help.commaSep unused
165 )
166 ( Help.stack
167 [ text "You probably need to change the declaration like this:"
168 , dullyellow $ hsep $
169 map text ("type" : "alias" : typeName : filter (`notElem` unused) givenVars ++ ["=", "..."])
170 ]
171 )
173 MessyTypeVarsInAlias typeName givenVars unused unbound ->
174 Report.report
175 "TYPE VARIABLE PROBLEMS"
176 Nothing
177 ( "Type alias `" ++ typeName ++ "` has some problems with type variables."
178 )
179 ( Help.stack
180 [ Help.reflowParagraph $
181 "The declaration says it uses certain type variables ("
182 ++ Help.commaSep unused ++ ") but they do not appear in the aliased type. "
183 ++ "Furthermore, the aliased type says it uses type variables ("
184 ++ Help.commaSep unbound
185 ++ ") that do not appear in the declaration."
186 , text "You probably need to change the declaration like this:"
187 , dullyellow $ hsep $
188 map text ("type" : "alias" : typeName : filter (`notElem` unused) givenVars ++ unbound ++ ["=", "..."])
189 ]
190 )
193unboundTypeVars :: String -> String -> [String] -> [String] -> Report.Report
194unboundTypeVars declKind typeName givenVars unboundVars@(tvar:_) =
195 Report.report
196 "UNBOUND TYPE VARIABLES"
197 Nothing
198 ( Help.capitalize declKind ++ " `" ++ typeName ++ "` must declare its use of type variable"
199 ++ Help.commaSep unboundVars
200 )
201 ( Help.stack
202 [ text "You probably need to change the declaration like this:"
203 , hsep $
204 map text (declKind : typeName : givenVars)
205 ++ map (dullyellow . text) unboundVars
206 ++ map text ["=", "..."]
207 , Help.reflowParagraph $
208 "Here's why. Imagine one `" ++ typeName ++ "` where `" ++ tvar ++ "` is an Int and\
209 \ another where it is a Bool. When we explicitly list the type variables, type\
210 \ checker can see that they are actually different types."
211 ]
212 )
216-- TAGGING PARSE ERRORS
219newline :: String
220newline = "NEWLINE"
223freshLine :: String
224freshLine = "FRESH_LINE"
227whitespace :: String
228whitespace = "WHITESPACE"
231tab :: String
232tab = "TAB"
235keyword :: String -> String
236keyword kwd =
237 "KEYWORD=" ++ kwd
240unkeyword :: String -> Maybe String
241unkeyword message =
242 if List.isPrefixOf "KEYWORD=" message then
243 Just (drop (length "KEYWORD=") message)
245 else
246 Nothing
249-- REPORTING PARSE ERRORS
251parseErrorReport :: [Parsec.Message] -> Report.Report
252parseErrorReport messages =
253 let
254 addMsg message hint =
255 case message of
256 Parsec.SysUnExpect _msg ->
257 hint
259 Parsec.UnExpect _msg ->
260 hint
262 Parsec.Expect msg ->
263 let
264 msg' =
265 if msg `elem` [whitespace, newline, freshLine] then
266 "whitespace"
267 else
268 msg
269 in
270 if msg == tab then
271 -- Our parser looks for tab so it can throw a custom tab error message.
272 -- Exclude tabs from the list of expected tokens so that no one thinks they are supported.
273 hint
274 else
275 hint { _expected = Set.insert msg' (_expected hint) }
277 Parsec.Message msg ->
278 hint { _messages = msg : _messages hint }
280 (ParseHint msgs expects) =
281 foldr addMsg emptyHint messages
283 (preHint, maybePost) =
284 case msgs of
285 [msg] ->
286 case unkeyword msg of
287 Just kwd ->
288 ( "It looks like the keyword `" ++ kwd ++ "` is being used as a variable."
289 , Just "Rename it to something else."
290 )
292 Nothing ->
293 ( msg
294 , Nothing
295 )
297 _ ->
298 ( "I ran into something unexpected when parsing your code!"
299 , Nothing
300 )
302 postHint =
303 if Set.null expects then
304 "Maybe <http://elm-lang.org/docs/syntax> can help you figure it out."
306 else
307 "I am looking for one of the following things:\n"
308 ++ concatMap ("\n "++) (Set.toList expects)
309 in
310 Report.report
311 "SYNTAX PROBLEM"
312 Nothing
313 preHint
314 (text (maybe postHint id maybePost))
317data ParseHint = ParseHint
318 { _messages :: [String]
319 , _expected :: Set.Set String
320 }
321 deriving (Show)
324emptyHint :: ParseHint
325emptyHint =
326 ParseHint [] Set.empty