From 8d4664278ad385157746cbc8dfa8f16f539a46c1 Mon Sep 17 00:00:00 2001 From: raskad <32105367+raskad@users.noreply.github.com> Date: Sun, 3 Apr 2022 18:44:32 +0200 Subject: [PATCH] Allow initializer after `ArrayBindingPattern` --- boa_engine/src/syntax/parser/function/mod.rs | 77 ++++++++++++-------- 1 file changed, 45 insertions(+), 32 deletions(-) diff --git a/boa_engine/src/syntax/parser/function/mod.rs b/boa_engine/src/syntax/parser/function/mod.rs index fbfce13e7fc..c8bd86e7350 100644 --- a/boa_engine/src/syntax/parser/function/mod.rs +++ b/boa_engine/src/syntax/parser/function/mod.rs @@ -399,50 +399,63 @@ where if let Some(t) = cursor.peek(0, interner)? { let declaration = match *t.kind() { TokenKind::Punctuator(Punctuator::OpenBlock) => { - let param = ObjectBindingPattern::new(true, self.allow_yield, self.allow_await) - .parse(cursor, interner)?; - - let init = cursor + let bindings = + ObjectBindingPattern::new(true, self.allow_yield, self.allow_await) + .parse(cursor, interner)?; + let init = if *cursor .peek(0, interner)? - .cloned() - .filter(|t| { - // Check that this is an initializer before attempting parse. - *t.kind() == TokenKind::Punctuator(Punctuator::Assign) - }) - .map(|_| { + .ok_or(ParseError::AbruptEnd)? + .kind() + == TokenKind::Punctuator(Punctuator::Assign) + { + Some( Initializer::new(None, true, self.allow_yield, self.allow_await) - .parse(cursor, interner) - }) - .transpose()?; + .parse(cursor, interner)?, + ) + } else { + None + }; - Declaration::new_with_object_pattern(param, init) + Declaration::new_with_object_pattern(bindings, init) } - TokenKind::Punctuator(Punctuator::OpenBracket) => { - Declaration::new_with_array_pattern( + let bindings = ArrayBindingPattern::new(true, self.allow_yield, self.allow_await) - .parse(cursor, interner)?, - None, - ) - } + .parse(cursor, interner)?; + let init = if *cursor + .peek(0, interner)? + .ok_or(ParseError::AbruptEnd)? + .kind() + == TokenKind::Punctuator(Punctuator::Assign) + { + Some( + Initializer::new(None, true, self.allow_yield, self.allow_await) + .parse(cursor, interner)?, + ) + } else { + None + }; + Declaration::new_with_array_pattern(bindings, init) + } _ => { - let params = BindingIdentifier::new(self.allow_yield, self.allow_await) + let ident = BindingIdentifier::new(self.allow_yield, self.allow_await) .parse(cursor, interner)?; - let init = cursor + let init = if *cursor .peek(0, interner)? - .cloned() - .filter(|t| { - // Check that this is an initializer before attempting parse. - *t.kind() == TokenKind::Punctuator(Punctuator::Assign) - }) - .map(|_| { + .ok_or(ParseError::AbruptEnd)? + .kind() + == TokenKind::Punctuator(Punctuator::Assign) + { + Some( Initializer::new(None, true, self.allow_yield, self.allow_await) - .parse(cursor, interner) - }) - .transpose()?; + .parse(cursor, interner)?, + ) + } else { + None + }; - Declaration::new_with_identifier(params, init) + Declaration::new_with_identifier(ident, init) } }; Ok(Self::Output::new(declaration, false))