From b802c5136946f42eb589a884a747f64ba2236787 Mon Sep 17 00:00:00 2001 From: Klesti Selimaj Date: Sat, 16 May 2026 22:53:08 +0200 Subject: [PATCH] updated expr grammar --- parser/src/grammar.pest | 212 +++++++++++++++++----------------------- 1 file changed, 91 insertions(+), 121 deletions(-) diff --git a/parser/src/grammar.pest b/parser/src/grammar.pest index 64fb96b..8c611b6 100644 --- a/parser/src/grammar.pest +++ b/parser/src/grammar.pest @@ -51,6 +51,7 @@ program = { visibility = { "pub" ~ ("(" ~ static_path ~ ")")? } mutable = { "mut" } var = { "var" } +deref_px = { "*" } // ====================================================== // ERROR RECOVERY @@ -417,137 +418,106 @@ match_stmt = { } // ====================================================== -// EXPRESSIONS +// EXPRESSIONS (Nested by Precedence, Low to High) // ====================================================== -// ------------------------------------------------------ -// TUPLES -// ------------------------------------------------------ +// Base entry point for a pure expression +expr = { logical_or_expr } -tuple = { - "(" ~ (expr ~ ("," ~ expr)*)? ~ ")" +// 1. Logical OR +logical_or_expr = { logical_and_expr ~ ("||" ~ logical_and_expr)* } + +// 2. Logical AND +logical_and_expr = { bit_or_expr ~ ("&&" ~ bit_or_expr)* } + +// 3. Bitwise OR +bit_or_expr = { bit_xor_expr ~ ("|" ~ bit_xor_expr)* } + +// 4. Bitwise XOR +bit_xor_expr = { bit_and_expr ~ ("^" ~ bit_and_expr)* } + +// 5. Bitwise AND +bit_and_expr = { comparison_expr ~ ("&" ~ comparison_expr)* } + +// 6. Comparisons +comparison_expr = { bshift_expr ~ (comp_op ~ bshift_expr)? } +comp_op = { "==" | "!=" | "<=" | ">=" | "<" | ">" } + +// 7. Bitwise Shifts +bshift_expr = { add_sub_expr ~ (shift_op ~ add_sub_expr)* } +shift_op = { "<<" | ">>" } + +// 8. Addition and Subtraction +add_sub_expr = { mul_div_mod_expr ~ (add_sub_op ~ mul_div_mod_expr)* } +add_sub_op = { "+" | "-" } + +// 9. Multiplication, Division, and Remainder +mul_div_mod_expr = { cast_expr ~ (mul_div_mod_op ~ cast_expr)* } +mul_div_mod_op = { "*" | "/" | "%" } + +// 10. Type Casting (as) +cast_expr = { range_expr ~ ("as" ~ type_expr)* } + +// 11. Ranges (e.g., start..end, start.., ..end, ..) +range_expr = { + (unary_expr? ~ ".." ~ "="? ~ unary_expr?) + | unary_expr } +// 12. Unary / Prefix Operators (Right-associative recursion) +unary_expr = { + (unary_op ~ unary_expr) + | postfix_expr +} +unary_op = { "-" | "!" | "*" | "&" ~ "mut"? } + +// 13. Postfix Operators (Method calls, indexing, fields, try, macro calls) +postfix_expr = { primary_expr ~ postfix_suffix* } +postfix_suffix = { + call_suffix + | macro_call_suffix + | index_suffix + | field_suffix + | try_suffix + | struct_suffix +} + +// Postfix Sub-rules +call_suffix = { "(" ~ (expr ~ ("," ~ expr)*)? ~ ","? ~ ")" } +macro_call_suffix = { "!" ~ "(" ~ macro_inner_v* ~ ")" } +index_suffix = { "[" ~ expr ~ "]" } +field_suffix = { "." ~ (identifier | tuple_index) } +try_suffix = { "?" } +struct_suffix = { "{" ~ (struct_field_decl ~ ("," ~ struct_field_decl)*)? ~ ","? ~ "}" } + // ------------------------------------------------------ -// MACROS +// PRIMARY EXPRESSIONS & ATOMS // ------------------------------------------------------ +primary_expr = { + literal + | macro_rules_call // Standalone macro like println!(...) + + | static_path // Variables, paths + + | group_or_tuple // (expr) or (a, b) + + | array_expr // [1, 2, 3] +} + +group_or_tuple = { "(" ~ (expr ~ ("," ~ expr)*)? ~ ","? ~ ")" } +array_expr = { "[" ~ (expr ~ ("," ~ expr)*)? ~ ","? ~ "]" } + +struct_field_decl = { identifier ~ ":" ~ expr } + macro_inner_v = { "(" ~ macro_inner_v* ~ ")" - | (!("(" | ")") ~ ANY) + | "[" ~ macro_inner_v* ~ "]" + | "{" ~ macro_inner_v* ~ "}" + | (!("(" | ")" | "[" | "]" | "{" | "}") ~ ANY) } -// ------------------------------------------------------ -// PRIMARY EXPRESSIONS -// ------------------------------------------------------ +macro_rules_call = { static_path ~ "!" ~ (("(" ~ macro_inner_v* ~ ")") | ("{" ~ macro_inner_v* ~ "}")) } -primary = { - literal - | static_path - | tuple -} - -// ------------------------------------------------------ -// POSTFIX EXPRESSIONS -// ------------------------------------------------------ - -field_px = { - "." ~ identifier -} - -call_px = { - "(" ~ (expr ~ ("," ~ expr)*)? ~ ")" -} - -macro_call_px = { - "!" ~ "(" ~ macro_inner_v* ~ ")" -} - -struct_field_decl = { - identifier ~ ":" ~ expr -} - -struct_px = { - "{" ~ (struct_field_decl ~ ("," ~ struct_field_decl)*)? ~ ","? ~ "}" -} - -index_px = { - "[" ~ expr ~ "]" -} - -binary_px = { - bin_op ~ expr -} - -postfix = { - field_px - | call_px - | macro_call_px - | struct_px - | index_px - | binary_px -} - -// ------------------------------------------------------ -// PREFIX EXPRESSIONS -// ------------------------------------------------------ - -deref_px = { - "*" -} - -mut_ref_px = { - "&" ~ mutable -} - -ref_px = { - "&" -} - -new_px = { - "new" -} - -not_px = { - "!" -} - -prefix = { - deref_px - | mut_ref_px - | ref_px - | new_px - | not_px -} - -prefix_list = { - prefix* -} - -// ------------------------------------------------------ -// OPERATORS -// ------------------------------------------------------ - -bin_op = { - "<=" - | ">=" - | "==" - | "!=" - | "&&" - | "||" - | "+" - | "-" - | "*" - | "/" - | "%" - | "<" - | ">" -} - -// ------------------------------------------------------ -// EXPRESSIONS -// ------------------------------------------------------ - -expr = { - prefix_list ~ primary ~ postfix* -} +// (Assumed rules from your codebase) +tuple_index = { ASCII_DIGIT+ }