@@ -23,7 +23,7 @@ use ast::{DeclLocal, DefaultBlock, UnDeref, BiDiv, EMPTY_CTXT, EnumDef, Explicit
2323use ast:: { Expr , Expr_ , ExprAddrOf , ExprMatch , ExprAgain } ;
2424use ast:: { ExprAssign , ExprAssignOp , ExprBinary , ExprBlock , ExprBox } ;
2525use ast:: { ExprBreak , ExprCall , ExprCast } ;
26- use ast:: { ExprField , ExprTupField , ExprFnBlock , ExprIf , ExprIndex , ExprSlice } ;
26+ use ast:: { ExprField , ExprTupField , ExprFnBlock , ExprIf , ExprIfLet , ExprIndex , ExprSlice } ;
2727use ast:: { ExprLit , ExprLoop , ExprMac } ;
2828use ast:: { ExprMethodCall , ExprParen , ExprPath , ExprProc } ;
2929use ast:: { ExprRepeat , ExprRet , ExprStruct , ExprTup , ExprUnary , ExprUnboxedFn } ;
@@ -576,13 +576,10 @@ impl<'a> Parser<'a> {
576576 /// If the next token is the given keyword, eat it and return
577577 /// true. Otherwise, return false.
578578 pub fn eat_keyword ( & mut self , kw : keywords:: Keyword ) -> bool {
579- match self . token {
580- token:: IDENT ( sid, false ) if kw. to_name ( ) == sid. name => {
581- self . bump ( ) ;
582- true
583- }
584- _ => false
585- }
579+ if self . is_keyword ( kw) {
580+ self . bump ( ) ;
581+ true
582+ } else { false }
586583 }
587584
588585 /// If the given word is not a keyword, signal an error.
@@ -2860,8 +2857,11 @@ impl<'a> Parser<'a> {
28602857 }
28612858 }
28622859
2863- /// Parse an 'if' expression ('if' token already eaten)
2860+ /// Parse an 'if' or 'if let' expression ('if' token already eaten)
28642861 pub fn parse_if_expr ( & mut self ) -> P < Expr > {
2862+ if self . is_keyword ( keywords:: Let ) {
2863+ return self . parse_if_let_expr ( ) ;
2864+ }
28652865 let lo = self . last_span . lo ;
28662866 let cond = self . parse_expr_res ( RestrictionNoStructLiteral ) ;
28672867 let thn = self . parse_block ( ) ;
@@ -2875,6 +2875,23 @@ impl<'a> Parser<'a> {
28752875 self . mk_expr ( lo, hi, ExprIf ( cond, thn, els) )
28762876 }
28772877
2878+ /// Parse an 'if let' expression ('if' token already eaten)
2879+ pub fn parse_if_let_expr ( & mut self ) -> P < Expr > {
2880+ let lo = self . last_span . lo ;
2881+ self . expect_keyword ( keywords:: Let ) ;
2882+ let pat = self . parse_pat ( ) ;
2883+ self . expect ( & token:: EQ ) ;
2884+ let expr = self . parse_expr_res ( RestrictionNoStructLiteral ) ;
2885+ let thn = self . parse_block ( ) ;
2886+ let ( hi, els) = if self . eat_keyword ( keywords:: Else ) {
2887+ let expr = self . parse_else_expr ( ) ;
2888+ ( expr. span . hi , Some ( expr) )
2889+ } else {
2890+ ( thn. span . hi , None )
2891+ } ;
2892+ self . mk_expr ( lo, hi, ExprIfLet ( pat, expr, thn, els) )
2893+ }
2894+
28782895 // `|args| expr`
28792896 pub fn parse_lambda_expr ( & mut self , capture_clause : CaptureClause )
28802897 -> P < Expr > {
0 commit comments