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 qualified Data.Text as Text
14import Data.Text (Text)
16import qualified Reporting.Render.Type as RenderType
17import qualified Reporting.Region as Region
18import qualified Reporting.Report as Report
19import qualified Reporting.Helpers as Help
20import Reporting.Helpers ((<>), dullyellow, hsep, i2t, reflowParagraph, text)
34 | SettingsOnNormalModule
35 | SettingsOnPortModule
36 | DuplicateSettingOnEffectModule Text
37 | BadSettingOnEffectModule Text
38 | NoSettingsOnEffectModule
39 | MissingManagerOnEffectModule Text
42 | TypeWithoutDefinition Text
44 | DuplicateArgument Text Text
45 | DuplicateFieldName Region.Region Text
46 | DuplicateValueDeclaration Text
47 | DuplicateTypeDeclaration Text
48 | DuplicateDefinition Text
49 | UnboundTypeVarsInUnion Text [Text] [Text]
50 | UnboundTypeVarsInAlias Text [Text] [Text]
51 | UnusedTypeVarsInAlias Text [Text] [Text]
52 | MessyTypeVarsInAlias Text [Text] [Text] [Text]
59data ParseError = ParseError !Int !Int !Problem
67 | EndOfFile_MultiString
77 | BadOp BadOp [Context]
78 | Theories [Context] [Theory]
81data BadOp = HasType | Equals | Arrow | Pipe | Dot
110data NextDecl = ModuleDecl | DocCommentDecl | ImportDecl | OtherDecl
140 deriving (Eq, Ord, Show)
147toReport :: RenderType.Localizer -> Error -> Report.Report
148toReport _localizer err =
151 problemToReport problem
153 BadFunctionName arity ->
155 "BAD FUNCTION DEFINITION"
157 "Complex patterns cannot be used as function names."
159 "This function definition has " <> i2t arity <> " arguments, but instead\
160 \ of a normal name like `add` or `reverse` it has a pattern. There is no\
161 \ way to \"deconstruct\" a function with pattern matching, so this needs to\
162 \ be changed to a normal name."
169 ("The free variable `" <> name <> "` appears more than once in this pattern.")
171 [ text "Rename the variables so there are no duplicates."
173 "In Elm, pattern matching works by binding these free variables to subsections\
174 \ of the matching value. It does not make sense to have the same name for two\
175 \ different subsections though! When you say `" <> name <> "` in some code, there\
176 \ is no way for me to know which subsection you mean!"
184 ("This documentation comment is not followed by anything.")
186 "All documentation comments need to be right above the declaration they\
187 \ describe. Maybe some code got deleted or commented out by accident? Or\
188 \ maybe this comment is here by accident?"
191 SettingsOnNormalModule ->
193 "BAD MODULE DECLARATION"
195 "A normal module can expose values, but not settings like this."
197 "If you want a normal module, just remove this stuff. If you want to create\
198 \ an `effect module` you forgot to use the `effect` keyword. In that\
199 \ case, change `module` to `effect module` and you should be headed in\
200 \ the right direction!"
203 SettingsOnPortModule ->
205 "BAD MODULE DECLARATION"
207 "A port module can expose values, but not have settings like this."
209 "If you want a port module, just remove this stuff. If you want to create\
210 \ a custom effect module instead (which is rare) change `port module`\
211 \ to `effect module` and you should be headed in the right direction!"
214 DuplicateSettingOnEffectModule name ->
216 "EFFECT MODULE PROBLEM"
218 ("You have defined " <> Help.functionName name <> " multiple times.")
220 "There can only be one " <> Help.functionName name
221 <> " though! Remove until there is one left."
224 BadSettingOnEffectModule name ->
226 "EFFECT MODULE PROBLEM"
228 ("Setting " <> Help.functionName name <> " is not recognized.")
230 "Remove this entry and you should be all set!"
233 NoSettingsOnEffectModule ->
235 "EFFECT MODULE PROBLEM"
237 ("You are defining an effect module, but it has no settings.")
239 "If you just wanted a normal module, change the keywords `effect module`\
240 \ to `module` and you should be all set. If you want a proper effect module,\
241 \ you need to specify your commands and/or subscriptions. Read more about this\
242 \ here: <http://guide.elm-lang.org/effect_managers/>"
245 MissingManagerOnEffectModule name ->
247 "EFFECT MODULE PROBLEM"
249 ("You are defining an effect module, but there is no " <> Help.functionName name <> " defined.")
251 "There is a small set of top-level functions and values that must be defined\
252 \ in any complete effect module. The best thing is probably to just read more\
253 \ about effect modules here:\
254 \ <http://guide.elm-lang.org/effect_managers/>"
257 UnexpectedPort name ->
261 ("You are declaring port " <> Help.functionName name <> " in a normal module.")
263 "All ports must be defined in a `port module`. You should probably have only\
264 \ one of these for your project. This way all of your foreign interactions\
265 \ stay relatively organized."
268 TypeWithoutDefinition valueName ->
272 ("There is a type annotation for `" <> valueName <> "` but there"
273 <> " is no corresponding definition!"
276 "Directly below the type annotation, put a definition like:\n\n"
277 <> " " <> valueName <> " = 42"
280 DuplicateArgument funcName argName ->
284 ( "The name `" <> argName
285 <> "` is used more than once in the arguments of "
286 <> Help.functionName funcName <> "."
289 "Rename things until `" <> argName <> "` is used only once.\
290 \ Otherwise how can we tell which one you want when you\
291 \ say `" <> argName <> "` it in the body of your function?"
294 DuplicateFieldName region name ->
298 ("This record has more than one field named `" <> name <> "`.")
299 (text "There can only be one. Do some renaming to make sure the names are distinct!")
301 DuplicateValueDeclaration name ->
303 "DUPLICATE DEFINITION"
305 ( "Naming multiple top-level values " <> Help.functionName name <> " makes things\n"
306 <> "ambiguous. When you say " <> Help.functionName name <> " which one do you want?"
309 "Find all the top-level values named " <> Help.functionName name
310 <> " and do some renaming. Make sure the names are distinct!"
313 DuplicateTypeDeclaration name ->
315 "DUPLICATE DEFINITION"
317 ( "There are multiple types named `" <> name <> "` in this module."
320 "Search through this module, find all the types named `"
321 <> name <> "`, and give each of them a unique name."
324 DuplicateDefinition name ->
326 "DUPLICATE DEFINITION"
328 ("There are multiple values named `" <> name <> "` in this let-expression."
331 "Search through this let-expression, find all the values named `"
332 <> name <> "`, and give each of them a unique name."
335 UnboundTypeVarsInUnion typeName givenVars unbound ->
336 unboundTypeVars "type" typeName givenVars unbound
338 UnboundTypeVarsInAlias typeName givenVars unbound ->
339 unboundTypeVars "type alias" typeName givenVars unbound
341 UnusedTypeVarsInAlias typeName givenVars unused ->
343 "UNUSED TYPE VARIABLES"
345 ( "Type alias `" <> typeName <> "` cannot have unused type variables: "
346 <> Help.commaSep unused
349 [ text "You probably need to change the declaration like this:"
350 , dullyellow $ hsep $
351 map text ("type" : "alias" : typeName : filter (`notElem` unused) givenVars ++ ["=", "..."])
355 MessyTypeVarsInAlias typeName givenVars unused unbound ->
357 "TYPE VARIABLE PROBLEMS"
359 ( "Type alias `" <> typeName <> "` has some problems with type variables."
363 "The declaration says it uses certain type variables ("
364 <> Help.commaSep unused <> ") but they do not appear in the aliased type. "
365 <> "Furthermore, the aliased type says it uses type variables ("
366 <> Help.commaSep unbound
367 <> ") that do not appear in the declaration."
368 , text "You probably need to change the declaration like this:"
369 , dullyellow $ hsep $
370 map text ("type" : "alias" : typeName : filter (`notElem` unused) givenVars ++ unbound ++ ["=", "..."])
375unboundTypeVars :: Text -> Text -> [Text] -> [Text] -> Report.Report
376unboundTypeVars declKind typeName givenVars unboundVars =
378 "UNBOUND TYPE VARIABLES"
380 ( Help.capitalize declKind <> " `" <> typeName <> "` must declare its use of type variable"
381 <> Help.commaSep unboundVars
384 [ text "You probably need to change the declaration like this:"
386 map text (declKind : typeName : givenVars)
387 ++ map (dullyellow . text) unboundVars
388 ++ map text ["=", "..."]
390 "Here's why. Imagine one `" <> typeName <> "` where `" <> head unboundVars <>
391 "` is an Int and another where it is a Bool. When we explicitly list the type\
392 \ variables, the type checker can see that they are actually different types."
398-- PARSE ERROR TO REPORT
401parseReport :: Text -> Help.Doc -> Report.Report
402parseReport pre post =
403 Report.report "PARSE ERROR" Nothing pre post
406problemToReport :: Problem -> Report.Report
407problemToReport problem =
411 "I ran into a tab, but tabs are not allowed in Elm files."
412 (reflowParagraph "Replace the tab with spages.")
416 "I got to the end of the file while parsing a multi-line comment."
419 "Multi-line comments look like {- comment -}, and it looks like\
420 \ you are missing the closing marker."
422 "Nested multi-line comments like {- this {- and this -} -} are allowed.\
423 \ That means the opening and closing markers must be balanced, just\
424 \ like parentheses in normal code. Maybe that is the problem?"
430 "I got to the end of the file while parsing a GLSL block."
432 "A shader should be defined in a block like this: [glsl| ... |]"
437 "I got to the end of the file while parsing a string."
439 "Strings look like \"this\" with double quotes on each end.\
440 \ So the closing double quote seems to be missing."
443 EndOfFile_MultiString ->
445 "I got to the end of the file while parsing a multi-line string."
447 "Multi-line strings begin and end with three double quotes in a\
448 \ row, like \"\"\"this\"\"\". So the closing marker seems\
454 "This string is missing the closing quote."
457 "Elm strings must start and end with a double quote, like \"this\"."
459 "If you want a string that can contain newlines, use three double quotes in a\
460 \ row, like \"\"\"this\"\"\"."
466 "Ran into a bad escape code."
468 "Elm allows typical escape characters like \\n and \\t, but you can\
469 \ also use \\x0040 to refer to unicode characters by their hex code.\
470 \ It seems like something has gone wrong with one of these codes."
475 "Ran into a bad use of single quotes."
478 "If you want to create a string, switch to double quotes:"
480 dullyellow (text "'this'")
482 <> dullyellow (text "\"this\"")
484 "Unlike JavaScript, Elm distinguishes between strings like \"hello\"\
485 \ and individual characters like 'A' and '3'. It is not all strings!\
486 \ If you really do want a character though, something went wrong\
487 \ and I did not find the closing single quote."
493 "Numbers cannot end with a decimal points."
495 "Leave it off or add some digits after it, like or 3 or 3.0"
500 "Numbers cannot have letters or underscores in them."
502 "Maybe a space is missing between a number and a variable?"
507 "If you put the letter E in a number, it should followed by more digits."
509 "If you want to say 1000, you can also say 1e3.\
510 \ You cannot just end it with an E though!"
515 "I see the start of a hex number, but not the end."
517 "A hex number looks like 0x123ABC, where the 0x is followed by hexidecimal digits (i.e. 0123456789abcdefABCDEF)"
522 "Normal numbers cannot start with a zero. Take the zeros off the front."
524 "Only numbers like 0x0040 or 0.25 can start with a zero."
529 "I ran into a problem while parsing this GLSL block."
530 (reflowParagraph msg)
535 badOp context "the \"has type\" operator" "(:)"
536 "type annotations and record types"
537 "Maybe you want :: instead? Or maybe your indentation is a bit off?"
540 badOp context "an equals sign" "(=)"
542 "Maybe you want == instead? Or maybe your indentation is a bit off?"
545 if isCaseRelated context then
547 "I ran into a stray arrow (->) while parsing this `case` expression."
549 "All branches in a `case` must be indented the exact\
550 \ same amount, so the patterns are vertically\
551 \ aligned. Maybe this branch is indented too much?"
555 badOp context "an arrow" "(->)"
556 "cases expressions and anonymous functions"
557 "Maybe you want > or >= instead?"
560 badOp context "a vertical bar" "(|)"
562 "Maybe you want || instead?"
566 "I was not expecting a dot (.) here."
568 "Dots are for record access and decimal points, so\
569 \ they cannot float around on their own. Maybe\
570 \ there is some extra whitespace?"
573 Theories context allTheories ->
576 "I got confused while trying to parse "
577 <> contextToText "your code" "" context
580 parseReport starter $
581 case Set.toList (Set.fromList allTheories) of
585 "I do not have any suggestions though!"
587 "Can you get it down to a <http://sscce.org> and share it at\
588 \ <https://github.com/elm-lang/error-message-catalog/issues>?\
589 \ That way we can figure out how to give better advice!"
594 "I was expecting to see "
595 <> addPeriod (theoryToText context theory)
599 [ text "I was expecting:"
602 ++ map (bullet . theoryToText context) theories
605bullet :: Text -> Help.Doc
607 Help.hang 4 (" - " <> reflowParagraph point)
610addPeriod :: Text -> Text
612 if Text.last msg `elem` ['`', ')', '.', '!', '?'] then
618badOp :: [Context] -> Text -> Text -> Text -> Text -> Report.Report
619badOp context opName op setting hint =
620 parseReport ("I was not expecting " <> opName <> " here.") $
623 "A " <> op <> " should only appear in " <> setting
624 <> contextToText "" ", but I think I am parsing " context <> "."
625 , reflowParagraph hint
629contextToText :: Text -> Text -> [Context] -> Text
630contextToText defaultText prefixText contextStack =
636 let anchor = getAnchor rest in
639 ExprIf -> "an `if`" <> anchor
640 ExprLet -> "a `let`" <> anchor
641 ExprFunc -> "an anonymous function" <> anchor
642 ExprCase -> "a `case`" <> anchor
643 ExprList -> "a list" <> anchor
644 ExprTuple -> "a expression (in parentheses)" <> anchor
645 ExprRecord -> "a record" <> anchor
646 Definition name -> name <> "'s definition"
647 Annotation name -> name <> "'s type annotation"
648 TypeTuple -> "a type (in parentheses)" <> anchor
649 TypeRecord -> "a record type" <> anchor
650 PatternList -> "a list pattern" <> anchor
651 PatternTuple -> "a pattern (in parentheses)" <> anchor
652 PatternRecord -> "a record pattern" <> anchor
653 Module -> "a module declaration"
654 Import -> "an import"
655 DocComment -> "a documentation comment"
656 TypeUnion -> "a union type"
657 TypeAlias -> "a type alias"
658 Infix -> "an infix declaration"
659 Port -> "a port declaration"
662getAnchor :: [Context] -> Text
666 Definition name : _ -> " in " <> name <> "'s definition"
667 Annotation name : _ -> " in " <> name <> "'s type annotation"
668 _ : rest -> getAnchor rest
671isCaseRelated :: [Context] -> Bool
672isCaseRelated context =
676 _ : rest -> isCaseRelated rest
679theoryToText :: [Context] -> Theory -> Text
680theoryToText context theory =
683 "the `" <> keyword <> "` keyword"
687 "=" -> equalsTheory context
688 "->" -> "an arrow (->) followed by an expression"
689 ":" -> "the \"has type\" symbol (:) followed by a type"
691 "|" -> barTheory context
692 "::" -> "the cons operator (::) followed by more list elements"
694 "-" -> "a minus sign (-)"
695 "_" -> "an underscore"
696 "(" -> "a left paren, for grouping or starting tuples"
697 ")" -> "a closing paren"
698 "[" -> "a left square bracket, for starting lists"
699 "]" -> "a right square bracket, to end a list"
700 "{" -> "a left curly brace, for starting records"
701 "}" -> "a right curly brace, to end a record"
702 _ -> "the (" <> symbol <> ") symbol"
705 "a lower-case variable, like `x` or `user`"
708 "an upper-case variable, like `Maybe` or `Just`"
711 "an infix operator, like (+) or (==)"
714 "a digit from 0 to 9"
717 "the end of the file"
722 FreshLine nextDecl ->
723 "a fresh line (without any leading spaces) for " <>
725 ModuleDecl -> "an `import`"
726 DocCommentDecl -> "an `import`"
727 ImportDecl -> "an `import` or a declarations"
728 OtherDecl -> "a top-level declaration"
732 Expr -> "an expression, like x or 42"
733 AfterOpExpr op -> "an expression after that (" <> op <> ") operator, like x or 42"
734 ElseBranch -> "an `else` branch. An `if` must handle both possibilities."
735 Arg -> "an argument, like `name` or `total`"
736 Pattern -> "a pattern, like `name` or (Just x)"
737 Type -> "a type, like Int or (List String)"
738 Listing -> "a list of exposed values and types, like (..) or (x,y,z)"
739 Exposing -> "something like `exposing (..)`"
742equalsTheory :: [Context] -> Text
743equalsTheory context =
750 ExprRecord -> "an equals sign (=) followed by an expression"
751 Definition name -> "an equals sign (=) followed by " <> name <> "'s definition"
752 TypeUnion -> "an equals sign (=) followed by the first union type constructor"
753 TypeAlias -> "an equals sign (=) followed by the aliased type"
754 _ -> equalsTheory rest
757barTheory :: [Context] -> Text
765 ExprRecord -> "a vertical bar (|) followed by the record fields you want to update"
766 TypeRecord -> "a vertical bar (|) followed by some record field types"
767 TypeUnion -> "a vertical bar (|) followed by more union type constructors"
771badSpace :: [Context] -> Text
775 "more indentation? I was not done with that last thing yet."
779 ExprIf -> "the end of that `if`" <> badSpaceEnd
780 ExprLet -> "the end of that `let`" <> badSpaceEnd
781 ExprFunc -> badSpace rest
782 ExprCase -> "more of that `case`" <> badSpaceEnd
783 ExprList -> "the end of that list" <> badSpaceEnd
784 ExprTuple -> "a closing paren" <> badSpaceEnd
785 ExprRecord -> "the end of that record" <> badSpaceEnd
786 Definition name -> "the rest of " <> name <> "'s definition" <> badSpaceEnd
787 Annotation name -> "the rest of " <> name <> "'s type annotation" <> badSpaceEnd
788 TypeTuple -> "a closing paren" <> badSpaceEnd
789 TypeRecord -> "the end of that record" <> badSpaceEnd
790 PatternList -> "the end of that list" <> badSpaceEnd
791 PatternTuple -> "a closing paren" <> badSpaceEnd
792 PatternRecord -> "the end of that record" <> badSpaceEnd
793 Module -> "something like `module Main exposing (..)`"
794 Import -> "something like `import Html exposing (..)`"
795 DocComment -> badSpace rest
796 TypeUnion -> "more of that union type" <> badSpaceEnd
797 TypeAlias -> "more of that type alias" <> badSpaceEnd
798 Infix -> "more of that infix declaration" <> badSpaceEnd
799 Port -> "more of that port declaration" <> badSpaceEnd
804 ". Maybe you forgot some code? Or you need more indentation?"
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