on master help sign in
Add a bunch of syntax errors for `case` expressions
commit8922ac5fd966d8c2872f8ec51fb16c348bf2e71a authorEvan Czaplicki <evancz@users.noreply.github.com> dateFri 10 May 2019, 16:39 (7 years ago) parentfdf16a5
1 file changed+169−3
compiler/src/Reporting/Error/Syntax.hs+169−3
compiler/src/Reporting/Error/Syntax.hs+169−3
@@ -1066,6 +1066,8 @@ data Node
10661066 | NCond
10671067 | NThen
10681068 | NElse
1069+ | NCase
1070+ | NBranch
10691071 deriving (Eq)
10701072
10711073
@@ -1093,8 +1095,8 @@ toExprReport source context expr startRow startCol =
10931095 Let _ _ _ ->
10941096 error "TODO Let"
10951097
1096− Case _ _ _ ->
1097− error "TODO Case"
1098+ Case case_ row col ->
1099+ toCaseReport source context case_ row col
10981100
10991101 If if_ row col ->
11001102 toIfReport source context if_ row col
@@ -1182,6 +1184,8 @@ toExprReport source context expr startRow startCol =
11821184 InNode NCond r c _ -> (r, c, "an `if` expression")
11831185 InNode NThen r c _ -> (r, c, "an `if` expression")
11841186 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")
11851189 -- TODO check if this is actually the best way to get error messages here
11861190
11871191 surroundings = A.Region (A.Position contextRow contextCol) (A.Position row col)
@@ -1599,7 +1603,7 @@ toOperatorReport source context operator row col =
15991603 in
16001604 Report.Report "UNEXPECTED ARROW" region [] $
16011605 Code.toSnippet source region Nothing $
1602− if error "TODO inCase" then
1606+ if isWithin NBranch context then
16031607 (
16041608 D.reflow $
16051609 "I was not expecting to see an arrow here:"
@@ -1688,6 +1692,168 @@ toOperatorReport source context operator row col =
16881692
16891693
16901694
1695+-- CASE
1696+
1697+
1698+toCaseReport :: Code.Source -> Context -> Case -> Row -> Col -> Report.Report
1699+toCaseReport source context case_ startRow startCol =
1700+ case case_ of
1701+ CaseSpace space row col ->
1702+ toSpaceReport source space row col
1703+
1704+ CaseOf row col ->
1705+ toUnfinishCaseReport source row col startRow startCol $
1706+ D.fillSep ["I","was","expecting","to","see","the",D.dullyellow "of","keyword","next."]
1707+
1708+ CasePattern pattern row col ->
1709+ error "TODO CasePattern" pattern row col
1710+
1711+ CaseArrow row col ->
1712+ let
1713+ surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
1714+ region = toRegion row col
1715+ in
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)
1720+ (
1721+ D.reflow $
1722+ "I am partway through parsing a `case` expression, but I got stuck here:"
1723+ ,
1724+ D.reflow $
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?"
1727+ )
1728+
1729+ Code.Operator ":" ->
1730+ Report.Report "UNEXPECTED OPERATOR" region [] $
1731+ Code.toSnippet source surroundings (Just region)
1732+ (
1733+ D.reflow $
1734+ "I am partway through parsing a `case` expression, but I got stuck here:"
1735+ ,
1736+ D.fillSep $
1737+ ["I","am","seeing",D.dullyellow ":","but","maybe","you","want",D.green "::","instead?"
1738+ ,"For","pattern","matching","on","lists?"
1739+ ]
1740+ )
1741+
1742+ Code.Operator "=" ->
1743+ Report.Report "UNEXPECTED OPERATOR" region [] $
1744+ Code.toSnippet source surroundings (Just region)
1745+ (
1746+ D.reflow $
1747+ "I am partway through parsing a `case` expression, but I got stuck here:"
1748+ ,
1749+ D.fillSep $
1750+ ["I","am","seeing",D.dullyellow "=","but","maybe","you","want",D.green "->","instead?"
1751+ ]
1752+ )
1753+
1754+ _ ->
1755+ Report.Report "MISSING ARROW" region [] $
1756+ Code.toSnippet source surroundings (Just region)
1757+ (
1758+ D.reflow $
1759+ "I am partway through parsing a `case` expression, but I got stuck here:"
1760+ ,
1761+ addNoteForCaseIndentError $
1762+ D.reflow $
1763+ "I was expecting to see an arrow next."
1764+ )
1765+
1766+ CaseExpr expr row col ->
1767+ toExprReport source (InNode NCase startRow startCol context) expr row col
1768+
1769+ CaseBranch expr row col ->
1770+ toExprReport source (InNode NBranch startRow startCol context) expr row col
1771+
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."]
1775+
1776+ CaseIndentExpr row col ->
1777+ toUnfinishCaseReport source row col startRow startCol $
1778+ D.reflow "I was expecting to see a expression next."
1779+
1780+ CaseIndentPattern row col ->
1781+ toUnfinishCaseReport source row col startRow startCol $
1782+ D.reflow "I was expecting to see a pattern next."
1783+
1784+ CaseIndentArrow row col ->
1785+ toUnfinishCaseReport source row col startRow startCol $
1786+ D.fillSep
1787+ ["I","just","saw","a","pattern,","so","I","was","expecting"
1788+ ,"to","see","a",D.dullyellow "->","next."
1789+ ]
1790+
1791+ CaseIndentBranch row col ->
1792+ toUnfinishCaseReport source row col startRow startCol $
1793+ D.reflow $
1794+ "I was expecting to see an expression next. What should I do when\
1795+ \ I run into this particular pattern?"
1796+
1797+ CasePatternAlignment indent row col ->
1798+ toUnfinishCaseReport source row col startRow startCol $
1799+ D.reflow $
1800+ "I suspect this is a pattern that is not indented far enough? (" ++ show indent ++ " spaces)"
1801+
1802+
1803+toUnfinishCaseReport :: Code.Source -> Row -> Col -> Row -> Col -> D.Doc -> Report.Report
1804+toUnfinishCaseReport source row col startRow startCol message =
1805+ let
1806+ surroundings = A.Region (A.Position startRow startCol) (A.Position row col)
1807+ region = toRegion row col
1808+ in
1809+ Report.Report "UNFINISHED CASE" region [] $
1810+ Code.toSnippet source surroundings (Just region)
1811+ (
1812+ D.reflow $
1813+ "I was partway through parsing a `case` expression, but I got stuck here:"
1814+ ,
1815+ D.stack
1816+ [ message
1817+ , D.toSimpleNote $
1818+ "Here is an example of a valid `case` expression for reference."
1819+ , D.vcat $
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"]
1823+ , ""
1824+ , D.indent 6 $ D.fillSep [D.green "Nothing","->"]
1825+ , D.indent 8 $ D.fillSep [D.yellow "400"]
1826+ ]
1827+ , D.reflow $
1828+ "Notice the indentation. Each pattern is aligned, and each branch is indented\
1829+ \ a bit more than the corresponding pattern. That is important!"
1830+ ]
1831+ )
1832+
1833+
1834+addNoteForCaseIndentError :: D.Doc -> D.Doc
1835+addNoteForCaseIndentError message =
1836+ D.stack $
1837+ message
1838+ :
1839+ [ D.toSimpleNote $
1840+ "Sometimes I get confused by indentation, so try to make your `case` look\
1841+ \ something like this:"
1842+ , D.vcat $
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"]
1846+ , ""
1847+ , D.indent 6 $ D.fillSep [D.green "Nothing","->"]
1848+ , D.indent 8 $ D.fillSep [D.yellow "400"]
1849+ ]
1850+ , D.reflow $
1851+ "Notice the indentation! Each pattern is aligned, and each branch is indented\
1852+ \ a bit more than the corresponding pattern. That is important!"
1853+ ]
1854+
1855+
1856+
16911857-- IF
16921858
16931859