generics parsing and AST

This commit is contained in:
2026-05-08 16:48:31 +02:00
parent 630528a843
commit 205e2d9fd3
3 changed files with 41 additions and 13 deletions
+4
View File
@@ -81,6 +81,7 @@ pub enum TopLevelKind {
},
StructDecl {
visibility: Visibility,
generics: Generics,
name: Identifier,
fields: FieldList,
},
@@ -94,6 +95,9 @@ pub enum TopLevelKind {
},
}
#[derive(Debug, Clone, Serialize)]
pub struct Generics(pub Vec<(Identifier, Vec<Path>)>);
#[derive(Debug, Clone, Serialize)]
pub enum Pattern {
NamedTuple(Path, Vec<Identifier>),
+7 -1
View File
@@ -52,6 +52,12 @@ export = { "public" }
mutable = { "mut" }
var = { "var" }
// ======================================================
// GENERICS
// ======================================================
generic = { identifier ~ (":" ~ static_path ~ ("+" ~ static_path)*)? }
generics = { "<" ~ (generic ~ ("," ~ generic)*)? ~ ">" }
// ======================================================
// TYPES
// ======================================================
@@ -168,7 +174,7 @@ mod_package = {
// ======================================================
struct_decl = {
export? ~ "struct" ~ identifier ~ "{" ~ field_list? ~ "}"
export? ~ "struct" ~ identifier ~ generics? ~ "{" ~ field_list? ~ "}"
}
// ======================================================
+30 -12
View File
@@ -223,6 +223,28 @@ impl From<pest::iterators::Pair<'_, Rule>> for ClassConstructor {
}
}
impl From<pest::iterators::Pair<'_, Rule>> for Generics {
fn from(pair: pest::iterators::Pair<'_, Rule>) -> Self {
let rule = pair.as_rule();
let inner = pair.clone().into_inner();
match rule {
Rule::generics => Generics(
inner
.map(|pair| {
let mut inner = pair.into_inner();
(
Identifier::from(inner.next().unwrap()),
inner.map(Path::from).collect(),
)
})
.collect(),
),
_ => unimplemented!("{rule:#?}"),
}
}
}
impl From<pest::iterators::Pair<'_, Rule>> for TopLevelKind {
fn from(pair: pest::iterators::Pair<'_, Rule>) -> Self {
let rule = pair.as_rule();
@@ -233,18 +255,14 @@ impl From<pest::iterators::Pair<'_, Rule>> for TopLevelKind {
Rule::function_decl => TopLevelKind::FunctionDecl(FunctionDecl::from(pair)),
Rule::struct_decl => {
let visibility = Visibility::from(&mut inner);
let name = Identifier::from(inner.next().unwrap());
let fields_pair = inner.next().unwrap();
let fields = FieldList::from(fields_pair);
TopLevelKind::StructDecl {
visibility,
name,
fields,
}
}
Rule::struct_decl => TopLevelKind::StructDecl {
visibility: Visibility::from(&mut inner),
name: Identifier::from(inner.next().unwrap()),
generics: consume_rule(&mut inner, Rule::generics)
.map(Generics::from)
.unwrap_or(Generics(Vec::new())),
fields: FieldList::from(inner.next().unwrap()),
},
Rule::class_decl => TopLevelKind::ClassDecl {
visibility: Visibility::from(&mut inner),