Skip to content

Commit

Permalink
Fix yield expression to end on line terminator
Browse files Browse the repository at this point in the history
  • Loading branch information
raskad committed Aug 13, 2022
1 parent a8bf59d commit 1b30a3a
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 12 deletions.
8 changes: 4 additions & 4 deletions boa_engine/src/syntax/parser/cursor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,15 +263,15 @@ where

/// Check if the peeked token is a line terminator.
#[inline]
pub(super) fn peek_expect_is_line_terminator(
pub(super) fn peek_is_line_terminator(
&mut self,
skip_n: usize,
interner: &mut Interner,
) -> Result<bool, ParseError> {
) -> Result<Option<bool>, ParseError> {
if let Some(t) = self.buffered_lexer.peek(skip_n, false, interner)? {
Ok(t.kind() == &TokenKind::LineTerminator)
Ok(Some(t.kind() == &TokenKind::LineTerminator))
} else {
Err(ParseError::AbruptEnd)
Ok(None)
}
}

Expand Down
5 changes: 4 additions & 1 deletion boa_engine/src/syntax/parser/expression/assignment/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,10 @@ where
TokenKind::Identifier(_) | TokenKind::Keyword((Keyword::Yield | Keyword::Await, _)) => {
// Because we already peeked the identifier token, there may be a line terminator before the identifier token.
// In that case we have to skip an additional token on the next peek.
let skip_n = if cursor.peek_expect_is_line_terminator(0, interner)? {
let skip_n = if cursor
.peek_is_line_terminator(0, interner)?
.ok_or(ParseError::AbruptEnd)?
{
2
} else {
1
Expand Down
14 changes: 8 additions & 6 deletions boa_engine/src/syntax/parser/expression/assignment/yield.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use crate::syntax::{
Keyword, Punctuator,
},
lexer::TokenKind,
parser::{AllowAwait, AllowIn, Cursor, ParseResult, TokenParser},
parser::{AllowAwait, AllowIn, Cursor, ParseError, ParseResult, TokenParser},
};
use boa_interner::Interner;
use boa_profiler::Profiler;
Expand Down Expand Up @@ -63,12 +63,14 @@ where
interner,
)?;

let token = if let Some(token) = cursor.peek(0, interner)? {
token
} else {
return Ok(Node::Yield(Yield::new::<Node>(None, false)));
};
match cursor.peek_is_line_terminator(0, interner)? {
Some(false) => {}
Some(true) | None => {
return Ok(Node::Yield(Yield::new::<Node>(None, false)));
}
}

let token = cursor.peek(0, interner)?.ok_or(ParseError::AbruptEnd)?;
match token.kind() {
TokenKind::Punctuator(Punctuator::Mul) => {
cursor.next(interner)?.expect("token disappeared");
Expand Down
4 changes: 3 additions & 1 deletion boa_engine/src/syntax/parser/expression/primary/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,9 @@ where
let is_arrow = if let Some(TokenKind::Punctuator(Punctuator::Arrow)) =
cursor.peek(0, interner)?.map(Token::kind)
{
!cursor.peek_expect_is_line_terminator(0, interner)?
!cursor
.peek_is_line_terminator(0, interner)?
.ok_or(ParseError::AbruptEnd)?
} else {
false
};
Expand Down

0 comments on commit 1b30a3a

Please sign in to comment.