on master help sign in
Add parse error messages for some list cases

Also add some color to some anonymous function errors

commit74416843f4386dc8a2570e8b6532940647654474 authorEvan Czaplicki <evancz@users.noreply.github.com> dateMon 6 May 2019, 19:02 (7 years ago) parent13a0ae5
1 file changed+135−15
compiler/src/Reporting/Error/Syntax.hs+135−15
compiler/src/Reporting/Error/Syntax.hs+135−15
@@ -801,8 +801,8 @@ toExprReport source name expr _ _ =
801801 If _ _ _ ->
802802 error "TODO If"
803803
804− List _ _ _ ->
805− error "TODO List"
804+ List list row col ->
805+ toListReport source name list row col
806806
807807 Record _ _ _ ->
808808 error "TODO Record"
@@ -1327,8 +1327,121 @@ toOperatorReport source name operator row col =
13271327 )
13281328
13291329
1330+toListReport :: Code.Source -> Name.Name -> List -> Row -> Col -> Report.Report
1331+toListReport source name func startRow startCol =
1332+ case func of
1333+ ListSpace space row col ->
1334+ toSpaceReport source space row col
1335+
1336+ ListOpen row col ->
1337+ error "TODO ListOpen" row col
1338+
1339+ ListExpr expr row col ->
1340+ toExprReport source name expr row col
1341+
1342+ ListEnd row col ->
1343+ error "TODO ListEnd" row col
1344+
1345+ ListIndentOpen row col ->
1346+ let
1347+ surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
1348+ region = toRegion row col
1349+ in
1350+ Report.Report "UNFINISHED LIST" region [] $
1351+ Report.toCodeSnippet source surroundings (Just region)
1352+ (
1353+ D.reflow $
1354+ "I cannot find the end of this list:"
1355+ ,
1356+ D.stack
1357+ [ D.fillSep $
1358+ ["You","could","change","it","to","something","like"
1359+ ,D.dullyellow "[3,4,5]"
1360+ ,"or","even","just"
1361+ ,D.dullyellow "[]" <> "."
1362+ ,"Anything","where","there","is","an","open","and","close","square","brace,"
1363+ ,"and","where","the","elements","of","the","list","are","separated","by","commas."
1364+ ]
1365+ , D.toSimpleNote
1366+ "I may be confused by indentation. For example, if you are trying to define\
1367+ \ a list across multiple lines, I recommend using this format:"
1368+ , D.indent 4 $ D.vcat $
1369+ [ "[ \"Alice\""
1370+ , ", \"Bob\""
1371+ , ", \"Chuck\""
1372+ , "]"
1373+ ]
1374+ , D.reflow $
1375+ "Notice that nothing comes directly after a newline. Each line starts with some spaces.\
1376+ \ This style can be jarring for people coming from C-like syntax, but folks generally\
1377+ \ report that they are used to it (and often prefer it!) after a week or two of using Elm."
1378+ ]
1379+ )
1380+
1381+ ListIndentEnd row col ->
1382+ let
1383+ surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
1384+ region = toRegion row col
1385+ in
1386+ Report.Report "UNFINISHED LIST" region [] $
1387+ Report.toCodeSnippet source surroundings (Just region)
1388+ (
1389+ D.reflow $
1390+ "I cannot find the end of this list:"
1391+ ,
1392+ D.stack
1393+ [ D.fillSep $
1394+ ["You","can","just","add","a","closing",D.dullyellow "]"
1395+ ,"right","here,","and","I","will","be","all","set!"
1396+ ]
1397+ , D.toSimpleNote
1398+ "I may be confused by indentation. For example, if you are trying to define\
1399+ \ a list across multiple lines, I recommend using this format:"
1400+ , D.indent 4 $ D.vcat $
1401+ [ "[ \"Alice\""
1402+ , ", \"Bob\""
1403+ , ", \"Chuck\""
1404+ , "]"
1405+ ]
1406+ , D.reflow $
1407+ "Notice that nothing comes directly after a newline. Each line starts with some spaces.\
1408+ \ This style can be jarring for people coming from C-like syntax, but folks generally\
1409+ \ report that they are used to it (and often prefer it!) after a week or two of using Elm."
1410+ ]
1411+ )
1412+
1413+ ListIndentExpr row col ->
1414+ let
1415+ surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
1416+ region = toRegion row col
1417+ in
1418+ Report.Report "UNFINISHED LIST" region [] $
1419+ Report.toCodeSnippet source surroundings (Just region)
1420+ (
1421+ D.reflow $
1422+ "I was expecting to see another list entry after this comma:"
1423+ ,
1424+ D.stack
1425+ [ D.reflow $
1426+ "Trailing commas are not allowed in lists, so the fix may be to delete the comma?"
1427+ , D.toSimpleNote
1428+ "I recommend using the following format for lists that span multiple lines:"
1429+ , D.indent 4 $ D.vcat $
1430+ [ "[ \"Alice\""
1431+ , ", \"Bob\""
1432+ , ", \"Chuck\""
1433+ , "]"
1434+ ]
1435+ , D.reflow $
1436+ "Notice that nothing comes directly after a newline. Each line starts with some spaces.\
1437+ \ This style can be jarring for people coming from C-like syntax, but folks generally\
1438+ \ report that they are used to it (and often prefer it!) after a week or two of using Elm."
1439+ ]
1440+ )
1441+
1442+
13301443toFuncReport :: Code.Source -> Name.Name -> Func -> Row -> Col -> Report.Report
1331−toFuncReport source name func frow fcol =
1444+toFuncReport source name func startRow startCol =
13321445 case func of
13331446 FuncSpace space row col ->
13341447 toSpaceReport source space row col
@@ -1341,7 +1454,7 @@ toFuncReport source name func frow fcol =
13411454
13421455 FuncArrow row col ->
13431456 let
1344− surroundings = A.Region (A.Position frow fcol) (A.Position row col)
1457+ surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
13451458 region = toRegion row col
13461459 in
13471460 Report.Report "UNFINISHED ANONYMOUS FUNCTION" region [] $
@@ -1350,14 +1463,16 @@ toFuncReport source name func frow fcol =
13501463 D.reflow $
13511464 "I was expecting to see an arrow here:"
13521465 ,
1353− D.reflow $
1354− "The syntax for anonymous functions is (\\x -> x + 1) so I am missing the arrow\
1355− \ and the body of the function."
1466+ D.fillSep $
1467+ ["The","syntax","for","anonymous","functions","is"
1468+ ,D.dullyellow "(\\x -> x + 1)"
1469+ ,"so","I","am","missing","the","arrow","and","the","body","of","the","function."
1470+ ]
13561471 )
13571472
13581473 FuncIndentArg row col ->
13591474 let
1360− surroundings = A.Region (A.Position frow fcol) (A.Position row col)
1475+ surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
13611476 region = toRegion row col
13621477 in
13631478 Report.Report "MISSING ARGUMENT" region [] $
@@ -1380,7 +1495,7 @@ toFuncReport source name func frow fcol =
13801495
13811496 FuncIndentArrow row col ->
13821497 let
1383− surroundings = A.Region (A.Position frow fcol) (A.Position row col)
1498+ surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
13841499 region = toRegion row col
13851500 in
13861501 Report.Report "UNFINISHED ANONYMOUS FUNCTION" region [] $
@@ -1390,9 +1505,11 @@ toFuncReport source name func frow fcol =
13901505 "I was expecting to see an arrow here:"
13911506 ,
13921507 D.stack
1393− [ D.reflow $
1394− "The syntax for anonymous functions is (\\x -> x + 1) so I am missing the arrow\
1395− \ and the body of the function."
1508+ [ D.fillSep $
1509+ ["The","syntax","for","anonymous","functions","is"
1510+ ,D.dullyellow "(\\x -> x + 1)"
1511+ ,"so","I","am","missing","the","arrow","and","the","body","of","the","function."
1512+ ]
13961513 , D.toSimpleNote $
13971514 "It is possible that I am confused about indetation! I generally recommend\
13981515 \ switching to named functions if the definition cannot fit inline nicely, so\
@@ -1403,7 +1520,7 @@ toFuncReport source name func frow fcol =
14031520
14041521 FuncIndentBody row col ->
14051522 let
1406− surroundings = A.Region (A.Position frow fcol) (A.Position row col)
1523+ surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
14071524 region = toRegion row col
14081525 in
14091526 Report.Report "UNFINISHED ANONYMOUS FUNCTION" region [] $
@@ -1413,8 +1530,11 @@ toFuncReport source name func frow fcol =
14131530 "I was expecting to see the body of your anonymous function here:"
14141531 ,
14151532 D.stack
1416− [ D.reflow $
1417− "The syntax for anonymous functions is (\\x -> x + 1) so I am missing all the stuff after the arrow!"
1533+ [ D.fillSep $
1534+ ["The","syntax","for","anonymous","functions","is"
1535+ ,D.dullyellow "(\\x -> x + 1)"
1536+ ,"so","I","am","missing","all","the","stuff","after","the","arrow!"
1537+ ]
14181538 , D.toSimpleNote $
14191539 "It is possible that I am confused about indetation! I generally recommend\
14201540 \ switching to named functions if the definition cannot fit inline nicely, so\