about summary refs log tree commit diff
diff options
context:
space:
mode:
authorXFFXFF <1247714429@qq.com>2023-03-07 08:24:05 +0800
committerXFFXFF <1247714429@qq.com>2023-03-07 17:27:52 +0800
commit6e97527eaeb77144b45de519372364c25ca40a69 (patch)
tree78da23a472530180881fc889f094ac65b2d99921
parent98990affe546bc4371e0b6daee686bda0194e95d (diff)
add is_blocklike func on BlockLike
-rw-r--r--crates/parser/src/grammar.rs4
-rw-r--r--crates/parser/src/grammar/expressions.rs21
-rw-r--r--crates/parser/src/grammar/expressions/atom.rs6
3 files changed, 14 insertions, 17 deletions
diff --git a/crates/parser/src/grammar.rs b/crates/parser/src/grammar.rs
index 15ec9e167e0..15435a26cea 100644
--- a/crates/parser/src/grammar.rs
+++ b/crates/parser/src/grammar.rs
@@ -198,6 +198,10 @@ impl BlockLike {
     fn is_block(self) -> bool {
         self == BlockLike::Block
     }
+
+    fn is_blocklike(kind: SyntaxKind) -> bool {
+        matches!(kind, BLOCK_EXPR | IF_EXPR | WHILE_EXPR | FOR_EXPR | LOOP_EXPR | MATCH_EXPR)
+    }
 }
 
 const VISIBILITY_FIRST: TokenSet = TokenSet::new(&[T![pub], T![crate]]);
diff --git a/crates/parser/src/grammar/expressions.rs b/crates/parser/src/grammar/expressions.rs
index 7232cdfef06..a884d8b6ec8 100644
--- a/crates/parser/src/grammar/expressions.rs
+++ b/crates/parser/src/grammar/expressions.rs
@@ -121,27 +121,22 @@ pub(super) fn stmt(p: &mut Parser<'_>, semicolon: Semicolon) {
             types::ascription(p);
         }
 
-        let mut is_block_like_expr_after_eq = false;
+        let mut expr_after_eq: Option<CompletedMarker> = None;
         if p.eat(T![=]) {
             // test let_stmt_init
             // fn f() { let x = 92; }
-            let expr = expressions::expr(p);
-
-            if let Some(expr) = expr {
-                is_block_like_expr_after_eq = match expr.kind() {
-                    IF_EXPR | WHILE_EXPR | FOR_EXPR | LOOP_EXPR | MATCH_EXPR | BLOCK_EXPR => true,
-                    _ => false,
-                };
-            }
+            expr_after_eq = expressions::expr(p);
         }
 
         if p.at(T![else]) {
             // test_err let_else_right_curly_brace
             // fn func() { let Some(_) = {Some(1)} else { panic!("h") };}
-            if is_block_like_expr_after_eq {
-                p.error(
-                    "right curly brace `}` before `else` in a `let...else` statement not allowed",
-                )
+            if let Some(expr) = expr_after_eq {
+                if BlockLike::is_blocklike(expr.kind()) {
+                    p.error(
+                        "right curly brace `}` before `else` in a `let...else` statement not allowed",
+                    )
+                }
             }
 
             // test let_else
diff --git a/crates/parser/src/grammar/expressions/atom.rs b/crates/parser/src/grammar/expressions/atom.rs
index a33a1d2543e..d051dd2682f 100644
--- a/crates/parser/src/grammar/expressions/atom.rs
+++ b/crates/parser/src/grammar/expressions/atom.rs
@@ -163,10 +163,8 @@ pub(super) fn atom_expr(
             return None;
         }
     };
-    let blocklike = match done.kind() {
-        IF_EXPR | WHILE_EXPR | FOR_EXPR | LOOP_EXPR | MATCH_EXPR | BLOCK_EXPR => BlockLike::Block,
-        _ => BlockLike::NotBlock,
-    };
+    let blocklike =
+        if BlockLike::is_blocklike(done.kind()) { BlockLike::Block } else { BlockLike::NotBlock };
     Some((done, blocklike))
 }