From 0d2199ee3a8078ff205de7d7803e3fbfa446d0a9 Mon Sep 17 00:00:00 2001 From: Klesti Selimaj Date: Thu, 9 Jul 2026 06:23:04 +0200 Subject: [PATCH] virtual keyword parsing and ast --- crates/codegen/src/class_decl.rs | 1 + crates/parser/src/ast/top_level.rs | 6 +++--- crates/parser/src/grammar.pest | 5 ++++- crates/parser/src/parser/items/function.rs | 5 ++++- 4 files changed, 12 insertions(+), 5 deletions(-) diff --git a/crates/codegen/src/class_decl.rs b/crates/codegen/src/class_decl.rs index 0c205ce..e17dbdb 100644 --- a/crates/codegen/src/class_decl.rs +++ b/crates/codegen/src/class_decl.rs @@ -380,6 +380,7 @@ impl ClassProcessedData { column: constructor.column, item: FunctionDecl { visibility: constructor.item.visibility.clone(), + is_virtual: false, is_override: None, name: Identifier(String::from("constructor")), generics: constructor.item.generics.clone(), diff --git a/crates/parser/src/ast/top_level.rs b/crates/parser/src/ast/top_level.rs index 2e95aed..fd8427e 100644 --- a/crates/parser/src/ast/top_level.rs +++ b/crates/parser/src/ast/top_level.rs @@ -107,12 +107,13 @@ pub struct Override(pub Option); #[derive(Debug, Clone, Serialize)] pub struct FunctionDecl { pub visibility: Visibility, - pub is_override: Option, + pub is_virtual: bool, + pub return_type: Option, pub name: Identifier, pub generics: GenericsDecl, pub self_param: Option<(bool, Option, bool)>, pub params: ParamList, - pub return_type: Option, + pub is_override: Option, pub body: Option, } @@ -129,7 +130,6 @@ pub struct FieldDecl { pub visibility: Visibility, pub type_: TypeExpr, pub name: Identifier, - // pub tuple_names: Vec, } #[derive(Debug, Clone, Serialize)] diff --git a/crates/parser/src/grammar.pest b/crates/parser/src/grammar.pest index 24b188b..a51c7e2 100644 --- a/crates/parser/src/grammar.pest +++ b/crates/parser/src/grammar.pest @@ -36,6 +36,7 @@ keyword = { | "override" | "const" | "type" + | "virtual" } identifier = @{ @@ -373,10 +374,12 @@ trait_decl = { // FUNCTIONS // ====================================================== +virtual_kw = { "virtual" } + override_kw = { "override" ~ ("(" ~ expr_path ~ ")")? } function_decl = { - visibility? ~ ("void" | type_expr) ~ identifier ~ generics_decl? ~ "(" ~ method_args ~ ")" ~ override_kw? ~ (block | semicolon) + visibility? ~ virtual_kw? ~ ("void" | type_expr) ~ identifier ~ generics_decl? ~ "(" ~ method_args ~ ")" ~ override_kw? ~ (block | semicolon) } // ====================================================== diff --git a/crates/parser/src/parser/items/function.rs b/crates/parser/src/parser/items/function.rs index 7c7ac46..25ea082 100644 --- a/crates/parser/src/parser/items/function.rs +++ b/crates/parser/src/parser/items/function.rs @@ -14,6 +14,8 @@ impl<'a> TryFrom> for FunctionDecl { let mut inner = pair.into_inner(); let visibility = Visibility::try_from(&mut inner)?; + let is_virtual = listen_rule(&mut inner, Rule::virtual_kw); + let return_type = consume_rule(&mut inner, Rule::type_expr) .map(TypeExpr::try_from) .transpose()?; @@ -45,12 +47,13 @@ impl<'a> TryFrom> for FunctionDecl { Ok(Self { visibility: visibility, - is_override: is_override, + is_virtual: is_virtual, return_type: return_type, name: name, generics: generics, self_param: self_param, params: params, + is_override: is_override, body: body, }) })