Teach libsyntax about if let
This commit is contained in:
parent
38015eeb70
commit
5d8cfd53b5
6 changed files with 71 additions and 13 deletions
|
@ -23,7 +23,7 @@ use ast::{DeclLocal, DefaultBlock, UnDeref, BiDiv, EMPTY_CTXT, EnumDef, Explicit
|
|||
use ast::{Expr, Expr_, ExprAddrOf, ExprMatch, ExprAgain};
|
||||
use ast::{ExprAssign, ExprAssignOp, ExprBinary, ExprBlock, ExprBox};
|
||||
use ast::{ExprBreak, ExprCall, ExprCast};
|
||||
use ast::{ExprField, ExprTupField, ExprFnBlock, ExprIf, ExprIndex, ExprSlice};
|
||||
use ast::{ExprField, ExprTupField, ExprFnBlock, ExprIf, ExprIfLet, ExprIndex, ExprSlice};
|
||||
use ast::{ExprLit, ExprLoop, ExprMac};
|
||||
use ast::{ExprMethodCall, ExprParen, ExprPath, ExprProc};
|
||||
use ast::{ExprRepeat, ExprRet, ExprStruct, ExprTup, ExprUnary, ExprUnboxedFn};
|
||||
|
@ -576,13 +576,10 @@ impl<'a> Parser<'a> {
|
|||
/// If the next token is the given keyword, eat it and return
|
||||
/// true. Otherwise, return false.
|
||||
pub fn eat_keyword(&mut self, kw: keywords::Keyword) -> bool {
|
||||
match self.token {
|
||||
token::IDENT(sid, false) if kw.to_name() == sid.name => {
|
||||
self.bump();
|
||||
true
|
||||
}
|
||||
_ => false
|
||||
}
|
||||
if self.is_keyword(kw) {
|
||||
self.bump();
|
||||
true
|
||||
} else { false }
|
||||
}
|
||||
|
||||
/// If the given word is not a keyword, signal an error.
|
||||
|
@ -2860,8 +2857,11 @@ impl<'a> Parser<'a> {
|
|||
}
|
||||
}
|
||||
|
||||
/// Parse an 'if' expression ('if' token already eaten)
|
||||
/// Parse an 'if' or 'if let' expression ('if' token already eaten)
|
||||
pub fn parse_if_expr(&mut self) -> P<Expr> {
|
||||
if self.is_keyword(keywords::Let) {
|
||||
return self.parse_if_let_expr();
|
||||
}
|
||||
let lo = self.last_span.lo;
|
||||
let cond = self.parse_expr_res(RestrictionNoStructLiteral);
|
||||
let thn = self.parse_block();
|
||||
|
@ -2875,6 +2875,23 @@ impl<'a> Parser<'a> {
|
|||
self.mk_expr(lo, hi, ExprIf(cond, thn, els))
|
||||
}
|
||||
|
||||
/// Parse an 'if let' expression ('if' token already eaten)
|
||||
pub fn parse_if_let_expr(&mut self) -> P<Expr> {
|
||||
let lo = self.last_span.lo;
|
||||
self.expect_keyword(keywords::Let);
|
||||
let pat = self.parse_pat();
|
||||
self.expect(&token::EQ);
|
||||
let expr = self.parse_expr_res(RestrictionNoStructLiteral);
|
||||
let thn = self.parse_block();
|
||||
let (hi, els) = if self.eat_keyword(keywords::Else) {
|
||||
let expr = self.parse_else_expr();
|
||||
(expr.span.hi, Some(expr))
|
||||
} else {
|
||||
(thn.span.hi, None)
|
||||
};
|
||||
self.mk_expr(lo, hi, ExprIfLet(pat, expr, thn, els))
|
||||
}
|
||||
|
||||
// `|args| expr`
|
||||
pub fn parse_lambda_expr(&mut self, capture_clause: CaptureClause)
|
||||
-> P<Expr> {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue