on master help sign in
Parse nested ifs as a single chunk
b17a1a3Evan Czaplicki
src/Parse/Expression.hs+10−2
@@ -213,6 +213,11 @@ binaryExpr =
213
213
214ifExpr :: IParser Source.Expr'
214ifExpr :: IParser Source.Expr'
215ifExpr =
215ifExpr =
216+ ifHelp []
217+
218+
219+ifHelp :: [(Source.Expr, Source.Expr)] -> IParser Source.Expr'
220+ifHelp branches =
216 do try (reserved "if")
221 do try (reserved "if")
217 whitespace
222 whitespace
218 condition <- expr
223 condition <- expr
@@ -221,8 +226,11 @@ ifExpr =
221 whitespace <?> "an 'else' branch"
226 whitespace <?> "an 'else' branch"
222 reserved "else" <?> "an 'else' branch"
227 reserved "else" <?> "an 'else' branch"
223 whitespace
228 whitespace
224− elseBranch <- expr
229+ let newBranches = (condition, thenBranch) : branches
225− return (E.If [(condition, thenBranch)] elseBranch)
230+ choice
231+ [ ifHelp newBranches
232+ , E.If (reverse newBranches) <$> expr
233+ ]
226
234
227
235
228lambdaExpr :: IParser Source.Expr
236lambdaExpr :: IParser Source.Expr