Skip to content

Commit

Permalink
[parser] folder structure for VariableStatement and ExpressionStateme…
Browse files Browse the repository at this point in the history
…nt (#489)
  • Loading branch information
croraf authored Jun 13, 2020
1 parent 542b2cc commit d042ddd
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 44 deletions.
48 changes: 48 additions & 0 deletions boa/src/syntax/parser/statement/expression/mod.rs
Original file line number Diff line number Diff line change
@@ -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<Y, A>(allow_yield: Y, allow_await: A) -> Self
where
Y: Into<AllowYield>,
A: Into<AllowAwait>,
{
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)
}
}
47 changes: 3 additions & 44 deletions boa/src/syntax/parser/statement/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ mod block;
mod break_stm;
mod continue_stm;
mod declaration;
mod expression;
mod if_stm;
mod iteration;
mod return_stm;
Expand All @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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<Y, A>(allow_yield: Y, allow_await: A) -> Self
where
Y: Into<AllowYield>,
A: Into<AllowAwait>,
{
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`.
Expand Down
File renamed without changes.

0 comments on commit d042ddd

Please sign in to comment.