@@ -1066,6 +1066,8 @@ data Node
1069 deriving (Eq)
1071 deriving (Eq)
@@ -1093,8 +1095,8 @@ toExprReport source context expr startRow startCol =
1093 Let _ _ _ ->
1095 Let _ _ _ ->
1094 error "TODO Let"
1096 error "TODO Let"
1096− Case _ _ _ ->
1098+ Case case_ row col ->
1097− error "TODO Case"
1099+ toCaseReport source context case_ row col
1099 If if_ row col ->
1101 If if_ row col ->
1100 toIfReport source context if_ row col
1102 toIfReport source context if_ row col
@@ -1182,6 +1184,8 @@ toExprReport source context expr startRow startCol =
1182 InNode NCond r c _ -> (r, c, "an `if` expression")
1184 InNode NCond r c _ -> (r, c, "an `if` expression")
1183 InNode NThen r c _ -> (r, c, "an `if` expression")
1185 InNode NThen r c _ -> (r, c, "an `if` expression")
1184 InNode NElse r c _ -> (r, c, "an `if` expression")
1186 InNode NElse r c _ -> (r, c, "an `if` expression")
1187+ InNode NCase r c _ -> (r, c, "a `case` expression")
1188+ InNode NBranch r c _ -> (r, c, "a `case` expression")
1185 -- TODO check if this is actually the best way to get error messages here
1189 -- TODO check if this is actually the best way to get error messages here
1187 surroundings = A.Region (A.Position contextRow contextCol) (A.Position row col)
1191 surroundings = A.Region (A.Position contextRow contextCol) (A.Position row col)
@@ -1599,7 +1603,7 @@ toOperatorReport source context operator row col =
1600 Report.Report "UNEXPECTED ARROW" region [] $
1604 Report.Report "UNEXPECTED ARROW" region [] $
1601 Code.toSnippet source region Nothing $
1605 Code.toSnippet source region Nothing $
1602− if error "TODO inCase" then
1606+ if isWithin NBranch context then
1604 D.reflow $
1608 D.reflow $
1605 "I was not expecting to see an arrow here:"
1609 "I was not expecting to see an arrow here:"
@@ -1688,6 +1692,168 @@ toOperatorReport source context operator row col =
1698+toCaseReport :: Code.Source -> Context -> Case -> Row -> Col -> Report.Report
1699+toCaseReport source context case_ startRow startCol =
1701+ CaseSpace space row col ->
1702+ toSpaceReport source space row col
1705+ toUnfinishCaseReport source row col startRow startCol $
1706+ D.fillSep ["I","was","expecting","to","see","the",D.dullyellow "of","keyword","next."]
1708+ CasePattern pattern row col ->
1709+ error "TODO CasePattern" pattern row col
1711+ CaseArrow row col ->
1713+ surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
1714+ region = toRegion row col
1716+ case Code.whatIsNext source row col of
1717+ Code.Keyword keyword ->
1718+ Report.Report "RESERVED WORD" region [] $
1719+ Code.toSnippet source surroundings (Just region)
1722+ "I am partway through parsing a `case` expression, but I got stuck here:"
1725+ "It looks like you are trying to use `" ++ keyword ++ "` in one of your\
1726+ \ patterns, but it is a reserved word. Try using a different name?"
1729+ Code.Operator ":" ->
1730+ Report.Report "UNEXPECTED OPERATOR" region [] $
1731+ Code.toSnippet source surroundings (Just region)
1734+ "I am partway through parsing a `case` expression, but I got stuck here:"
1737+ ["I","am","seeing",D.dullyellow ":","but","maybe","you","want",D.green "::","instead?"
1738+ ,"For","pattern","matching","on","lists?"
1742+ Code.Operator "=" ->
1743+ Report.Report "UNEXPECTED OPERATOR" region [] $
1744+ Code.toSnippet source surroundings (Just region)
1747+ "I am partway through parsing a `case` expression, but I got stuck here:"
1750+ ["I","am","seeing",D.dullyellow "=","but","maybe","you","want",D.green "->","instead?"
1755+ Report.Report "MISSING ARROW" region [] $
1756+ Code.toSnippet source surroundings (Just region)
1759+ "I am partway through parsing a `case` expression, but I got stuck here:"
1761+ addNoteForCaseIndentError $
1763+ "I was expecting to see an arrow next."
1766+ CaseExpr expr row col ->
1767+ toExprReport source (InNode NCase startRow startCol context) expr row col
1769+ CaseBranch expr row col ->
1770+ toExprReport source (InNode NBranch startRow startCol context) expr row col
1772+ CaseIndentOf row col ->
1773+ toUnfinishCaseReport source row col startRow startCol $
1774+ D.fillSep ["I","was","expecting","to","see","the",D.dullyellow "of","keyword","next."]
1776+ CaseIndentExpr row col ->
1777+ toUnfinishCaseReport source row col startRow startCol $
1778+ D.reflow "I was expecting to see a expression next."
1780+ CaseIndentPattern row col ->
1781+ toUnfinishCaseReport source row col startRow startCol $
1782+ D.reflow "I was expecting to see a pattern next."
1784+ CaseIndentArrow row col ->
1785+ toUnfinishCaseReport source row col startRow startCol $
1787+ ["I","just","saw","a","pattern,","so","I","was","expecting"
1788+ ,"to","see","a",D.dullyellow "->","next."
1791+ CaseIndentBranch row col ->
1792+ toUnfinishCaseReport source row col startRow startCol $
1794+ "I was expecting to see an expression next. What should I do when\
1795+ \ I run into this particular pattern?"
1797+ CasePatternAlignment indent row col ->
1798+ toUnfinishCaseReport source row col startRow startCol $
1800+ "I suspect this is a pattern that is not indented far enough? (" ++ show indent ++ " spaces)"
1803+toUnfinishCaseReport :: Code.Source -> Row -> Col -> Row -> Col -> D.Doc -> Report.Report
1804+toUnfinishCaseReport source row col startRow startCol message =
1806+ surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
1807+ region = toRegion row col
1809+ Report.Report "UNFINISHED CASE" region [] $
1810+ Code.toSnippet source surroundings (Just region)
1813+ "I was partway through parsing a `case` expression, but I got stuck here:"
1818+ "Here is an example of a valid `case` expression for reference."
1820+ [ D.indent 4 $ D.fillSep [D.cyan "case","maybeWidth",D.cyan "of"]
1821+ , D.indent 6 $ D.fillSep [D.green "Just","width","->"]
1822+ , D.indent 8 $ D.fillSep ["width","+",D.yellow "200"]
1824+ , D.indent 6 $ D.fillSep [D.green "Nothing","->"]
1825+ , D.indent 8 $ D.fillSep [D.yellow "400"]
1828+ "Notice the indentation. Each pattern is aligned, and each branch is indented\
1829+ \ a bit more than the corresponding pattern. That is important!"
1834+addNoteForCaseIndentError :: D.Doc -> D.Doc
1835+addNoteForCaseIndentError message =
1840+ "Sometimes I get confused by indentation, so try to make your `case` look\
1841+ \ something like this:"
1843+ [ D.indent 4 $ D.fillSep [D.cyan "case","maybeWidth",D.cyan "of"]
1844+ , D.indent 6 $ D.fillSep [D.green "Just","width","->"]
1845+ , D.indent 8 $ D.fillSep ["width","+",D.yellow "200"]
1847+ , D.indent 6 $ D.fillSep [D.green "Nothing","->"]
1848+ , D.indent 8 $ D.fillSep [D.yellow "400"]
1851+ "Notice the indentation! Each pattern is aligned, and each branch is indented\
1852+ \ a bit more than the corresponding pattern. That is important!"