Browse Source

Fix Regex literal parsing in MemberExpression (#2328)

This Pull Request fixes/closes #2327 

It changes the following:
- set_goal(RegExp) for PrimaryExpression parsing
pull/2369/head
Choongwoo Han 2 years ago
parent
commit
904e422d36
  1. 3
      boa_engine/src/syntax/parser/expression/left_hand_side/arguments.rs
  2. 6
      boa_engine/src/syntax/parser/expression/left_hand_side/member.rs
  3. 54
      boa_engine/src/syntax/parser/expression/tests.rs

3
boa_engine/src/syntax/parser/expression/left_hand_side/arguments.rs

@ -59,6 +59,7 @@ where
cursor.expect(Punctuator::OpenParen, "arguments", interner)?;
let mut args = Vec::new();
loop {
cursor.set_goal(InputElement::RegExp);
let next_token = cursor.peek(0, interner)?.ok_or(ParseError::AbruptEnd)?;
match next_token.kind() {
@ -102,13 +103,13 @@ where
.into(),
);
} else {
cursor.set_goal(InputElement::RegExp);
args.push(
AssignmentExpression::new(None, true, self.allow_yield, self.allow_await)
.parse(cursor, interner)?,
);
}
}
cursor.set_goal(InputElement::Div);
Ok(args.into_boxed_slice())
}
}

6
boa_engine/src/syntax/parser/expression/left_hand_side/member.rs

@ -17,7 +17,7 @@ use crate::syntax::{
},
Keyword, Punctuator,
},
lexer::TokenKind,
lexer::{InputElement, TokenKind},
parser::{
expression::{
left_hand_side::template::TaggedTemplateLiteral, primary::PrimaryExpression, Expression,
@ -67,6 +67,8 @@ where
fn parse(self, cursor: &mut Cursor<R>, interner: &mut Interner) -> ParseResult<Self::Output> {
let _timer = Profiler::global().start_event("MemberExpression", "Parsing");
cursor.set_goal(InputElement::RegExp);
let token = cursor.peek(0, interner)?.ok_or(ParseError::AbruptEnd)?;
let mut lhs = match token.kind() {
TokenKind::Keyword((Keyword::New | Keyword::Super, true)) => {
@ -160,6 +162,8 @@ where
.parse(cursor, interner)?,
};
cursor.set_goal(InputElement::TemplateTail);
while let Some(tok) = cursor.peek(0, interner)? {
match tok.kind() {
TokenKind::Punctuator(Punctuator::Dot) => {

54
boa_engine/src/syntax/parser/expression/tests.rs

@ -119,6 +119,60 @@ fn check_numeric_operations() {
interner,
);
let mut interner = Interner::default();
check_parser(
"fn(/=/);",
vec![Statement::Expression(Expression::from(Call::new(
Identifier::new(interner.get_or_intern_static("fn", utf16!("fn"))).into(),
vec![New::from(Call::new(
Identifier::new(Sym::REGEXP).into(),
vec![
Literal::from(interner.get_or_intern_static("=", utf16!("="))).into(),
Literal::from(Sym::EMPTY_STRING).into(),
]
.into(),
))
.into()]
.into(),
)))
.into()],
interner,
);
let mut interner = Interner::default();
check_parser(
"fn(a / b);",
vec![Statement::Expression(Expression::from(Call::new(
Identifier::new(interner.get_or_intern_static("fn", utf16!("fn"))).into(),
vec![Expression::from(Binary::new(
ArithmeticOp::Div.into(),
Identifier::new(interner.get_or_intern_static("a", utf16!("a"))).into(),
Identifier::new(interner.get_or_intern_static("b", utf16!("b"))).into(),
))
.into()]
.into(),
)))
.into()],
interner,
);
let mut interner = Interner::default();
check_parser(
"fn(a) / b;",
vec![Statement::Expression(Expression::from(Binary::new(
ArithmeticOp::Div.into(),
Call::new(
Identifier::new(interner.get_or_intern_static("fn", utf16!("fn"))).into(),
vec![Identifier::new(interner.get_or_intern_static("a", utf16!("a"))).into()]
.into(),
)
.into(),
Identifier::new(interner.get_or_intern_static("b", utf16!("b"))).into(),
)))
.into()],
interner,
);
let mut interner = Interner::default();
check_parser(
"a * b",

Loading…
Cancel
Save