on master help sign in
Finish syntax messages for types and declarations
fb43331Evan Czaplicki
compiler/src/Parse/Module.hs+1−1
@@ -171,7 +171,7 @@ chompDecls :: [Decl.Decl] -> Parser E.Decl [Decl.Decl]
171chompDecls decls =
171chompDecls decls =
172 do (decl, _) <- Decl.declaration
172 do (decl, _) <- Decl.declaration
173 oneOfWithFallback
173 oneOfWithFallback
174− [ do Space.checkFreshLine E.DeclFreshLineStart
174+ [ do Space.checkFreshLine E.DeclStart
175 chompDecls (decl:decls)
175 chompDecls (decl:decls)
176 ]
176 ]
177 (reverse (decl:decls))
177 (reverse (decl:decls))
compiler/src/Parse/Type.hs+1−1
@@ -95,7 +95,7 @@ expression =
95 ]
95 ]
96 oneOfWithFallback
96 oneOfWithFallback
97 [ do Space.checkIndent end1 E.TIndentStart -- should never trigger
97 [ do Space.checkIndent end1 E.TIndentStart -- should never trigger
98− word2 0x2D 0x3E {-->-} E.TArrow
98+ word2 0x2D 0x3E {-->-} E.TStart -- could just be another type instead
99 Space.chompAndCheckIndent E.TSpace E.TIndentStart
99 Space.chompAndCheckIndent E.TSpace E.TIndentStart
100 (tipe2, end2) <- expression
100 (tipe2, end2) <- expression
101 let tipe = A.at start end2 (Src.TLambda tipe1 tipe2)
101 let tipe = A.at start end2 (Src.TLambda tipe1 tipe2)
compiler/src/Reporting/Error/Syntax.hs+213−34
@@ -134,7 +134,6 @@ data Decl
134 | DeclType DeclType Row Col
134 | DeclType DeclType Row Col
135 | DeclDef Name.Name DeclDef Row Col
135 | DeclDef Name.Name DeclDef Row Col
136 --
136 --
137− | DeclFreshLineStart Row Col
138 | DeclFreshLineAfterDocComment Row Col
137 | DeclFreshLineAfterDocComment Row Col
139
138
140
139
@@ -408,7 +407,6 @@ data PList
408data Type
407data Type
409 = TRecord TRecord Row Col
408 = TRecord TRecord Row Col
410 | TTuple TTuple Row Col
409 | TTuple TTuple Row Col
411− | TArrow Row Col
412 --
410 --
413 | TStart Row Col
411 | TStart Row Col
414 | TSpace Space Row Col
412 | TSpace Space Row Col
@@ -1234,7 +1232,106 @@ toDeclarationsReport :: Code.Source -> Decl -> Report.Report
1234toDeclarationsReport source decl =
1232toDeclarationsReport source decl =
1235 case decl of
1233 case decl of
1236 DeclStart row col ->
1234 DeclStart row col ->
1237− error "TODO DeclStart" row col
1235+ case Code.whatIsNext source row col of
1236+ Code.Close term _ ->
1237+ let
1238+ region = toRegion row col
1239+ in
1240+ Report.Report ("STRAY " ++ map Char.toUpper term) region [] $
1241+ Code.toSnippet source region Nothing
1242+ (
1243+ D.reflow $
1244+ "I was not expecting to see a " ++ term ++ " here:"
1245+ , D.reflow $
1246+ "It does not match up with anything. Maybe try deleting it?"
1247+ )
1248+
1249+ Code.Keyword keyword ->
1250+ let
1251+ region = toKeywordRegion row col keyword
1252+ in
1253+ Report.Report "RESERVED WORD" region [] $
1254+ Code.toSnippet source region Nothing
1255+ (
1256+ D.reflow $
1257+ "I was not expecting to run into the `" ++ keyword ++ "` keyword here:"
1258+ ,
1259+ case keyword of
1260+ "import" ->
1261+ D.reflow $
1262+ "It is reserved for declaring imports at the top of your module. If you want\
1263+ \ another import, try moving it up top with the other imports. If you want to\
1264+ \ define a value or function, try changing the name to something else!"
1265+
1266+ "case" ->
1267+ D.stack
1268+ [ D.reflow $
1269+ "It is reserved for writing `case` expressions. Try using a different name?"
1270+ , D.toSimpleNote $
1271+ "If you are trying to write a `case` expression, it needs to be part of a\
1272+ \ definition. So you could write something like this instead:"
1273+ , D.vcat $
1274+ [ D.indent 0 $ D.fillSep [D.green "getWidth","maybeWidth","="]
1275+ , D.indent 2 $ D.fillSep [D.cyan "case","maybeWidth",D.cyan "of"]
1276+ , D.indent 4 $ D.fillSep [D.blue "Just","width","->"]
1277+ , D.indent 6 $ D.fillSep ["width","+",D.dullyellow "200"]
1278+ , ""
1279+ , D.indent 4 $ D.fillSep [D.blue "Nothing","->"]
1280+ , D.indent 6 $ D.fillSep [D.dullyellow "400"]
1281+ ]
1282+ , D.reflow $
1283+ "This defines a `getWidth` function that we can use elsewhere in our program."
1284+ ]
1285+
1286+ "if" ->
1287+ D.stack
1288+ [ D.reflow $
1289+ "It is reserved for writing `if` expressions. Try using a different name?"
1290+ , D.toSimpleNote $
1291+ "If you are trying to write an `if` expression, it needs to be part of a\
1292+ \ definition. So you could write something like this instead:"
1293+ , D.vcat $
1294+ [ D.indent 0 $ D.fillSep [D.green "reviewPowerLevel","powerLevel","="]
1295+ , D.indent 2 $ D.fillSep $
1296+ [D.cyan "if","powerLevel",">",D.dullyellow "9000"
1297+ ,D.cyan "then",D.dullyellow "\"OVER 9000!!!\""
1298+ ,D.cyan "else",D.dullyellow "\"not bad\""
1299+ ]
1300+ ]
1301+ , D.reflow $
1302+ "This defines a `reviewPowerLevel` function that we can use elsewhere in our program."
1303+ ]
1304+
1305+ _ ->
1306+ D.reflow $
1307+ "It is a reserved word. Try changing the name to something else?"
1308+ )
1309+
1310+ _ ->
1311+ let
1312+ region = toRegion row col
1313+ in
1314+ Report.Report "SYNTAX PROBLEM" region [] $
1315+ Code.toSnippet source region Nothing
1316+ (
1317+ D.reflow $
1318+ "I was expecting to see a new declaration here:"
1319+ ,
1320+ D.stack
1321+ [ D.reflow $
1322+ "I am not sure what is going wrong exactly, but here are a couple valid\
1323+ \ declarations for reference:"
1324+ , D.vcat
1325+ [ D.fillSep [D.green "add",":","Int","->","Int","->","Int"]
1326+ , D.fillSep [D.green "add","x","y","="]
1327+ , D.fillSep [" ","x","+","y"]
1328+ ]
1329+ , D.fillSep [D.cyan "type",D.green "User","=","Anonymous","|","LoggedIn String"]
1330+ , D.reflow $
1331+ "Note that all declarations start with lower-case letters in Elm. Capitalization\
1332+ \ makes a difference, so be consious of that if you are working from examples!"
1333+ ]
1334+ )
1238
1335
1239 DeclSpace space row col ->
1336 DeclSpace space row col ->
1240 toSpaceReport source space row col
1337 toSpaceReport source space row col
@@ -1248,11 +1345,20 @@ toDeclarationsReport source decl =
1248 DeclDef name declDef row col ->
1345 DeclDef name declDef row col ->
1249 toDeclDefReport source name declDef row col
1346 toDeclDefReport source name declDef row col
1250
1347
1251− DeclFreshLineStart row col ->
1252− error "TODO DeclFreshLineStart" row col
1253−
1254 DeclFreshLineAfterDocComment row col ->
1348 DeclFreshLineAfterDocComment row col ->
1255− error "TODO DeclFreshLineAfterDocComment" row col
1349+ let
1350+ region = toRegion row col
1351+ in
1352+ Report.Report "EXPECTING DECLARATION" region [] $
1353+ Code.toSnippet source region Nothing
1354+ (
1355+ D.reflow $
1356+ "I just saw a doc comment, but then I got stuck here:"
1357+ ,
1358+ D.reflow $
1359+ "I was expecting to see the corresponding declaration next, starting on a fresh\
1360+ \ line with no indentation."
1361+ )
1256
1362
1257
1363
1258
1364
@@ -1324,7 +1430,7 @@ toPortReport source port_ startRow startCol =
1324 )
1430 )
1325
1431
1326 PortType tipe row col ->
1432 PortType tipe row col ->
1327− toTypeReport source tipe row col
1433+ toTypeReport source TC_Port tipe row col
1328
1434
1329 PortIndentName row col ->
1435 PortIndentName row col ->
1330 let
1436 let
@@ -1536,7 +1642,7 @@ toTypeAliasReport source typeAlias startRow startCol =
1536 )
1642 )
1537
1643
1538 AliasBody tipe row col ->
1644 AliasBody tipe row col ->
1539− toTypeReport source tipe row col
1645+ toTypeReport source TC_TypeAlias tipe row col
1540
1646
1541 AliasIndentEquals row col ->
1647 AliasIndentEquals row col ->
1542 let
1648 let
@@ -1703,7 +1809,7 @@ toCustomTypeReport source customType startRow startCol =
1703 )
1809 )
1704
1810
1705 CT_VariantArg tipe row col ->
1811 CT_VariantArg tipe row col ->
1706− toTypeReport source tipe row col
1812+ toTypeReport source TC_CustomType tipe row col
1707
1813
1708 CT_IndentEquals row col ->
1814 CT_IndentEquals row col ->
1709 let
1815 let
@@ -1876,7 +1982,7 @@ toDeclDefReport source name declDef startRow startCol =
1876 )
1982 )
1877
1983
1878 DeclDefType tipe row col ->
1984 DeclDefType tipe row col ->
1879− toTypeReport source tipe row col
1985+ toTypeReport source (TC_Annotation name) tipe row col
1880
1986
1881 DeclDefArg pattern row col ->
1987 DeclDefArg pattern row col ->
1882 toPatternReport source PArg pattern row col
1988 toPatternReport source PArg pattern row col
@@ -2916,10 +3022,10 @@ toUnfinishCaseReport source row col startRow startCol message =
2916 "Here is an example of a valid `case` expression for reference."
3022 "Here is an example of a valid `case` expression for reference."
2917 , D.vcat $
3023 , D.vcat $
2918 [ D.indent 4 $ D.fillSep [D.cyan "case","maybeWidth",D.cyan "of"]
3024 [ D.indent 4 $ D.fillSep [D.cyan "case","maybeWidth",D.cyan "of"]
2919− , D.indent 6 $ D.fillSep [D.green "Just","width","->"]
3025+ , D.indent 6 $ D.fillSep [D.blue "Just","width","->"]
2920 , D.indent 8 $ D.fillSep ["width","+",D.dullyellow "200"]
3026 , D.indent 8 $ D.fillSep ["width","+",D.dullyellow "200"]
2921 , ""
3027 , ""
2922− , D.indent 6 $ D.fillSep [D.green "Nothing","->"]
3028+ , D.indent 6 $ D.fillSep [D.blue "Nothing","->"]
2923 , D.indent 8 $ D.fillSep [D.dullyellow "400"]
3029 , D.indent 8 $ D.fillSep [D.dullyellow "400"]
2924 ]
3030 ]
2925 , D.reflow $
3031 , D.reflow $
@@ -2939,10 +3045,10 @@ addNoteForCaseIndentError message =
2939 \ something like this:"
3045 \ something like this:"
2940 , D.vcat $
3046 , D.vcat $
2941 [ D.indent 4 $ D.fillSep [D.cyan "case","maybeWidth",D.cyan "of"]
3047 [ D.indent 4 $ D.fillSep [D.cyan "case","maybeWidth",D.cyan "of"]
2942− , D.indent 6 $ D.fillSep [D.green "Just","width","->"]
3048+ , D.indent 6 $ D.fillSep [D.blue "Just","width","->"]
2943 , D.indent 8 $ D.fillSep ["width","+",D.dullyellow "200"]
3049 , D.indent 8 $ D.fillSep ["width","+",D.dullyellow "200"]
2944 , ""
3050 , ""
2945− , D.indent 6 $ D.fillSep [D.green "Nothing","->"]
3051+ , D.indent 6 $ D.fillSep [D.blue "Nothing","->"]
2946 , D.indent 8 $ D.fillSep [D.dullyellow "400"]
3052 , D.indent 8 $ D.fillSep [D.dullyellow "400"]
2947 ]
3053 ]
2948 , D.reflow $
3054 , D.reflow $
@@ -3326,7 +3432,7 @@ toRecordReport source context record startRow startCol =
3326 ,
3432 ,
3327 addNoteForRecordIndentError $
3433 addNoteForRecordIndentError $
3328 D.fillSep $
3434 D.fillSep $
3329− ["Try","putting","something,","like"
3435+ ["Try","putting","something","like"
3330 ,D.dullyellow "42","or",D.dullyellow"\"hello\"","for","now?"
3436 ,D.dullyellow "42","or",D.dullyellow"\"hello\"","for","now?"
3331 ]
3437 ]
3332 )
3438 )
@@ -4276,33 +4382,106 @@ toPListReport source context list startRow startCol =
4276-- TYPES
4382-- TYPES
4277
4383
4278
4384
4279−-- TODO add context? say something special if you are in type annotation vs custom type vs type alias?
4385+data TContext
4280−--
4386+ = TC_Annotation Name.Name
4387+ | TC_CustomType
4388+ | TC_TypeAlias
4389+ | TC_Port
4390+
4391+
4281−toTypeReport :: Code.Source -> Type -> Row -> Col -> Report.Report
4392+toTypeReport :: Code.Source -> TContext -> Type -> Row -> Col -> Report.Report
4282−toTypeReport source tipe startRow startCol =
4393+toTypeReport source context tipe startRow startCol =
4283 case tipe of
4394 case tipe of
4284 TRecord record row col ->
4395 TRecord record row col ->
4285− toTRecordReport source record row col
4396+ toTRecordReport source context record row col
4286
4397
4287 TTuple tuple row col ->
4398 TTuple tuple row col ->
4288− toTTupleReport source tuple row col
4399+ toTTupleReport source context tuple row col
4289−
4290− TArrow row col ->
4291− error "TODO TArrow" row col
4292
4400
4293 TStart row col ->
4401 TStart row col ->
4294− error "TODO TStart" row col startRow startCol
4402+ case Code.whatIsNext source row col of
4403+ Code.Keyword keyword ->
4404+ let
4405+ surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
4406+ region = toKeywordRegion row col keyword
4407+ in
4408+ Report.Report "RESERVED WORD" region [] $
4409+ Code.toSnippet source surroundings (Just region)
4410+ (
4411+ D.reflow $
4412+ "I was expecting to see a type next, but I got stuck on this reserved word:"
4413+ ,
4414+ D.reflow $
4415+ "It looks like you are trying to use `" ++ keyword ++ "` as a type variable, but \
4416+ \ it is a reserved word. Try using a different name!"
4417+ )
4418+
4419+ _ ->
4420+ let
4421+ surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
4422+ region = toRegion row col
4423+
4424+ thing =
4425+ case context of
4426+ TC_Annotation _ -> "type annotation"
4427+ TC_CustomType -> "custom type"
4428+ TC_TypeAlias -> "type alias"
4429+ TC_Port -> "port"
4430+
4431+ something =
4432+ case context of
4433+ TC_Annotation name -> "the `" ++ Name.toChars name ++ "` type annotation"
4434+ TC_CustomType -> "a custom type"
4435+ TC_TypeAlias -> "a type alias"
4436+ TC_Port -> "a port"
4437+ in
4438+ Report.Report ("PROBLEM IN " ++ map Char.toUpper thing) region [] $
4439+ Code.toSnippet source surroundings (Just region)
4440+ (
4441+ D.reflow $
4442+ "I was partway through parsing " ++ something ++ ", but I got stuck here:"
4443+ ,
4444+ D.fillSep $
4445+ ["I","was","expecting","to","see","a","type","next."
4446+ ,"Try","putting",D.dullyellow "Int","or",D.dullyellow "String","for","now?"
4447+ ]
4448+ )
4295
4449
4296 TSpace space row col ->
4450 TSpace space row col ->
4297 toSpaceReport source space row col
4451 toSpaceReport source space row col
4298
4452
4299 TIndentStart row col ->
4453 TIndentStart row col ->
4300− error "TODO TIndentStart" row col
4454+ let
4455+ surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
4456+ region = toRegion row col
4301
4457
4458+ thing =
4459+ case context of
4460+ TC_Annotation _ -> "type annotation"
4461+ TC_CustomType -> "custom type"
4462+ TC_TypeAlias -> "type alias"
4463+ TC_Port -> "port"
4464+ in
4465+ Report.Report ("UNFINISHED " ++ map Char.toUpper thing) region [] $
4466+ Code.toSnippet source surroundings (Just region)
4467+ (
4468+ D.reflow $
4469+ "I was partway through parsing a " ++ thing ++ ", but I got stuck here:"
4470+ ,
4471+ D.stack
4472+ [ D.fillSep $
4473+ ["I","was","expecting","to","see","a","type","next."
4474+ ,"Try","putting",D.dullyellow "Int","or",D.dullyellow "String","for","now?"
4475+ ]
4476+ , D.toSimpleNote $
4477+ "I can get confused by indentation. If you think there is already a type\
4478+ \ next, maybe it is not indented enough?"
4479+ ]
4480+ )
4302
4481
4303
4482
4304−toTRecordReport :: Code.Source -> TRecord -> Row -> Col -> Report.Report
4483+toTRecordReport :: Code.Source -> TContext -> TRecord -> Row -> Col -> Report.Report
4305−toTRecordReport source record startRow startCol =
4484+toTRecordReport source context record startRow startCol =
4306 case record of
4485 case record of
4307 TRecordOpen row col ->
4486 TRecordOpen row col ->
4308 case Code.whatIsNext source row col of
4487 case Code.whatIsNext source row col of
@@ -4416,7 +4595,7 @@ toTRecordReport source record startRow startCol =
4416 )
4595 )
4417
4596
4418 TRecordType tipe row col ->
4597 TRecordType tipe row col ->
4419− toTypeReport source tipe row col
4598+ toTypeReport source context tipe row col
4420
4599
4421 TRecordSpace space row col ->
4600 TRecordSpace space row col ->
4422 toSpaceReport source space row col
4601 toSpaceReport source space row col
@@ -4505,7 +4684,7 @@ toTRecordReport source record startRow startCol =
4505 ,
4684 ,
4506 addNoteForRecordTypeIndentError $
4685 addNoteForRecordTypeIndentError $
4507 D.fillSep $
4686 D.fillSep $
4508− ["Try","putting","something,","like"
4687+ ["Try","putting","something","like"
4509 ,D.dullyellow "Int","or",D.dullyellow "String","for","now?"
4688 ,D.dullyellow "Int","or",D.dullyellow "String","for","now?"
4510 ]
4689 ]
4511 )
4690 )
@@ -4552,8 +4731,8 @@ addNoteForRecordTypeIndentError normalRecommendation =
4552 ]
4731 ]
4553
4732
4554
4733
4555−toTTupleReport :: Code.Source -> TTuple -> Row -> Col -> Report.Report
4734+toTTupleReport :: Code.Source -> TContext -> TTuple -> Row -> Col -> Report.Report
4556−toTTupleReport source tuple startRow startCol =
4735+toTTupleReport source context tuple startRow startCol =
4557 case tuple of
4736 case tuple of
4558 TTupleOpen row col ->
4737 TTupleOpen row col ->
4559 case Code.whatIsNext source row col of
4738 case Code.whatIsNext source row col of
@@ -4612,7 +4791,7 @@ toTTupleReport source tuple startRow startCol =
4612 )
4791 )
4613
4792
4614 TTupleType tipe row col ->
4793 TTupleType tipe row col ->
4615− toTypeReport source tipe row col
4794+ toTypeReport source context tipe row col
4616
4795
4617 TTupleSpace space row col ->
4796 TTupleSpace space row col ->
4618 toSpaceReport source space row col
4797 toSpaceReport source space row col