Support CastExpressions in the parser

This commit is contained in:
Pranav Gaddamadugu 2023-06-23 00:24:01 -04:00
parent 72c6dcd343
commit 9677682580

View File

@ -221,13 +221,21 @@ impl ParserContext<'_> {
/// Returns an [`Expression`] AST node if the next tokens represent a
/// binary exponentiation expression.
///
/// Otherwise, tries to parse the next token using [`parse_unary_expression`].
/// Otherwise, tries to parse the next token using [`parse_cast_expression`].
fn parse_exponential_expression(&mut self) -> Result<Expression> {
let mut expr = self.parse_unary_expression()?;
self.parse_bin_expr(&[Token::Pow], Self::parse_cast_expression)
}
if let Some(op) = self.eat_bin_op(&[Token::Pow]) {
let right = self.parse_exponential_expression()?;
expr = Self::bin_expr(expr, right, op);
/// Returns an [`Expression`] AST node if the next tokens represent a
/// cast expression.
///
/// Otherwise, tries to parse the next token using [`parse_unary_expression`].
fn parse_cast_expression(&mut self) -> Result<Expression> {
let mut expr = self.parse_unary_expression()?;
if self.eat(&Token::As) {
let (type_, end_span) = self.parse_primitive_type()?;
let span = expr.span() + end_span;
expr = Expression::Cast(CastExpression { expression: Box::new(expr), type_, span });
}
Ok(expr)