on master help sign in
Parse nested ifs as a single chunk

Should improve error message quality for expressions like (if … else if …. else if … else …)

commitb17a1a3e88d8c300a3f319f25913bf31a2e91f14 authorEvan Czaplicki <info@elm-lang.org> dateThu 27 Aug 2015, 18:25 (11 years ago) parent2b577fe
1 file changed+10−2
src/Parse/Expression.hs+10−2
src/Parse/Expression.hs+10−2
@@ -213,6 +213,11 @@ binaryExpr =
213213
214214ifExpr :: IParser Source.Expr'
215215ifExpr =
216+ ifHelp []
217+
218+
219+ifHelp :: [(Source.Expr, Source.Expr)] -> IParser Source.Expr'
220+ifHelp branches =
216221 do try (reserved "if")
217222 whitespace
218223 condition <- expr
@@ -221,8 +226,11 @@ ifExpr =
221226 whitespace <?> "an 'else' branch"
222227 reserved "else" <?> "an 'else' branch"
223228 whitespace
224− elseBranch <- expr
225− return (E.If [(condition, thenBranch)] elseBranch)
229+ let newBranches = (condition, thenBranch) : branches
230+ choice
231+ [ ifHelp newBranches
232+ , E.If (reverse newBranches) <$> expr
233+ ]
226234
227235
228236lambdaExpr :: IParser Source.Expr