1{-# OPTIONS_GHC -Wall #-}
2{-# LANGUAGE OverloadedStrings #-}
3module Reporting.Error.Syntax
6 , Problem(..), Theory(..)
7 , ContextStack, Context(..)
8 , BadOp(..), Next(..), NextDecl(..)
13import qualified Data.Set as Set
14import qualified Data.Text as Text
15import Data.Text (Text)
17import qualified Reporting.Render.Type as RenderType
18import qualified Reporting.Region as R
19import qualified Reporting.Report as Report
20import qualified Reporting.Helpers as Help
21import Reporting.Helpers ((<>), dullyellow, hsep, i2t, reflowParagraph, text)
29 = Parse (Maybe R.Region) Problem
35 | SettingsOnNormalModule
36 | SettingsOnPortModule
37 | DuplicateSettingOnEffectModule Text
38 | BadSettingOnEffectModule Text
39 | NoSettingsOnEffectModule
40 | MissingManagerOnEffectModule Text
43 | TypeWithoutDefinition Text
45 | DuplicateArgument Text Text
46 | DuplicateFieldName R.Region Text
47 | DuplicateValueDeclaration Text
48 | DuplicateTypeDeclaration Text
49 | DuplicateDefinition Text
50 | UnboundTypeVarsInUnion Text [Text] [Text]
51 | UnboundTypeVarsInAlias Text [Text] [Text]
52 | UnusedTypeVarsInAlias Text [Text] [Text]
53 | MessyTypeVarsInAlias Text [Text] [Text] [Text]
60data ParseError = ParseError !Int !Int !Problem
68 | EndOfFile_MultiString
78 | BadOp BadOp ContextStack
79 | Theories ContextStack [Theory]
82data BadOp = HasType | Equals | Arrow | Pipe | Dot
112data NextDecl = ModuleDecl | DocCommentDecl | ImportDecl | OtherDecl
116type ContextStack = [(Context, R.Position)]
144 deriving (Eq, Ord, Show)
151toReport :: RenderType.Localizer -> Error -> Report.Report
152toReport _localizer err =
154 Parse subRegion problem ->
155 problemToReport subRegion problem
157 BadFunctionName arity ->
159 "BAD FUNCTION DEFINITION"
161 "Complex patterns cannot be used as function names."
163 "This function definition has " <> i2t arity <> " arguments, but instead\
164 \ of a normal name like `add` or `reverse` it has a pattern. There is no\
165 \ way to \"deconstruct\" a function with pattern matching, so this needs to\
166 \ be changed to a normal name."
173 ("The free variable `" <> name <> "` appears more than once in this pattern.")
175 [ text "Rename the variables so there are no duplicates."
177 "In Elm, pattern matching works by binding these free variables to subsections\
178 \ of the matching value. It does not make sense to have the same name for two\
179 \ different subsections though! When you say `" <> name <> "` in some code, there\
180 \ is no way for me to know which subsection you mean!"
188 ("This documentation comment is not followed by anything.")
190 "All documentation comments need to be right above the declaration they\
191 \ describe. Maybe some code got deleted or commented out by accident? Or\
192 \ maybe this comment is here by accident?"
195 SettingsOnNormalModule ->
197 "BAD MODULE DECLARATION"
199 "A normal module can expose values, but not settings like this."
201 "If you want a normal module, just remove this stuff. If you want to create\
202 \ an `effect module` you forgot to use the `effect` keyword. In that\
203 \ case, change `module` to `effect module` and you should be headed in\
204 \ the right direction!"
207 SettingsOnPortModule ->
209 "BAD MODULE DECLARATION"
211 "A port module can expose values, but not have settings like this."
213 "If you want a port module, just remove this stuff. If you want to create\
214 \ a custom effect module instead (which is rare) change `port module`\
215 \ to `effect module` and you should be headed in the right direction!"
218 DuplicateSettingOnEffectModule name ->
220 "EFFECT MODULE PROBLEM"
222 ("You have defined " <> Help.functionName name <> " multiple times.")
224 "There can only be one " <> Help.functionName name
225 <> " though! Remove until there is one left."
228 BadSettingOnEffectModule name ->
230 "EFFECT MODULE PROBLEM"
232 ("Setting " <> Help.functionName name <> " is not recognized.")
234 "Remove this entry and you should be all set!"
237 NoSettingsOnEffectModule ->
239 "EFFECT MODULE PROBLEM"
241 ("You are defining an effect module, but it has no settings.")
243 "If you just wanted a normal module, change the keywords `effect module`\
244 \ to `module` and you should be all set. If you want a proper effect module,\
245 \ you need to specify your commands and/or subscriptions. Read more about this\
246 \ here: <http://guide.elm-lang.org/effect_managers/>"
249 MissingManagerOnEffectModule name ->
251 "EFFECT MODULE PROBLEM"
253 ("You are defining an effect module, but there is no " <> Help.functionName name <> " defined.")
255 "There is a small set of top-level functions and values that must be defined\
256 \ in any complete effect module. The best thing is probably to just read more\
257 \ about effect modules here:\
258 \ <http://guide.elm-lang.org/effect_managers/>"
261 UnexpectedPort name ->
265 ("You are declaring port " <> Help.functionName name <> " in a normal module.")
267 "All ports must be defined in a `port module`. You should probably have only\
268 \ one of these for your project. This way all of your foreign interactions\
269 \ stay relatively organized."
272 TypeWithoutDefinition valueName ->
276 ("There is a type annotation for `" <> valueName <> "` but there"
277 <> " is no corresponding definition!"
280 "Directly below the type annotation, put a definition like:\n\n"
281 <> " " <> valueName <> " = 42"
284 DuplicateArgument funcName argName ->
288 ( "The name `" <> argName
289 <> "` is used more than once in the arguments of "
290 <> Help.functionName funcName <> "."
293 "Rename things until `" <> argName <> "` is used only once.\
294 \ Otherwise how can we tell which one you want when you\
295 \ say `" <> argName <> "` it in the body of your function?"
298 DuplicateFieldName region name ->
302 ("This record has more than one field named `" <> name <> "`.")
303 (text "There can only be one. Do some renaming to make sure the names are distinct!")
305 DuplicateValueDeclaration name ->
307 "DUPLICATE DEFINITION"
309 ( "Naming multiple top-level values " <> Help.functionName name <> " makes things\n"
310 <> "ambiguous. When you say " <> Help.functionName name <> " which one do you want?"
313 "Find all the top-level values named " <> Help.functionName name
314 <> " and do some renaming. Make sure the names are distinct!"
317 DuplicateTypeDeclaration name ->
319 "DUPLICATE DEFINITION"
321 ( "There are multiple types named `" <> name <> "` in this module."
324 "Search through this module, find all the types named `"
325 <> name <> "`, and give each of them a unique name."
328 DuplicateDefinition name ->
330 "DUPLICATE DEFINITION"
332 ("There are multiple values named `" <> name <> "` in this let-expression."
335 "Search through this let-expression, find all the values named `"
336 <> name <> "`, and give each of them a unique name."
339 UnboundTypeVarsInUnion typeName givenVars unbound ->
340 unboundTypeVars "type" typeName givenVars unbound
342 UnboundTypeVarsInAlias typeName givenVars unbound ->
343 unboundTypeVars "type alias" typeName givenVars unbound
345 UnusedTypeVarsInAlias typeName givenVars unused ->
347 "UNUSED TYPE VARIABLES"
349 ( "Type alias `" <> typeName <> "` cannot have unused type variables: "
350 <> Help.commaSep unused
353 [ text "You probably need to change the declaration like this:"
354 , dullyellow $ hsep $
355 map text ("type" : "alias" : typeName : filter (`notElem` unused) givenVars ++ ["=", "..."])
359 MessyTypeVarsInAlias typeName givenVars unused unbound ->
361 "TYPE VARIABLE PROBLEMS"
363 ( "Type alias `" <> typeName <> "` has some problems with type variables."
367 "The declaration says it uses certain type variables ("
368 <> Help.commaSep unused <> ") but they do not appear in the aliased type. "
369 <> "Furthermore, the aliased type says it uses type variables ("
370 <> Help.commaSep unbound
371 <> ") that do not appear in the declaration."
372 , text "You probably need to change the declaration like this:"
373 , dullyellow $ hsep $
374 map text ("type" : "alias" : typeName : filter (`notElem` unused) givenVars ++ unbound ++ ["=", "..."])
379unboundTypeVars :: Text -> Text -> [Text] -> [Text] -> Report.Report
380unboundTypeVars declKind typeName givenVars unboundVars =
382 "UNBOUND TYPE VARIABLES"
384 ( Help.capitalize declKind <> " `" <> typeName <> "` must declare its use of type variable"
385 <> Help.commaSep unboundVars
388 [ text "You probably need to change the declaration like this:"
390 map text (declKind : typeName : givenVars)
391 ++ map (dullyellow . text) unboundVars
392 ++ map text ["=", "..."]
394 "Here's why. Imagine one `" <> typeName <> "` where `" <> head unboundVars <>
395 "` is an Int and another where it is a Bool. When we explicitly list the type\
396 \ variables, the type checker can see that they are actually different types."
402-- PARSE ERROR TO REPORT
405makeParseReport :: Maybe R.Region -> Text -> Help.Doc -> Report.Report
406makeParseReport subRegion pre post =
407 Report.report "PARSE ERROR" subRegion pre post
410problemToReport :: Maybe R.Region -> Problem -> Report.Report
411problemToReport subRegion problem =
412 let parseReport = makeParseReport subRegion in
416 "I ran into a tab, but tabs are not allowed in Elm files."
417 (reflowParagraph "Replace the tab with spages.")
421 "I got to the end of the file while parsing a multi-line comment."
424 "Multi-line comments look like {- comment -}, and it looks like\
425 \ you are missing the closing marker."
427 "Nested multi-line comments like {- this {- and this -} -} are allowed.\
428 \ That means the opening and closing markers must be balanced, just\
429 \ like parentheses in normal code. Maybe that is the problem?"
435 "I got to the end of the file while parsing a GLSL block."
437 "A shader should be defined in a block like this: [glsl| ... |]"
442 "I got to the end of the file while parsing a string."
444 "Strings look like \"this\" with double quotes on each end.\
445 \ So the closing double quote seems to be missing."
448 EndOfFile_MultiString ->
450 "I got to the end of the file while parsing a multi-line string."
452 "Multi-line strings begin and end with three double quotes in a\
453 \ row, like \"\"\"this\"\"\". So the closing marker seems\
459 "This string is missing the closing quote."
462 "Elm strings must start and end with a double quote, like \"this\"."
464 "If you want a string that can contain newlines, use three double quotes in a\
465 \ row, like \"\"\"this\"\"\"."
471 "Ran into a bad escape code."
473 "Elm allows typical escape characters like \\n and \\t, but you can\
474 \ also use \\x0040 to refer to unicode characters by their hex code.\
475 \ It seems like something has gone wrong with one of these codes."
480 "Ran into a bad use of single quotes."
483 "If you want to create a string, switch to double quotes:"
485 dullyellow (text "'this'")
487 <> dullyellow (text "\"this\"")
489 "Unlike JavaScript, Elm distinguishes between strings like \"hello\"\
490 \ and individual characters like 'A' and '3'. It is not all strings!\
491 \ If you really do want a character though, something went wrong\
492 \ and I did not find the closing single quote."
498 "Numbers cannot end with a decimal points."
500 "Leave it off or add some digits after it, like or 3 or 3.0"
505 "Numbers cannot have letters or underscores in them."
507 "Maybe a space is missing between a number and a variable?"
512 "If you put the letter E in a number, it should followed by more digits."
514 "If you want to say 1000, you can also say 1e3.\
515 \ You cannot just end it with an E though!"
520 "I see the start of a hex number, but not the end."
522 "A hex number looks like 0x123ABC, where the 0x is followed by hexidecimal digits (i.e. 0123456789abcdefABCDEF)"
527 "Normal numbers cannot start with a zero. Take the zeros off the front."
529 "Only numbers like 0x0040 or 0.25 can start with a zero."
534 "I ran into a problem while parsing this GLSL block."
535 (reflowParagraph msg)
540 badOp subRegion stack "the \"has type\" operator" "(:)"
541 "type annotations and record types"
542 "Maybe you want :: instead? Or maybe your indentation is a bit off?"
545 badEquals subRegion stack
548 if isCaseRelated stack then
550 "I ran into a stray arrow (->) while parsing this `case` expression."
552 "All branches in a `case` must be indented the exact\
553 \ same amount, so the patterns are vertically\
554 \ aligned. Maybe this branch is indented too much?"
558 badOp subRegion stack "an arrow" "(->)"
559 "cases expressions and anonymous functions"
560 "Maybe you want > or >= instead?"
563 badOp subRegion stack "a vertical bar" "(|)"
565 "Maybe you want || instead?"
569 "I was not expecting a dot (.) here."
571 "Dots are for record access and decimal points, so\
572 \ they cannot float around on their own. Maybe\
573 \ there is some extra whitespace?"
576 Theories stack allTheories ->
579 "Something went wrong while parsing "
580 <> contextToText "your code" "" stack
583 parseReport starter $
584 case Set.toList (Set.fromList allTheories) of
588 "I do not have any suggestions though!"
590 "Can you get it down to a <http://sscce.org> and share it at\
591 \ <https://github.com/elm-lang/error-message-catalog/issues>?\
592 \ That way we can figure out how to give better advice!"
597 "I was expecting to see "
598 <> addPeriod (theoryToText stack theory)
602 [ text "I was expecting:"
605 ++ map (bullet . theoryToText stack) theories
608bullet :: Text -> Help.Doc
610 Help.hang 4 (" - " <> reflowParagraph point)
613addPeriod :: Text -> Text
615 if Text.last msg `elem` ['`', ')', '.', '!', '?'] then
621badOp :: Maybe R.Region -> ContextStack -> Text -> Text -> Text -> Text -> Report.Report
622badOp subRegion stack opName op setting hint =
623 makeParseReport subRegion ("I was not expecting " <> opName <> " here.") $
626 "A " <> op <> " should only appear in " <> setting
627 <> contextToText "" ", but I think I am parsing " stack <> "."
628 , reflowParagraph hint
632badEquals :: Maybe R.Region -> ContextStack -> Report.Report
633badEquals subRegion stack =
636 "I was not expecting this equals sign"
637 <> contextToText "" " while parsing " stack <> "."
639 makeParseReport subRegion pre (badEqualsHelp stack)
642badEqualsHelp :: ContextStack -> Help.Doc
647 "Maybe you want == instead? Or maybe your indentation is a bit off?"
649 (ExprRecord, _) : _ ->
651 "Records look like { x = 3, y = 4 } with the equals sign right\
652 \ after the field name. Maybe you forgot a comma?"
654 (Definition name, _) : rest ->
656 "Maybe this is supposed to be a separate definition? If so, it\
657 \ is indented too far. "
659 if any ((==) ExprLet . fst) rest then
660 "All definitions in a `let` expression must be vertically aligned."
662 "Spaces are not allowed before top-level definitions."
668contextToText :: Text -> Text -> ContextStack -> Text
669contextToText defaultText prefixText stack =
674 (context, _) : rest ->
675 let anchor = getAnchor rest in
678 ExprIf -> "an `if`" <> anchor
679 ExprLet -> "a `let`" <> anchor
680 ExprFunc -> "an anonymous function" <> anchor
681 ExprCase -> "a `case`" <> anchor
682 ExprList -> "a list" <> anchor
683 ExprTuple -> "an expression (in parentheses)" <> anchor
684 ExprRecord -> "a record" <> anchor
685 Definition name -> name <> "'s definition"
686 Annotation name -> name <> "'s type annotation"
687 TypeTuple -> "a type (in parentheses)" <> anchor
688 TypeRecord -> "a record type" <> anchor
689 PatternList -> "a list pattern" <> anchor
690 PatternTuple -> "a pattern (in parentheses)" <> anchor
691 PatternRecord -> "a record pattern" <> anchor
692 Module -> "a module declaration"
693 Import -> "an import"
694 TypeUnion -> "a union type"
695 TypeAlias -> "a type alias"
696 Infix -> "an infix declaration"
697 Port -> "a port declaration"
700getAnchor :: ContextStack -> Text
706 (context, _) : rest ->
709 " in " <> name <> "'s definition"
712 " in " <> name <> "'s type annotation"
718isCaseRelated :: ContextStack -> Bool
724 (context, _) : rest ->
725 context == ExprCase || isCaseRelated rest
728theoryToText :: ContextStack -> Theory -> Text
729theoryToText context theory =
732 "the `" <> keyword <> "` keyword"
736 "=" -> equalsTheory context
737 "->" -> "an arrow (->) followed by an expression"
738 ":" -> "the \"has type\" symbol (:) followed by a type"
740 "|" -> barTheory context
741 "::" -> "the cons operator (::) followed by more list elements"
743 "-" -> "a minus sign (-)"
744 "_" -> "an underscore"
745 "(" -> "a left paren, for grouping or starting tuples"
746 ")" -> "a closing paren"
747 "[" -> "a left square bracket, for starting lists"
748 "]" -> "a right square bracket, to end a list"
749 "{" -> "a left curly brace, for starting records"
750 "}" -> "a right curly brace, to end a record"
751 "{-|" -> "a doc comment, like {-| example -}"
752 _ -> "the (" <> symbol <> ") symbol"
755 "a lower-case variable, like `x` or `user`"
758 "an upper-case variable, like `Maybe` or `Just`"
761 "an infix operator, like (+) or (==)"
764 "a digit from 0 to 9"
767 "the end of the file"
772 FreshLine nextDecl ->
773 "a fresh line (without any leading spaces) for " <>
775 ModuleDecl -> "something like `module Main exposing (..)`"
776 DocCommentDecl -> "something like `import Html exposing (text)`"
777 ImportDecl -> "an `import` or a declarations"
778 OtherDecl -> "a top-level declaration, like `view model = div [] [...]`"
782 Decl -> "a declaration, like `x = 5` or `type alias Model = { ... }`"
783 Expr -> "an expression, like x or 42"
784 AfterOpExpr op -> "an expression after that (" <> op <> ") operator, like x or 42"
785 ElseBranch -> "an `else` branch. An `if` must handle both possibilities."
786 Arg -> "an argument, like `name` or `total`"
787 Pattern -> "a pattern, like `name` or (Just x)"
788 Type -> "a type, like Int or (List String)"
789 Listing -> "a list of exposed values and types, like (..) or (x,y,z)"
790 Exposing -> "something like `exposing (..)`"
793equalsTheory :: ContextStack -> Text
799 (context, _) : rest ->
801 ExprRecord -> "an equals sign (=) followed by an expression"
802 Definition name -> "an equals sign (=) followed by " <> name <> "'s definition"
803 TypeUnion -> "an equals sign (=) followed by the first union type constructor"
804 TypeAlias -> "an equals sign (=) followed by the aliased type"
805 _ -> equalsTheory rest
808barTheory :: ContextStack -> Text
814 (context, _) : rest ->
816 ExprRecord -> "a vertical bar (|) followed by the record fields you want to update"
817 TypeRecord -> "a vertical bar (|) followed by some record field types"
818 TypeUnion -> "a vertical bar (|) followed by more union type constructors"
822badSpace :: ContextStack -> Text
826 "more indentation? I was not done with that last thing yet."
828 (context, _) : rest ->
830 ExprIf -> "the end of that `if`" <> badSpaceExprEnd rest
831 ExprLet -> "the end of that `let`" <> badSpaceExprEnd rest
832 ExprFunc -> badSpace rest
833 ExprCase -> "more of that `case`" <> badSpaceExprEnd rest
834 ExprList -> "the end of that list" <> badSpaceExprEnd rest
835 ExprTuple -> "a closing paren" <> badSpaceExprEnd rest
836 ExprRecord -> "the end of that record" <> badSpaceExprEnd rest
837 Definition name -> "the rest of " <> name <> "'s definition" <> badSpaceExprEnd stack
838 Annotation name -> "the rest of " <> name <> "'s type annotation" <> badSpaceEnd
839 TypeTuple -> "a closing paren" <> badSpaceEnd
840 TypeRecord -> "the end of that record" <> badSpaceEnd
841 PatternList -> "the end of that list" <> badSpaceEnd
842 PatternTuple -> "a closing paren" <> badSpaceEnd
843 PatternRecord -> "the end of that record" <> badSpaceEnd
844 Module -> "something like `module Main exposing (..)`"
845 Import -> "something like `import Html exposing (..)`"
846 TypeUnion -> "more of that union type" <> badSpaceEnd
847 TypeAlias -> "more of that type alias" <> badSpaceEnd
848 Infix -> "more of that infix declaration" <> badSpaceEnd
849 Port -> "more of that port declaration" <> badSpaceEnd
854 ". Maybe you forgot some code? Or you need more indentation?"
857badSpaceExprEnd :: ContextStack -> Text
858badSpaceExprEnd stack =
863 (Definition name, R.Position _ column) : _ ->
869 "more indentation? (Try " <> i2t (column + 1) <> "+ spaces.)"
871 ". Maybe you forgot some code? Or maybe the body of `" <> name
872 <> "` needs " <> ending
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