From f1912c588bc13710dfc455e8c712bf71638d1a4e Mon Sep 17 00:00:00 2001 From: raskad <32105367+raskad@users.noreply.github.com> Date: Sat, 9 Apr 2022 04:38:09 +0200 Subject: [PATCH] parse bindingidentifier correctly --- boa_engine/src/syntax/parser/statement/mod.rs | 103 ++++++++---------- 1 file changed, 47 insertions(+), 56 deletions(-) diff --git a/boa_engine/src/syntax/parser/statement/mod.rs b/boa_engine/src/syntax/parser/statement/mod.rs index 97f31088f68..242263639db 100644 --- a/boa_engine/src/syntax/parser/statement/mod.rs +++ b/boa_engine/src/syntax/parser/statement/mod.rs @@ -51,7 +51,7 @@ use crate::syntax::{ }, Keyword, Node, Position, Punctuator, }, - lexer::{Error as LexError, InputElement, TokenKind}, + lexer::{Error as LexError, InputElement, Token, TokenKind}, parser::expression::{await_expr::AwaitExpression, Initializer}, }; use boa_interner::{Interner, Sym}; @@ -653,11 +653,13 @@ where let mut rest_property_name = None; loop { - let property_name = match cursor - .peek(0, interner)? + let next_token_is_colon = *cursor + .peek(1, interner)? .ok_or(ParseError::AbruptEnd)? .kind() - { + == TokenKind::Punctuator(Punctuator::Colon); + let token = cursor.peek(0, interner)?.ok_or(ParseError::AbruptEnd)?; + match token.kind() { TokenKind::Punctuator(Punctuator::CloseBlock) => { cursor.expect( TokenKind::Punctuator(Punctuator::CloseBlock), @@ -684,48 +686,26 @@ where break; } _ => { - PropertyName::new(self.allow_yield, self.allow_await).parse(cursor, interner)? - } - }; - - if let Some(name) = property_name.prop_name() { - property_names.push(name); - } - - if let Some(peek_token) = cursor.peek(0, interner)? { - match peek_token.kind() { - TokenKind::Punctuator(Punctuator::Assign) => { - let name = if let Some(name) = property_name.literal() { - name - } else { - return Err(ParseError::expected( - [":".to_owned()], - peek_token.to_string(interner), - peek_token.span(), - "binding property", - )); - }; - - let init = Initializer::new( - property_name.prop_name(), - self.allow_in, - self.allow_yield, - self.allow_await, - ) - .parse(cursor, interner)?; - patterns.push(BindingPatternTypeObject::SingleName { - ident: name, - property_name, - default_init: Some(init), - }); - } - TokenKind::Punctuator(Punctuator::Colon) => { + let is_property_name = match token.kind() { + TokenKind::Punctuator(Punctuator::OpenBracket) + | TokenKind::StringLiteral(_) + | TokenKind::NumericLiteral(_) => true, + TokenKind::Identifier(_) if next_token_is_colon => true, + TokenKind::Keyword(_) if next_token_is_colon => true, + _ => false, + }; + + if is_property_name { + let property_name = PropertyName::new(self.allow_yield, self.allow_await) + .parse(cursor, interner)?; + if let Some(name) = property_name.prop_name() { + property_names.push(name); + } cursor.expect( TokenKind::Punctuator(Punctuator::Colon), "object binding pattern", interner, )?; - if let Some(peek_token) = cursor.peek(0, interner)? { match peek_token.kind() { TokenKind::Punctuator(Punctuator::OpenBlock) => { @@ -859,21 +839,32 @@ where } } } - } - _ => { - if let Some(name) = property_name.literal() { - patterns.push(BindingPatternTypeObject::SingleName { - ident: name, - property_name, - default_init: None, - }); - } else { - return Err(ParseError::expected( - [":".to_owned()], - peek_token.to_string(interner), - peek_token.span(), - "binding property", - )); + } else { + let name = BindingIdentifier::new(self.allow_yield, self.allow_await) + .parse(cursor, interner)?; + property_names.push(name); + match cursor.peek(0, interner)?.map(Token::kind) { + Some(TokenKind::Punctuator(Punctuator::Assign)) => { + let init = Initializer::new( + Some(name), + self.allow_in, + self.allow_yield, + self.allow_await, + ) + .parse(cursor, interner)?; + patterns.push(BindingPatternTypeObject::SingleName { + ident: name, + property_name: name.into(), + default_init: Some(init), + }); + } + _ => { + patterns.push(BindingPatternTypeObject::SingleName { + ident: name, + property_name: name.into(), + default_init: None, + }); + } } } }