From 8faf6635898cdba95257bdd993e08e4ca11bcce5 Mon Sep 17 00:00:00 2001 From: Selimaj Dev Date: Mon, 4 May 2026 16:39:55 +0200 Subject: [PATCH] Implementing static path --- parser/src/ast.rs | 3 +++ parser/src/grammar.pest | 17 +++++++++-------- parser/src/lib.rs | 17 +++++++++++++++++ test/src/main.mist | 4 +--- 4 files changed, 30 insertions(+), 11 deletions(-) diff --git a/parser/src/ast.rs b/parser/src/ast.rs index 83637e7..53fc170 100644 --- a/parser/src/ast.rs +++ b/parser/src/ast.rs @@ -16,6 +16,9 @@ pub enum TypeExpr { Identifier(String), } +#[derive(Debug, Clone, Serialize)] +pub struct StaticPath(pub Vec); + #[derive(Debug, Clone, Serialize)] pub enum BinaryOp { Plus, diff --git a/parser/src/grammar.pest b/parser/src/grammar.pest index c08f422..84191f6 100644 --- a/parser/src/grammar.pest +++ b/parser/src/grammar.pest @@ -4,12 +4,13 @@ COMMENT = _{ "//" ~ (!"\n" ~ ANY)* } program = { SOI ~ top_level* ~ EOI } // primitives -identifier = @{ (ASCII_ALPHA | "_") ~ (ASCII_ALPHANUMERIC | "_")* } -integer = @{ ASCII_DIGIT+ } -float = @{ ASCII_DIGIT+ ~ "." ~ ASCII_DIGIT+ } -boolean = { "true" | "false" } -string_lit = { "\"" ~ inner_str ~ "\"" } -inner_str = @{ (!"\"" ~ ANY)* } +identifier = @{ (ASCII_ALPHA | "_") ~ (ASCII_ALPHANUMERIC | "_")* } +integer = @{ ASCII_DIGIT+ } +float = @{ ASCII_DIGIT+ ~ "." ~ ASCII_DIGIT+ } +boolean = { "true" | "false" } +string_lit = { "\"" ~ inner_str ~ "\"" } +inner_str = @{ (!"\"" ~ ANY)* } +static_path = { identifier ~ ("::" ~ identifier)* } export = { "public" } mutable = { "mut" } @@ -72,14 +73,14 @@ while_stmt = { } // ================= EXPRESSIONS ================= -type_expr = { identifier } +type_expr = { static_path } primary = { integer | float | boolean | string_lit - | identifier + | static_path } postfix = { diff --git a/parser/src/lib.rs b/parser/src/lib.rs index 3ef52ed..d260193 100644 --- a/parser/src/lib.rs +++ b/parser/src/lib.rs @@ -45,6 +45,23 @@ impl TryFrom> for TypeExpr { } } +impl TryFrom> for StaticPath { + type Error = (); + + fn try_from(pair: pest::iterators::Pair<'_, Rule>) -> Result { + if pair.as_str() == "void" { + return Err(()); + } + + Ok(match pair.as_rule() { + Rule::static_path => { + StaticPath(pair.into_inner().map(|i| i.as_str().to_string()).collect()) + } + _ => unimplemented!("{pair:#?}"), + }) + } +} + impl From> for FieldList { fn from(pair: pest::iterators::Pair) -> Self { let params = pair diff --git a/test/src/main.mist b/test/src/main.mist index 1aa1923..42f1d16 100644 --- a/test/src/main.mist +++ b/test/src/main.mist @@ -1,3 +1 @@ -int test(int mut b) { - int mut a = 10; -} +int test() {}