#[cfg(test)]
mod tests;
use crate::{
syntax::{
ast::{
node::{ArrayDecl, Node, Spread},
Const, Punctuator,
},
parser::{
expression::AssignmentExpression, AllowAwait, AllowYield, Cursor, ParseError,
TokenParser,
},
},
BoaProfiler,
};
use std::io::Read;
#[derive(Debug, Clone, Copy)]
pub(super) struct ArrayLiteral {
allow_yield: AllowYield,
allow_await: AllowAwait,
}
impl ArrayLiteral {
pub(super) 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<R> TokenParser<R> for ArrayLiteral
where
R: Read,
{
type Output = ArrayDecl;
fn parse(self, cursor: &mut Cursor<R>) -> Result<Self::Output, ParseError> {
let _timer = BoaProfiler::global().start_event("ArrayLiteral", "Parsing");
let mut elements = Vec::new();
loop {
while cursor.next_if(Punctuator::Comma)?.is_some() {
elements.push(Node::Const(Const::Undefined));
}
if cursor.next_if(Punctuator::CloseBracket)?.is_some() {
break;
}
let _ = cursor.peek(0)?.ok_or(ParseError::AbruptEnd);
if cursor.next_if(Punctuator::Spread)?.is_some() {
let node = AssignmentExpression::new(true, self.allow_yield, self.allow_await)
.parse(cursor)?;
elements.push(Spread::new(node).into());
} else {
elements.push(
AssignmentExpression::new(true, self.allow_yield, self.allow_await)
.parse(cursor)?,
);
}
cursor.next_if(Punctuator::Comma)?;
}
Ok(elements.into())
}
}