From d042ddda3fb5239293b28db8383bc54991c039ee Mon Sep 17 00:00:00 2001 From: croraf Date: Sat, 13 Jun 2020 22:29:30 +0200 Subject: [PATCH] [parser] folder structure for VariableStatement and ExpressionStatement (#489) --- .../syntax/parser/statement/expression/mod.rs | 48 +++++++++++++++++++ boa/src/syntax/parser/statement/mod.rs | 47 ++---------------- .../{variable.rs => variable/mod.rs} | 0 3 files changed, 51 insertions(+), 44 deletions(-) create mode 100644 boa/src/syntax/parser/statement/expression/mod.rs rename boa/src/syntax/parser/statement/{variable.rs => variable/mod.rs} (100%) diff --git a/boa/src/syntax/parser/statement/expression/mod.rs b/boa/src/syntax/parser/statement/expression/mod.rs new file mode 100644 index 0000000000..1306e5b3d4 --- /dev/null +++ b/boa/src/syntax/parser/statement/expression/mod.rs @@ -0,0 +1,48 @@ +use super::super::{expression::Expression, ParseResult}; +use crate::{ + syntax::{ + ast::node::Node, + parser::{AllowAwait, AllowYield, Cursor, TokenParser}, + }, + BoaProfiler, +}; + +/// Expression statement parsing. +/// +/// More information: +/// - [ECMAScript specification][spec] +/// +/// [spec]: https://tc39.es/ecma262/#prod-ExpressionStatement +#[derive(Debug, Clone, Copy)] +pub(in crate::syntax::parser::statement) struct ExpressionStatement { + allow_yield: AllowYield, + allow_await: AllowAwait, +} + +impl ExpressionStatement { + /// Creates a new `ExpressionStatement` parser. + pub(in crate::syntax::parser::statement) fn new(allow_yield: Y, allow_await: A) -> Self + where + Y: Into, + A: Into, + { + Self { + allow_yield: allow_yield.into(), + allow_await: allow_await.into(), + } + } +} + +impl TokenParser for ExpressionStatement { + type Output = Node; + + fn parse(self, cursor: &mut Cursor<'_>) -> ParseResult { + let _timer = BoaProfiler::global().start_event("ExpressionStatement", "Parsing"); + // TODO: lookahead + let expr = Expression::new(true, self.allow_yield, self.allow_await).parse(cursor)?; + + cursor.expect_semicolon(false, "expression statement")?; + + Ok(expr) + } +} diff --git a/boa/src/syntax/parser/statement/mod.rs b/boa/src/syntax/parser/statement/mod.rs index 461ab4c672..274871fd43 100644 --- a/boa/src/syntax/parser/statement/mod.rs +++ b/boa/src/syntax/parser/statement/mod.rs @@ -11,6 +11,7 @@ mod block; mod break_stm; mod continue_stm; mod declaration; +mod expression; mod if_stm; mod iteration; mod return_stm; @@ -24,6 +25,7 @@ use self::{ break_stm::BreakStatement, continue_stm::ContinueStatement, declaration::Declaration, + expression::ExpressionStatement, if_stm::IfStatement, iteration::{DoWhileStatement, ForStatement, WhileStatement}, return_stm::ReturnStatement, @@ -32,10 +34,7 @@ use self::{ try_stm::TryStatement, variable::VariableStatement, }; -use super::{ - expression::Expression, AllowAwait, AllowReturn, AllowYield, Cursor, ParseError, ParseResult, - TokenParser, -}; +use super::{AllowAwait, AllowReturn, AllowYield, Cursor, ParseError, TokenParser}; use crate::{ syntax::ast::{node, Keyword, Node, Punctuator, TokenKind}, BoaProfiler, @@ -343,46 +342,6 @@ impl TokenParser for StatementListItem { } } -/// Expression statement parsing. -/// -/// More information: -/// - [ECMAScript specification][spec] -/// -/// [spec]: https://tc39.es/ecma262/#prod-ExpressionStatement -#[derive(Debug, Clone, Copy)] -struct ExpressionStatement { - allow_yield: AllowYield, - allow_await: AllowAwait, -} - -impl ExpressionStatement { - /// Creates a new `ExpressionStatement` parser. - fn new(allow_yield: Y, allow_await: A) -> Self - where - Y: Into, - A: Into, - { - Self { - allow_yield: allow_yield.into(), - allow_await: allow_await.into(), - } - } -} - -impl TokenParser for ExpressionStatement { - type Output = Node; - - fn parse(self, cursor: &mut Cursor<'_>) -> ParseResult { - let _timer = BoaProfiler::global().start_event("ExpressionStatement", "Parsing"); - // TODO: lookahead - let expr = Expression::new(true, self.allow_yield, self.allow_await).parse(cursor)?; - - cursor.expect_semicolon(false, "expression statement")?; - - Ok(expr) - } -} - /// Label identifier parsing. /// /// This seems to be the same as a `BindingIdentifier`. diff --git a/boa/src/syntax/parser/statement/variable.rs b/boa/src/syntax/parser/statement/variable/mod.rs similarity index 100% rename from boa/src/syntax/parser/statement/variable.rs rename to boa/src/syntax/parser/statement/variable/mod.rs