use crate::syntax::lexer::TokenKind;
use crate::{
syntax::{
ast::{node::DoWhileLoop, Keyword, Punctuator},
parser::{
expression::Expression, statement::Statement, AllowAwait, AllowReturn, AllowYield,
Cursor, ParseError, TokenParser,
},
},
BoaProfiler,
};
use std::io::Read;
#[derive(Debug, Clone, Copy)]
pub(in crate::syntax::parser::statement) struct DoWhileStatement {
allow_yield: AllowYield,
allow_await: AllowAwait,
allow_return: AllowReturn,
}
impl DoWhileStatement {
pub(in crate::syntax::parser::statement) 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 DoWhileStatement
where
R: Read,
{
type Output = DoWhileLoop;
fn parse(self, cursor: &mut Cursor<R>) -> Result<Self::Output, ParseError> {
let _timer = BoaProfiler::global().start_event("DoWhileStatement", "Parsing");
cursor.expect(Keyword::Do, "do while statement")?;
let body =
Statement::new(self.allow_yield, self.allow_await, self.allow_return).parse(cursor)?;
let next_token = cursor.peek(0)?.ok_or(ParseError::AbruptEnd)?;
if next_token.kind() != &TokenKind::Keyword(Keyword::While) {
return Err(ParseError::expected(
vec![TokenKind::Keyword(Keyword::While)],
next_token.clone(),
"do while statement",
));
}
cursor.expect(Keyword::While, "do while statement")?;
cursor.expect(Punctuator::OpenParen, "do while statement")?;
let cond = Expression::new(true, self.allow_yield, self.allow_await).parse(cursor)?;
cursor.expect(Punctuator::CloseParen, "do while statement")?;
if let Some(tok) = cursor.peek(0)? {
if let TokenKind::Punctuator(Punctuator::Semicolon) = *tok.kind() {
cursor.next()?;
}
}
Ok(DoWhileLoop::new(body, cond))
}
}