1{-# OPTIONS_GHC -Wall #-}
2{-# LANGUAGE OverloadedStrings #-}
3module Reporting.Error.Syntax
6 , Problem(..), Theory(..), Context(..)
7 , BadOp(..), Next(..), NextDecl(..)
12import qualified Data.Set as Set
13import Data.Text (Text)
15import qualified Reporting.Render.Type as RenderType
16import qualified Reporting.Region as Region
17import qualified Reporting.Report as Report
18import qualified Reporting.Helpers as Help
19import Reporting.Helpers ((<>), dullyellow, hsep, i2t, reflowParagraph, text)
33 | SettingsOnNormalModule
34 | SettingsOnPortModule
35 | DuplicateSettingOnEffectModule Text
36 | BadSettingOnEffectModule Text
37 | NoSettingsOnEffectModule
38 | MissingManagerOnEffectModule Text
41 | TypeWithoutDefinition Text
43 | DuplicateArgument Text Text
44 | DuplicateFieldName Region.Region Text
45 | DuplicateValueDeclaration Text
46 | DuplicateTypeDeclaration Text
47 | DuplicateDefinition Text
48 | UnboundTypeVarsInUnion Text [Text] [Text]
49 | UnboundTypeVarsInAlias Text [Text] [Text]
50 | UnusedTypeVarsInAlias Text [Text] [Text]
51 | MessyTypeVarsInAlias Text [Text] [Text] [Text]
58data ParseError = ParseError !Int !Int !Problem
66 | EndOfFile_MultiString
76 | BadOp BadOp [Context]
77 | Theories [Context] [Theory]
80data BadOp = HasType | Equals | Arrow | Pipe | Dot
94 deriving (Eq, Ord, Show)
97data Next = Expr | ElseBranch | Arg | Pattern | Type | Listing | Exposing
98 deriving (Eq, Ord, Show)
101data NextDecl = ModuleDecl | DocCommentDecl | ImportDecl | OtherDecl
102 deriving (Eq, Ord, Show)
131 deriving (Eq, Ord, Show)
138toReport :: RenderType.Localizer -> Error -> Report.Report
139toReport _localizer err =
142 problemToReport problem
144 BadFunctionName arity ->
146 "BAD FUNCTION DEFINITION"
148 "Complex patterns cannot be used as function names."
150 "This function definition has " <> i2t arity <> " arguments, but instead\
151 \ of a normal name like `add` or `reverse` it has a pattern. There is no\
152 \ way to \"deconstruct\" a function with pattern matching, so this needs to\
153 \ be changed to a normal name."
160 ("The free variable `" <> name <> "` appears more than once in this pattern.")
162 [ text "Rename the variables so there are no duplicates."
164 "In Elm, pattern matching works by binding these free variables to subsections\
165 \ of the matching value. It does not make sense to have the same name for two\
166 \ different subsections though! When you say `" <> name <> "` in some code, there\
167 \ is no way for me to know which subsection you mean!"
175 ("This documentation comment is not followed by anything.")
177 "All documentation comments need to be right above the declaration they\
178 \ describe. Maybe some code got deleted or commented out by accident? Or\
179 \ maybe this comment is here by accident?"
182 SettingsOnNormalModule ->
184 "BAD MODULE DECLARATION"
186 "A normal module can expose values, but not settings like this."
188 "If you want a normal module, just remove this stuff. If you want to create\
189 \ an `effect module` you forgot to use the `effect` keyword. In that\
190 \ case, change `module` to `effect module` and you should be headed in\
191 \ the right direction!"
194 SettingsOnPortModule ->
196 "BAD MODULE DECLARATION"
198 "A port module can expose values, but not have settings like this."
200 "If you want a port module, just remove this stuff. If you want to create\
201 \ a custom effect module instead (which is rare) change `port module`\
202 \ to `effect module` and you should be headed in the right direction!"
205 DuplicateSettingOnEffectModule name ->
207 "EFFECT MODULE PROBLEM"
209 ("You have defined " <> Help.functionName name <> " multiple times.")
211 "There can only be one " <> Help.functionName name
212 <> " though! Remove until there is one left."
215 BadSettingOnEffectModule name ->
217 "EFFECT MODULE PROBLEM"
219 ("Setting " <> Help.functionName name <> " is not recognized.")
221 "Remove this entry and you should be all set!"
224 NoSettingsOnEffectModule ->
226 "EFFECT MODULE PROBLEM"
228 ("You are defining an effect module, but it has no settings.")
230 "If you just wanted a normal module, change the keywords `effect module`\
231 \ to `module` and you should be all set. If you want a proper effect module,\
232 \ you need to specify your commands and/or subscriptions. Read more about this\
233 \ here: <http://guide.elm-lang.org/effect_managers/>"
236 MissingManagerOnEffectModule name ->
238 "EFFECT MODULE PROBLEM"
240 ("You are defining an effect module, but there is no " <> Help.functionName name <> " defined.")
242 "There is a small set of top-level functions and values that must be defined\
243 \ in any complete effect module. The best thing is probably to just read more\
244 \ about effect modules here:\
245 \ <http://guide.elm-lang.org/effect_managers/>"
248 UnexpectedPort name ->
252 ("You are declaring port " <> Help.functionName name <> " in a normal module.")
254 "All ports must be defined in a `port module`. You should probably have only\
255 \ one of these for your project. This way all of your foreign interactions\
256 \ stay relatively organized."
259 TypeWithoutDefinition valueName ->
263 ("There is a type annotation for `" <> valueName <> "` but there"
264 <> " is no corresponding definition!"
267 "Directly below the type annotation, put a definition like:\n\n"
268 <> " " <> valueName <> " = 42"
271 DuplicateArgument funcName argName ->
275 ( "The name `" <> argName
276 <> "` is used more than once in the arguments of "
277 <> Help.functionName funcName <> "."
280 "Rename things until `" <> argName <> "` is used only once.\
281 \ Otherwise how can we tell which one you want when you\
282 \ say `" <> argName <> "` it in the body of your function?"
285 DuplicateFieldName region name ->
289 ("This record has more than one field named `" <> name <> "`.")
290 (text "There can only be one. Do some renaming to make sure the names are distinct!")
292 DuplicateValueDeclaration name ->
294 "DUPLICATE DEFINITION"
296 ( "Naming multiple top-level values " <> Help.functionName name <> " makes things\n"
297 <> "ambiguous. When you say " <> Help.functionName name <> " which one do you want?"
300 "Find all the top-level values named " <> Help.functionName name
301 <> " and do some renaming. Make sure the names are distinct!"
304 DuplicateTypeDeclaration name ->
306 "DUPLICATE DEFINITION"
308 ( "There are multiple types named `" <> name <> "` in this module."
311 "Search through this module, find all the types named `"
312 <> name <> "`, and give each of them a unique name."
315 DuplicateDefinition name ->
317 "DUPLICATE DEFINITION"
319 ("There are multiple values named `" <> name <> "` in this let-expression."
322 "Search through this let-expression, find all the values named `"
323 <> name <> "`, and give each of them a unique name."
326 UnboundTypeVarsInUnion typeName givenVars unbound ->
327 unboundTypeVars "type" typeName givenVars unbound
329 UnboundTypeVarsInAlias typeName givenVars unbound ->
330 unboundTypeVars "type alias" typeName givenVars unbound
332 UnusedTypeVarsInAlias typeName givenVars unused ->
334 "UNUSED TYPE VARIABLES"
336 ( "Type alias `" <> typeName <> "` cannot have unused type variables: "
337 <> Help.commaSep unused
340 [ text "You probably need to change the declaration like this:"
341 , dullyellow $ hsep $
342 map text ("type" : "alias" : typeName : filter (`notElem` unused) givenVars ++ ["=", "..."])
346 MessyTypeVarsInAlias typeName givenVars unused unbound ->
348 "TYPE VARIABLE PROBLEMS"
350 ( "Type alias `" <> typeName <> "` has some problems with type variables."
354 "The declaration says it uses certain type variables ("
355 <> Help.commaSep unused <> ") but they do not appear in the aliased type. "
356 <> "Furthermore, the aliased type says it uses type variables ("
357 <> Help.commaSep unbound
358 <> ") that do not appear in the declaration."
359 , text "You probably need to change the declaration like this:"
360 , dullyellow $ hsep $
361 map text ("type" : "alias" : typeName : filter (`notElem` unused) givenVars ++ unbound ++ ["=", "..."])
366unboundTypeVars :: Text -> Text -> [Text] -> [Text] -> Report.Report
367unboundTypeVars declKind typeName givenVars unboundVars =
369 "UNBOUND TYPE VARIABLES"
371 ( Help.capitalize declKind <> " `" <> typeName <> "` must declare its use of type variable"
372 <> Help.commaSep unboundVars
375 [ text "You probably need to change the declaration like this:"
377 map text (declKind : typeName : givenVars)
378 ++ map (dullyellow . text) unboundVars
379 ++ map text ["=", "..."]
381 "Here's why. Imagine one `" <> typeName <> "` where `" <> head unboundVars <>
382 "` is an Int and another where it is a Bool. When we explicitly list the type\
383 \ variables, the type checker can see that they are actually different types."
389-- PARSE ERROR TO REPORT
392parseReport :: Text -> Help.Doc -> Report.Report
393parseReport pre post =
394 Report.report "PARSE ERROR" Nothing pre post
397problemToReport :: Problem -> Report.Report
398problemToReport problem =
402 "I ran into a tab, but tabs are not allowed in Elm files."
403 (reflowParagraph "Replace the tab with spages.")
407 "I got to the end of the file while parsing a multi-line comment."
410 "Multi-line comments look like {- comment -}, and it looks like\
411 \ you are missing the closing marker."
413 "Nested multi-line comments like {- this {- and this -} -} are allowed.\
414 \ That means the opening and closing markers must be balanced, just\
415 \ like parentheses in normal code. Maybe that is the problem?"
421 "I got to the end of the file while parsing a GLSL block."
423 "A shader should be defined in a block like this: [glsl| ... |]"
428 "I got to the end of the file while parsing a string."
430 "Strings look like \"this\" with double quotes on each end.\
431 \ So the closing double quote seems to be missing."
434 EndOfFile_MultiString ->
436 "I got to the end of the file while parsing a multi-line string."
438 "Multi-line strings begin and end with three double quotes in a\
439 \ row, like \"\"\"this\"\"\". So the closing marker seems\
445 "This string is missing the closing quote."
448 "Elm strings must start and end with a double quote, like \"this\"."
450 "If you want a string that can contain newlines, use three double quotes in a\
451 \ row, like \"\"\"this\"\"\"."
457 "Ran into a bad escape code."
459 "Elm allows typical escape characters like \\n and \\t, but you can\
460 \ also use \\x0040 to refer to unicode characters by their hex code.\
461 \ It seems like something has gone wrong with one of these codes."
466 "Ran into a bad use of single quotes."
469 "If you want to create a string, switch to double quotes:"
471 dullyellow (text "'this'")
473 <> dullyellow (text "\"this\"")
475 "Unlike JavaScript, Elm distinguishes between strings like \"hello\"\
476 \ and individual characters like 'A' and '3'. It is not all strings!\
477 \ If you really do want a character though, something went wrong\
478 \ and I did not find the closing single quote."
484 "Numbers cannot end with a decimal points."
486 "Leave it off or add some digits after it, like or 3 or 3.0"
491 "Numbers cannot have letters or underscores in them."
493 "Maybe a space is missing between a number and a variable?"
498 "If you put the letter E in a number, it should followed by more digits."
500 "If you want to say 1000, you can also say 1e3.\
501 \ You cannot just end it with an E though!"
506 "I see the start of a hex number, but not the end."
508 "A hex number looks like 0x123ABC, where the 0x is followed by hexidecimal digits (i.e. 0123456789abcdefABCDEF)"
513 "Normal numbers cannot start with a zero. Take the zeros off the front."
515 "Only numbers like 0x0040 or 0.25 can start with a zero."
520 "I ran into a problem while parsing this GLSL block."
521 (reflowParagraph msg)
526 badOp context "the \"has type\" operator (:)"
527 "type annotations and record types"
528 "Maybe you want :: instead? Or maybe your indentation is a bit off?"
531 badOp context "an equals sign (=)"
533 "Maybe you want == instead? Or maybe your indentation is a bit off?"
536 if isCaseRelated context then
538 "I am parsing a case expression, but this arrow (->) is out of place."
540 "Each branch in a case expression needs to start on the same\
541 \ column, so maybe this branch is indented too much?"
545 badOp context "an arrow (->)"
546 "cases expressions and anonymous functions"
547 "Maybe you want > or >= instead?"
550 badOp context "a vertical bar (|)"
552 "Maybe you want || instead?"
556 "I was not expecting a dot (.) here."
558 "Dots are for record access and decimal points, so\
559 \ they cannot float around on their own. Maybe\
560 \ there is some extra whitespace?"
563 Theories context allTheories ->
564 case Set.toList (Set.fromList allTheories) of
567 "Something went wrong when parsing this code."
569 "I do not have any suggestions though, and I suspect there is some\
570 \ sort of bug. Can you get it down to an <http://sscce.org> and\
571 \ share it at <https://github.com/elm-lang/error-message-catalog/issues>?\
572 \ That way we can figure out how to give better advice!"
577 "I got confused while trying to parse your code."
579 contextToText context
580 <> " right now, so I was expecting "
581 <> theoryToText theory
586 "I got confused while trying to parse your code."
589 contextToText context <> ", so I was expecting:"
592 ++ map (bullet . theoryToText) theories
596bullet :: Text -> Help.Doc
598 Help.indent 2 (reflowParagraph point)
601badOp :: [Context] -> Text -> Text -> Text -> Report.Report
602badOp context opName setting hint =
603 parseReport ("I was not expecting " <> opName <> " here.") $
605 "It should only appear in " <> setting <> ", but "
606 <> contextToText context <> ". " <> hint
609contextToText :: [Context] -> Text
610contextToText contextStack =
613 "I am in between top-level declarations right now."
616 let anchor = getAnchor rest in
617 "I think I am parsing " <>
619 ExprIf -> "an `if`" <> anchor
620 ExprLet -> "a `let`" <> anchor
621 ExprFunc -> "an anonymous function" <> anchor
622 ExprCase -> "a `case`" <> anchor
623 ExprList -> "a list" <> anchor
624 ExprTuple -> "a tuple" <> anchor
625 ExprRecord -> "a record" <> anchor
626 Definition name -> "the definition of `" <> name <> "`"
627 Annotation name -> "the type annotation for `" <> name <> "`"
628 TypeTuple -> "a tuple type" <> anchor
629 TypeRecord -> "a record type" <> anchor
630 PatternList -> "a pattern" <> anchor
631 PatternTuple -> "a pattern" <> anchor
632 PatternRecord -> "a pattern" <> anchor
633 Module -> "a module declaration"
634 Import -> "an import"
635 DocComment -> "a documentation comment"
636 TypeUnion -> "a union type"
637 TypeAlias -> "a type alias"
638 Infix -> "an infix declaration"
639 Port -> "a port declaration"
642getAnchor :: [Context] -> Text
646 Definition name : _ -> " in the definition of `" <> name <> "`"
647 Annotation name : _ -> " in the type annotation for `" <> name <> "`"
648 _ : rest -> getAnchor rest
651isCaseRelated :: [Context] -> Bool
652isCaseRelated context =
656 _ : rest -> isCaseRelated rest
659theoryToText :: Theory -> Text
663 "the `" <> keyword <> "` keyword"
667 "=" -> "an equals sign (=)"
668 "->" -> "an arrow (->) followed by an expression"
669 ":" -> "the \"has type\" symbol (:) followed by a type"
671 "|" -> "a vertical bar (|)"
672 "::" -> "the cons operator (::)"
674 "-" -> "a minus sign (-)"
675 "_" -> "an underscore"
676 "(" -> "a left paren, for grouping or starting tuples"
677 ")" -> "a closing paren"
678 "[" -> "a left square bracket, for starting lists"
679 "]" -> "a right square bracket, to end a list"
680 "{" -> "a left curly brace, for starting records"
681 "}" -> "a right curly brace, to end a record"
682 _ -> "the (" <> symbol <> ") symbol"
685 "a lower-case variable, like `x` or `user`"
688 "an upper-case variable, like `Maybe` or `Just`"
691 "an infix operator, like + or =="
694 "a digit from 0 to 9"
697 "the end of the file"
702 FreshLine nextDecl ->
703 "a fresh line with no indentation for " <>
705 ModuleDecl -> "an `import`"
706 DocCommentDecl -> "an `import`"
707 ImportDecl -> "an `import` or a declarations"
708 OtherDecl -> "a top-level declaration"
712 Expr -> "an expression, like x or 42"
713 ElseBranch -> "an `else` branch. An `if` must handle both possibilities!"
714 Arg -> "an argument, like `name` or `total`"
715 Pattern -> "a pattern, like `name` or (Just x)"
716 Type -> "a type, like Int or (List String)"
717 Listing -> "a list of exposed values and types, like (..) or (x,y,z)"
718 Exposing -> "something like `exposing (..)`"
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