From 23cf00975da586152e3b23d3901a0e3169e8af22 Mon Sep 17 00:00:00 2001 From: Klesti Selimaj Date: Sun, 17 May 2026 03:25:02 +0200 Subject: [PATCH] Updated grammar for more extensive expressions --- parser/src/grammar.pest | 93 +++++++++++++++++++++++++++++------------ 1 file changed, 66 insertions(+), 27 deletions(-) diff --git a/parser/src/grammar.pest b/parser/src/grammar.pest index 00cb2da..df95248 100644 --- a/parser/src/grammar.pest +++ b/parser/src/grammar.pest @@ -421,13 +421,19 @@ match_stmt = { // ====================================================== // ------------------------------------------------------ -// TUPLES +// TUPLES & ARRAYS // ------------------------------------------------------ tuple = { "(" ~ (expr ~ ("," ~ expr)*)? ~ ")" } +array = { + "[" ~ expr ~ ";" ~ expr ~ "]" // Repeat notation: [0; 10] + + | "[" ~ (expr ~ ("," ~ expr)* ~ ","?)? ~ "]" // Standard elements initialization +} + // ------------------------------------------------------ // MACROS // ------------------------------------------------------ @@ -441,10 +447,15 @@ macro_inner_v = { // PRIMARY EXPRESSIONS // ------------------------------------------------------ +expr_path = { + identifier ~ ("::" ~ generics)? ~ ("::" ~ identifier ~ ("::" ~ generics)?)* +} + primary = { literal - | static_path + | expr_path | tuple + | array } // ------------------------------------------------------ @@ -452,7 +463,7 @@ primary = { // ------------------------------------------------------ field_px = { - "." ~ identifier + "." ~ identifier ~ ("::" ~ generics)? } call_px = { @@ -475,23 +486,36 @@ index_px = { "[" ~ expr ~ "]" } +as_px = { + "as" ~ type_expr +} + +range_end_inc_px = { "..=" } +range_end_exc_px = { ".." } + postfix = { field_px | call_px | macro_call_px | struct_px | index_px + | as_px + | range_end_inc_px + | range_end_exc_px } // ------------------------------------------------------ // PREFIX EXPRESSIONS // ------------------------------------------------------ -deref_px = { "*" } -mut_ref_px = { "&" ~ mutable } -ref_px = { "&" } -new_px = { "new" } -not_px = { "!" } +deref_px = { "*" } +mut_ref_px = { "&" ~ mutable } +ref_px = { "&" } +new_px = { "new" } +not_px = { "!" } +neg_px = { "-" } +range_start_inc_px = { "..=" } +range_start_exc_px = { ".." } prefix = { deref_px @@ -499,48 +523,63 @@ prefix = { | ref_px | new_px | not_px + | neg_px + | range_start_inc_px + | range_start_exc_px } // ------------------------------------------------------ -// OPERATORS (Named individually for Pratt Parser configuration) +// OPERATORS (Arranged carefully for PEG priority matching) // ------------------------------------------------------ -lte = { "<=" } -gte = { ">=" } -eq = { "==" } -neq = { "!=" } -and = { "&&" } -or = { "||" } -add = { "+" } -sub = { "-" } -mul = { "*" } -div = { "/" } -rem = { "%" } -lt = { "<" } -gt = { ">" } +shl = { "<<" } +shr = { ">>" } +range_inc = { "..=" } +range_exc = { ".." } +lte = { "<=" } +gte = { ">=" } +eq = { "==" } +neq = { "!=" } +and = { "&&" } +or = { "||" } +add = { "+" } +sub = { "-" } +mul = { "*" } +div = { "/" } +rem = { "%" } +lt = { "<" } +gt = { ">" } +bitand = { "&" } +bitor = { "|" } +bitxor = { "^" } bin_op = _{ - lte + shl + | shr + | range_inc + | range_exc + | lte | gte | eq | neq | and | or + | lt + | gt | add | sub | mul | div | rem - | lt - | gt + | bitand + | bitor + | bitxor } // ------------------------------------------------------ // EXPRESSIONS // ------------------------------------------------------ -// Top-level entry point for expressions expr = { term ~ (bin_op ~ term)* } -// A term handles your Prefix and Postfix combinations around a core Primary term = { prefix* ~ primary ~ postfix* }