Type expr parse refactor

This commit is contained in:
2026-05-04 17:45:23 +02:00
parent d263f1a41f
commit 0e84e1ca3d
2 changed files with 38 additions and 8 deletions
+7 -4
View File
@@ -17,12 +17,15 @@ pub enum TypePostfix {
} }
#[derive(Debug, Clone, Serialize)] #[derive(Debug, Clone, Serialize)]
pub enum TypeExpr { pub enum TypeExprKind {
Path(StaticPath, Vec<TypePostfix>), Path(StaticPath),
PathParams(StaticPath, Vec<TypeExpr>, Vec<TypePostfix>), PathParams(StaticPath, Vec<TypeExpr>),
Tuple(Vec<TypeExpr>, Vec<TypePostfix>), Tuple(Vec<TypeExpr>),
} }
#[derive(Debug, Clone, Serialize)]
pub struct TypeExpr(pub TypeExprKind, pub Vec<TypePostfix>);
#[derive(Debug, Clone, Serialize)] #[derive(Debug, Clone, Serialize)]
pub struct StaticPath(pub Vec<String>); pub struct StaticPath(pub Vec<String>);
+31 -4
View File
@@ -29,17 +29,44 @@ pub fn parse(source: &str) -> Result<Vec<TopLevel>, ParseError> {
impl From<pest::iterators::Pair<'_, Rule>> for TypeExpr { impl From<pest::iterators::Pair<'_, Rule>> for TypeExpr {
fn from(pair: pest::iterators::Pair<'_, Rule>) -> Self { fn from(pair: pest::iterators::Pair<'_, Rule>) -> Self {
match pair.as_rule() { match pair.as_rule() {
Rule::type_expr => TypeExpr::from(pair.into_inner().next().unwrap()), Rule::type_expr => {
Rule::tuple_type => TypeExpr::Tuple(pair.into_inner().map(TypeExpr::from).collect()), let mut inner = pair.into_inner();
TypeExpr(
TypeExprKind::from(inner.next().unwrap()),
inner.map(TypePostfix::from).collect(),
)
}
_ => unimplemented!("{pair:#?}"),
}
}
}
impl From<pest::iterators::Pair<'_, Rule>> 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<pest::iterators::Pair<'_, Rule>> 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 => { Rule::path_type => {
let mut inner = pair.into_inner(); let mut inner = pair.into_inner();
let path = StaticPath::from(inner.next().unwrap()); let path = StaticPath::from(inner.next().unwrap());
let params = inner.map(TypeExpr::from).collect::<Vec<_>>(); let params = inner.map(TypeExpr::from).collect::<Vec<_>>();
if params.len() == 0 { if params.len() == 0 {
TypeExpr::Path(path) TypeExprKind::Path(path)
} else { } else {
TypeExpr::PathParams(path, params) TypeExprKind::PathParams(path, params)
} }
} }
_ => unimplemented!("{pair:#?}"), _ => unimplemented!("{pair:#?}"),