Skip to content

Commit

Permalink
check for label before running expression
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonwilliams committed Sep 21, 2020
1 parent 1848798 commit 9a98fab
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 14 deletions.
2 changes: 1 addition & 1 deletion boa/src/exec/iteration/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ fn for_loop_break_label() {
}
str = str + i;
}
str;
str
"#;
assert_eq!(&exec(scenario), "\"01\"")
}
32 changes: 19 additions & 13 deletions boa/src/syntax/parser/statement/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ use self::{

use super::{AllowAwait, AllowReturn, AllowYield, Cursor, ParseError, TokenParser};

use crate::syntax::lexer::TokenKind;
use crate::syntax::lexer::{InputElement, TokenKind};
use crate::{
syntax::ast::{node, Keyword, Node, Punctuator},
BoaProfiler,
Expand Down Expand Up @@ -105,7 +105,7 @@ where
fn parse(self, cursor: &mut Cursor<R>) -> Result<Self::Output, ParseError> {
let _timer = BoaProfiler::global().start_event("Statement", "Parsing");
// TODO: add BreakableStatement and divide Whiles, fors and so on to another place.
let tok = cursor.peek(0)?.ok_or(ParseError::AbruptEnd)?.to_owned();
let tok = cursor.peek(0)?.ok_or(ParseError::AbruptEnd)?;

match tok.kind() {
TokenKind::Keyword(Keyword::If) => {
Expand Down Expand Up @@ -172,21 +172,27 @@ where
.parse(cursor)
.map(Node::from)
}
// Create guard to check if the next token is a `:` then we know we're sitting on a label
// if not fall back to ExpressionStatement
TokenKind::Identifier(_)
if cursor.peek_expect_no_lineterminator(1).is_ok()
_ => {
// Before falling to expression check for a label
cursor.set_goal(InputElement::Div);
let tok = cursor.peek(1)?;
if tok.is_some()
&& matches!(
cursor.peek_expect_no_lineterminator(1)?.kind(),
tok.unwrap().kind(),
TokenKind::Punctuator(Punctuator::Colon)
) =>
{
LabelledStatement::new(self.allow_yield, self.allow_await, self.allow_return)
)
{
return LabelledStatement::new(
self.allow_yield,
self.allow_await,
self.allow_return,
)
.parse(cursor)
.map(Node::from)
}
.map(Node::from);
}

_ => ExpressionStatement::new(self.allow_yield, self.allow_await).parse(cursor),
ExpressionStatement::new(self.allow_yield, self.allow_await).parse(cursor)
}
}
}
}
Expand Down

0 comments on commit 9a98fab

Please sign in to comment.