use crate::{
Error,
parser::{
AllowAwait, AllowReturn, AllowYield, Cursor, OrAbrupt, ParseResult, TokenParser,
expression::Expression, statement::Statement,
},
source::ReadChar,
};
use boa_ast::{Keyword, Punctuator, Spanned, statement::WhileLoop};
use boa_interner::Interner;
#[derive(Debug, Clone, Copy)]
pub(in crate::parser::statement) struct WhileStatement {
allow_yield: AllowYield,
allow_await: AllowAwait,
allow_return: AllowReturn,
}
impl WhileStatement {
pub(in crate::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 WhileStatement
where
R: ReadChar,
{
type Output = WhileLoop;
fn parse(self, cursor: &mut Cursor<R>, interner: &mut Interner) -> ParseResult<Self::Output> {
cursor.expect((Keyword::While, false), "while statement", interner)?;
cursor.expect(Punctuator::OpenParen, "while statement", interner)?;
let cond =
Expression::new(true, self.allow_yield, self.allow_await).parse(cursor, interner)?;
cursor.expect(Punctuator::CloseParen, "while statement", interner)?;
let position = cursor.peek(0, interner).or_abrupt()?.span().start();
let body = Statement::new(self.allow_yield, self.allow_await, self.allow_return)
.parse(cursor, interner)?;
if body.is_labelled_function() {
return Err(Error::wrong_labelled_function_declaration(position));
}
Ok(WhileLoop::new(cond, body))
}
}