on master help sign in
Add the beginnings of syntax errors for type declarations
commit3cb82f2b90fd72262b73d8cd737735f499616212 authorEvan Czaplicki <evancz@users.noreply.github.com> dateFri 17 May 2019, 11:18 (7 years ago) parent5c849ca
1 file changed+216−9
compiler/src/Reporting/Error/Syntax.hs+216−9
compiler/src/Reporting/Error/Syntax.hs+216−9
@@ -417,7 +417,6 @@ data PList
417417data Type
418418 = TRecord TRecord Row Col
419419 | TTuple TTuple Row Col
420− | TVariant Row Col
421420 | TArrow Row Col
422421 --
423422 | TStart Row Col
@@ -973,11 +972,11 @@ toDeclarationsReport source decl =
973972 DeclSpace space row col ->
974973 toSpaceReport source space row col
975974
976− Port port row col ->
977− error "TODO Port" port row col
975+ Port port_ row col ->
976+ error "TODO Port" port_ row col
978977
979978 DeclType declType row col ->
980− error "TODO DeclType" declType row col
979+ toDeclTypeReport source declType row col
981980
982981 DeclDef name declDef row col ->
983982 toDeclDefReport source name declDef row col
@@ -989,6 +988,215 @@ toDeclarationsReport source decl =
989988 error "TODO DeclFreshLineAfterDocComment" row col
990989
991990
991+
992+-- DECL TYPE
993+
994+
995+toDeclTypeReport :: Code.Source -> DeclType -> Row -> Col -> Report.Report
996+toDeclTypeReport source declType startRow startCol =
997+ case declType of
998+ DT_Space space row col ->
999+ toSpaceReport source space row col
1000+
1001+ DT_Name row col ->
1002+ let
1003+ surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
1004+ region = toRegion row col
1005+ in
1006+ Report.Report "EXPECTING TYPE NAME" region [] $
1007+ Code.toSnippet source surroundings (Just region)
1008+ (
1009+ D.reflow $
1010+ "I think I am parsing a type declaration, but I got stuck here:"
1011+ ,
1012+ D.stack
1013+ [ D.fillSep $
1014+ ["I","was","expecting","a","name","like",D.dullyellow "Status","or",D.dullyellow "Style"
1015+ ,"next.","Just","make","sure","it","is","a","name","that","starts","with","a","capital","letter!"
1016+ ]
1017+ , customTypeNote
1018+ ]
1019+ )
1020+
1021+ DT_Alias typeAlias row col ->
1022+ toTypeAliasReport source typeAlias row col
1023+
1024+ DT_Union customType row col ->
1025+ toCustomTypeReport source customType row col
1026+
1027+ DT_IndentName row col ->
1028+ let
1029+ surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
1030+ region = toRegion row col
1031+ in
1032+ Report.Report "EXPECTING TYPE NAME" region [] $
1033+ Code.toSnippet source surroundings (Just region)
1034+ (
1035+ D.reflow $
1036+ "I think I am parsing a type declaration, but I got stuck here:"
1037+ ,
1038+ D.stack
1039+ [ D.fillSep $
1040+ ["I","was","expecting","a","name","like",D.dullyellow "Status","or",D.dullyellow "Style"
1041+ ,"next.","Just","make","sure","it","is","a","name","that","starts","with","a","capital","letter!"
1042+ ]
1043+ , customTypeNote
1044+ ]
1045+ )
1046+
1047+
1048+toTypeAliasReport :: Code.Source -> TypeAlias -> Row -> Col -> Report.Report
1049+toTypeAliasReport source typeAlias startRow startCol =
1050+ case typeAlias of
1051+ AliasSpace space row col ->
1052+ toSpaceReport source space row col
1053+
1054+ AliasName row col ->
1055+ let
1056+ surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
1057+ region = toRegion row col
1058+ in
1059+ Report.Report "EXPECTING TYPE ALIAS NAME" region [] $
1060+ Code.toSnippet source surroundings (Just region)
1061+ (
1062+ D.reflow $
1063+ "I think I am parsing a type alias, but I got stuck here:"
1064+ ,
1065+ D.stack
1066+ [ D.fillSep $
1067+ ["I","was","expecting","a","name","like",D.dullyellow "Person","or",D.dullyellow "Point"
1068+ ,"next.","Just","make","sure","it","is","a","name","that","starts","with","a","capital","letter!"
1069+ ]
1070+ , typeAliasNote
1071+ ]
1072+ )
1073+
1074+ AliasEquals row col ->
1075+ error "TODO AliasEquals" row col
1076+
1077+ AliasBody tipe row col ->
1078+ toTypeReport source tipe row col
1079+
1080+ AliasIndentEquals row col ->
1081+ error "TODO AliasIndentEquals" row col
1082+
1083+ AliasIndentBody row col ->
1084+ let
1085+ surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
1086+ region = toRegion row col
1087+ in
1088+ Report.Report "UNFINISHED TYPE ALIAS" region [] $
1089+ Code.toSnippet source surroundings (Just region)
1090+ (
1091+ D.reflow $
1092+ "I am partway through parsing a type alias, but I got stuck here:"
1093+ ,
1094+ D.stack
1095+ [ D.fillSep $
1096+ ["I","was","expecting","to","see","a","type","next.","Something","as","simple"
1097+ ,"as",D.dullyellow "Int","or",D.dullyellow "Float","would","work!"
1098+ ]
1099+ , typeAliasNote
1100+ ]
1101+ )
1102+
1103+
1104+typeAliasNote :: D.Doc
1105+typeAliasNote =
1106+ D.stack
1107+ [ D.toSimpleNote $
1108+ "Here is an example of a valid `type alias` for reference:"
1109+ , D.vcat $
1110+ [ D.indent 0 $ D.fillSep [D.cyan "type",D.cyan "alias",D.green "Person","="]
1111+ , D.indent 2 $ D.vcat $
1112+ ["{ name : String"
1113+ ,", age : Int"
1114+ ,", height : Float"
1115+ ,"}"
1116+ ]
1117+ ]
1118+ , D.reflow $
1119+ "This would let us use `Person` as a shorthand for that record type. Using this\
1120+ \ shorthand makes type annotations much easier to read, and makes changing code\
1121+ \ easier if you decide later that there is more to a person than age and height!"
1122+ ]
1123+
1124+
1125+toCustomTypeReport :: Code.Source -> CustomType -> Row -> Col -> Report.Report
1126+toCustomTypeReport source customType startRow startCol =
1127+ case customType of
1128+ CT_Space space row col ->
1129+ toSpaceReport source space row col
1130+
1131+ CT_Name row col ->
1132+ let
1133+ surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
1134+ region = toRegion row col
1135+ in
1136+ Report.Report "EXPECTING TYPE NAME" region [] $
1137+ Code.toSnippet source surroundings (Just region)
1138+ (
1139+ D.reflow $
1140+ "I think I am parsing a type declaration, but I got stuck here:"
1141+ ,
1142+ D.stack
1143+ [ D.fillSep $
1144+ ["I","was","expecting","a","name","like",D.dullyellow "Status","or",D.dullyellow "Style"
1145+ ,"next.","Just","make","sure","it","is","a","name","that","starts","with","a","capital","letter!"
1146+ ]
1147+ , customTypeNote
1148+ ]
1149+ )
1150+
1151+ CT_Equals row col ->
1152+ error "TODO CT_Equals" row col
1153+
1154+ CT_Bar row col ->
1155+ error "TODO CT_Bar" row col
1156+
1157+ CT_Variant row col ->
1158+ error "TODO CT_Variant" row col
1159+
1160+ CT_VariantArg tipe row col ->
1161+ toTypeReport source tipe row col
1162+
1163+ CT_IndentEquals row col ->
1164+ error "TODO CT_IndentEquals" row col
1165+
1166+ CT_IndentBar row col ->
1167+ error "TODO CT_IndentBar" row col
1168+
1169+ CT_IndentAfterBar row col ->
1170+ error "TODO CT_IndentAfterBar" row col
1171+
1172+ CT_IndentAfterEquals row col ->
1173+ error "TODO CT_IndentAfterEquals" row col
1174+
1175+
1176+customTypeNote :: D.Doc
1177+customTypeNote =
1178+ D.stack
1179+ [ D.toSimpleNote $
1180+ "Here is an example of a valid `type` declaration for reference:"
1181+ , D.vcat $
1182+ [ D.indent 0 $ D.fillSep [D.cyan "type",D.green "Status"]
1183+ , D.indent 2 $ D.fillSep ["=","Failure"]
1184+ , D.indent 2 $ D.fillSep ["|","Waiting"]
1185+ , D.indent 2 $ D.fillSep ["|","Success","String"]
1186+ ]
1187+ , D.reflow $
1188+ "This defines a new `Status` type with three variants. This could be useful if\
1189+ \ we are waiting for an HTTP request. Maybe we start with `Waiting` and then\
1190+ \ switch to `Failure` or `Success \"message from server\"` depending on how\
1191+ \ things go. Notice that the Success variant has some associated data, allowing\
1192+ \ us to store a String if the request goes well!"
1193+ ]
1194+
1195+
1196+
1197+-- DECL DEF
1198+
1199+
9921200toDeclDefReport :: Code.Source -> Name.Name -> DeclDef -> Row -> Col -> Report.Report
9931201toDeclDefReport source name declDef startRow startCol =
9941202 case declDef of
@@ -1063,7 +1271,7 @@ toDeclDefReport source name declDef startRow startCol =
10631271 )
10641272
10651273 DeclDefType tipe row col ->
1066− error "TODO DeclDefType" toTypeReport tipe row col
1274+ toTypeReport source tipe row col
10671275
10681276 DeclDefArg pattern row col ->
10691277 toPatternReport source PArg pattern row col
@@ -3400,6 +3608,8 @@ toPListReport source context list startRow startCol =
34003608-- TYPES
34013609
34023610
3611+-- TODO add context? say something special if you are in type annotation vs custom type vs type alias?
3612+--
34033613toTypeReport :: Code.Source -> Type -> Row -> Col -> Report.Report
34043614toTypeReport source tipe startRow startCol =
34053615 case tipe of
@@ -3409,14 +3619,11 @@ toTypeReport source tipe startRow startCol =
34093619 TTuple tuple row col ->
34103620 toTTupleReport source tuple row col
34113621
3412− TVariant row col ->
3413− error "TODO TVariant" row col startRow startCol
3414−
34153622 TArrow row col ->
34163623 error "TODO TArrow" row col
34173624
34183625 TStart row col ->
3419− error "TODO TStart" row col
3626+ error "TODO TStart" row col startRow startCol
34203627
34213628 TSpace space row col ->
34223629 toSpaceReport source space row col