Browse Source

AsyncFunctionExpr parsing

pull/836/head
Paul Lancaster 4 years ago
parent
commit
35fb7455ca
  1. 51
      boa/src/syntax/parser/expression/primary/async_function_expression.rs
  2. 6
      boa/src/syntax/parser/expression/primary/mod.rs

51
boa/src/syntax/parser/expression/primary/async_function_expression.rs

@ -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,
};
@ -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
@ -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
boa/src/syntax/parser/expression/primary/mod.rs

@ -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 =

Loading…
Cancel
Save