summaryrefslogtreecommitdiff
path: root/compiler/parser/Parser.y.pp
diff options
context:
space:
mode:
authorSimon Marlow <marlowsd@gmail.com>2013-09-27 20:59:41 +0100
committerSimon Marlow <marlowsd@gmail.com>2013-10-01 11:45:46 +0100
commitaab65608f9a26990b2c5083e4b65b9d1f6c9b48a (patch)
tree6af79a93a7e17dedf73f3e9d66e9a522ce6c0ece /compiler/parser/Parser.y.pp
parentc0f89a1b536d9cd3640803138d5f794982049cce (diff)
downloadhaskell-aab65608f9a26990b2c5083e4b65b9d1f6c9b48a.tar.gz
Add layout to MultiWayIf (#7783)
This makes it possible to write x = if | False -> if | False -> 1 | False -> 2 | True -> 3 Layout normally inserts semicolons between declarations at the same indentation level, so I added optional semicolons to the syntax for guards in MultiWayIf syntax. This is a bit of a hack, but the alternative (a special kind of layout that doesn't insert semicolons) seemed worse, or at least equally bad.
Diffstat (limited to 'compiler/parser/Parser.y.pp')
-rw-r--r--compiler/parser/Parser.y.pp15
1 files changed, 14 insertions, 1 deletions
diff --git a/compiler/parser/Parser.y.pp b/compiler/parser/Parser.y.pp
index ea192a0060..c2ddf452cc 100644
--- a/compiler/parser/Parser.y.pp
+++ b/compiler/parser/Parser.y.pp
@@ -1459,7 +1459,7 @@ exp10 :: { LHsExpr RdrName }
| 'if' exp optSemi 'then' exp optSemi 'else' exp
{% checkDoAndIfThenElse $2 $3 $5 $6 $8 >>
return (LL $ mkHsIf $2 $5 $8) }
- | 'if' gdpats {% hintMultiWayIf (getLoc $1) >>
+ | 'if' ifgdpats {% hintMultiWayIf (getLoc $1) >>
return (LL $ HsMultiIf placeHolderType (reverse $ unLoc $2)) }
| 'case' exp 'of' altslist { LL $ HsCase $2 (mkMatchGroup (unLoc $4)) }
| '-' fexp { LL $ NegApp $2 noSyntaxExpr }
@@ -1754,6 +1754,19 @@ gdpats :: { Located [LGRHS RdrName (LHsExpr RdrName)] }
: gdpats gdpat { LL ($2 : unLoc $1) }
| gdpat { L1 [$1] }
+-- optional semi-colons between the guards of a MultiWayIf, because we use
+-- layout here, but we don't need (or want) the semicolon as a separator (#7783).
+gdpatssemi :: { Located [LGRHS RdrName (LHsExpr RdrName)] }
+ : gdpatssemi gdpat optSemi { sL (comb2 $1 $2) ($2 : unLoc $1) }
+ | gdpat optSemi { L1 [$1] }
+
+-- layout for MultiWayIf doesn't begin with an open brace, because it's hard to
+-- generate the open brace in addition to the vertical bar in the lexer, and
+-- we don't need it.
+ifgdpats :: { Located [LGRHS RdrName (LHsExpr RdrName)] }
+ : '{' gdpatssemi '}' { LL (unLoc $2) }
+ | gdpatssemi close { $1 }
+
gdpat :: { LGRHS RdrName (LHsExpr RdrName) }
: '|' guardquals '->' exp { sL (comb2 $1 $>) $ GRHS (unLoc $2) $4 }