use crate::{
syntax::{
ast::{node::FunctionExpr, Punctuator},
parser::{
function::{FormalParameters, FunctionBody},
statement::BindingIdentifier,
Cursor, ParseError, TokenParser,
},
},
BoaProfiler,
};
#[derive(Debug, Clone, Copy)]
pub(super) struct FunctionExpression;
impl TokenParser for FunctionExpression {
type Output = FunctionExpr;
fn parse(self, cursor: &mut Cursor<'_>) -> Result<Self::Output, ParseError> {
let _timer = BoaProfiler::global().start_event("FunctionExpression", "Parsing");
let name = BindingIdentifier::new(false, false).try_parse(cursor);
cursor.expect(Punctuator::OpenParen, "function expression")?;
let params = FormalParameters::new(false, false).parse(cursor)?;
cursor.expect(Punctuator::CloseParen, "function expression")?;
cursor.expect(Punctuator::OpenBlock, "function expression")?;
let body = FunctionBody::new(false, false).parse(cursor)?;
cursor.expect(Punctuator::CloseBlock, "function expression")?;
Ok(FunctionExpr::new(name, params, body))
}
}