Enums and impl decl

This commit is contained in:
2026-05-14 21:44:00 +02:00
parent 00e10d1097
commit 5912032dff
4 changed files with 90 additions and 77 deletions
-77
View File
@@ -91,83 +91,6 @@ impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Generics {
} }
} }
impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for ImplDecl {
type Error = ParseError<'a>;
fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result<Self, Self::Error> {
let rule = pair.as_rule();
let mut inner = pair.clone().into_inner();
match rule {
Rule::impl_for_decl => Ok(ImplDecl {
generics: consume_rule(&mut inner, Rule::generics)
.map(Generics::try_from)
.transpose()?
.unwrap_or_default(),
trait_: Some(TypeExpr::try_from(inner.next().unwrap())?),
target: TypeExpr::try_from(inner.next().unwrap())?,
methods: inner
.map(FunctionDecl::try_from)
.collect::<ParseResult<'a, Vec<_>>>()?,
}),
Rule::impl_decl => Ok(ImplDecl {
generics: consume_rule(&mut inner, Rule::generics)
.map(Generics::try_from)
.transpose()?
.unwrap_or_default(),
trait_: None,
target: TypeExpr::try_from(inner.next().unwrap())?,
methods: inner
.map(FunctionDecl::try_from)
.collect::<ParseResult<'a, Vec<_>>>()?,
}),
_ => unimplemented!("{rule:#?}"),
}
}
}
impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for EnumItem {
type Error = ParseError<'a>;
fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result<Self, Self::Error> {
let rule = pair.as_rule();
let mut inner = pair.clone().into_inner();
match rule {
Rule::enum_named => Ok(EnumItem::Named(Identifier::try_from(
inner.next().unwrap(),
)?)),
Rule::enum_tuple => Ok(EnumItem::Tuple(
Identifier::try_from(inner.next().unwrap())?,
inner
.next()
.unwrap()
.into_inner()
.map(TypeExpr::try_from)
.collect::<ParseResult<'a, Vec<_>>>()?,
)),
Rule::enum_struct => Ok(EnumItem::Struct(
Identifier::try_from(inner.next().unwrap())?,
inner
.next()
.map(|pair| {
pair.into_inner()
.map(FieldDecl::try_from)
.collect::<ParseResult<'a, Vec<_>>>()
})
.transpose()?
.unwrap_or_default(),
)),
_ => unimplemented!("{rule:#?}"),
}
}
}
impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Block { impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for Block {
type Error = ParseError<'a>; type Error = ParseError<'a>;
+45
View File
@@ -0,0 +1,45 @@
use crate::{
Rule,
ast::*,
error::{ParseError, ParseResult},
};
impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for EnumItem {
type Error = ParseError<'a>;
fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result<Self, Self::Error> {
let rule = pair.as_rule();
let mut inner = pair.clone().into_inner();
match rule {
Rule::enum_named => Ok(EnumItem::Named(Identifier::try_from(
inner.next().unwrap(),
)?)),
Rule::enum_tuple => Ok(EnumItem::Tuple(
Identifier::try_from(inner.next().unwrap())?,
inner
.next()
.unwrap()
.into_inner()
.map(TypeExpr::try_from)
.collect::<ParseResult<'a, Vec<_>>>()?,
)),
Rule::enum_struct => Ok(EnumItem::Struct(
Identifier::try_from(inner.next().unwrap())?,
inner
.next()
.map(|pair| {
pair.into_inner()
.map(FieldDecl::try_from)
.collect::<ParseResult<'a, Vec<_>>>()
})
.transpose()?
.unwrap_or_default(),
)),
_ => unimplemented!("{rule:#?}"),
}
}
}
+43
View File
@@ -0,0 +1,43 @@
use crate::{
Rule,
ast::*,
error::{ParseError, ParseResult},
parser::consume_rule,
};
impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for ImplDecl {
type Error = ParseError<'a>;
fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result<Self, Self::Error> {
let rule = pair.as_rule();
let mut inner = pair.clone().into_inner();
match rule {
Rule::impl_for_decl => Ok(ImplDecl {
generics: consume_rule(&mut inner, Rule::generics)
.map(Generics::try_from)
.transpose()?
.unwrap_or_default(),
trait_: Some(TypeExpr::try_from(inner.next().unwrap())?),
target: TypeExpr::try_from(inner.next().unwrap())?,
methods: inner
.map(FunctionDecl::try_from)
.collect::<ParseResult<'a, Vec<_>>>()?,
}),
Rule::impl_decl => Ok(ImplDecl {
generics: consume_rule(&mut inner, Rule::generics)
.map(Generics::try_from)
.transpose()?
.unwrap_or_default(),
trait_: None,
target: TypeExpr::try_from(inner.next().unwrap())?,
methods: inner
.map(FunctionDecl::try_from)
.collect::<ParseResult<'a, Vec<_>>>()?,
}),
_ => unimplemented!("{rule:#?}"),
}
}
}
+2
View File
@@ -1,6 +1,8 @@
pub mod attribute; pub mod attribute;
pub mod class; pub mod class;
pub mod enums;
pub mod function; pub mod function;
pub mod impl_decl;
use crate::{ use crate::{
Rule, Rule,