1{-# OPTIONS_GHC -Wall #-}
2{-# LANGUAGE OverloadedStrings #-}
3module Reporting.Error.Syntax
6 , Problem(..), Theory(..)
8 , ContextStack, Context(..)
15import Data.Monoid ((<>))
16import qualified Data.Text as Text
17import Data.Text (Text)
19import qualified Elm.Name as N
20import qualified Reporting.Region as R
21import qualified Reporting.Render.Code as Code
22import qualified Reporting.Report as Report
23import qualified Reporting.Helpers as H
31 = CommentOnNothing R.Region
32 | UnexpectedPort R.Region N.Name
33 | TypeWithoutDefinition R.Region N.Name
34 | Parse R.Region (Maybe R.Region) Problem
41data ParseError = ParseError !Int !Int !Problem
49 | EndOfFile_MultiString
53 | BadEscape Int EscapeProblem
63 | BadOp BadOp ContextStack
64 | Theories ContextStack [Theory]
71 | UnicodeLength Int String
74data BadOp = HasType | Equals | Arrow | Pipe | Dot
102type ContextStack = [(Context, R.Position)]
130 deriving (Eq, Ord, Show)
137toReport :: Code.Source -> Error -> Report.Report
140 CommentOnNothing region ->
141 Report.Report "STRAY COMMENT" region [] $
142 Report.toCodeSnippet source region Nothing
144 "This documentation comment is not followed by anything."
147 "All documentation comments need to be right above the declaration they\
148 \ describe. Maybe some code got deleted or commented out by accident? Or\
149 \ maybe this comment is here by accident?"
152 UnexpectedPort region name ->
153 Report.Report "BAD PORT" region [] $
154 Report.toCodeSnippet source region Nothing
157 "You are declaring port `" <> N.toString name <> "` in a normal module."
160 [ "It needs to be in a `port` module."
162 "Ports are not a traditional FFI for calling JS functions directly. They need a different mindset! Read"
164 "to learn how to use ports effectively."
168 TypeWithoutDefinition region valueName ->
169 Report.Report "MISSING DEFINITION" region [] $
170 Report.toCodeSnippet source region Nothing
173 "There is a type annotation for `" <> N.toString valueName
174 <> "` but there is no corresponding definition!"
176 "Directly below the type annotation, put a definition like:\n\n"
177 <> " " <> H.nameToDoc valueName <> " = 42"
180 Parse region subRegion problem ->
181 Report.Report "PARSE ERROR" (maybe region id subRegion) [] $
182 Report.toCodeSnippet source region subRegion $
183 problemToDocs problem
187-- PARSE ERROR TO DOCS
190problemToDocs :: Problem -> (H.Doc, H.Doc)
191problemToDocs problem =
195 "I ran into a tab, but tabs are not allowed in Elm files."
197 "Replace the tab with spaces."
202 "I got to the end of the file while parsing a multi-line comment."
206 "Multi-line comments look like {- comment -}, and it looks like\
207 \ you are missing the closing marker."
209 "Nested multi-line comments like {- this {- and this -} -} are allowed.\
210 \ The opening and closing markers must be balanced though, just\
211 \ like parentheses in normal code. Maybe that is the problem?"
217 "I got to the end of the file while parsing a GLSL block."
220 "A shader should be defined in a block like this: [glsl| ... |]"
225 "I got to the end of the file while parsing a string."
228 "Strings look like \"this\" with double quotes on each end.\
229 \ Is the closing double quote missing in your code?"
232 EndOfFile_MultiString ->
234 "I got to the end of the file while parsing a multi-line string."
237 "Multi-line strings look like \"\"\"this\"\"\" with three double quotes on each\
238 \ end. Is the closing triple quote missing in your code?"
243 "I got to the end of the file while parsing a character."
246 "Characters look like 'c' with single quotes on each end.\
247 \ Is the closing single quote missing in your code?"
252 "This string is missing the closing quote."
256 "Elm strings like \"this\" cannot contain newlines."
258 "For strings that CAN contain newlines, say \"\"\"this\"\"\" for Elm’s\
259 \ multi-line string syntax. It allows unescaped newlines and double quotes."
265 "This character is missing the closing quote."
267 "Elm characters must start and end with a single quote. Valid examples\n\
268 \ include 'a', 'b', '\n', 'ø', and '\\u{00F8}' for unicode code points."
271 BadEscape _ escapeProblem ->
272 case escapeProblem of
275 "Backslashes always start escaped characters, but I do not recognize this one:"
277 [ "Maybe there is some typo?"
278 , H.toSimpleHint "Valid escape characters include:"
279 , H.indent 4 $ H.vcat $
289 "The last one lets encode ANY character by its Unicode code\
290 \ point, so use that for anything outside the ordinary six."
296 "I ran into an invalid Unicode escape character:"
299 [ "Here are some examples of valid Unicode escape characters:"
300 , H.indent 4 $ H.vcat $
307 "Notice that the code point is always surrounded by curly\
308 \ braces. They are required!"
314 "This is not a real Unicode code point:"
316 "All valid Unicode code points are between 0 and 10FFFF."
319 UnicodeLength numDigits badCode ->
320 if numDigits < 4 then
322 "Every Unicode code point needs at least four digits:"
325 goodCode = replicate (4 - numDigits) '0' ++ badCode
326 escape = "\\u{" <> H.text goodCode <> "}"
328 H.hsep [ "Try", H.dullyellow escape, "instead?" ]
333 "This Unicode code point has too many digits:"
336 ["Valid","code","points","are","between"
337 , H.dullyellow "\\u{0000}", "and", H.dullyellow "\\u{10FFFF}"
338 ,"so","it","must","have","between","four","and","six","digits."
344 "Ran into a bad use of single quotes."
348 "If you want to create a string, switch to double quotes:"
350 H.dullyellow "'this'" <> " => " <> H.green "\"this\""
352 "Unlike JavaScript, Elm distinguishes between strings like \"hello\"\
353 \ and individual characters like 'A' and '3'. If you really do want\
354 \ a character though, something went wrong and I did not find the\
355 \ closing single quote."
359 BadNumberDot numberBeforeDot ->
361 "Numbers cannot end with a decimal points."
365 H.text (show numberBeforeDot)
367 "Saying " <> H.green number <> " or " <> H.green (number <> ".0") <> " will work though!"
372 "Numbers cannot have letters or underscores in them."
375 "Maybe a space is missing between a number and a variable?"
380 "If you put the letter E in a number, it should followed by more digits."
383 "If you want to say 1000, you can also say 1e3.\
384 \ You cannot just end it with an E though!"
389 "I see the start of a hex number, but not the end."
392 "A hex number looks like 0x123ABC, where the 0x is followed by hexidecimal\
393 \ digits. Valid hexidecimal digits include: 0123456789abcdefABCDEF"
398 "Normal numbers cannot start with zeros. Take the zeros off the front."
401 "Only numbers like 0x0040 or 0.25 can start with a zero."
406 "I cannot pattern match with floating point numbers:"
409 "Equality on floats can be unreliable, so you usually want to check that they\
410 \ are nearby with some sort of (abs (actual - expected) < 0.001) check."
415 "I ran into a problem while parsing this GLSL block."
417 H.reflow (Text.unpack msg)
422 "A variable name cannot start with an underscore:"
425 "You can (1) use a wildcard like _ to ignore the value or you can (2) use\
426 \ a name that starts with a letter to access the value later. Pick one!"
432 badOp stack "A" "\"has type\" operator"
433 "type annotations and record types"
434 "Maybe you want :: instead? Or maybe something is indented too much?"
439 "I was not expecting this equals sign"
440 <> contextToString " here" " while parsing " stack <> "."
442 toBadEqualsHint stack
446 if isCaseRelated stack then
448 "I ran into a stray arrow while parsing this `case` expression."
451 "All branches in a `case` must be indented the exact\
452 \ same amount, so the patterns are vertically\
453 \ aligned. Maybe this branch is indented too much?"
457 badOp stack "An" "arrow"
458 "cases expressions and anonymous functions"
459 "Maybe you want > or >= instead?"
462 badOp stack "A" "vertical bar"
464 "Maybe you want || instead?"
468 "I was not expecting this dot."
471 "Dots are for record access and decimal points, so\
472 \ they cannot float around on their own. Maybe\
473 \ there is some extra whitespace?"
476 Theories stack allTheories ->
479 "Something went wrong while parsing " <> contextToString "your code" "" stack <> "."
485 "I do not have any suggestions though!"
487 "Can you get it down to a <http://sscce.org> and share it at\
488 \ <https://github.com/elm-lang/error-message-catalog/issues>?\
489 \ That way we can figure out how to give better advice!"
494 "I was expecting to see "
495 <> addPeriod (theoryToString stack theory)
502 ++ map (bullet . theoryToString stack) theories
510badOp :: ContextStack -> String -> String -> String -> String -> ( H.Doc, H.Doc )
511badOp stack article opName setting hint =
514 "I was not expecting this " <> opName
515 <> contextToString " here" " while parsing " stack <> "."
518 article <> " " <> opName <> " should only appear in "
519 <> setting <> ". " <> hint
523toBadEqualsHint :: ContextStack -> H.Doc
524toBadEqualsHint stack =
528 "Maybe you want == instead? Or maybe something is indented too much?"
530 (ExprRecord, _) : _ ->
532 "Records look like { x = 3, y = 4 } with the equals sign right\
533 \ after the field name. Maybe you forgot a comma?"
535 (Definition _, _) : rest ->
537 "Maybe this is supposed to be a separate definition? If so, it\
538 \ is indented too far. "
540 if any ((==) ExprLet . fst) rest then
541 "All definitions in a `let` expression must be vertically aligned."
543 "Spaces are not allowed before top-level definitions."
550isCaseRelated :: ContextStack -> Bool
556 (context, _) : rest ->
557 context == ExprCase || isCaseRelated rest
564contextToString :: String -> String -> ContextStack -> String
565contextToString defaultString prefixString stack =
570 (context, _) : rest ->
571 let anchor = getAnchor rest in
574 ExprIf -> "an `if` expression" <> anchor
575 ExprLet -> "a `let` expression" <> anchor
576 ExprFunc -> "an anonymous function" <> anchor
577 ExprCase -> "a `case` expression" <> anchor
578 ExprList -> "a list" <> anchor
579 ExprTuple -> "an expression (in parentheses)" <> anchor
580 ExprRecord -> "a record" <> anchor
581 Definition name -> N.toString name <> "'s definition"
582 Annotation name -> N.toString name <> "'s type annotation"
583 TypeTuple -> "a type (in parentheses)" <> anchor
584 TypeRecord -> "a record type" <> anchor
585 PatternList -> "a list pattern" <> anchor
586 PatternTuple -> "a pattern (in parentheses)" <> anchor
587 PatternRecord -> "a record pattern" <> anchor
588 Module -> "a module declaration"
589 Import -> "an import"
590 TypeUnion -> "a union type"
591 TypeAlias -> "a type alias"
592 Infix -> "an infix declaration"
593 Port -> "a port declaration"
596getAnchor :: ContextStack -> String
602 (context, _) : rest ->
605 " in " <> N.toString name <> "'s definition"
608 " in " <> N.toString name <> "'s type annotation"
618bullet :: String -> H.Doc
620 H.hang 4 (" - " <> H.fillSep (map H.text (words point)))
623addPeriod :: String -> String
625 if last msg `elem` ['`', ')', '.', '!', '?'] then
631theoryToString :: ContextStack -> Theory -> String
632theoryToString context theory =
635 "the `" <> keyword <> "` keyword"
639 "=" -> equalsTheory context
640 "->" -> "an arrow (->) followed by an expression"
641 ":" -> "the \"has type\" symbol (:) followed by a type"
643 "|" -> barTheory context
644 "::" -> "the cons operator (::) followed by more list elements"
646 "-" -> "a minus sign (-)"
647 "_" -> "an underscore"
648 "(" -> "a left paren, for grouping or starting tuples"
649 ")" -> "a closing paren"
650 "[" -> "a left square bracket, for starting lists"
651 "]" -> "a right square bracket, to end a list"
652 "{" -> "a left curly brace, for starting records"
653 "}" -> "a right curly brace, to end a record"
654 "{-|" -> "a doc comment, like {-| this -}"
655 _ -> "the (" <> symbol <> ") symbol"
658 "a lower-case variable, like `x` or `user`"
661 "an upper-case variable, like `Maybe` or `Just`"
664 "an infix operator, like (+) or (==)"
667 "a digit from 0 to 9"
674 Decl -> "a declaration, like `x = 5` or `type alias Model = { ... }`"
675 Expr -> "an expression, like x or 42"
676 AfterOpExpr op -> "an expression after that (" <> N.toString op <> ") operator, like x or 42"
677 ElseBranch -> "an `else` branch. An `if` must handle both possibilities."
678 Arg -> "an argument, like `name` or `total`"
679 Pattern -> "a pattern, like `name` or (Just x)"
680 Type -> "a type, like Int or (List String)"
681 Listing -> "a list of exposed values and types, like (..) or (x,y,z)"
682 Exposing -> "something like `exposing (..)`"
685equalsTheory :: ContextStack -> String
691 (context, _) : rest ->
693 ExprRecord -> "an equals sign (=) followed by an expression"
694 Definition name -> "an equals sign (=) followed by " <> N.toString name <> "'s definition"
695 TypeUnion -> "an equals sign (=) followed by the first union type constructor"
696 TypeAlias -> "an equals sign (=) followed by the aliased type"
697 _ -> equalsTheory rest
700barTheory :: ContextStack -> String
706 (context, _) : rest ->
708 ExprRecord -> "a vertical bar (|) followed by the record fields you want to update"
709 TypeRecord -> "a vertical bar (|) followed by some record field types"
710 TypeUnion -> "a vertical bar (|) followed by more union type constructors"
714badSpace :: ContextStack -> String
718 "more indentation? I was not done with that last thing yet."
720 (context, _) : rest ->
722 ExprIf -> "the end of that `if`" <> badSpaceExprEnd rest
723 ExprLet -> "the end of that `let`" <> badSpaceExprEnd rest
724 ExprFunc -> badSpace rest
725 ExprCase -> "more of that `case`" <> badSpaceExprEnd rest
726 ExprList -> "the end of that list" <> badSpaceExprEnd rest
727 ExprTuple -> "a closing paren" <> badSpaceExprEnd rest
728 ExprRecord -> "the end of that record" <> badSpaceExprEnd rest
729 Definition name -> "the rest of " <> N.toString name <> "'s definition" <> badSpaceExprEnd stack
730 Annotation name -> "the rest of " <> N.toString name <> "'s type annotation" <> badSpaceEnd
731 TypeTuple -> "a closing paren" <> badSpaceEnd
732 TypeRecord -> "the end of that record" <> badSpaceEnd
733 PatternList -> "the end of that list" <> badSpaceEnd
734 PatternTuple -> "a closing paren" <> badSpaceEnd
735 PatternRecord -> "the end of that record" <> badSpaceEnd
736 Module -> "something like `module Main exposing (..)`"
737 Import -> "something like `import Html exposing (..)`"
738 TypeUnion -> "more of that union type" <> badSpaceEnd
739 TypeAlias -> "more of that type alias" <> badSpaceEnd
740 Infix -> "more of that infix declaration" <> badSpaceEnd
741 Port -> "more of that port declaration" <> badSpaceEnd
746 ". Maybe you forgot some code? Or you need more indentation?"
749badSpaceExprEnd :: ContextStack -> String
750badSpaceExprEnd stack =
755 (Definition name, R.Position _ column) : _ ->
761 "more indentation? (Try " <> show (column + 1) <> "+ spaces.)"
763 ". Maybe you forgot some code? Or maybe the body of `"
765 <> "` 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