#[cfg(test)]
mod tests;
use super::StatementList;
use crate::syntax::lexer::TokenKind;
use crate::{
profiler::BoaProfiler,
syntax::{
ast::{node, Punctuator},
parser::{AllowAwait, AllowReturn, AllowYield, Cursor, ParseError, TokenParser},
},
};
use std::io::Read;
pub(super) type BlockStatement = Block;
#[derive(Debug, Clone, Copy)]
pub(super) struct Block {
allow_yield: AllowYield,
allow_await: AllowAwait,
allow_return: AllowReturn,
}
impl Block {
pub(super) fn new<Y, A, R>(allow_yield: Y, allow_await: A, allow_return: R) -> Self
where
Y: Into<AllowYield>,
A: Into<AllowAwait>,
R: Into<AllowReturn>,
{
Self {
allow_yield: allow_yield.into(),
allow_await: allow_await.into(),
allow_return: allow_return.into(),
}
}
}
impl<R> TokenParser<R> for Block
where
R: Read,
{
type Output = node::Block;
fn parse(self, cursor: &mut Cursor<R>) -> Result<Self::Output, ParseError> {
let _timer = BoaProfiler::global().start_event("Block", "Parsing");
cursor.expect(Punctuator::OpenBlock, "block")?;
if let Some(tk) = cursor.peek(0)? {
if tk.kind() == &TokenKind::Punctuator(Punctuator::CloseBlock) {
cursor.next()?.expect("} token vanished");
return Ok(node::Block::from(vec![]));
}
}
let statement_list =
StatementList::new(self.allow_yield, self.allow_await, self.allow_return, true)
.parse(cursor)
.map(node::Block::from)?;
cursor.expect(Punctuator::CloseBlock, "block")?;
Ok(statement_list)
}
}