mirror of https://github.com/boa-dev/boa.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
102 lines
3.6 KiB
102 lines
3.6 KiB
//! Call expression parsing. |
|
//! |
|
//! More information: |
|
//! - [MDN documentation][mdn] |
|
//! - [ECMAScript specification][spec] |
|
//! |
|
//! [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Functions |
|
//! [spec]: https://tc39.es/ecma262/#prod-CallExpression |
|
|
|
use super::arguments::Arguments; |
|
use crate::syntax::{ |
|
ast::{node::Node, punc::Punctuator, token::TokenKind}, |
|
parser::{ |
|
expression::Expression, AllowAwait, AllowYield, Cursor, ParseError, ParseResult, |
|
TokenParser, |
|
}, |
|
}; |
|
|
|
/// Parses a call expression. |
|
/// |
|
/// More information: |
|
/// - [ECMAScript specification][spec] |
|
/// |
|
/// [spec]: https://tc39.es/ecma262/#prod-CallExpression |
|
#[derive(Debug)] |
|
pub(super) struct CallExpression { |
|
allow_yield: AllowYield, |
|
allow_await: AllowAwait, |
|
first_member_expr: Node, |
|
} |
|
|
|
impl CallExpression { |
|
/// Creates a new `CallExpression` parser. |
|
pub(super) fn new<Y, A>(allow_yield: Y, allow_await: A, first_member_expr: Node) -> Self |
|
where |
|
Y: Into<AllowYield>, |
|
A: Into<AllowAwait>, |
|
{ |
|
Self { |
|
allow_yield: allow_yield.into(), |
|
allow_await: allow_await.into(), |
|
first_member_expr, |
|
} |
|
} |
|
} |
|
|
|
impl TokenParser for CallExpression { |
|
type Output = Node; |
|
|
|
fn parse(self, cursor: &mut Cursor<'_>) -> ParseResult { |
|
let mut lhs = match cursor.peek(0) { |
|
Some(tk) if tk.kind == TokenKind::Punctuator(Punctuator::OpenParen) => { |
|
let args = Arguments::new(self.allow_yield, self.allow_await).parse(cursor)?; |
|
Node::call(self.first_member_expr, args) |
|
} |
|
_ => { |
|
let next_token = cursor.next().ok_or(ParseError::AbruptEnd)?; |
|
return Err(ParseError::Expected( |
|
vec![TokenKind::Punctuator(Punctuator::OpenParen)], |
|
next_token.clone(), |
|
"call expression", |
|
)); |
|
} |
|
}; |
|
|
|
while let Some(tok) = cursor.peek(0) { |
|
match tok.kind { |
|
TokenKind::Punctuator(Punctuator::OpenParen) => { |
|
let args = Arguments::new(self.allow_yield, self.allow_await).parse(cursor)?; |
|
lhs = Node::call(lhs, args); |
|
} |
|
TokenKind::Punctuator(Punctuator::Dot) => { |
|
let _ = cursor.next().ok_or(ParseError::AbruptEnd)?; // We move the cursor. |
|
match &cursor.next().ok_or(ParseError::AbruptEnd)?.kind { |
|
TokenKind::Identifier(name) => { |
|
lhs = Node::get_const_field(lhs, name); |
|
} |
|
TokenKind::Keyword(kw) => { |
|
lhs = Node::get_const_field(lhs, kw.to_string()); |
|
} |
|
_ => { |
|
return Err(ParseError::Expected( |
|
vec![TokenKind::identifier("identifier")], |
|
tok.clone(), |
|
"call expression", |
|
)); |
|
} |
|
} |
|
} |
|
TokenKind::Punctuator(Punctuator::OpenBracket) => { |
|
let _ = cursor.next().ok_or(ParseError::AbruptEnd)?; // We move the cursor. |
|
let idx = |
|
Expression::new(true, self.allow_yield, self.allow_await).parse(cursor)?; |
|
cursor.expect(Punctuator::CloseBracket, "call expression")?; |
|
lhs = Node::get_field(lhs, idx); |
|
} |
|
_ => break, |
|
} |
|
} |
|
Ok(lhs) |
|
} |
|
}
|
|
|