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 | TypeWithoutDefinition String
31 | DuplicateArgument String String
32 | DuplicateFieldName String
33 | DuplicateValueDeclaration String
34 | DuplicateTypeDeclaration String
35 | DuplicateDefinition String
36 | UnboundTypeVarsInUnion String [String] [String]
37 | UnboundTypeVarsInAlias String [String] [String]
38 | UnusedTypeVarsInAlias String [String] [String]
39 | MessyTypeVarsInAlias String [String] [String] [String]
44toReport :: RenderType.Localizer -> Error -> Report.Report
45toReport _localizer err =
48 parseErrorReport messages
50 BadFunctionName arity ->
52 "BAD FUNCTION DEFINITION"
54 "Complex patterns cannot be used as function names."
55 ( Help.reflowParagraph $
56 "This function definition has " ++ show arity ++ " arguments, but instead\
57 \ of a normal name like `add` or `reverse` it has a pattern. There is no\
58 \ way to \"deconstruct\" a function with pattern matching, so this needs to\
59 \ be changed to a normal name."
66 ("The free variable `" ++ name ++ "` appears more than once in this pattern.")
68 [ text "Rename the variables so there are no duplicates."
69 , Help.reflowParagraph $
70 "In Elm, pattern matching works by binding these free variables to subsections\
71 \ of the matching value. It does not make sense to have the same name for two\
72 \ different subsections though! When you say `" ++ name ++ "` in some code, there\
73 \ is no way for me to know which subsection you mean!"
81 ("This documentation comment is not followed by anything.")
82 ( Help.reflowParagraph $
83 "All documentation comments need to be right above the declaration they\
84 \ describe. Maybe some code got deleted or commented out by accident? Or\
85 \ maybe this comment is here by accident?"
88 SettingsOnNormalModule ->
90 "BAD MODULE DECLARATION"
92 "A normal module can expose values, but not settings like this."
93 ( Help.reflowParagraph $
94 "If you want a normal module, just remove this stuff. If you want to create\
95 \ an `effect module` you just forgot to use the `effect` keyword. In that\
96 \ case, just change `module` to `effect module` and you should be headed in\
97 \ the right direction!"
100 SettingsOnPortModule ->
102 "BAD MODULE DECLARATION"
104 "A port module can expose values, but not have settings like this."
105 ( Help.reflowParagraph $
106 "If you want a port module, just remove this stuff. If you want to create\
107 \ a custom effect module instead (which is rare) just change `port module`\
108 \ to `effect module` and you should be headed in the right direction!"
111 DuplicateSettingOnEffectModule name ->
113 "EFFECT MODULE PROBLEM"
115 ("You have defined " ++ Help.functionName name ++ " multiple times.")
116 ( Help.reflowParagraph $
117 "There can only be one " ++ Help.functionName name
118 ++ " though! Remove until there is one left."
121 BadSettingOnEffectModule name ->
123 "EFFECT MODULE PROBLEM"
125 ("Setting " ++ Help.functionName name ++ " is not recognized.")
126 ( Help.reflowParagraph $
127 "Remove this entry and you should be all set!"
130 NoSettingsOnEffectModule ->
132 "EFFECT MODULE PROBLEM"
134 ("You are defining an effect module, but it has no settings.")
135 ( Help.reflowParagraph $
136 "If you just wanted a normal module, change the keywords `effect module`\
137 \ to `module` and you should be all set. If you want a proper effect module,\
138 \ you need to specify your commands and/or subscriptions. Read more about this\
139 \ here: <http://guide.elm-lang.org/effect_managers/>"
142 MissingManagerOnEffectModule name ->
144 "EFFECT MODULE PROBLEM"
146 ("You are defining an effect module, but there is no " ++ Help.functionName name ++ " defined.")
147 ( Help.reflowParagraph $
148 "There is a small set of top-level functions and values that must be defined\
149 \ in any complete effect module. The best thing is probably to just read more\
150 \ about effect modules here:\
151 \ <http://guide.elm-lang.org/effect_managers/>"
154 UnexpectedPort name ->
158 ("You are declaring port " ++ Help.functionName name ++ " in a normal module.")
159 ( Help.reflowParagraph $
160 "All ports must be defined in a `port module`. You should probably have just\
161 \ one of these for your project. This way all of your foreign interactions\
162 \ stay relatively organized."
165 TypeWithoutDefinition valueName ->
169 ("There is a type annotation for `" ++ valueName ++ "` but there"
170 ++ " is no corresponding definition!"
173 "Directly below the type annotation, put a definition like:\n\n"
174 ++ " " ++ valueName ++ " = 42"
177 DuplicateArgument funcName argName ->
181 ( "The name `" ++ argName
182 ++ "` is used more than once in the arguments of `"
185 ( Help.reflowParagraph $
186 "Rename things until `" ++ argName ++ "` is used only once.\
187 \ Otherwise how can we tell which one you want when you\
188 \ say `" ++ argName ++ "` it in the body of your function?"
191 DuplicateFieldName name ->
195 ("This record has more than one field named `" ++ name ++ "`.")
196 (text "There can only be one. Do some renaming to make sure the names are distinct!")
198 DuplicateValueDeclaration name ->
200 "DUPLICATE DEFINITION"
202 ( "Naming multiple top-level values `" ++ name ++ "` makes things\n"
203 ++ "ambiguous. When you say `" ++ name ++ "` which one do you want?"
205 ( Help.reflowParagraph $
206 "Find all the top-level values named " ++ Help.functionName name
207 ++ " and do some renaming. Make sure the names are distinct!"
210 DuplicateTypeDeclaration name ->
212 "DUPLICATE DEFINITION"
214 ( "There are multiple types named `" ++ name ++ "` in this module."
216 ( Help.reflowParagraph $
217 "Search through this module, find all the types named `" ++ name
218 ++ "`, and give each of them a unique name."
221 DuplicateDefinition name ->
223 "DUPLICATE DEFINITION"
225 ("There are multiple values named `" ++ name ++ "` in this let-expression."
227 ( Help.reflowParagraph $
228 "Search through this let-expression, find all the values named `" ++ name
229 ++ "`, and give each of them a unique name."
232 UnboundTypeVarsInUnion typeName givenVars unbound ->
233 unboundTypeVars "type" typeName givenVars unbound
235 UnboundTypeVarsInAlias typeName givenVars unbound ->
236 unboundTypeVars "type alias" typeName givenVars unbound
238 UnusedTypeVarsInAlias typeName givenVars unused ->
240 "UNUSED TYPE VARIABLES"
242 ( "Type alias `" ++ typeName ++ "` cannot have unused type variables: "
243 ++ Help.commaSep unused
246 [ text "You probably need to change the declaration like this:"
247 , dullyellow $ hsep $
248 map text ("type" : "alias" : typeName : filter (`notElem` unused) givenVars ++ ["=", "..."])
252 MessyTypeVarsInAlias typeName givenVars unused unbound ->
254 "TYPE VARIABLE PROBLEMS"
256 ( "Type alias `" ++ typeName ++ "` has some problems with type variables."
259 [ Help.reflowParagraph $
260 "The declaration says it uses certain type variables ("
261 ++ Help.commaSep unused ++ ") but they do not appear in the aliased type. "
262 ++ "Furthermore, the aliased type says it uses type variables ("
263 ++ Help.commaSep unbound
264 ++ ") that do not appear in the declaration."
265 , text "You probably need to change the declaration like this:"
266 , dullyellow $ hsep $
267 map text ("type" : "alias" : typeName : filter (`notElem` unused) givenVars ++ unbound ++ ["=", "..."])
272unboundTypeVars :: String -> String -> [String] -> [String] -> Report.Report
273unboundTypeVars declKind typeName givenVars unboundVars =
275 "UNBOUND TYPE VARIABLES"
277 ( Help.capitalize declKind ++ " `" ++ typeName ++ "` must declare its use of type variable"
278 ++ Help.commaSep unboundVars
281 [ text "You probably need to change the declaration like this:"
283 map text (declKind : typeName : givenVars)
284 ++ map (dullyellow . text) unboundVars
285 ++ map text ["=", "..."]
286 , Help.reflowParagraph $
287 "Here's why. Imagine one `" ++ typeName ++ "` where `" ++ head unboundVars ++
288 "` is an Int and another where it is a Bool. When we explicitly list the type\
289 \ variables, the type checker can see that they are actually different types."
295-- TAGGING PARSE ERRORS
303freshLine = "FRESH_LINE"
307whitespace = "WHITESPACE"
314singleQuotes :: String
315singleQuotes = "SINGLE_QUOTES"
318keyword :: String -> String
323prime :: String -> String
335extractSpecialMessage :: String -> Maybe SpecialMessage
336extractSpecialMessage message =
337 if List.isPrefixOf "KEYWORD=" message then
338 Just $ MsgKeyword (drop (length "KEYWORD=") message)
340 else if List.isPrefixOf "PRIME=" message then
341 Just $ MsgPrime (drop (length "PRIME=") message)
343 else if tab == message then
346 else if singleQuotes == message then
353-- REPORTING PARSE ERRORS
355parseErrorReport :: [Parsec.Message] -> Report.Report
356parseErrorReport messages =
358 addMsg message hint =
360 Parsec.SysUnExpect _msg ->
363 Parsec.UnExpect _msg ->
369 if msg `elem` [whitespace, newline, freshLine] then
375 -- Our parser looks for tab so it can throw a custom tab error message.
376 -- Exclude tabs from the list of expected tokens so that no one thinks they are supported.
379 hint { _expected = Set.insert msg' (_expected hint) }
381 Parsec.Message msg ->
382 hint { _messages = msg : _messages hint }
384 (ParseHint msgs expects) =
385 foldr addMsg emptyHint messages
387 (preHint, maybePost) =
390 case extractSpecialMessage msg of
391 Just (MsgKeyword kwd) ->
392 ( "It looks like the keyword `" ++ kwd ++ "` is being used as a variable."
393 , Just "Rename it to something else."
396 Just (MsgPrime var) ->
397 ( "Ran into a single quote in a variable name. This was removed in 0.18!"
399 "Change it to a number or an underscore, like " ++ var ++ "_ or " ++ var
400 ++ "1\n\nOr better yet, choose a more descriptive name!"
404 ( "A tab character was found, but all whitespace (including indentation) must be spaces not tabs."
405 , Just "I am looking for spaces, not tabs."
408 Just MsgSingleQuotes ->
409 ( "It likes you are trying to create a string with single quotes."
410 , Just "Switch from ' to \" and you should be all set!"
419 ( "I ran into something unexpected when parsing your code!"
424 if Set.null expects then
425 "Maybe <http://elm-lang.org/docs/syntax> can help you figure it out."
428 "I am looking for one of the following things:\n"
429 ++ concatMap ("\n "++) (Set.toList expects)
435 (text (maybe postHint id maybePost))
438data ParseHint = ParseHint
439 { _messages :: [String]
440 , _expected :: Set.Set String
445emptyHint :: ParseHint
447 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