Updated grammar for more extensive expressions

This commit is contained in:
2026-05-17 03:25:02 +02:00
parent 2c67d4e316
commit 23cf00975d
+66 -27
View File
@@ -421,13 +421,19 @@ match_stmt = {
// ====================================================== // ======================================================
// ------------------------------------------------------ // ------------------------------------------------------
// TUPLES // TUPLES & ARRAYS
// ------------------------------------------------------ // ------------------------------------------------------
tuple = { tuple = {
"(" ~ (expr ~ ("," ~ expr)*)? ~ ")" "(" ~ (expr ~ ("," ~ expr)*)? ~ ")"
} }
array = {
"[" ~ expr ~ ";" ~ expr ~ "]" // Repeat notation: [0; 10]
| "[" ~ (expr ~ ("," ~ expr)* ~ ","?)? ~ "]" // Standard elements initialization
}
// ------------------------------------------------------ // ------------------------------------------------------
// MACROS // MACROS
// ------------------------------------------------------ // ------------------------------------------------------
@@ -441,10 +447,15 @@ macro_inner_v = {
// PRIMARY EXPRESSIONS // PRIMARY EXPRESSIONS
// ------------------------------------------------------ // ------------------------------------------------------
expr_path = {
identifier ~ ("::" ~ generics)? ~ ("::" ~ identifier ~ ("::" ~ generics)?)*
}
primary = { primary = {
literal literal
| static_path | expr_path
| tuple | tuple
| array
} }
// ------------------------------------------------------ // ------------------------------------------------------
@@ -452,7 +463,7 @@ primary = {
// ------------------------------------------------------ // ------------------------------------------------------
field_px = { field_px = {
"." ~ identifier "." ~ identifier ~ ("::" ~ generics)?
} }
call_px = { call_px = {
@@ -475,23 +486,36 @@ index_px = {
"[" ~ expr ~ "]" "[" ~ expr ~ "]"
} }
as_px = {
"as" ~ type_expr
}
range_end_inc_px = { "..=" }
range_end_exc_px = { ".." }
postfix = { postfix = {
field_px field_px
| call_px | call_px
| macro_call_px | macro_call_px
| struct_px | struct_px
| index_px | index_px
| as_px
| range_end_inc_px
| range_end_exc_px
} }
// ------------------------------------------------------ // ------------------------------------------------------
// PREFIX EXPRESSIONS // PREFIX EXPRESSIONS
// ------------------------------------------------------ // ------------------------------------------------------
deref_px = { "*" } deref_px = { "*" }
mut_ref_px = { "&" ~ mutable } mut_ref_px = { "&" ~ mutable }
ref_px = { "&" } ref_px = { "&" }
new_px = { "new" } new_px = { "new" }
not_px = { "!" } not_px = { "!" }
neg_px = { "-" }
range_start_inc_px = { "..=" }
range_start_exc_px = { ".." }
prefix = { prefix = {
deref_px deref_px
@@ -499,48 +523,63 @@ prefix = {
| ref_px | ref_px
| new_px | new_px
| not_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 = { "<=" } shl = { "<<" }
gte = { ">=" } shr = { ">>" }
eq = { "==" } range_inc = { "..=" }
neq = { "!=" } range_exc = { ".." }
and = { "&&" } lte = { "<=" }
or = { "||" } gte = { ">=" }
add = { "+" } eq = { "==" }
sub = { "-" } neq = { "!=" }
mul = { "*" } and = { "&&" }
div = { "/" } or = { "||" }
rem = { "%" } add = { "+" }
lt = { "<" } sub = { "-" }
gt = { ">" } mul = { "*" }
div = { "/" }
rem = { "%" }
lt = { "<" }
gt = { ">" }
bitand = { "&" }
bitor = { "|" }
bitxor = { "^" }
bin_op = _{ bin_op = _{
lte shl
| shr
| range_inc
| range_exc
| lte
| gte | gte
| eq | eq
| neq | neq
| and | and
| or | or
| lt
| gt
| add | add
| sub | sub
| mul | mul
| div | div
| rem | rem
| lt | bitand
| gt | bitor
| bitxor
} }
// ------------------------------------------------------ // ------------------------------------------------------
// EXPRESSIONS // EXPRESSIONS
// ------------------------------------------------------ // ------------------------------------------------------
// Top-level entry point for expressions
expr = { term ~ (bin_op ~ term)* } expr = { term ~ (bin_op ~ term)* }
// A term handles your Prefix and Postfix combinations around a core Primary
term = { prefix* ~ primary ~ postfix* } term = { prefix* ~ primary ~ postfix* }