#[cfg(test)]
mod tests;
use crate::syntax::{
ast::{
node::{self},
Punctuator, TokenKind,
},
parser::{
expression::Initializer,
statement::{BindingIdentifier, StatementList},
AllowAwait, AllowYield, Cursor, ParseError, TokenParser,
},
};
#[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 TokenParser for FormalParameters {
type Output = Box<[node::FormalParameter]>;
fn parse(self, cursor: &mut Cursor<'_>) -> Result<Self::Output, ParseError> {
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;
params.push(if cursor.next_if(Punctuator::Spread).is_some() {
rest_param = true;
FunctionRestParameter::new(self.allow_yield, self.allow_await).parse(cursor)?
} else {
FormalParameter::new(self.allow_yield, self.allow_await).parse(cursor)?
});
if cursor.peek(0).ok_or(ParseError::AbruptEnd)?.kind
== TokenKind::Punctuator(Punctuator::CloseParen)
{
break;
}
if rest_param {
return Err(ParseError::unexpected(
cursor
.peek_prev()
.expect("current token disappeared")
.clone(),
"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 TokenParser for BindingRestElement {
type Output = node::FormalParameter;
fn parse(self, cursor: &mut Cursor<'_>) -> Result<Self::Output, ParseError> {
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 TokenParser for FormalParameter {
type Output = node::FormalParameter;
fn parse(self, cursor: &mut Cursor<'_>) -> Result<Self::Output, ParseError> {
let param = BindingIdentifier::new(self.allow_yield, self.allow_await).parse(cursor)?;
let init = Initializer::new(true, self.allow_yield, self.allow_await).try_parse(cursor);
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 TokenParser for FunctionStatementList {
type Output = node::StatementList;
fn parse(self, cursor: &mut Cursor<'_>) -> Result<Self::Output, ParseError> {
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)
}
}