@@ -8,13 +8,14 @@ module Reporting.Error.Syntax
11+import qualified Data.Set as Set
11import Data.Text (Text)
12import Data.Text (Text)
13import qualified Reporting.Render.Type as RenderType
14import qualified Reporting.Render.Type as RenderType
14import qualified Reporting.Region as Region
15import qualified Reporting.Region as Region
15import qualified Reporting.Report as Report
16import qualified Reporting.Report as Report
16import qualified Reporting.Helpers as Help
17import qualified Reporting.Helpers as Help
17−import Reporting.Helpers ((<>), dullyellow, hsep, i2t, text)
18+import Reporting.Helpers ((<>), dullyellow, hsep, i2t, reflowParagraph, text)
@@ -59,7 +60,9 @@ data ParseError
60data Problem
61data Problem
62− | EndOfFile Text
63+ | EndOfFile_Comment
65+ | EndOfFile_MultiString
63 | NewLineInString
66 | NewLineInString
64 | BadEscape
67 | BadEscape
@@ -69,11 +72,11 @@ data Problem
69 | BadNumberHex
72 | BadNumberHex
70 | BadNumberZero
73 | BadNumberZero
71 | BadChunkInQualifiedCapVar
74 | BadChunkInQualifiedCapVar
72− | HasType -- "A single colon is for type annotations. Maybe you want :: instead? Or maybe you are defining a type annotation, but there is whitespace before it?"
75+ | HasType
73− | Equals -- "The = operator is reserved for defining variables. Maybe you want == instead? Or maybe you are defining a variable, but there is whitespace before it?"
76+ | Equals
74− | Arrow -- "Arrows are reserved for cases and anonymous functions. Maybe you want > or >= instead?"
77+ | Arrow
75− | Pipe -- "Vertical bars are reserved for use in union type declarations. Maybe you want || instead?"
78+ | Pipe
76− | Dot -- "Dots are for record access. They cannot float around on their own!"
79+ | Dot
77 | Theories [Theory]
80 | Theories [Theory]
@@ -86,6 +89,7 @@ data Theory
86 | BadIndent
89 | BadIndent
87 | FreshLine
90 | FreshLine
88 | Expecting Text
91 | Expecting Text
@@ -95,15 +99,15 @@ data Theory
95toReport :: RenderType.Localizer -> Error -> Report.Report
99toReport :: RenderType.Localizer -> Error -> Report.Report
96toReport _localizer err =
100toReport _localizer err =
97 case err of
101 case err of
98− Parse _ ->
102+ Parse problem ->
99− Report.report "PARSE ERROR" Nothing "Something is wrong." (text "TODO")
103+ problemToReport problem
101 BadFunctionName arity ->
105 BadFunctionName arity ->
102 Report.report
106 Report.report
103 "BAD FUNCTION DEFINITION"
107 "BAD FUNCTION DEFINITION"
105 "Complex patterns cannot be used as function names."
109 "Complex patterns cannot be used as function names."
106− ( Help.reflowParagraph $
110+ ( reflowParagraph $
107 "This function definition has " <> i2t arity <> " arguments, but instead\
111 "This function definition has " <> i2t arity <> " arguments, but instead\
108 \ of a normal name like `add` or `reverse` it has a pattern. There is no\
112 \ of a normal name like `add` or `reverse` it has a pattern. There is no\
109 \ way to \"deconstruct\" a function with pattern matching, so this needs to\
113 \ way to \"deconstruct\" a function with pattern matching, so this needs to\
@@ -117,7 +121,7 @@ toReport _localizer err =
117 ("The free variable `" <> name <> "` appears more than once in this pattern.")
121 ("The free variable `" <> name <> "` appears more than once in this pattern.")
118 ( Help.stack
122 ( Help.stack
119 [ text "Rename the variables so there are no duplicates."
123 [ text "Rename the variables so there are no duplicates."
120− , Help.reflowParagraph $
124+ , reflowParagraph $
121 "In Elm, pattern matching works by binding these free variables to subsections\
125 "In Elm, pattern matching works by binding these free variables to subsections\
122 \ of the matching value. It does not make sense to have the same name for two\
126 \ of the matching value. It does not make sense to have the same name for two\
123 \ different subsections though! When you say `" <> name <> "` in some code, there\
127 \ different subsections though! When you say `" <> name <> "` in some code, there\
@@ -130,7 +134,7 @@ toReport _localizer err =
130 "STRAY COMMENT"
134 "STRAY COMMENT"
132 ("This documentation comment is not followed by anything.")
136 ("This documentation comment is not followed by anything.")
133− ( Help.reflowParagraph $
137+ ( reflowParagraph $
134 "All documentation comments need to be right above the declaration they\
138 "All documentation comments need to be right above the declaration they\
135 \ describe. Maybe some code got deleted or commented out by accident? Or\
139 \ describe. Maybe some code got deleted or commented out by accident? Or\
136 \ maybe this comment is here by accident?"
140 \ maybe this comment is here by accident?"
@@ -141,7 +145,7 @@ toReport _localizer err =
141 "BAD MODULE DECLARATION"
145 "BAD MODULE DECLARATION"
143 "A normal module can expose values, but not settings like this."
147 "A normal module can expose values, but not settings like this."
144− ( Help.reflowParagraph $
148+ ( reflowParagraph $
145 "If you want a normal module, just remove this stuff. If you want to create\
149 "If you want a normal module, just remove this stuff. If you want to create\
146 \ an `effect module` you just forgot to use the `effect` keyword. In that\
150 \ an `effect module` you just forgot to use the `effect` keyword. In that\
147 \ case, just change `module` to `effect module` and you should be headed in\
151 \ case, just change `module` to `effect module` and you should be headed in\
@@ -153,7 +157,7 @@ toReport _localizer err =
153 "BAD MODULE DECLARATION"
157 "BAD MODULE DECLARATION"
155 "A port module can expose values, but not have settings like this."
159 "A port module can expose values, but not have settings like this."
156− ( Help.reflowParagraph $
160+ ( reflowParagraph $
157 "If you want a port module, just remove this stuff. If you want to create\
161 "If you want a port module, just remove this stuff. If you want to create\
158 \ a custom effect module instead (which is rare) just change `port module`\
162 \ a custom effect module instead (which is rare) just change `port module`\
159 \ to `effect module` and you should be headed in the right direction!"
163 \ to `effect module` and you should be headed in the right direction!"
@@ -164,7 +168,7 @@ toReport _localizer err =
164 "EFFECT MODULE PROBLEM"
168 "EFFECT MODULE PROBLEM"
166 ("You have defined " <> Help.functionName name <> " multiple times.")
170 ("You have defined " <> Help.functionName name <> " multiple times.")
167− ( Help.reflowParagraph $
171+ ( reflowParagraph $
168 "There can only be one " <> Help.functionName name
172 "There can only be one " <> Help.functionName name
169 <> " though! Remove until there is one left."
173 <> " though! Remove until there is one left."
@@ -174,7 +178,7 @@ toReport _localizer err =
174 "EFFECT MODULE PROBLEM"
178 "EFFECT MODULE PROBLEM"
176 ("Setting " <> Help.functionName name <> " is not recognized.")
180 ("Setting " <> Help.functionName name <> " is not recognized.")
177− ( Help.reflowParagraph $
181+ ( reflowParagraph $
178 "Remove this entry and you should be all set!"
182 "Remove this entry and you should be all set!"
@@ -183,7 +187,7 @@ toReport _localizer err =
183 "EFFECT MODULE PROBLEM"
187 "EFFECT MODULE PROBLEM"
185 ("You are defining an effect module, but it has no settings.")
189 ("You are defining an effect module, but it has no settings.")
186− ( Help.reflowParagraph $
190+ ( reflowParagraph $
187 "If you just wanted a normal module, change the keywords `effect module`\
191 "If you just wanted a normal module, change the keywords `effect module`\
188 \ to `module` and you should be all set. If you want a proper effect module,\
192 \ to `module` and you should be all set. If you want a proper effect module,\
189 \ you need to specify your commands and/or subscriptions. Read more about this\
193 \ you need to specify your commands and/or subscriptions. Read more about this\
@@ -195,7 +199,7 @@ toReport _localizer err =
195 "EFFECT MODULE PROBLEM"
199 "EFFECT MODULE PROBLEM"
197 ("You are defining an effect module, but there is no " <> Help.functionName name <> " defined.")
201 ("You are defining an effect module, but there is no " <> Help.functionName name <> " defined.")
198− ( Help.reflowParagraph $
202+ ( reflowParagraph $
199 "There is a small set of top-level functions and values that must be defined\
203 "There is a small set of top-level functions and values that must be defined\
200 \ in any complete effect module. The best thing is probably to just read more\
204 \ in any complete effect module. The best thing is probably to just read more\
201 \ about effect modules here:\
205 \ about effect modules here:\
@@ -207,7 +211,7 @@ toReport _localizer err =
207 "BAD PORT"
211 "BAD PORT"
209 ("You are declaring port " <> Help.functionName name <> " in a normal module.")
213 ("You are declaring port " <> Help.functionName name <> " in a normal module.")
210− ( Help.reflowParagraph $
214+ ( reflowParagraph $
211 "All ports must be defined in a `port module`. You should probably have just\
215 "All ports must be defined in a `port module`. You should probably have just\
212 \ one of these for your project. This way all of your foreign interactions\
216 \ one of these for your project. This way all of your foreign interactions\
213 \ stay relatively organized."
217 \ stay relatively organized."
@@ -233,7 +237,7 @@ toReport _localizer err =
233 <> "` is used more than once in the arguments of "
237 <> "` is used more than once in the arguments of "
234 <> Help.functionName funcName <> "."
238 <> Help.functionName funcName <> "."
236− ( Help.reflowParagraph $
240+ ( reflowParagraph $
237 "Rename things until `" <> argName <> "` is used only once.\
241 "Rename things until `" <> argName <> "` is used only once.\
238 \ Otherwise how can we tell which one you want when you\
242 \ Otherwise how can we tell which one you want when you\
239 \ say `" <> argName <> "` it in the body of your function?"
243 \ say `" <> argName <> "` it in the body of your function?"
@@ -253,7 +257,7 @@ toReport _localizer err =
253 ( "Naming multiple top-level values " <> Help.functionName name <> " makes things\n"
257 ( "Naming multiple top-level values " <> Help.functionName name <> " makes things\n"
254 <> "ambiguous. When you say " <> Help.functionName name <> " which one do you want?"
258 <> "ambiguous. When you say " <> Help.functionName name <> " which one do you want?"
256− ( Help.reflowParagraph $
260+ ( reflowParagraph $
257 "Find all the top-level values named " <> Help.functionName name
261 "Find all the top-level values named " <> Help.functionName name
258 <> " and do some renaming. Make sure the names are distinct!"
262 <> " and do some renaming. Make sure the names are distinct!"
@@ -264,7 +268,7 @@ toReport _localizer err =
265 ( "There are multiple types named `" <> name <> "` in this module."
269 ( "There are multiple types named `" <> name <> "` in this module."
267− ( Help.reflowParagraph $
271+ ( reflowParagraph $
268 "Search through this module, find all the types named `"
272 "Search through this module, find all the types named `"
269 <> name <> "`, and give each of them a unique name."
273 <> name <> "`, and give each of them a unique name."
@@ -275,7 +279,7 @@ toReport _localizer err =
276 ("There are multiple values named `" <> name <> "` in this let-expression."
280 ("There are multiple values named `" <> name <> "` in this let-expression."
278− ( Help.reflowParagraph $
282+ ( reflowParagraph $
279 "Search through this let-expression, find all the values named `"
283 "Search through this let-expression, find all the values named `"
280 <> name <> "`, and give each of them a unique name."
284 <> name <> "`, and give each of them a unique name."
@@ -307,7 +311,7 @@ toReport _localizer err =
307 ( "Type alias `" <> typeName <> "` has some problems with type variables."
311 ( "Type alias `" <> typeName <> "` has some problems with type variables."
309 ( Help.stack
313 ( Help.stack
310− [ Help.reflowParagraph $
314+ [ reflowParagraph $
311 "The declaration says it uses certain type variables ("
315 "The declaration says it uses certain type variables ("
312 <> Help.commaSep unused <> ") but they do not appear in the aliased type. "
316 <> Help.commaSep unused <> ") but they do not appear in the aliased type. "
313 <> "Furthermore, the aliased type says it uses type variables ("
317 <> "Furthermore, the aliased type says it uses type variables ("
@@ -334,9 +338,217 @@ unboundTypeVars declKind typeName givenVars unboundVars =
334 map text (declKind : typeName : givenVars)
338 map text (declKind : typeName : givenVars)
335 ++ map (dullyellow . text) unboundVars
339 ++ map (dullyellow . text) unboundVars
336 ++ map text ["=", "..."]
340 ++ map text ["=", "..."]
337− , Help.reflowParagraph $
341+ , reflowParagraph $
338 "Here's why. Imagine one `" <> typeName <> "` where `" <> head unboundVars <>
342 "Here's why. Imagine one `" <> typeName <> "` where `" <> head unboundVars <>
339 "` is an Int and another where it is a Bool. When we explicitly list the type\
343 "` is an Int and another where it is a Bool. When we explicitly list the type\
340 \ variables, the type checker can see that they are actually different types."
344 \ variables, the type checker can see that they are actually different types."
350+-- PARSE ERROR TO REPORT
353+parseReport :: Text -> Help.Doc -> Report.Report
354+parseReport pre post =
355+ Report.report "PARSE ERROR" Nothing pre post
358+problemToReport :: Problem -> Report.Report
359+problemToReport problem =
363+ "I ran into a tab, but tabs are not allowed in Elm files."
364+ (reflowParagraph "Replace the tab with spages.")
366+ EndOfFile_Comment ->
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)
529+theoryToText :: Theory -> Text
530+theoryToText theory =
545+ "a digit, like 0 or 6"