#[cfg(test)]
mod tests;
use crate::{
syntax::{
ast::{
node::{self},
Punctuator,
},
lexer::{InputElement, TokenKind},
parser::{
expression::Initializer,
statement::{BindingIdentifier, StatementList},
AllowAwait, AllowYield, Cursor, ParseError, TokenParser,
},
},
BoaProfiler,
};
use std::io::Read;
#[derive(Debug, Clone, Copy)]
pub(in crate::syntax::parser) struct FormalParameters {
allow_yield: AllowYield,
allow_await: AllowAwait,
}
impl FormalParameters {
pub(in crate::syntax::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<R> TokenParser<R> for FormalParameters
where
R: Read,
{
type Output = Box<[node::FormalParameter]>;
fn parse(self, cursor: &mut Cursor<R>) -> Result<Self::Output, ParseError> {
let _timer = BoaProfiler::global().start_event("FormalParameters", "Parsing");
cursor.set_goal(InputElement::RegExp);
let mut params = Vec::new();
if cursor.peek(0)?.ok_or(ParseError::AbruptEnd)?.kind()
== &TokenKind::Punctuator(Punctuator::CloseParen)
{
return Ok(params.into_boxed_slice());
}
loop {
let mut rest_param = false;
let next_param = match cursor.peek(0)? {
Some(tok) if tok.kind() == &TokenKind::Punctuator(Punctuator::Spread) => {
rest_param = true;
FunctionRestParameter::new(self.allow_yield, self.allow_await).parse(cursor)?
}
_ => FormalParameter::new(self.allow_yield, self.allow_await).parse(cursor)?,
};
params.push(next_param);
if cursor.peek(0)?.ok_or(ParseError::AbruptEnd)?.kind()
== &TokenKind::Punctuator(Punctuator::CloseParen)
{
break;
}
if rest_param {
return Err(ParseError::unexpected(
cursor.next()?.expect("peeked token disappeared"),
"rest parameter must be the last formal parameter",
));
}
cursor.expect(Punctuator::Comma, "parameter list")?;
}
Ok(params.into_boxed_slice())
}
}
type FunctionRestParameter = BindingRestElement;
#[derive(Debug, Clone, Copy)]
struct BindingRestElement {
allow_yield: AllowYield,
allow_await: AllowAwait,
}
impl BindingRestElement {
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 BindingRestElement
where
R: Read,
{
type Output = node::FormalParameter;
fn parse(self, cursor: &mut Cursor<R>) -> Result<Self::Output, ParseError> {
let _timer = BoaProfiler::global().start_event("BindingRestElement", "Parsing");
cursor.expect(Punctuator::Spread, "rest parameter")?;
let param = BindingIdentifier::new(self.allow_yield, self.allow_await).parse(cursor)?;
Ok(Self::Output::new(param, None, true))
}
}
#[derive(Debug, Clone, Copy)]
struct FormalParameter {
allow_yield: AllowYield,
allow_await: AllowAwait,
}
impl FormalParameter {
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 FormalParameter
where
R: Read,
{
type Output = node::FormalParameter;
fn parse(self, cursor: &mut Cursor<R>) -> Result<Self::Output, ParseError> {
let _timer = BoaProfiler::global().start_event("FormalParameter", "Parsing");
let param = BindingIdentifier::new(self.allow_yield, self.allow_await).parse(cursor)?;
let init = if let Some(t) = cursor.peek(0)? {
if *t.kind() == TokenKind::Punctuator(Punctuator::Assign) {
Some(Initializer::new(true, self.allow_yield, self.allow_await).parse(cursor)?)
} else {
None
}
} else {
None
};
Ok(Self::Output::new(param, init, false))
}
}
pub(in crate::syntax::parser) type FunctionBody = FunctionStatementList;
#[derive(Debug, Clone, Copy)]
pub(in crate::syntax::parser) struct FunctionStatementList {
allow_yield: AllowYield,
allow_await: AllowAwait,
}
impl FunctionStatementList {
pub(in crate::syntax::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<R> TokenParser<R> for FunctionStatementList
where
R: Read,
{
type Output = node::StatementList;
fn parse(self, cursor: &mut Cursor<R>) -> Result<Self::Output, ParseError> {
let _timer = BoaProfiler::global().start_event("FunctionStatementList", "Parsing");
if let Some(tk) = cursor.peek(0)? {
if tk.kind() == &Punctuator::CloseBlock.into() {
return Ok(Vec::new().into());
}
}
StatementList::new(self.allow_yield, self.allow_await, true, true).parse(cursor)
}
}