on master help sign in
Finish syntax messages for types and declarations
commitfb43331a849c81a674c48014c12163c631ccecbb authorEvan Czaplicki <evancz@users.noreply.github.com> dateMon 20 May 2019, 17:02 (7 years ago) parent2a1ace1
3 files changed+215−36
compiler/src/Parse/Module.hs+1−1 compiler/src/Parse/Type.hs+1−1 compiler/src/Reporting/Error/Syntax.hs+213−34
compiler/src/Parse/Module.hs+1−1
@@ -171,7 +171,7 @@ chompDecls :: [Decl.Decl] -> Parser E.Decl [Decl.Decl]
171171chompDecls decls =
172172 do (decl, _) <- Decl.declaration
173173 oneOfWithFallback
174− [ do Space.checkFreshLine E.DeclFreshLineStart
174+ [ do Space.checkFreshLine E.DeclStart
175175 chompDecls (decl:decls)
176176 ]
177177 (reverse (decl:decls))
compiler/src/Parse/Type.hs+1−1
@@ -95,7 +95,7 @@ expression =
9595 ]
9696 oneOfWithFallback
9797 [ 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
9999 Space.chompAndCheckIndent E.TSpace E.TIndentStart
100100 (tipe2, end2) <- expression
101101 let tipe = A.at start end2 (Src.TLambda tipe1 tipe2)
compiler/src/Reporting/Error/Syntax.hs+213−34
@@ -134,7 +134,6 @@ data Decl
134134 | DeclType DeclType Row Col
135135 | DeclDef Name.Name DeclDef Row Col
136136 --
137− | DeclFreshLineStart Row Col
138137 | DeclFreshLineAfterDocComment Row Col
139138
140139
@@ -408,7 +407,6 @@ data PList
408407data Type
409408 = TRecord TRecord Row Col
410409 | TTuple TTuple Row Col
411− | TArrow Row Col
412410 --
413411 | TStart Row Col
414412 | TSpace Space Row Col
@@ -1234,7 +1232,106 @@ toDeclarationsReport :: Code.Source -> Decl -> Report.Report
12341232toDeclarationsReport source decl =
12351233 case decl of
12361234 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+ )
12381335
12391336 DeclSpace space row col ->
12401337 toSpaceReport source space row col
@@ -1248,11 +1345,20 @@ toDeclarationsReport source decl =
12481345 DeclDef name declDef row col ->
12491346 toDeclDefReport source name declDef row col
12501347
1251− DeclFreshLineStart row col ->
1252− error "TODO DeclFreshLineStart" row col
1253−
12541348 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+ )
12561362
12571363
12581364
@@ -1324,7 +1430,7 @@ toPortReport source port_ startRow startCol =
13241430 )
13251431
13261432 PortType tipe row col ->
1327− toTypeReport source tipe row col
1433+ toTypeReport source TC_Port tipe row col
13281434
13291435 PortIndentName row col ->
13301436 let
@@ -1536,7 +1642,7 @@ toTypeAliasReport source typeAlias startRow startCol =
15361642 )
15371643
15381644 AliasBody tipe row col ->
1539− toTypeReport source tipe row col
1645+ toTypeReport source TC_TypeAlias tipe row col
15401646
15411647 AliasIndentEquals row col ->
15421648 let
@@ -1703,7 +1809,7 @@ toCustomTypeReport source customType startRow startCol =
17031809 )
17041810
17051811 CT_VariantArg tipe row col ->
1706− toTypeReport source tipe row col
1812+ toTypeReport source TC_CustomType tipe row col
17071813
17081814 CT_IndentEquals row col ->
17091815 let
@@ -1876,7 +1982,7 @@ toDeclDefReport source name declDef startRow startCol =
18761982 )
18771983
18781984 DeclDefType tipe row col ->
1879− toTypeReport source tipe row col
1985+ toTypeReport source (TC_Annotation name) tipe row col
18801986
18811987 DeclDefArg pattern row col ->
18821988 toPatternReport source PArg pattern row col
@@ -2916,10 +3022,10 @@ toUnfinishCaseReport source row col startRow startCol message =
29163022 "Here is an example of a valid `case` expression for reference."
29173023 , D.vcat $
29183024 [ 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","->"]
29203026 , D.indent 8 $ D.fillSep ["width","+",D.dullyellow "200"]
29213027 , ""
2922− , D.indent 6 $ D.fillSep [D.green "Nothing","->"]
3028+ , D.indent 6 $ D.fillSep [D.blue "Nothing","->"]
29233029 , D.indent 8 $ D.fillSep [D.dullyellow "400"]
29243030 ]
29253031 , D.reflow $
@@ -2939,10 +3045,10 @@ addNoteForCaseIndentError message =
29393045 \ something like this:"
29403046 , D.vcat $
29413047 [ 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","->"]
29433049 , D.indent 8 $ D.fillSep ["width","+",D.dullyellow "200"]
29443050 , ""
2945− , D.indent 6 $ D.fillSep [D.green "Nothing","->"]
3051+ , D.indent 6 $ D.fillSep [D.blue "Nothing","->"]
29463052 , D.indent 8 $ D.fillSep [D.dullyellow "400"]
29473053 ]
29483054 , D.reflow $
@@ -3326,7 +3432,7 @@ toRecordReport source context record startRow startCol =
33263432 ,
33273433 addNoteForRecordIndentError $
33283434 D.fillSep $
3329− ["Try","putting","something,","like"
3435+ ["Try","putting","something","like"
33303436 ,D.dullyellow "42","or",D.dullyellow"\"hello\"","for","now?"
33313437 ]
33323438 )
@@ -4276,33 +4382,106 @@ toPListReport source context list startRow startCol =
42764382-- TYPES
42774383
42784384
4279−-- TODO add context? say something special if you are in type annotation vs custom type vs type alias?
4280−--
4281−toTypeReport :: Code.Source -> Type -> Row -> Col -> Report.Report
4282−toTypeReport source tipe startRow startCol =
4385+data TContext
4386+ = TC_Annotation Name.Name
4387+ | TC_CustomType
4388+ | TC_TypeAlias
4389+ | TC_Port
4390+
4391+
4392+toTypeReport :: Code.Source -> TContext -> Type -> Row -> Col -> Report.Report
4393+toTypeReport source context tipe startRow startCol =
42834394 case tipe of
42844395 TRecord record row col ->
4285− toTRecordReport source record row col
4396+ toTRecordReport source context record row col
42864397
42874398 TTuple tuple row col ->
4288− toTTupleReport source tuple row col
4289−
4290− TArrow row col ->
4291− error "TODO TArrow" row col
4399+ toTTupleReport source context tuple row col
42924400
42934401 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+ )
42954449
42964450 TSpace space row col ->
42974451 toSpaceReport source space row col
42984452
42994453 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
43014457
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+ )
43024481
43034482
4304−toTRecordReport :: Code.Source -> TRecord -> Row -> Col -> Report.Report
4305−toTRecordReport source record startRow startCol =
4483+toTRecordReport :: Code.Source -> TContext -> TRecord -> Row -> Col -> Report.Report
4484+toTRecordReport source context record startRow startCol =
43064485 case record of
43074486 TRecordOpen row col ->
43084487 case Code.whatIsNext source row col of
@@ -4416,7 +4595,7 @@ toTRecordReport source record startRow startCol =
44164595 )
44174596
44184597 TRecordType tipe row col ->
4419− toTypeReport source tipe row col
4598+ toTypeReport source context tipe row col
44204599
44214600 TRecordSpace space row col ->
44224601 toSpaceReport source space row col
@@ -4505,7 +4684,7 @@ toTRecordReport source record startRow startCol =
45054684 ,
45064685 addNoteForRecordTypeIndentError $
45074686 D.fillSep $
4508− ["Try","putting","something,","like"
4687+ ["Try","putting","something","like"
45094688 ,D.dullyellow "Int","or",D.dullyellow "String","for","now?"
45104689 ]
45114690 )
@@ -4552,8 +4731,8 @@ addNoteForRecordTypeIndentError normalRecommendation =
45524731 ]
45534732
45544733
4555−toTTupleReport :: Code.Source -> TTuple -> Row -> Col -> Report.Report
4556−toTTupleReport source tuple startRow startCol =
4734+toTTupleReport :: Code.Source -> TContext -> TTuple -> Row -> Col -> Report.Report
4735+toTTupleReport source context tuple startRow startCol =
45574736 case tuple of
45584737 TTupleOpen row col ->
45594738 case Code.whatIsNext source row col of
@@ -4612,7 +4791,7 @@ toTTupleReport source tuple startRow startCol =
46124791 )
46134792
46144793 TTupleType tipe row col ->
4615− toTypeReport source tipe row col
4794+ toTypeReport source context tipe row col
46164795
46174796 TTupleSpace space row col ->
46184797 toSpaceReport source space row col