From 5bc89413debbbb0211c4a99e55e6cc009767250b Mon Sep 17 00:00:00 2001 From: Paul Lancaster Date: Sat, 10 Oct 2020 00:00:55 +0100 Subject: [PATCH] Add async to HoistableDeclaration --- .../parser/statement/declaration/hoistable.rs | 22 ++++++++++++++----- .../parser/statement/declaration/mod.rs | 2 +- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/boa/src/syntax/parser/statement/declaration/hoistable.rs b/boa/src/syntax/parser/statement/declaration/hoistable.rs index a059bbe95d..1fb5fe1fc5 100644 --- a/boa/src/syntax/parser/statement/declaration/hoistable.rs +++ b/boa/src/syntax/parser/statement/declaration/hoistable.rs @@ -11,6 +11,7 @@ use crate::{ node::{AsyncFunctionDecl, FunctionDecl}, Keyword, Node, Punctuator, }, + lexer::TokenKind, parser::{ function::FormalParameters, function::FunctionBody, statement::BindingIdentifier, AllowAwait, AllowDefault, AllowYield, Cursor, ParseError, ParseResult, TokenParser, @@ -18,7 +19,6 @@ use crate::{ }, BoaProfiler, }; - use std::io::Read; /// Hoistable declaration parsing. @@ -58,10 +58,21 @@ where fn parse(self, cursor: &mut Cursor) -> ParseResult { let _timer = BoaProfiler::global().start_event("HoistableDeclaration", "Parsing"); - // TODO: check for generators and async functions + generators - FunctionDeclaration::new(self.allow_yield, self.allow_await, self.is_default) - .parse(cursor) - .map(Node::from) + let tok = cursor.peek(0)?.ok_or(ParseError::AbruptEnd)?; + + match tok.kind() { + TokenKind::Keyword(Keyword::Function) => { + FunctionDeclaration::new(self.allow_yield, self.allow_await, self.is_default) + .parse(cursor) + .map(Node::from) + } + TokenKind::Keyword(Keyword::Async) => { + AsyncFunctionDeclaration::new(self.allow_yield, self.allow_await, false) + .parse(cursor) + .map(Node::from) + } + _ => unreachable!("unknown token found: {:?}", tok), + } } } @@ -161,6 +172,7 @@ where type Output = AsyncFunctionDecl; fn parse(self, cursor: &mut Cursor) -> Result { + cursor.expect(Keyword::Async, "async function declaration")?; unimplemented!("AsyncFunctionDecl parse"); } } diff --git a/boa/src/syntax/parser/statement/declaration/mod.rs b/boa/src/syntax/parser/statement/declaration/mod.rs index d1c1c9dd98..518748d2e7 100644 --- a/boa/src/syntax/parser/statement/declaration/mod.rs +++ b/boa/src/syntax/parser/statement/declaration/mod.rs @@ -63,7 +63,7 @@ where let tok = cursor.peek(0)?.ok_or(ParseError::AbruptEnd)?; match tok.kind() { - TokenKind::Keyword(Keyword::Function) => { + TokenKind::Keyword(Keyword::Function) | TokenKind::Keyword(Keyword::Async) => { HoistableDeclaration::new(self.allow_yield, self.allow_await, false).parse(cursor) } TokenKind::Keyword(Keyword::Const) | TokenKind::Keyword(Keyword::Let) => {