Skip to content

Commit

Permalink
Finish ExportDeclaration parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
jedel1043 committed Feb 1, 2023
1 parent 76ebebe commit f0979cf
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 24 deletions.
64 changes: 50 additions & 14 deletions boa_parser/src/parser/statement/declaration/export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,20 @@ use crate::{
lexer::{token::ContainsEscapeSequence, TokenKind},
parser::{
cursor::Cursor,
expression::AssignmentExpression,
statement::{declaration::ClassDeclaration, variable::VariableStatement},
Error, OrAbrupt, ParseResult, TokenParser,
},
};
use boa_ast::{Keyword, Punctuator};
use boa_ast::{declaration::ExportDeclaration as AstExportDeclaration, Keyword, Punctuator};
use boa_interner::{Interner, Sym};
use boa_profiler::Profiler;
use std::io::Read;

use super::FromClause;
use super::{
hoistable::{AsyncFunctionDeclaration, AsyncGeneratorDeclaration, GeneratorDeclaration},
Declaration, FromClause, FunctionDeclaration,
};

/// Parses an export declaration.
///
Expand All @@ -37,7 +41,7 @@ impl<R> TokenParser<R> for ExportDeclaration
where
R: Read,
{
type Output = boa_ast::declaration::ExportDeclaration;
type Output = AstExportDeclaration;

fn parse(self, cursor: &mut Cursor<R>, interner: &mut Interner) -> ParseResult<Self::Output> {
let _timer = Profiler::global().start_event("ExportDeclaration", "Parsing");
Expand Down Expand Up @@ -97,9 +101,10 @@ where

let next = cursor.peek(0, interner).or_abrupt()?;

if let TokenKind::IdentifierName((Sym::FROM, ContainsEscapeSequence(false))) =
next.kind()
{
if matches!(
next.kind(),
TokenKind::IdentifierName((Sym::FROM, ContainsEscapeSequence(false)))
) {
let from = FromClause::new("export declaration").parse(cursor, interner)?;
boa_ast::declaration::ExportDeclaration::List {
list,
Expand All @@ -118,17 +123,48 @@ where
let tok = cursor.peek(0, interner).or_abrupt()?;

match tok.kind() {
TokenKind::Keyword((Keyword::Class, _)) => {
ClassDeclaration::new(false, true, true)
.parse(cursor, interner)
.map(boa_ast::declaration::ExportDeclaration::DefaultClassDeclaration)?
TokenKind::Keyword((Keyword::Function, false)) => {
let next_token = cursor.peek(1, interner).or_abrupt()?;
if next_token.kind() == &TokenKind::Punctuator(Punctuator::Mul) {
AstExportDeclaration::DefaultGenerator(
GeneratorDeclaration::new(false, true, true)
.parse(cursor, interner)?,
)
} else {
AstExportDeclaration::DefaultFunction(
FunctionDeclaration::new(false, true, true)
.parse(cursor, interner)?,
)
}
}
TokenKind::Keyword((Keyword::Async, false)) => {
let next_token = cursor.peek(2, interner).or_abrupt()?;
if next_token.kind() == &TokenKind::Punctuator(Punctuator::Mul) {
AstExportDeclaration::DefaultAsyncGenerator(
AsyncGeneratorDeclaration::new(false, true, true)
.parse(cursor, interner)?,
)
} else {
AstExportDeclaration::DefaultAsyncFunction(
AsyncFunctionDeclaration::new(false, true, true)
.parse(cursor, interner)?,
)
}
}
TokenKind::Keyword((Keyword::Class, false)) => {
AstExportDeclaration::DefaultClassDeclaration(
ClassDeclaration::new(false, true, true).parse(cursor, interner)?,
)
}
_ => todo!("default export parsing"),
_ => AstExportDeclaration::DefaultAssignmentExpression(
AssignmentExpression::new(None, true, false, true)
.parse(cursor, interner)?,
),
}
}
_ => {
todo!()
}
_ => AstExportDeclaration::Declaration(
Declaration::new(false, true).parse(cursor, interner)?,
),
};

Ok(export_clause)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ use std::io::Read;
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function
/// [spec]: https://tc39.es/ecma262/#prod-AsyncFunctionDeclaration
#[derive(Debug, Clone, Copy)]
pub(super) struct AsyncFunctionDeclaration {
pub(in crate::parser) struct AsyncFunctionDeclaration {
allow_yield: AllowYield,
allow_await: AllowAwait,
is_default: AllowDefault,
}

impl AsyncFunctionDeclaration {
/// Creates a new `FunctionDeclaration` parser.
pub(super) fn new<Y, A, D>(allow_yield: Y, allow_await: A, is_default: D) -> Self
pub(in crate::parser) fn new<Y, A, D>(allow_yield: Y, allow_await: A, is_default: D) -> Self
where
Y: Into<AllowYield>,
A: Into<AllowAwait>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ use std::io::Read;
///
/// [spec]: https://tc39.es/ecma262/#prod-AsyncGeneratorDeclaration
#[derive(Debug, Clone, Copy)]
pub(super) struct AsyncGeneratorDeclaration {
pub(in crate::parser) struct AsyncGeneratorDeclaration {
allow_yield: AllowYield,
allow_await: AllowAwait,
is_default: AllowDefault,
}

impl AsyncGeneratorDeclaration {
/// Creates a new `AsyncGeneratorDeclaration` parser.
pub(super) fn new<Y, A, D>(allow_yield: Y, allow_await: A, is_default: D) -> Self
pub(in crate::parser) fn new<Y, A, D>(allow_yield: Y, allow_await: A, is_default: D) -> Self
where
Y: Into<AllowYield>,
A: Into<AllowAwait>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ use std::io::Read;
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function*
/// [spec]: https://tc39.es/ecma262/#prod-GeneratorDeclaration
#[derive(Debug, Clone, Copy)]
pub(super) struct GeneratorDeclaration {
pub(in crate::parser) struct GeneratorDeclaration {
allow_yield: AllowYield,
allow_await: AllowAwait,
is_default: AllowDefault,
}

impl GeneratorDeclaration {
/// Creates a new `GeneratorDeclaration` parser.
pub(super) fn new<Y, A, D>(allow_yield: Y, allow_await: A, is_default: D) -> Self
pub(in crate::parser) fn new<Y, A, D>(allow_yield: Y, allow_await: A, is_default: D) -> Self
where
Y: Into<AllowYield>,
A: Into<AllowAwait>,
Expand Down
6 changes: 2 additions & 4 deletions boa_parser/src/parser/statement/declaration/hoistable/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@ mod generator_decl;

pub(crate) mod class_decl;

use self::{
async_function_decl::AsyncFunctionDeclaration, async_generator_decl::AsyncGeneratorDeclaration,
generator_decl::GeneratorDeclaration,
};
use crate::{
lexer::TokenKind,
parser::{
Expand All @@ -41,7 +37,9 @@ use boa_profiler::Profiler;
use std::io::Read;

pub(in crate::parser) use self::{
async_function_decl::AsyncFunctionDeclaration, async_generator_decl::AsyncGeneratorDeclaration,
class_decl::ClassDeclaration, function_decl::FunctionDeclaration,
generator_decl::GeneratorDeclaration,
};

/// Hoistable declaration parsing.
Expand Down

0 comments on commit f0979cf

Please sign in to comment.