use self::{
integer_literal::{IntegerLiteral, IntegerLiteralParsingError},
parens::{ParensExpression, ParensParsingError},
};
use crate::parser::{ast::{AstGeneratorContext, AstNode}, fragment::Fragment};
pub mod integer_literal;
pub mod parens;
#[derive(Debug)]
pub enum PrimaryExpression<'src> {
IntegerLiteral(IntegerLiteral<'src>),
ParensExpression(ParensExpression<'src>),
}
#[derive(Clone, Debug)]
pub enum PrimaryExpressionParsingError<'src> {
ExpectedPrimaryExpression {
at: Fragment<'src>,
},
OtherIntegerLiteralParsingError(IntegerLiteralParsingError<'src>),
OtherParensExpressionParsingError(ParensParsingError<'src>),
}
#[rustfmt::skip] impl<'src> AstNode<'src> for PrimaryExpression<'src> {
type Error = PrimaryExpressionParsingError<'src>;
fn fragment(&self) -> Fragment<'src> {
match self {
PrimaryExpression::IntegerLiteral(integer_literal) => integer_literal.fragment(),
PrimaryExpression::ParensExpression(parens_expr) => parens_expr.fragment(),
}
}
fn try_parse(ctx: &mut AstGeneratorContext<'src>) -> Result<Self, Self::Error>
where
Self: Sized,
{
match IntegerLiteral::try_parse(ctx) {
Ok(int_lit) => return Ok(PrimaryExpression::IntegerLiteral(int_lit)),
Err(num_err @ IntegerLiteralParsingError::NumParsingError { .. }) => {
return Err(PrimaryExpressionParsingError::OtherIntegerLiteralParsingError(
num_err,
));
}
Err(IntegerLiteralParsingError::ExpectedIntegerLiteral { .. }) => {}
}
match ParensExpression::try_parse(ctx) {
Ok(parens_expr) => return Ok(PrimaryExpression::ParensExpression(parens_expr)),
Err(err @ (
| ParensParsingError::ClosingParenNotFound { .. }
| ParensParsingError::ErrorInParentheses { .. }
)) => return Err(PrimaryExpressionParsingError::OtherParensExpressionParsingError(err)),
Err(ParensParsingError::ExpectedParensExpression { .. }) => {}
}
Err(PrimaryExpressionParsingError::ExpectedPrimaryExpression {
at: ctx.peek_fragment(),
})
}
}