1{-# OPTIONS_GHC -Wall #-}
2{-# LANGUAGE OverloadedStrings #-}
3module Reporting.Error.Syntax
6 , Problem(..), Theory(..)
11import qualified Data.Set as Set
12import Data.Text (Text)
14import qualified Reporting.Render.Type as RenderType
15import qualified Reporting.Region as Region
16import qualified Reporting.Report as Report
17import qualified Reporting.Helpers as Help
18import Reporting.Helpers ((<>), dullyellow, hsep, i2t, reflowParagraph, text)
32 | SettingsOnNormalModule
33 | SettingsOnPortModule
34 | DuplicateSettingOnEffectModule Text
35 | BadSettingOnEffectModule Text
36 | NoSettingsOnEffectModule
37 | MissingManagerOnEffectModule Text
40 | TypeWithoutDefinition Text
42 | DuplicateArgument Text Text
43 | DuplicateFieldName Region.Region Text
44 | DuplicateValueDeclaration Text
45 | DuplicateTypeDeclaration Text
46 | DuplicateDefinition Text
47 | UnboundTypeVarsInUnion Text [Text] [Text]
48 | UnboundTypeVarsInAlias Text [Text] [Text]
49 | UnusedTypeVarsInAlias Text [Text] [Text]
50 | MessyTypeVarsInAlias Text [Text] [Text] [Text]
58 = ParseError !Int !Int !Problem
65 | EndOfFile_MultiString
74 | BadChunkInQualifiedCapVar
99toReport :: RenderType.Localizer -> Error -> Report.Report
100toReport _localizer err =
103 problemToReport problem
105 BadFunctionName arity ->
107 "BAD FUNCTION DEFINITION"
109 "Complex patterns cannot be used as function names."
111 "This function definition has " <> i2t arity <> " arguments, but instead\
112 \ of a normal name like `add` or `reverse` it has a pattern. There is no\
113 \ way to \"deconstruct\" a function with pattern matching, so this needs to\
114 \ be changed to a normal name."
121 ("The free variable `" <> name <> "` appears more than once in this pattern.")
123 [ text "Rename the variables so there are no duplicates."
125 "In Elm, pattern matching works by binding these free variables to subsections\
126 \ of the matching value. It does not make sense to have the same name for two\
127 \ different subsections though! When you say `" <> name <> "` in some code, there\
128 \ is no way for me to know which subsection you mean!"
136 ("This documentation comment is not followed by anything.")
138 "All documentation comments need to be right above the declaration they\
139 \ describe. Maybe some code got deleted or commented out by accident? Or\
140 \ maybe this comment is here by accident?"
143 SettingsOnNormalModule ->
145 "BAD MODULE DECLARATION"
147 "A normal module can expose values, but not settings like this."
149 "If you want a normal module, just remove this stuff. If you want to create\
150 \ an `effect module` you just forgot to use the `effect` keyword. In that\
151 \ case, just change `module` to `effect module` and you should be headed in\
152 \ the right direction!"
155 SettingsOnPortModule ->
157 "BAD MODULE DECLARATION"
159 "A port module can expose values, but not have settings like this."
161 "If you want a port module, just remove this stuff. If you want to create\
162 \ a custom effect module instead (which is rare) just change `port module`\
163 \ to `effect module` and you should be headed in the right direction!"
166 DuplicateSettingOnEffectModule name ->
168 "EFFECT MODULE PROBLEM"
170 ("You have defined " <> Help.functionName name <> " multiple times.")
172 "There can only be one " <> Help.functionName name
173 <> " though! Remove until there is one left."
176 BadSettingOnEffectModule name ->
178 "EFFECT MODULE PROBLEM"
180 ("Setting " <> Help.functionName name <> " is not recognized.")
182 "Remove this entry and you should be all set!"
185 NoSettingsOnEffectModule ->
187 "EFFECT MODULE PROBLEM"
189 ("You are defining an effect module, but it has no settings.")
191 "If you just wanted a normal module, change the keywords `effect module`\
192 \ to `module` and you should be all set. If you want a proper effect module,\
193 \ you need to specify your commands and/or subscriptions. Read more about this\
194 \ here: <http://guide.elm-lang.org/effect_managers/>"
197 MissingManagerOnEffectModule name ->
199 "EFFECT MODULE PROBLEM"
201 ("You are defining an effect module, but there is no " <> Help.functionName name <> " defined.")
203 "There is a small set of top-level functions and values that must be defined\
204 \ in any complete effect module. The best thing is probably to just read more\
205 \ about effect modules here:\
206 \ <http://guide.elm-lang.org/effect_managers/>"
209 UnexpectedPort name ->
213 ("You are declaring port " <> Help.functionName name <> " in a normal module.")
215 "All ports must be defined in a `port module`. You should probably have just\
216 \ one of these for your project. This way all of your foreign interactions\
217 \ stay relatively organized."
220 TypeWithoutDefinition valueName ->
224 ("There is a type annotation for `" <> valueName <> "` but there"
225 <> " is no corresponding definition!"
228 "Directly below the type annotation, put a definition like:\n\n"
229 <> " " <> valueName <> " = 42"
232 DuplicateArgument funcName argName ->
236 ( "The name `" <> argName
237 <> "` is used more than once in the arguments of "
238 <> Help.functionName funcName <> "."
241 "Rename things until `" <> argName <> "` is used only once.\
242 \ Otherwise how can we tell which one you want when you\
243 \ say `" <> argName <> "` it in the body of your function?"
246 DuplicateFieldName region name ->
250 ("This record has more than one field named `" <> name <> "`.")
251 (text "There can only be one. Do some renaming to make sure the names are distinct!")
253 DuplicateValueDeclaration name ->
255 "DUPLICATE DEFINITION"
257 ( "Naming multiple top-level values " <> Help.functionName name <> " makes things\n"
258 <> "ambiguous. When you say " <> Help.functionName name <> " which one do you want?"
261 "Find all the top-level values named " <> Help.functionName name
262 <> " and do some renaming. Make sure the names are distinct!"
265 DuplicateTypeDeclaration name ->
267 "DUPLICATE DEFINITION"
269 ( "There are multiple types named `" <> name <> "` in this module."
272 "Search through this module, find all the types named `"
273 <> name <> "`, and give each of them a unique name."
276 DuplicateDefinition name ->
278 "DUPLICATE DEFINITION"
280 ("There are multiple values named `" <> name <> "` in this let-expression."
283 "Search through this let-expression, find all the values named `"
284 <> name <> "`, and give each of them a unique name."
287 UnboundTypeVarsInUnion typeName givenVars unbound ->
288 unboundTypeVars "type" typeName givenVars unbound
290 UnboundTypeVarsInAlias typeName givenVars unbound ->
291 unboundTypeVars "type alias" typeName givenVars unbound
293 UnusedTypeVarsInAlias typeName givenVars unused ->
295 "UNUSED TYPE VARIABLES"
297 ( "Type alias `" <> typeName <> "` cannot have unused type variables: "
298 <> Help.commaSep unused
301 [ text "You probably need to change the declaration like this:"
302 , dullyellow $ hsep $
303 map text ("type" : "alias" : typeName : filter (`notElem` unused) givenVars ++ ["=", "..."])
307 MessyTypeVarsInAlias typeName givenVars unused unbound ->
309 "TYPE VARIABLE PROBLEMS"
311 ( "Type alias `" <> typeName <> "` has some problems with type variables."
315 "The declaration says it uses certain type variables ("
316 <> Help.commaSep unused <> ") but they do not appear in the aliased type. "
317 <> "Furthermore, the aliased type says it uses type variables ("
318 <> Help.commaSep unbound
319 <> ") that do not appear in the declaration."
320 , text "You probably need to change the declaration like this:"
321 , dullyellow $ hsep $
322 map text ("type" : "alias" : typeName : filter (`notElem` unused) givenVars ++ unbound ++ ["=", "..."])
327unboundTypeVars :: Text -> Text -> [Text] -> [Text] -> Report.Report
328unboundTypeVars declKind typeName givenVars unboundVars =
330 "UNBOUND TYPE VARIABLES"
332 ( Help.capitalize declKind <> " `" <> typeName <> "` must declare its use of type variable"
333 <> Help.commaSep unboundVars
336 [ text "You probably need to change the declaration like this:"
338 map text (declKind : typeName : givenVars)
339 ++ map (dullyellow . text) unboundVars
340 ++ map text ["=", "..."]
342 "Here's why. Imagine one `" <> typeName <> "` where `" <> head unboundVars <>
343 "` is an Int and another where it is a Bool. When we explicitly list the type\
344 \ variables, the type checker can see that they are actually different types."
350-- PARSE ERROR TO REPORT
353parseReport :: Text -> Help.Doc -> Report.Report
354parseReport pre post =
355 Report.report "PARSE ERROR" Nothing pre post
358problemToReport :: Problem -> Report.Report
359problemToReport problem =
363 "I ran into a tab, but tabs are not allowed in Elm files."
364 (reflowParagraph "Replace the tab with spages.")
368 "I got to the end of the file while parsing a multi-line comment."
371 "Multi-line comments look like {- comment -}, and it looks like\
372 \ you are missing the closing marker."
374 "Nested multi-line comments like {- this {- and this -} -} are allowed.\
375 \ That means the opening and closing markers must be balanced, just\
376 \ like parentheses in normal code. Maybe that is the problem?"
382 "I got to the end of the file while parsing a string."
384 "Strings look like \"this\" with double quotes on each end.\
385 \ So the closing double quote seems to be missing."
388 EndOfFile_MultiString ->
390 "I got to the end of the file while parsing a multi-line string."
392 "Multi-line strings begin and end with three double quotes in a\
393 \ row, like \"\"\"this\"\"\". So the closing marker seems\
399 "This string is missing the closing quote."
402 "Elm strings must start and end with a double quote, like \"this\"."
404 "If you want a string that can contain newlines, use three double quotes in a\
405 \ row, like \"\"\"this\"\"\"."
411 "Ran into a bad escape code."
413 "Elm allows typical escape characters like \\n and \\t, but you can\
414 \ also use \\x0040 to refer to unicode characters by their hex code.\
415 \ It seems like something has gone wrong with one of these codes."
420 "Ran into a bad use of single quotes."
423 "If you want to create a string, just switch to double quotes:"
425 dullyellow (text "'this'")
427 <> dullyellow (text "\"this\"")
429 "Unlike JavaScript, Elm distinguishes between strings like \"hello\"\
430 \ and individual characters like 'A' and '3'. It is not all strings!\
431 \ If you really do want a character though, something went wrong\
432 \ and I did not find the closing single quote."
438 "Numbers cannot end with a decimal points."
440 "Leave it off or add some digits after it, like or 3 or 3.0"
445 "Numbers cannot have letters or underscores in them."
447 "Maybe a space is missing between a number and a variable?"
452 "If you put the letter E in a number, it should followed by more digits."
454 "If you want to say 1000, you can also say 1e3.\
455 \ You cannot just end it with an E though!"
460 "I see the start of a hex number, but not the end."
462 "A hex number looks like 0x123ABC, where the 0x is followed by hexidecimal digits (i.e. 0123456789abcdefABCDEF)"
467 "Normal numbers cannot start with a zero. Take the zeros off the front."
469 "Only numbers like 0x0040 or 0.25 can start with a zero."
472 BadChunkInQualifiedCapVar ->
477 "Single colons should only show up in types."
479 "Maybe you want :: instead? Or maybe you are defining\
480 \ a type annotation but there are extra spaces before it?"
485 "The = operator is reserved for defining variables."
487 "Maybe you want == instead? Or maybe you are defining a\
488 \ variable but there are extra spaces before it?"
493 "Arrows are reserved for cases and anonymous functions."
495 "Maybe you want > or >= instead?"
500 "Vertical bars are reserved for use in type declarations."
502 "Maybe you want || instead?"
507 "Dots are for record access and decimal points."
509 "They cannot float around on their own though, so\
510 \ maybe there is some extra whitespace?"
513 Theories allTheories ->
514 case Set.toList (Set.fromList allTheories) of
517 "Something went wrong when parsing this code."
519 "I do not have any suggestions though, and I suspect there is some\
520 \ sort of bug. Can you get it down to an <http://sscce.org> and\
521 \ share it at <https://github.com/elm-lang/error-message-catalog/issues>?\
522 \ That way we can figure out how to give better advice!"
526 error "TODO" (map theoryToText theories)
529theoryToText :: Theory -> Text
545 "a digit, like 0 or 6"
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