Browse Source

[parser] folder structure for VariableStatement and ExpressionStatement (#489)

pull/490/head
croraf 4 years ago committed by GitHub
parent
commit
d042ddda3f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 48
      boa/src/syntax/parser/statement/expression/mod.rs
  2. 47
      boa/src/syntax/parser/statement/mod.rs
  3. 0
      boa/src/syntax/parser/statement/variable/mod.rs

48
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<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
boa/src/syntax/parser/statement/mod.rs

@ -11,6 +11,7 @@ mod block;
mod break_stm; mod break_stm;
mod continue_stm; mod continue_stm;
mod declaration; mod declaration;
mod expression;
mod if_stm; mod if_stm;
mod iteration; mod iteration;
mod return_stm; mod return_stm;
@ -24,6 +25,7 @@ use self::{
break_stm::BreakStatement, break_stm::BreakStatement,
continue_stm::ContinueStatement, continue_stm::ContinueStatement,
declaration::Declaration, declaration::Declaration,
expression::ExpressionStatement,
if_stm::IfStatement, if_stm::IfStatement,
iteration::{DoWhileStatement, ForStatement, WhileStatement}, iteration::{DoWhileStatement, ForStatement, WhileStatement},
return_stm::ReturnStatement, return_stm::ReturnStatement,
@ -32,10 +34,7 @@ use self::{
try_stm::TryStatement, try_stm::TryStatement,
variable::VariableStatement, variable::VariableStatement,
}; };
use super::{ use super::{AllowAwait, AllowReturn, AllowYield, Cursor, ParseError, TokenParser};
expression::Expression, AllowAwait, AllowReturn, AllowYield, Cursor, ParseError, ParseResult,
TokenParser,
};
use crate::{ use crate::{
syntax::ast::{node, Keyword, Node, Punctuator, TokenKind}, syntax::ast::{node, Keyword, Node, Punctuator, TokenKind},
BoaProfiler, 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<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. /// Label identifier parsing.
/// ///
/// This seems to be the same as a `BindingIdentifier`. /// This seems to be the same as a `BindingIdentifier`.

0
boa/src/syntax/parser/statement/variable.rs → boa/src/syntax/parser/statement/variable/mod.rs

Loading…
Cancel
Save