on master help sign in
Add error messages for most `import` syntax problems
commite5abcf4090eff27310208d7da716f1096a62ab9e authorEvan Czaplicki <evancz@users.noreply.github.com> dateThu 9 May 2019, 11:53 (7 years ago) parent0bdcdd8
1 file changed+90−11
compiler/src/Reporting/Error/Syntax.hs+90−11
compiler/src/Reporting/Error/Syntax.hs+90−11
@@ -620,31 +620,72 @@ toParseErrorReport source modul =
620620 error "TODO FreshLineAfterDocComment" row col
621621
622622 ImportStart row col ->
623− error "TODO ImportStart" row col
623+ toImportReport source row col
624624
625625 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+ )
627648
628649 ImportAs row col ->
629− error "TODO ImportAs" row col
650+ toImportReport source row col
630651
631652 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+ )
633674
634675 ImportExposing row col ->
635− error "TODO ImportExposing" row col
676+ toImportReport source row col
636677
637678 ImportExposingList exposing row col ->
638679 error "TODO ImportExposingList" exposing row col
639680
640681 ImportEnd row col ->
641− error "TODO ImportEnd" row col
682+ toImportReport source row col
642683
643684 ImportIndentName row col ->
644− error "TODO ImportIndentName" row col
685+ toImportReport source row col
645686
646687 ImportIndentAlias row col ->
647− error "TODO ImportIndentAlias" row col
688+ toImportReport source row col
648689
649690 ImportIndentExposingList row col ->
650691 let
@@ -661,8 +702,8 @@ toParseErrorReport source modul =
661702 "I was expecting to see the list of exposed values next. For example, here\
662703 \ are two ways to expose values from the `Html` module:"
663704 , D.indent 4 $ D.vcat $
664− [ D.fillSep [D.blue "import","Html",D.blue "exposing",D.green "(..)"]
665− , D.fillSep [D.blue "import","Html",D.blue "exposing",D.green "(Html, div, text)"]
705+ [ D.fillSep [D.blue "import","Html",D.blue "exposing","(..)"]
706+ , D.fillSep [D.blue "import","Html",D.blue "exposing","(Html, div, text)"]
666707 ]
667708 , D.reflow $
668709 "I generally recommend the second style. It is more explicit, making it\
@@ -689,6 +730,40 @@ toParseErrorReport source modul =
689730 toDeclarationsReport source decl
690731
691732
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+
692767toSpaceReport :: Code.Source -> Space -> Row -> Col -> Report.Report
693768toSpaceReport source space row col =
694769 case space of
@@ -726,6 +801,10 @@ toSpaceReport source space row col =
726801 )
727802
728803
804+
805+-- DECLARATIONS
806+
807+
729808toRegion :: Row -> Col -> A.Region
730809toRegion row col =
731810 let
@@ -1621,7 +1700,7 @@ toRecordReport source context record startRow startCol =
16211700 region = toRegion row col
16221701 in
16231702 case Code.whatIsNext source row col of
1624− Keyword keyword ->
1703+ Code.Keyword keyword ->
16251704 Report.Report "RESERVED WORD" region [] $
16261705 Code.toSnippet source surroundings (Just region)
16271706 (