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 { StructDecl {
visibility: Visibility, visibility: Visibility,
generics: Generics,
name: Identifier, name: Identifier,
fields: FieldList, 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)] #[derive(Debug, Clone, Serialize)]
pub enum Pattern { pub enum Pattern {
NamedTuple(Path, Vec<Identifier>), NamedTuple(Path, Vec<Identifier>),
+7 -1
View File
@@ -52,6 +52,12 @@ export = { "public" }
mutable = { "mut" } mutable = { "mut" }
var = { "var" } var = { "var" }
// ======================================================
// GENERICS
// ======================================================
generic = { identifier ~ (":" ~ static_path ~ ("+" ~ static_path)*)? }
generics = { "<" ~ (generic ~ ("," ~ generic)*)? ~ ">" }
// ====================================================== // ======================================================
// TYPES // TYPES
// ====================================================== // ======================================================
@@ -168,7 +174,7 @@ mod_package = {
// ====================================================== // ======================================================
struct_decl = { 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 { impl From<pest::iterators::Pair<'_, Rule>> for TopLevelKind {
fn from(pair: pest::iterators::Pair<'_, Rule>) -> Self { fn from(pair: pest::iterators::Pair<'_, Rule>) -> Self {
let rule = pair.as_rule(); 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::function_decl => TopLevelKind::FunctionDecl(FunctionDecl::from(pair)),
Rule::struct_decl => { Rule::struct_decl => TopLevelKind::StructDecl {
let visibility = Visibility::from(&mut inner); visibility: Visibility::from(&mut inner),
let name = Identifier::from(inner.next().unwrap()); name: Identifier::from(inner.next().unwrap()),
let fields_pair = inner.next().unwrap(); generics: consume_rule(&mut inner, Rule::generics)
let fields = FieldList::from(fields_pair); .map(Generics::from)
.unwrap_or(Generics(Vec::new())),
TopLevelKind::StructDecl { fields: FieldList::from(inner.next().unwrap()),
visibility, },
name,
fields,
}
}
Rule::class_decl => TopLevelKind::ClassDecl { Rule::class_decl => TopLevelKind::ClassDecl {
visibility: Visibility::from(&mut inner), visibility: Visibility::from(&mut inner),