update blocklike

This commit is contained in:
jnyfah 2025-01-16 12:11:55 +01:00
parent 11717ca5d4
commit e3deeec2f2
5 changed files with 10 additions and 13 deletions

View file

@ -12,7 +12,7 @@ use crate::{
/// `Parser` produces a flat list of `Event`s. /// `Parser` produces a flat list of `Event`s.
/// They are converted to a tree-structure in /// They are converted to a tree-structure in
/// a separate pass, via `TreeBuilder`. /// a separate pass, via `TreeBuilder`.
#[derive(Debug)] #[derive(Debug, PartialEq)]
pub(crate) enum Event { pub(crate) enum Event {
/// This event signifies the start of the node. /// This event signifies the start of the node.
/// It should be either abandoned (in which case the /// It should be either abandoned (in which case the

View file

@ -204,8 +204,9 @@ impl BlockLike {
self == BlockLike::Block self == BlockLike::Block
} }
fn is_blocklike(kind: SyntaxKind) -> bool { fn is_blocklike(expr: &CompletedMarker, p: &Parser<'_>) -> bool {
matches!(kind, BLOCK_EXPR | IF_EXPR | WHILE_EXPR | FOR_EXPR | LOOP_EXPR | MATCH_EXPR) matches!(expr.kind(), BLOCK_EXPR | IF_EXPR | WHILE_EXPR | FOR_EXPR | LOOP_EXPR | MATCH_EXPR)
|| (expr.last_token(p) == Some(T!['}']) && !matches!(expr.kind(), CLOSURE_EXPR))
} }
} }

View file

@ -134,12 +134,10 @@ pub(super) fn let_stmt(p: &mut Parser<'_>, with_semi: Semicolon) {
// test_err let_else_right_curly_brace // test_err let_else_right_curly_brace
// fn func() { let Some(_) = {Some(1)} else { panic!("h") };} // fn func() { let Some(_) = {Some(1)} else { panic!("h") };}
if let Some(expr) = expr_after_eq { if let Some(expr) = expr_after_eq {
if let Some(token) = expr.last_token(p) { if BlockLike::is_blocklike(&expr, p) {
if token == T!['}'] { p.error(
p.error( "right curly brace `}` before `else` in a `let...else` statement not allowed",
"right curly brace `}` before `else` in a `let...else` statement not allowed" )
)
}
} }
} }

View file

@ -198,7 +198,7 @@ pub(super) fn atom_expr(
} }
}; };
let blocklike = let blocklike =
if BlockLike::is_blocklike(done.kind()) { BlockLike::Block } else { BlockLike::NotBlock }; if BlockLike::is_blocklike(&done, p) { BlockLike::Block } else { BlockLike::NotBlock };
Some((done, blocklike)) Some((done, blocklike))
} }

View file

@ -391,9 +391,7 @@ impl CompletedMarker {
pub(crate) fn last_token(&self, p: &Parser<'_>) -> Option<SyntaxKind> { pub(crate) fn last_token(&self, p: &Parser<'_>) -> Option<SyntaxKind> {
let end_pos = self.end_pos as usize; let end_pos = self.end_pos as usize;
if end_pos > p.events.len() { debug_assert_eq!(p.events[end_pos - 1], Event::Finish);
return None;
}
p.events[..end_pos].iter().rev().find_map(|event| match event { p.events[..end_pos].iter().rev().find_map(|event| match event {
Event::Token { kind, .. } => Some(*kind), Event::Token { kind, .. } => Some(*kind),
_ => None, _ => None,