diff options
| author | Graydon Hoare <[email protected]> | 2011-02-01 16:23:48 -0800 |
|---|---|---|
| committer | Graydon Hoare <[email protected]> | 2011-02-01 16:23:48 -0800 |
| commit | 70bf54bcac587c0bc8a3a593bda75115e4c23aa8 (patch) | |
| tree | fc04fe2f7bc4fa1dd902689e4db20e193bec9097 /src/comp/front/parser.rs | |
| parent | Use dynamic GEP and silly offset-encoding on tydescs. Successful call into a ... (diff) | |
| download | rust-70bf54bcac587c0bc8a3a593bda75115e4c23aa8.tar.xz rust-70bf54bcac587c0bc8a3a593bda75115e4c23aa8.zip | |
Implement 'else if'
Diffstat (limited to 'src/comp/front/parser.rs')
| -rw-r--r-- | src/comp/front/parser.rs | 42 |
1 files changed, 32 insertions, 10 deletions
diff --git a/src/comp/front/parser.rs b/src/comp/front/parser.rs index bef37a3c..0c90df5c 100644 --- a/src/comp/front/parser.rs +++ b/src/comp/front/parser.rs @@ -891,18 +891,40 @@ impure fn parse_if_expr(parser p) -> @ast.expr { auto cond = parse_expr(p); expect(p, token.RPAREN); auto thn = parse_block(p); - let option.t[ast.block] els = none[ast.block]; hi = thn.span; - alt (p.peek()) { - case (token.ELSE) { - p.bump(); - auto eblk = parse_block(p); - els = some(eblk); - hi = eblk.span; + + let vec[tup(@ast.expr, ast.block)] elifs = vec(); + let option.t[ast.block] els = none[ast.block]; + let bool parsing_elses = true; + while (parsing_elses) { + alt (p.peek()) { + case (token.ELSE) { + expect(p, token.ELSE); + alt (p.peek()) { + case (token.IF) { + expect(p, token.IF); + expect(p, token.LPAREN); + auto elifcond = parse_expr(p); + expect(p, token.RPAREN); + auto elifthn = parse_block(p); + elifs += tup(elifcond, elifthn); + hi = elifthn.span; + } + case (_) { + auto eblk = parse_block(p); + els = some(eblk); + hi = eblk.span; + parsing_elses = false; + } + } + } + case (_) { + parsing_elses = false; + } } - case (_) { /* fall through */ } } - ret @spanned(lo, hi, ast.expr_if(cond, thn, els, ast.ann_none)); + + ret @spanned(lo, hi, ast.expr_if(cond, thn, elifs, els, ast.ann_none)); } impure fn parse_head_local(parser p) -> @ast.decl { @@ -1331,7 +1353,7 @@ fn stmt_ends_with_semi(@ast.stmt stmt) -> bool { case (ast.expr_unary(_,_,_)) { ret true; } case (ast.expr_lit(_,_)) { ret true; } case (ast.expr_cast(_,_,_)) { ret true; } - case (ast.expr_if(_,_,_,_)) { ret false; } + case (ast.expr_if(_,_,_,_,_)) { ret false; } case (ast.expr_for(_,_,_,_)) { ret false; } case (ast.expr_while(_,_,_)) { ret false; } case (ast.expr_do_while(_,_,_)) { ret false; } |