Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Arrow parse #1051

Merged
merged 3 commits into from
Jan 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion boa/src/syntax/parser/cursor/buffered_lexer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use std::io::Read;
mod tests;

/// The maximum number of tokens which can be peeked ahead.
const MAX_PEEK_SKIP: usize = 2;
const MAX_PEEK_SKIP: usize = 3;

/// The fixed size of the buffer used for storing values that are peeked ahead.
///
Expand Down Expand Up @@ -46,6 +46,8 @@ where
None::<Token>,
None::<Token>,
None::<Token>,
None::<Token>,
None::<Token>,
],
read_index: 0,
write_index: 0,
Expand Down
2 changes: 1 addition & 1 deletion boa/src/syntax/parser/cursor/buffered_lexer/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ fn peek_skip_next_till_end() {
let mut cur = BufferedLexer::from(&b"a b c d e f g h i"[..]);

let mut peeked: [Option<Token>; super::MAX_PEEK_SKIP + 1] =
[None::<Token>, None::<Token>, None::<Token>];
[None::<Token>, None::<Token>, None::<Token>, None::<Token>];

loop {
for (i, peek) in peeked.iter_mut().enumerate() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ where

fn parse(self, cursor: &mut Cursor<R>) -> Result<Self::Output, ParseError> {
let _timer = BoaProfiler::global().start_event("ArrowFunction", "Parsing");

let next_token = cursor.peek(0)?.ok_or(ParseError::AbruptEnd)?;

let params = if let TokenKind::Punctuator(Punctuator::OpenParen) = &next_token.kind() {
// CoverParenthesizedExpressionAndArrowParameterList
cursor.expect(Punctuator::OpenParen, "arrow function")?;
Expand Down
2 changes: 1 addition & 1 deletion boa/src/syntax/parser/expression/assignment/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ where
TokenKind::Punctuator(Punctuator::CloseParen) => {
// Need to check if the token after the close paren is an arrow, if so then this is an ArrowFunction
// otherwise it is an expression of the form (b).
if let Some(t) = cursor.peek(2)? {
if let Some(t) = cursor.peek(3)? {
if t.kind() == &TokenKind::Punctuator(Punctuator::Arrow)
{
return ArrowFunction::new(
Expand Down
193 changes: 192 additions & 1 deletion boa/src/syntax/parser/function/tests.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::syntax::{
ast::node::{
ArrowFunctionDecl, BinOp, FormalParameter, FunctionDecl, Identifier, Node, Return,
ArrowFunctionDecl, BinOp, FormalParameter, FunctionDecl, Identifier, LetDecl, LetDeclList,
Node, Return,
},
ast::op::NumOp,
parser::tests::check_parser,
Expand Down Expand Up @@ -176,3 +177,193 @@ fn check_arrow_empty_return_semicolon_insertion() {
.into()],
);
}

#[test]
fn check_arrow_assignment() {
check_parser(
"let foo = (a) => { return a };",
vec![LetDeclList::from(vec![LetDecl::new::<_, Option<Node>>(
Identifier::from("foo"),
Some(
ArrowFunctionDecl::new(
vec![FormalParameter::new("a", None, false)],
vec![Return::new::<Node, Option<_>, Option<_>>(
Some(Identifier::from("a").into()),
None,
)
.into()],
)
.into(),
),
)])
.into()],
);
}

#[test]
fn check_arrow_assignment_nobrackets() {
check_parser(
"let foo = (a) => a;",
vec![LetDeclList::from(vec![LetDecl::new::<_, Option<Node>>(
Identifier::from("foo"),
Some(
ArrowFunctionDecl::new(
vec![FormalParameter::new("a", None, false)],
vec![Return::new::<Node, Option<_>, Option<_>>(
Some(Identifier::from("a").into()),
None,
)
.into()],
)
.into(),
),
)])
.into()],
);
}

#[test]
fn check_arrow_assignment_noparenthesis() {
check_parser(
"let foo = a => { return a };",
vec![LetDeclList::from(vec![LetDecl::new::<_, Option<Node>>(
Identifier::from("foo"),
Some(
ArrowFunctionDecl::new(
vec![FormalParameter::new("a", None, false)],
vec![Return::new::<Node, Option<_>, Option<_>>(
Some(Identifier::from("a").into()),
None,
)
.into()],
)
.into(),
),
)])
.into()],
);
}

#[test]
fn check_arrow_assignment_noparenthesis_nobrackets() {
check_parser(
"let foo = a => a;",
vec![LetDeclList::from(vec![LetDecl::new::<_, Option<Node>>(
Identifier::from("foo"),
Some(
ArrowFunctionDecl::new(
vec![FormalParameter::new("a", None, false)],
vec![Return::new::<Node, Option<_>, Option<_>>(
Some(Identifier::from("a").into()),
None,
)
.into()],
)
.into(),
),
)])
.into()],
);
}

#[test]
fn check_arrow_assignment_2arg() {
check_parser(
"let foo = (a, b) => { return a };",
vec![LetDeclList::from(vec![LetDecl::new::<_, Option<Node>>(
Identifier::from("foo"),
Some(
ArrowFunctionDecl::new(
vec![
FormalParameter::new("a", None, false),
FormalParameter::new("b", None, false),
],
vec![Return::new::<Node, Option<_>, Option<_>>(
Some(Identifier::from("a").into()),
None,
)
.into()],
)
.into(),
),
)])
.into()],
);
}

#[test]
fn check_arrow_assignment_2arg_nobrackets() {
check_parser(
"let foo = (a, b) => a;",
vec![LetDeclList::from(vec![LetDecl::new::<_, Option<Node>>(
Identifier::from("foo"),
Some(
ArrowFunctionDecl::new(
vec![
FormalParameter::new("a", None, false),
FormalParameter::new("b", None, false),
],
vec![Return::new::<Node, Option<_>, Option<_>>(
Some(Identifier::from("a").into()),
None,
)
.into()],
)
.into(),
),
)])
.into()],
);
}

#[test]
fn check_arrow_assignment_3arg() {
check_parser(
"let foo = (a, b, c) => { return a };",
vec![LetDeclList::from(vec![LetDecl::new::<_, Option<Node>>(
Identifier::from("foo"),
Some(
ArrowFunctionDecl::new(
vec![
FormalParameter::new("a", None, false),
FormalParameter::new("b", None, false),
FormalParameter::new("c", None, false),
],
vec![Return::new::<Node, Option<_>, Option<_>>(
Some(Identifier::from("a").into()),
None,
)
.into()],
)
.into(),
),
)])
.into()],
);
}

#[test]
fn check_arrow_assignment_3arg_nobrackets() {
check_parser(
"let foo = (a, b, c) => a;",
vec![LetDeclList::from(vec![LetDecl::new::<_, Option<Node>>(
Identifier::from("foo"),
Some(
ArrowFunctionDecl::new(
vec![
FormalParameter::new("a", None, false),
FormalParameter::new("b", None, false),
FormalParameter::new("c", None, false),
],
vec![Return::new::<Node, Option<_>, Option<_>>(
Some(Identifier::from("a").into()),
None,
)
.into()],
)
.into(),
),
)])
.into()],
);
}