Skip to content

Commit

Permalink
AsyncFunctionExpr parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul Lancaster committed Oct 10, 2020
1 parent 0bfe141 commit 35fb745
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,15 @@
//!
//! [mdn]:
//! [spec]:
use crate::{
syntax::{
ast::node::AsyncFunctionExpr,
parser::{Cursor, ParseError, TokenParser},
ast::{node::AsyncFunctionExpr, Keyword, Punctuator},
lexer::TokenKind,
parser::{
function::{FormalParameters, FunctionBody},
statement::BindingIdentifier,
AllowYield, Cursor, ParseError, TokenParser,
},
},
BoaProfiler,
};
Expand All @@ -26,7 +30,21 @@ use std::io::Read;
/// [mdn]:
/// [spec]:
#[derive(Debug, Clone, Copy)]
pub(super) struct AsyncFunctionExpression;
pub(super) struct AsyncFunctionExpression {
allow_yield: AllowYield,
}

impl AsyncFunctionExpression {
/// Creates a new `AsyncFunctionExpression` parser.
pub(super) fn new<Y>(allow_yield: Y) -> Self
where
Y: Into<AllowYield>,
{
Self {
allow_yield: allow_yield.into(),
}
}
}

impl<R> TokenParser<R> for AsyncFunctionExpression
where
Expand All @@ -36,7 +54,30 @@ where

fn parse(self, cursor: &mut Cursor<R>) -> Result<Self::Output, ParseError> {
let _timer = BoaProfiler::global().start_event("AsyncFunctionExpression", "Parsing");
cursor.expect_no_skip_lineterminator(Keyword::Function, "async function expression")?;

let tok = cursor.peek(0)?;

let name = if let Some(token) = tok {
match token.kind() {
TokenKind::Punctuator(Punctuator::OpenParen) => None,
_ => Some(BindingIdentifier::new(self.allow_yield, true).parse(cursor)?),
}
} else {
return Err(ParseError::AbruptEnd);
};

cursor.expect(Punctuator::OpenParen, "function expression")?;

let params = FormalParameters::new(!self.allow_yield.0, true).parse(cursor)?;

cursor.expect(Punctuator::CloseParen, "function expression")?;
cursor.expect(Punctuator::OpenBlock, "function expression")?;

let body = FunctionBody::new(!self.allow_yield.0, true).parse(cursor)?;

cursor.expect(Punctuator::CloseBlock, "function expression")?;

unimplemented!("Async function expression parse");
Ok(AsyncFunctionExpr::new(name, params, body))
}
}
6 changes: 3 additions & 3 deletions boa/src/syntax/parser/expression/primary/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,9 @@ where
TokenKind::Keyword(Keyword::Function) => {
FunctionExpression.parse(cursor).map(Node::from)
}
TokenKind::Keyword(Keyword::Async) => {
AsyncFunctionExpression.parse(cursor).map(Node::from)
}
TokenKind::Keyword(Keyword::Async) => AsyncFunctionExpression::new(self.allow_yield)
.parse(cursor)
.map(Node::from),
TokenKind::Punctuator(Punctuator::OpenParen) => {
cursor.set_goal(InputElement::RegExp);
let expr =
Expand Down

0 comments on commit 35fb745

Please sign in to comment.