on master help sign in
Improve error message for stray equals sign

Improves https://github.com/elm-lang/error-message-catalog/issues/55

commit60ac31bdbbc0dbce22cc5f3832ca0e317ef2482f authorEvan Czaplicki <evancz@users.noreply.github.com> dateThu 15 Dec 2016, 19:45 (10 years ago) parentc39a65c
1 file changed+37−3
src/Reporting/Error/Syntax.hs+37−3
src/Reporting/Error/Syntax.hs+37−3
@@ -542,9 +542,7 @@ problemToReport subRegion problem =
542542 "Maybe you want :: instead? Or maybe your indentation is a bit off?"
543543
544544 Equals ->
545− badOp subRegion stack "an equals sign" "(=)"
546− "definitions"
547− "Maybe you want == instead? Or maybe your indentation is a bit off?"
545+ badEquals subRegion stack
548546
549547 Arrow ->
550548 if isCaseRelated stack then
@@ -631,6 +629,42 @@ badOp subRegion stack opName op setting hint =
631629 ]
632630
633631
632+badEquals :: Maybe R.Region -> ContextStack -> Report.Report
633+badEquals subRegion stack =
634+ let
635+ pre =
636+ "I was not expecting this equals sign"
637+ <> contextToText "" " while parsing " stack <> "."
638+ in
639+ makeParseReport subRegion pre (badEqualsHelp stack)
640+
641+
642+badEqualsHelp :: ContextStack -> Help.Doc
643+badEqualsHelp stack =
644+ case stack of
645+ [] ->
646+ reflowParagraph $
647+ "Maybe you want == instead? Or maybe your indentation is a bit off?"
648+
649+ (ExprRecord, _) : _ ->
650+ reflowParagraph $
651+ "Records look like { x = 3, y = 4 } with the equals sign right\
652+ \ after the field name. Maybe you forgot a comma?"
653+
654+ (Definition name, _) : rest ->
655+ reflowParagraph $
656+ "Maybe this is supposed to be a separate definition? If so, it\
657+ \ is indented too far. "
658+ <>
659+ if any ((==) ExprLet . fst) rest then
660+ "All definitions in a `let` expression must be vertically aligned."
661+ else
662+ "Spaces are not allowed before top-level definitions."
663+
664+ _ : rest ->
665+ badEqualsHelp rest
666+
667+
634668contextToText :: Text -> Text -> ContextStack -> Text
635669contextToText defaultText prefixText stack =
636670 case stack of