use crate::{
profiler::BoaProfiler,
syntax::{
ast::{
node::{self, Node},
op::UnaryOp,
Keyword, Punctuator,
},
lexer::TokenKind,
parser::{
expression::update::UpdateExpression, AllowAwait, AllowYield, Cursor, ParseError,
ParseResult, TokenParser,
},
},
};
use std::io::Read;
#[derive(Debug, Clone, Copy)]
pub(super) struct UnaryExpression {
allow_yield: AllowYield,
allow_await: AllowAwait,
}
impl UnaryExpression {
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 UnaryExpression
where
R: Read,
{
type Output = Node;
fn parse(self, cursor: &mut Cursor<R>) -> ParseResult {
let _timer = BoaProfiler::global().start_event("UnaryExpression", "Parsing");
let tok = cursor.peek(0)?.ok_or(ParseError::AbruptEnd)?;
match tok.kind() {
TokenKind::Keyword(Keyword::Delete) => {
cursor.next()?.expect("Delete keyword vanished"); Ok(node::UnaryOp::new(UnaryOp::Delete, self.parse(cursor)?).into())
}
TokenKind::Keyword(Keyword::Void) => {
cursor.next()?.expect("Void keyword vanished"); Ok(node::UnaryOp::new(UnaryOp::Void, self.parse(cursor)?).into())
}
TokenKind::Keyword(Keyword::TypeOf) => {
cursor.next()?.expect("TypeOf keyword vanished"); Ok(node::UnaryOp::new(UnaryOp::TypeOf, self.parse(cursor)?).into())
}
TokenKind::Punctuator(Punctuator::Add) => {
cursor.next()?.expect("+ token vanished"); Ok(node::UnaryOp::new(UnaryOp::Plus, self.parse(cursor)?).into())
}
TokenKind::Punctuator(Punctuator::Sub) => {
cursor.next()?.expect("- token vanished"); Ok(node::UnaryOp::new(UnaryOp::Minus, self.parse(cursor)?).into())
}
TokenKind::Punctuator(Punctuator::Neg) => {
cursor.next()?.expect("~ token vanished"); Ok(node::UnaryOp::new(UnaryOp::Tilde, self.parse(cursor)?).into())
}
TokenKind::Punctuator(Punctuator::Not) => {
cursor.next()?.expect("! token vanished"); Ok(node::UnaryOp::new(UnaryOp::Not, self.parse(cursor)?).into())
}
_ => UpdateExpression::new(self.allow_yield, self.allow_await).parse(cursor),
}
}
}