on master help sign in
Start filling in syntax errors

Some are adapted from older messages, some are totally new.

commit5147a5a95524088a2926440a1c0691c435d48003 authorEvan Czaplicki <evancz@users.noreply.github.com> dateFri 3 May 2019, 17:07 (7 years ago) parent90f1d38
1 file changed+443−167
compiler/src/Reporting/Error/Syntax.hs+443−167
compiler/src/Reporting/Error/Syntax.hs+443−167
@@ -53,9 +53,8 @@ import Data.Word (Word16)
5353
5454import qualified Elm.ModuleName as ModuleName
5555import Parse.Primitives (Row, Col)
56−--import qualified Parse.Primitives as P
5756import qualified Reporting.Annotation as A
58−--import qualified Reporting.Doc as D
57+import qualified Reporting.Doc as D
5958import qualified Reporting.Report as Report
6059import qualified Reporting.Render.Code as Code
6160
@@ -159,8 +158,8 @@ data DeclDef
159158 | DeclDefType Type Row Col
160159 | DeclDefArg Pattern Row Col
161160 | DeclDefBody Expr Row Col
162− | DeclDefNameRepeat Name.Name Row Col
163− | DeclDefNameMatch Name.Name Name.Name Row Col
161+ | DeclDefNameRepeat Row Col
162+ | DeclDefNameMatch Name.Name Row Col
164163 --
165164 | DeclDefIndentType Row Col
166165 | DeclDefIndentEquals Row Col
@@ -501,7 +500,6 @@ data Number
501500data Space
502501 = HasTab
503502 | EndlessMultiComment
504− | UnexpectedDocComment
505503
506504
507505data Operator
@@ -518,7 +516,446 @@ data Operator
518516
519517toReport :: Code.Source -> Error -> Report.Report
520518toReport source err =
521− error "TODO" source err
519+ case err of
520+ ModuleNameUnspecified name ->
521+ let
522+ region = toRegion 1 1
523+ in
524+ Report.Report "MODULE NAME MISSING" region [] $
525+ D.stack
526+ [ D.reflow $
527+ "I need the module name to be declared at the top of this file, like this:"
528+ , D.indent 4 $ D.green $ D.fromChars $
529+ "module " ++ ModuleName.toChars name ++ " exposing (..)"
530+ , D.reflow $
531+ "Try adding that as the first line of your file!"
532+ , D.toSimpleNote $
533+ "It is best to replace (..) with an explicit list of types and\
534+ \ functions you want to expose. When you know a value is only used\
535+ \ within this module, you can refactor without worrying about uses\
536+ \ elsewhere. Limiting exposed values can also speed up compilation\
537+ \ because I can skip a bunch of work if I see that the exposed API\
538+ \ has not changed."
539+ ]
540+
541+ ModuleNameMismatch expectedName (A.At region actualName) ->
542+ Report.Report "MODULE NAME MISMATCH" region [ModuleName.toChars expectedName] $
543+ Report.toCodeSnippet source region Nothing
544+ (
545+ "It looks like this module name is out of sync:"
546+ ,
547+ D.stack
548+ [ D.reflow $
549+ "I need it to match the file path, so I was expecting it to see `"
550+ ++ ModuleName.toChars expectedName
551+ ++ "` here. Make the following change, and you should be all set!"
552+ , D.indent 4 $
553+ D.red (D.fromName actualName) <> " -> " <> D.green (D.fromName expectedName)
554+ , D.toSimpleNote $
555+ "I require that module names correspond to file paths. This makes it much\
556+ \ easier to explore unfamiliar codebases! So if you want to keep the current\
557+ \ module name, try renaming the file instead."
558+ ]
559+ )
560+
561+ UnexpectedPort region ->
562+ error "TODO UnexpectedPort" region
563+
564+ NoPorts region ->
565+ error "TODO NoPorts" region
566+
567+ ParseError modul ->
568+ toParseErrorReport source modul
569+
570+
571+toParseErrorReport :: Code.Source -> Module -> Report.Report
572+toParseErrorReport source modul =
573+ case modul of
574+ ModuleSpace space row col ->
575+ toSpaceReport source space row col
576+
577+ ModuleEndOfFile row col ->
578+ let
579+ region = toRegion row col
580+ in
581+ Report.Report "EXPECTING END OF FILE" region [] $
582+ Report.toCodeSnippet source region Nothing
583+ (
584+ D.reflow $
585+ "I was not expecting to see anything more here:"
586+ ,
587+ D.reflow $
588+ "I think I have read all the declarations in the file and that there is nothing\
589+ \ left to see, so whatever I am running into is confusing me a lot!"
590+ )
591+
592+ Module row col ->
593+ error "TODO Module" row col
594+
595+ ModuleName row col ->
596+ error "TODO ModuleName" row col
597+
598+ ModuleExposing row col ->
599+ error "TODO ModuleExposing" row col
600+
601+ ModuleExposingList exposing row col ->
602+ error "TODO ModuleExposingList" exposing row col
603+
604+ --
605+ ModulePortModule row col ->
606+ error "TODO ModulePortModule" row col
607+
608+ ModuleEffect row col ->
609+ error "TODO ModuleEffect" row col
610+
611+ ModuleIndentStart row col ->
612+ error "TODO ModuleIndentStart" row col
613+
614+ ModuleIndentName row col ->
615+ error "TODO ModuleIndentName" row col
616+
617+ ModuleIndentExposing row col ->
618+ error "TODO ModuleIndentExposing" row col
619+
620+ ModuleIndentExposingList row col ->
621+ error "TODO ModuleIndentExposingList" row col
622+
623+ ModuleIndentPortModule row col ->
624+ error "TODO ModuleIndentPortModule" row col
625+
626+ FreshLineModuleStart row col ->
627+ error "TODO FreshLineModuleStart" row col
628+
629+ FreshLineAfterModuleLine row col ->
630+ error "TODO FreshLineAfterModuleLine" row col
631+
632+ FreshLineAfterDocComment row col ->
633+ error "TODO FreshLineAfterDocComment" row col
634+
635+ ImportStart row col ->
636+ error "TODO ImportStart" row col
637+
638+ ImportName row col ->
639+ error "TODO ImportName" row col
640+
641+ ImportAs row col ->
642+ error "TODO ImportAs" row col
643+
644+ ImportAlias row col ->
645+ error "TODO ImportAlias" row col
646+
647+ ImportExposing row col ->
648+ error "TODO ImportExposing" row col
649+
650+ ImportExposingList exposing row col ->
651+ error "TODO ImportExposingList" exposing row col
652+
653+ ImportEnd row col ->
654+ error "TODO ImportEnd" row col
655+
656+ ImportIndentName row col ->
657+ error "TODO ImportIndentName" row col
658+
659+ ImportIndentAs row col ->
660+ error "TODO ImportIndentAs" row col
661+
662+ ImportIndentAlias row col ->
663+ error "TODO ImportIndentAlias" row col
664+
665+ ImportIndentExposing row col ->
666+ error "TODO ImportIndentExposing" row col
667+
668+ ImportIndentExposingList row col ->
669+ error "TODO ImportIndentExposingList" row col
670+
671+ Infix row col ->
672+ error "TODO Infix" row col
673+
674+ Declarations decl _ _ ->
675+ toDeclarationsReport source decl
676+
677+
678+toSpaceReport :: Code.Source -> Space -> Row -> Col -> Report.Report
679+toSpaceReport source space row col =
680+ case space of
681+ HasTab ->
682+ let
683+ region = toRegion row col
684+ in
685+ Report.Report "NO TABS" region [] $
686+ Report.toCodeSnippet source region Nothing
687+ (
688+ D.reflow $
689+ "I ran into a tab, but tabs are not allowed in Elm files."
690+ ,
691+ D.reflow $
692+ "Replace the tab with spaces."
693+ )
694+
695+ EndlessMultiComment ->
696+ let
697+ region = toWiderRegion row col 2
698+ in
699+ Report.Report "ENDLESS COMMENT" region [] $
700+ Report.toCodeSnippet source region Nothing
701+ (
702+ D.reflow $
703+ "I cannot find the end of this multi-line comment:"
704+ ,
705+ D.stack -- "{-"
706+ [ D.reflow "Add a -} somewhere after this to end the comment."
707+ , D.toSimpleHint
708+ "Multi-line comments can be nested in Elm, so {- {- -} -} is a comment\
709+ \ that happens to contain another comment. Like parentheses and curly braces,\
710+ \ the start and end markers must always be balanced. Maybe that is the problem?"
711+ ]
712+ )
713+
714+
715+toRegion :: Row -> Col -> A.Region
716+toRegion row col =
717+ let
718+ pos = A.Position row col
719+ in
720+ A.Region pos pos
721+
722+
723+toWiderRegion :: Row -> Col -> Word16 -> A.Region
724+toWiderRegion row col extra =
725+ A.Region
726+ (A.Position row col)
727+ (A.Position row (col + extra))
728+
729+
730+toDeclarationsReport :: Code.Source -> Decl -> Report.Report
731+toDeclarationsReport source decl =
732+ case decl of
733+ DeclStart row col ->
734+ error "TODO DeclStart" row col
735+
736+ DeclSpace space row col ->
737+ toSpaceReport source space row col
738+
739+ Port port row col ->
740+ error "TODO Port" port row col
741+
742+ DeclType declType row col ->
743+ error "TODO DeclType" declType row col
744+
745+ DeclDef name declDef row col ->
746+ toDeclDefReport source name declDef row col
747+
748+ DeclFreshLineStart row col ->
749+ error "TODO DeclFreshLineStart" row col
750+
751+ DeclFreshLineAfterDocComment row col ->
752+ error "TODO DeclFreshLineAfterDocComment" row col
753+
754+
755+toDeclDefReport :: Code.Source -> Name.Name -> DeclDef -> Row -> Col -> Report.Report
756+toDeclDefReport source name declDef _ _ =
757+ case declDef of
758+ DeclDefSpace space row col ->
759+ toSpaceReport source space row col
760+
761+ DeclDefEquals row col ->
762+ error "TODO DeclDefEquals" row col
763+
764+ DeclDefType tipe row col ->
765+ error "TODO DeclDefType" tipe row col
766+
767+ DeclDefArg pattern row col ->
768+ error "TODO DeclDefArg" pattern row col
769+
770+ DeclDefBody expr row col ->
771+ toExprReport source name expr row col
772+
773+ DeclDefNameRepeat row col ->
774+ error "TODO DeclDefNameRepeat" name row col
775+
776+ DeclDefNameMatch defName row col ->
777+ error "TODO DeclDefNameMatch" name defName row col
778+
779+ DeclDefIndentType row col ->
780+ error "TODO DeclDefIndentType" row col
781+
782+ DeclDefIndentEquals row col ->
783+ error "TODO DeclDefIndentEquals" row col
784+
785+ DeclDefIndentBody row col ->
786+ error "TODO DeclDefIndentBody" row col
787+
788+ DeclDefFreshLineAfterType row col ->
789+ error "TODO DeclDefFreshLineAfterType" row col
790+
791+
792+
793+toExprReport :: Code.Source -> Name.Name -> Expr -> Row -> Col -> Report.Report
794+toExprReport source _ expr _ _ =
795+ case expr of
796+ Let _ _ _ ->
797+ error "TODO Let"
798+
799+ Case _ _ _ ->
800+ error "TODO Case"
801+
802+ If _ _ _ ->
803+ error "TODO If"
804+
805+ List _ _ _ ->
806+ error "TODO List"
807+
808+ Record _ _ _ ->
809+ error "TODO Record"
810+
811+ Update _ _ _ ->
812+ error "TODO Update"
813+
814+ Tuple _ _ _ ->
815+ error "TODO Tuple"
816+
817+ Func _ _ _ ->
818+ error "TODO Func"
819+
820+ Dot _ _ ->
821+ error "TODO Dot"
822+
823+ Access _ _ ->
824+ error "TODO Access"
825+
826+ OperatorRHS _ _ ->
827+ error "TODO OperatorRHS"
828+
829+ OperatorRight _ _ ->
830+ error "TODO OperatorRight"
831+
832+ OperatorReserved _ _ _ ->
833+ error "TODO OperatorReserved"
834+
835+ Start _ _ ->
836+ error "TODO Start"
837+
838+ Char _ _ _ ->
839+ error "TODO Char"
840+
841+ String _ _ _ ->
842+ error "TODO String"
843+
844+ Number number row col ->
845+ toNumberReport source number row col
846+
847+ Space space row col ->
848+ toSpaceReport source space row col
849+
850+ EndlessShader row col ->
851+ let
852+ region = toWiderRegion row col 6
853+ in
854+ Report.Report "ENDLESS SHADER" region [] $
855+ Report.toCodeSnippet source region Nothing
856+ (
857+ D.reflow "I cannot find the end of this shader:"
858+ ,
859+ D.reflow "Add a |] somewhere after this to end the shader."
860+ )
861+
862+ ShaderProblem problem row col ->
863+ let
864+ region = toRegion row col
865+ in
866+ Report.Report "SHADER PROBLEM" region [] $
867+ Report.toCodeSnippet source region Nothing
868+ (
869+ D.reflow $
870+ "I ran into a problem while parsing this GLSL block."
871+ ,
872+ D.stack
873+ [ D.reflow $
874+ "I use a 3rd party GLSL parser for now, and I did my best to extract their error message:"
875+ , D.indent 4 $ D.vcat $
876+ map D.fromChars (filter (/="") (lines problem))
877+ ]
878+ )
879+
880+ IndentOperatorRight _ _ ->
881+ error "TODO IndentOperatorRight"
882+
883+ IndentMoreExpr _ _ ->
884+ error "TODO IndentMoreExpr"
885+
886+
887+toNumberReport :: Code.Source -> Number -> Row -> Col -> Report.Report
888+toNumberReport source number row col =
889+ let
890+ region = toRegion row col
891+ in
892+ case number of
893+ NumberEnd ->
894+ Report.Report "WEIRD NUMBER" region [] $
895+ Report.toCodeSnippet source region Nothing
896+ (
897+ D.reflow $
898+ "I thought I was reading a number, but I ran into some weird stuff here:"
899+ ,
900+ D.stack
901+ [ D.reflow $
902+ "I recognize numbers in the following formats:"
903+ , D.indent 4 $ D.vcat [ "42", "3.14", "6.022e23", "0x002B" ]
904+ , D.reflow $
905+ "So is there a way to write it like one of those?"
906+ ]
907+ )
908+
909+ NumberDot int ->
910+ Report.Report "WEIRD NUMBER" region [] $
911+ Report.toCodeSnippet source region Nothing
912+ (
913+ D.reflow $
914+ "Numbers cannot end with a dot like this:"
915+ ,
916+ D.fillSep
917+ ["Switching","to",D.green (D.fromChars (show int))
918+ ,"or",D.green (D.fromChars (show int ++ ".0"))
919+ ,"will","work","though!"
920+ ]
921+ )
922+
923+ NumberHexDigit ->
924+ Report.Report "WEIRD HEXIDECIMAL" region [] $
925+ Report.toCodeSnippet source region Nothing
926+ (
927+ D.reflow $
928+ "I thought I was reading a hexidecimal number until I got here:"
929+ ,
930+ D.stack
931+ [ D.reflow $
932+ "Valid hexidecimal digits include 0123456789abcdefABCDEF, so I can\
933+ \ only recognize things like this:"
934+ , D.indent 4 $ D.vcat [ "0x2B", "0x002B", "0x00ffb3" ]
935+ ]
936+ )
937+
938+ NumberNoLeadingZero ->
939+ Report.Report "LEADING ZEROS" region [] $
940+ Report.toCodeSnippet source region Nothing
941+ (
942+ D.reflow $
943+ "I do not accept numbers with leading zeros:"
944+ ,
945+ D.stack
946+ [ D.reflow $
947+ "Just delete the leading zeros and it should work!"
948+ , D.toSimpleNote $
949+ "Some languages let you to specify octal numbers by adding a leading zero.\
950+ \ So in C, writing 0111 is the same as writing 73. Some people are used to\
951+ \ that, but others probably want it to equal 111. Either path is going to\
952+ \ surprise people from certain backgrounds, so Elm tries to avoid this whole\
953+ \ situation."
954+ ]
955+ )
956+
957+
958+
522959{-
523960 case err of
524961 CommentOnNothing region ->
@@ -601,110 +1038,8 @@ toReport source err =
6011038 "Check out" "TODO" "for examples of doc comments."
6021039 )
6031040
604− MultiCommentEnd startRow startCol ->
605− let
606− startRegion =
607− A.Region
608− (A.Position startRow startCol)
609− (A.Position startRow (startCol + 3))
610− in
611− Report.Report "ENDLESS COMMENT" startRegion [] $
612− Report.toCodeSnippet source startRegion Nothing
613− (
614− D.reflow $
615− "I cannot find the end of this multi-line comment:"
616− ,
617− D.stack -- "{-"
618− [ D.reflow "Add a -} somewhere after this to end the comment."
619− , D.toSimpleHint
620− "Multi-line comments can be nested in Elm, so {- {- -} -} is a comment\
621− \ that happens to contain another comment. Like parentheses and curly braces,\
622− \ the start and end markers must always be balanced. Maybe that is the problem?"
623− ]
624− )
625−
6261041 -- Parse.Number
6271042
628− Number_Start ->
629− Report.Report "EXPECTING NUMBER" region [] $
630− Report.toCodeSnippet source region Nothing
631− (
632− D.reflow $
633− "I was expecting to see a number here:"
634− ,
635− D.stack
636− [ D.reflow $
637− "I recognize numbers in the following formats:"
638− , D.indent 4 $ D.vcat [ "42", "3.14", "6.022e23", "0x002B" ]
639− , D.reflow $
640− "So is there a way to write it like one of those?"
641− ]
642− )
643−
644− Number_End ->
645− Report.Report "WEIRD NUMBER" region [] $
646− Report.toCodeSnippet source region Nothing
647− (
648− D.reflow $
649− "I thought I was reading a number, but I ran into some weird stuff here:"
650− ,
651− D.stack
652− [ D.reflow $
653− "I recognize numbers in the following formats:"
654− , D.indent 4 $ D.vcat [ "42", "3.14", "6.022e23", "0x002B" ]
655− , D.reflow $
656− "So is there a way to write it like one of those?"
657− ]
658− )
659−
660− Number_Dot int ->
661− Report.Report "WEIRD NUMBER" region [] $
662− Report.toCodeSnippet source region Nothing
663− (
664− D.reflow $
665− "Numbers cannot end with a dot like this:"
666− ,
667− D.fillSep
668− ["Switching","to",D.green (D.fromChars (show int))
669− ,"or",D.green (D.fromChars (show int ++ ".0"))
670− ,"will","work","though!"
671− ]
672− )
673−
674− Number_HexDigit ->
675− Report.Report "WEIRD HEXIDECIMAL" region [] $
676− Report.toCodeSnippet source region Nothing
677− (
678− D.reflow $
679− "I thought I was reading a hexidecimal number until I got here:"
680− ,
681− D.stack
682− [ D.reflow $
683− "Valid hexidecimal digits include 0123456789abcdefABCDEF, so I can\
684− \ only recognize things like this:"
685− , D.indent 4 $ D.vcat [ "0x2B", "0x002B", "0x00ffb3" ]
686− ]
687− )
688−
689− Number_NoLeadingZero ->
690− Report.Report "LEADING ZEROS" region [] $
691− Report.toCodeSnippet source region Nothing
692− (
693− D.reflow $
694− "I do not accept numbers with leading zeros:"
695− ,
696− D.stack
697− [ D.reflow $
698− "Just delete the leading zeros and it should work!"
699− , D.toSimpleNote $
700− "Some languages let you to specify octal numbers by adding a leading zero.\
701− \ So in C, writing 0111 is the same as writing 73. Some people are used to\
702− \ that, but others probably want it to equal 111. Either path is going to\
703− \ surprise people from certain backgrounds, so Elm tries to avoid this whole\
704− \ situation."
705− ]
706− )
707−
7081043 Precedence ->
7091044 Report.Report "EXPECTING PRECEDENCE" region [] $
7101045 Report.toCodeSnippet source region Nothing
@@ -943,43 +1278,6 @@ toReport source err =
9431278 ]
9441279 )
9451280
946− -- Parse.Shader
947−
948− ShaderStart ->
949− Report.Report "EXPECTING A SHADER" region [] $
950− Report.toCodeSnippet source region Nothing $
951− (
952− D.reflow $
953− "I was expecting a GLSL shader block here:"
954− ,
955− D.reflow $
956− "These blocks start with [glsl| and end with |] and should be\
957− \ filled with GLSL code."
958− )
959−
960− ShaderEnd startCol endCol ->
961− let
962− shaderRegion =
963− A.Region (A.Position row startCol) (A.Position row endCol)
964− in
965− Report.Report "ENDLESS SHADER" shaderRegion [] $
966− Report.toCodeSnippet source shaderRegion Nothing
967− (
968− D.reflow "I cannot find the end of this shader:"
969− ,
970− D.reflow "Add a |] somewhere after this to end the shader."
971− )
972−
973− ShaderProblem problem ->
974− Report.Report "SHADER PROBLEM" region [] $
975− Report.toCodeSnippet source region Nothing
976− (
977− D.reflow $
978− "I ran into a problem while parsing this GLSL block."
979− ,
980− D.fromChars problem
981− )
982−
9831281 Operator ->
9841282 Report.Report "EXPECTING AN OPERATOR" region [] $
9851283 Report.toCodeSnippet source region Nothing
@@ -1027,14 +1325,6 @@ toReport source err =
10271325
10281326 -- Parse.Pattern
10291327
1030− Pattern ->
1031− error "TODO" context
1032−{-
1033− case context of
1034− P.Frame r c
1035− P.NoContext ->
1036−-}
1037−
10381328 FloatInPattern ->
10391329 Report.Report "INVALID PATTERN" region [] $
10401330 Report.toCodeSnippet source region Nothing
@@ -1076,20 +1366,6 @@ toReport source err =
10761366 )
10771367-}
10781368
1079−{-
1080− EndOfFile ->
1081− Report.Report "EXPECTING END OF FILE" region [] $
1082− Report.toCodeSnippet source region Nothing
1083− (
1084− D.reflow $
1085− "I was not expecting to see anything more here:"
1086− ,
1087− D.reflow $
1088− "I think I have read all the declarations in the file and that there is nothing\
1089− \ left to see, so whatever I am running into is confusing me a lot!"
1090− )
1091−
1092−-}
10931369{-
10941370
10951371 TypeWithBadDefinition region annName defName ->