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
15 = Parse [Parsec.Message]
21 | SettingsOnNormalModule
22 | SettingsOnPortModule
23 | DuplicateSettingOnEffectModule String
24 | BadSettingOnEffectModule String
25 | NoSettingsOnEffectModule
26 | MissingManagerOnEffectModule String
27 | UnexpectedPort String
29 | InfixDuplicate String
30 | TypeWithoutDefinition String
32 | DuplicateArgument String String
33 | DuplicateFieldName String
34 | DuplicateValueDeclaration String
35 | DuplicateTypeDeclaration String
36 | DuplicateDefinition String
37 | UnboundTypeVarsInUnion String [String] [String]
38 | UnboundTypeVarsInAlias String [String] [String]
39 | UnusedTypeVarsInAlias String [String] [String]
40 | MessyTypeVarsInAlias String [String] [String] [String]
45toReport :: RenderType.Localizer -> Error -> Report.Report
46toReport _localizer err =
49 parseErrorReport messages
51 BadFunctionName arity ->
53 "BAD FUNCTION DEFINITION"
55 "Complex patterns cannot be used as function names."
56 ( Help.reflowParagraph $
57 "This function definition has " ++ show arity ++ " arguments, but instead\
58 \ of a normal name like `add` or `reverse` it has a pattern. There is no\
59 \ way to \"deconstruct\" a function with pattern matching, so this needs to\
60 \ be changed to a normal name."
67 ("The free variable `" ++ name ++ "` appears more than once in this pattern.")
69 [ text "Rename the variables so there are no duplicates."
70 , Help.reflowParagraph $
71 "In Elm, pattern matching works by binding these free variables to subsections\
72 \ of the matching value. It does not make sense to have the same name for two\
73 \ different subsections though! When you say `" ++ name ++ "` in some code, there\
74 \ is no way for me to know which subsection you mean!"
82 ("This documentation comment is not followed by anything.")
83 ( Help.reflowParagraph $
84 "All documentation comments need to be right above the declaration they\
85 \ describe. Maybe some code got deleted or commented out by accident? Or\
86 \ maybe this comment is here by accident?"
89 SettingsOnNormalModule ->
91 "BAD MODULE DECLARATION"
93 "A normal module can expose values, but not settings like this."
94 ( Help.reflowParagraph $
95 "If you want a normal module, just remove this stuff. If you want to create\
96 \ an `effect module` you just forgot to use the `effect` keyword. In that\
97 \ case, just change `module` to `effect module` and you should be headed in\
98 \ the right direction!"
101 SettingsOnPortModule ->
103 "BAD MODULE DECLARATION"
105 "A port module can expose values, but not have settings like this."
106 ( Help.reflowParagraph $
107 "If you want a port module, just remove this stuff. If you want to create\
108 \ a custom effect module instead (which is rare) just change `port module`\
109 \ to `effect module` and you should be headed in the right direction!"
112 DuplicateSettingOnEffectModule name ->
114 "EFFECT MODULE PROBLEM"
116 ("You have defined " ++ Help.functionName name ++ " multiple times.")
117 ( Help.reflowParagraph $
118 "There can only be one " ++ Help.functionName name
119 ++ " though! Remove until there is one left."
122 BadSettingOnEffectModule name ->
124 "EFFECT MODULE PROBLEM"
126 ("Setting " ++ Help.functionName name ++ " is not recognized.")
127 ( Help.reflowParagraph $
128 "Remove this entry and you should be all set!"
131 NoSettingsOnEffectModule ->
133 "EFFECT MODULE PROBLEM"
135 ("You are defining an effect module, but it has no settings.")
136 ( Help.reflowParagraph $
137 "If you just wanted a normal module, change the keywords `effect module`\
138 \ to `module` and you should be all set. If you want a proper effect module,\
139 \ you need to specify your commands and/or subscriptions. Read more about this\
140 \ here: <http://guide.elm-lang.org/effect_managers/>"
143 MissingManagerOnEffectModule name ->
145 "EFFECT MODULE PROBLEM"
147 ("You are defining an effect module, but there is no " ++ Help.functionName name ++ " defined.")
148 ( Help.reflowParagraph $
149 "There is a small set of top-level functions and values that must be defined\
150 \ in any complete effect module. The best thing is probably to just read more\
151 \ about effect modules here:\
152 \ <http://guide.elm-lang.org/effect_managers/>"
155 UnexpectedPort name ->
159 ("You are declaring port " ++ Help.functionName name ++ " in a normal module.")
160 ( Help.reflowParagraph $
161 "All ports must be defined in a `port module`. You should probably have just\
162 \ one of these for your project. This way all of your foreign interactions\
163 \ stay relatively organized."
166 InfixDuplicate opName ->
170 ("The infix declarations for " ++ Help.functionName opName ++ " must be removed.")
171 ( Help.reflowParagraph $
172 "The precedence and associativity of a particular operator can only be set in\
173 \ one place in a code base, and " ++ Help.functionName opName
174 ++ " has already been set somewhere else."
177 TypeWithoutDefinition valueName ->
181 ("There is a type annotation for `" ++ valueName ++ "` but there"
182 ++ " is no corresponding definition!"
185 "Directly below the type annotation, put a definition like:\n\n"
186 ++ " " ++ valueName ++ " = 42"
189 DuplicateArgument funcName argName ->
193 ( "The name `" ++ argName
194 ++ "` is used more than once in the arguments of `"
197 ( Help.reflowParagraph $
198 "Rename things until `" ++ argName ++ "` is used only once.\
199 \ Otherwise how can we tell which one you want when you\
200 \ say `" ++ argName ++ "` it in the body of your function?"
203 DuplicateFieldName name ->
207 ("This record has more than one field named `" ++ name ++ "`.")
208 (text "There can only be one. Do some renaming to make sure the names are distinct!")
210 DuplicateValueDeclaration name ->
212 "DUPLICATE DEFINITION"
214 ( "Naming multiple top-level values `" ++ name ++ "` makes things\n"
215 ++ "ambiguous. When you say `" ++ name ++ "` which one do you want?"
217 ( Help.reflowParagraph $
218 "Find all the top-level values named " ++ Help.functionName name
219 ++ " and do some renaming. Make sure the names are distinct!"
222 DuplicateTypeDeclaration name ->
224 "DUPLICATE DEFINITION"
226 ( "There are multiple types named `" ++ name ++ "` in this module."
228 ( Help.reflowParagraph $
229 "Search through this module, find all the types named `" ++ name
230 ++ "`, and give each of them a unique name."
233 DuplicateDefinition name ->
235 "DUPLICATE DEFINITION"
237 ("There are multiple values named `" ++ name ++ "` in this let-expression."
239 ( Help.reflowParagraph $
240 "Search through this let-expression, find all the values named `" ++ name
241 ++ "`, and give each of them a unique name."
244 UnboundTypeVarsInUnion typeName givenVars unbound ->
245 unboundTypeVars "type" typeName givenVars unbound
247 UnboundTypeVarsInAlias typeName givenVars unbound ->
248 unboundTypeVars "type alias" typeName givenVars unbound
250 UnusedTypeVarsInAlias typeName givenVars unused ->
252 "UNUSED TYPE VARIABLES"
254 ( "Type alias `" ++ typeName ++ "` cannot have unused type variables: "
255 ++ Help.commaSep unused
258 [ text "You probably need to change the declaration like this:"
259 , dullyellow $ hsep $
260 map text ("type" : "alias" : typeName : filter (`notElem` unused) givenVars ++ ["=", "..."])
264 MessyTypeVarsInAlias typeName givenVars unused unbound ->
266 "TYPE VARIABLE PROBLEMS"
268 ( "Type alias `" ++ typeName ++ "` has some problems with type variables."
271 [ Help.reflowParagraph $
272 "The declaration says it uses certain type variables ("
273 ++ Help.commaSep unused ++ ") but they do not appear in the aliased type. "
274 ++ "Furthermore, the aliased type says it uses type variables ("
275 ++ Help.commaSep unbound
276 ++ ") that do not appear in the declaration."
277 , text "You probably need to change the declaration like this:"
278 , dullyellow $ hsep $
279 map text ("type" : "alias" : typeName : filter (`notElem` unused) givenVars ++ unbound ++ ["=", "..."])
284unboundTypeVars :: String -> String -> [String] -> [String] -> Report.Report
285unboundTypeVars declKind typeName givenVars unboundVars =
287 "UNBOUND TYPE VARIABLES"
289 ( Help.capitalize declKind ++ " `" ++ typeName ++ "` must declare its use of type variable"
290 ++ Help.commaSep unboundVars
293 [ text "You probably need to change the declaration like this:"
295 map text (declKind : typeName : givenVars)
296 ++ map (dullyellow . text) unboundVars
297 ++ map text ["=", "..."]
298 , Help.reflowParagraph $
299 "Here's why. Imagine one `" ++ typeName ++ "` where `" ++ head unboundVars ++
300 "` is an Int and another where it is a Bool. When we explicitly list the type\
301 \ variables, the type checker can see that they are actually different types."
307-- TAGGING PARSE ERRORS
315freshLine = "FRESH_LINE"
319whitespace = "WHITESPACE"
326keyword :: String -> String
336extractSpecialMessage :: String -> Maybe SpecialMessage
337extractSpecialMessage message =
338 if List.isPrefixOf "KEYWORD=" message then
339 Just $ MsgKeyword (drop (length "KEYWORD=") message)
341 else if tab == message then
348-- REPORTING PARSE ERRORS
350parseErrorReport :: [Parsec.Message] -> Report.Report
351parseErrorReport messages =
353 addMsg message hint =
355 Parsec.SysUnExpect _msg ->
358 Parsec.UnExpect _msg ->
364 if msg `elem` [whitespace, newline, freshLine] then
370 -- Our parser looks for tab so it can throw a custom tab error message.
371 -- Exclude tabs from the list of expected tokens so that no one thinks they are supported.
374 hint { _expected = Set.insert msg' (_expected hint) }
376 Parsec.Message msg ->
377 hint { _messages = msg : _messages hint }
379 (ParseHint msgs expects) =
380 foldr addMsg emptyHint messages
382 (preHint, maybePost) =
385 case extractSpecialMessage msg of
386 Just (MsgKeyword kwd) ->
387 ( "It looks like the keyword `" ++ kwd ++ "` is being used as a variable."
388 , Just "Rename it to something else."
392 ( "A tab character was found, but all whitespace (including indentation) must be spaces not tabs."
393 , Just "I am looking for spaces, not tabs."
402 ( "I ran into something unexpected when parsing your code!"
407 if Set.null expects then
408 "Maybe <http://elm-lang.org/docs/syntax> can help you figure it out."
411 "I am looking for one of the following things:\n"
412 ++ concatMap ("\n "++) (Set.toList expects)
418 (text (maybe postHint id maybePost))
421data ParseHint = ParseHint
422 { _messages :: [String]
423 , _expected :: Set.Set String
428emptyHint :: ParseHint
430 ParseHint [] Set.empty
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list