on master help sign in
Add error messages for most `import` syntax problems
e5abcf4Evan Czaplicki
compiler/src/Reporting/Error/Syntax.hs+90−11
@@ -620,31 +620,72 @@ toParseErrorReport source modul =
620 error "TODO FreshLineAfterDocComment" row col
620 error "TODO FreshLineAfterDocComment" row col
621
621
622 ImportStart row col ->
622 ImportStart row col ->
623− error "TODO ImportStart" row col
623+ toImportReport source row col
624
624
625 ImportName row col ->
625 ImportName row col ->
626− error "TODO ImportName" row col
626+ let
627+ region = toRegion row col
628+ in
629+ Report.Report "EXPECTING IMPORT NAME" region [] $
630+ Code.toSnippet source region Nothing
631+ (
632+ D.reflow $
633+ "I was parsing an `import` until I got stuck here:"
634+ ,
635+ D.stack
636+ [ D.reflow $
637+ "I was expecting to see a module name next, like in these examples:"
638+ , D.indent 4 $ D.vcat $
639+ [ D.fillSep [D.blue "import","Dict"]
640+ , D.fillSep [D.blue "import","Maybe"]
641+ , D.fillSep [D.blue "import","Html.Attributes"]
642+ , D.fillSep [D.blue "import","Json.Decode"]
643+ ]
644+ , D.reflow $
645+ "Notice that the module names all start with capital letters. That is required!"
646+ ]
647+ )
627
648
628 ImportAs row col ->
649 ImportAs row col ->
629− error "TODO ImportAs" row col
650+ toImportReport source row col
630
651
631 ImportAlias row col ->
652 ImportAlias row col ->
632− error "TODO ImportAlias" row col
653+ let
654+ region = toRegion row col
655+ in
656+ Report.Report "EXECTING IMPORT ALIAS" region [] $
657+ Code.toSnippet source region Nothing
658+ (
659+ D.reflow $
660+ "I was parsing an `import` until I got stuck here:"
661+ ,
662+ D.stack
663+ [ D.reflow $
664+ "I was expecting to see an alias next, like in these examples:"
665+ , D.indent 4 $ D.vcat $
666+ [ D.fillSep [D.blue "import","Html.Attributes",D.blue "as","Attr"]
667+ , D.fillSep [D.blue "import","WebGL.Texture",D.blue "as","Texture"]
668+ , D.fillSep [D.blue "import","Json.Decode",D.blue "as","D"]
669+ ]
670+ , D.reflow $
671+ "Notice that the alias always starts with a capital letter. That is required!"
672+ ]
673+ )
633
674
634 ImportExposing row col ->
675 ImportExposing row col ->
635− error "TODO ImportExposing" row col
676+ toImportReport source row col
636
677
637 ImportExposingList exposing row col ->
678 ImportExposingList exposing row col ->
638 error "TODO ImportExposingList" exposing row col
679 error "TODO ImportExposingList" exposing row col
639
680
640 ImportEnd row col ->
681 ImportEnd row col ->
641− error "TODO ImportEnd" row col
682+ toImportReport source row col
642
683
643 ImportIndentName row col ->
684 ImportIndentName row col ->
644− error "TODO ImportIndentName" row col
685+ toImportReport source row col
645
686
646 ImportIndentAlias row col ->
687 ImportIndentAlias row col ->
647− error "TODO ImportIndentAlias" row col
688+ toImportReport source row col
648
689
649 ImportIndentExposingList row col ->
690 ImportIndentExposingList row col ->
650 let
691 let
@@ -661,8 +702,8 @@ toParseErrorReport source modul =
661 "I was expecting to see the list of exposed values next. For example, here\
702 "I was expecting to see the list of exposed values next. For example, here\
662 \ are two ways to expose values from the `Html` module:"
703 \ are two ways to expose values from the `Html` module:"
663 , D.indent 4 $ D.vcat $
704 , D.indent 4 $ D.vcat $
664− [ D.fillSep [D.blue "import","Html",D.blue "exposing",D.green "(..)"]
705+ [ D.fillSep [D.blue "import","Html",D.blue "exposing","(..)"]
665− , D.fillSep [D.blue "import","Html",D.blue "exposing",D.green "(Html, div, text)"]
706+ , D.fillSep [D.blue "import","Html",D.blue "exposing","(Html, div, text)"]
666 ]
707 ]
667 , D.reflow $
708 , D.reflow $
668 "I generally recommend the second style. It is more explicit, making it\
709 "I generally recommend the second style. It is more explicit, making it\
@@ -689,6 +730,40 @@ toParseErrorReport source modul =
689 toDeclarationsReport source decl
730 toDeclarationsReport source decl
690
731
691
732
733+
734+-- IMPORTS
735+
736+
737+toImportReport :: Code.Source -> Row -> Col -> Report.Report
738+toImportReport source row col =
739+ let
740+ region = toRegion row col
741+ in
742+ Report.Report "UNFINISHED IMPORT" region [] $
743+ Code.toSnippet source region Nothing
744+ (
745+ D.reflow $
746+ "I am partway through parsing an import, but I got stuck here:"
747+ ,
748+ D.stack
749+ [ D.reflow $
750+ "Here are some examples of valid `import` declarations:"
751+ , D.indent 4 $ D.vcat $
752+ [ D.fillSep [D.blue "import","Html"]
753+ , D.fillSep [D.blue "import","Html",D.blue "as","H"]
754+ , D.fillSep [D.blue "import","Html",D.blue "exposing","(Html, div, text)"]
755+ , D.fillSep [D.blue "import","Html",D.blue "as","H",D.blue "exposing","(..)"]
756+ ]
757+ , D.reflow $
758+ "You are probably trying to import a different module, but try to make it look like one of these examples!"
759+ ]
760+ )
761+
762+
763+
764+-- SPACES
765+
766+
692toSpaceReport :: Code.Source -> Space -> Row -> Col -> Report.Report
767toSpaceReport :: Code.Source -> Space -> Row -> Col -> Report.Report
693toSpaceReport source space row col =
768toSpaceReport source space row col =
694 case space of
769 case space of
@@ -726,6 +801,10 @@ toSpaceReport source space row col =
726 )
801 )
727
802
728
803
804+
805+-- DECLARATIONS
806+
807+
729toRegion :: Row -> Col -> A.Region
808toRegion :: Row -> Col -> A.Region
730toRegion row col =
809toRegion row col =
731 let
810 let
@@ -1621,7 +1700,7 @@ toRecordReport source context record startRow startCol =
1621 region = toRegion row col
1700 region = toRegion row col
1622 in
1701 in
1623 case Code.whatIsNext source row col of
1702 case Code.whatIsNext source row col of
1624− Keyword keyword ->
1703+ Code.Keyword keyword ->
1625 Report.Report "RESERVED WORD" region [] $
1704 Report.Report "RESERVED WORD" region [] $
1626 Code.toSnippet source surroundings (Just region)
1705 Code.toSnippet source surroundings (Just region)
1627 (
1706 (