From 0e84e1ca3d4b2a190468838559bbec1c2c5c864b Mon Sep 17 00:00:00 2001 From: Selimaj Dev Date: Mon, 4 May 2026 17:45:23 +0200 Subject: [PATCH] Type expr parse refactor --- parser/src/ast.rs | 11 +++++++---- parser/src/lib.rs | 35 +++++++++++++++++++++++++++++++---- 2 files changed, 38 insertions(+), 8 deletions(-) diff --git a/parser/src/ast.rs b/parser/src/ast.rs index 90f4375..fabb675 100644 --- a/parser/src/ast.rs +++ b/parser/src/ast.rs @@ -17,12 +17,15 @@ pub enum TypePostfix { } #[derive(Debug, Clone, Serialize)] -pub enum TypeExpr { - Path(StaticPath, Vec), - PathParams(StaticPath, Vec, Vec), - Tuple(Vec, Vec), +pub enum TypeExprKind { + Path(StaticPath), + PathParams(StaticPath, Vec), + Tuple(Vec), } +#[derive(Debug, Clone, Serialize)] +pub struct TypeExpr(pub TypeExprKind, pub Vec); + #[derive(Debug, Clone, Serialize)] pub struct StaticPath(pub Vec); diff --git a/parser/src/lib.rs b/parser/src/lib.rs index 72cea59..aa07769 100644 --- a/parser/src/lib.rs +++ b/parser/src/lib.rs @@ -29,17 +29,44 @@ pub fn parse(source: &str) -> Result, ParseError> { impl From> for TypeExpr { fn from(pair: pest::iterators::Pair<'_, Rule>) -> Self { match pair.as_rule() { - Rule::type_expr => TypeExpr::from(pair.into_inner().next().unwrap()), - Rule::tuple_type => TypeExpr::Tuple(pair.into_inner().map(TypeExpr::from).collect()), + Rule::type_expr => { + let mut inner = pair.into_inner(); + TypeExpr( + TypeExprKind::from(inner.next().unwrap()), + inner.map(TypePostfix::from).collect(), + ) + } + _ => unimplemented!("{pair:#?}"), + } + } +} + +impl From> for TypePostfix { + fn from(pair: pest::iterators::Pair<'_, Rule>) -> Self { + match pair.as_rule() { + Rule::ref_type => match pair.as_str().trim() { + _ => Self::Ref, + }, + _ => unimplemented!("{pair:#?}"), + } + } +} + +impl From> for TypeExprKind { + fn from(pair: pest::iterators::Pair<'_, Rule>) -> Self { + match pair.as_rule() { + Rule::tuple_type => { + TypeExprKind::Tuple(pair.into_inner().map(TypeExpr::from).collect()) + } Rule::path_type => { let mut inner = pair.into_inner(); let path = StaticPath::from(inner.next().unwrap()); let params = inner.map(TypeExpr::from).collect::>(); if params.len() == 0 { - TypeExpr::Path(path) + TypeExprKind::Path(path) } else { - TypeExpr::PathParams(path, params) + TypeExprKind::PathParams(path, params) } } _ => unimplemented!("{pair:#?}"),