Files
mist/parser/src/grammar.pest
T
2026-05-16 22:53:08 +02:00

524 lines
11 KiB
Plaintext

// ======================================================
// LEXICAL
// ======================================================
WHITESPACE = _{ " " | "\t" | "\r" | "\n" }
COMMENT = _{ "//" ~ (!"\n" ~ ANY)* }
// ------------------------------------------------------
// PRIMITIVES
// ------------------------------------------------------
identifier = @{ (ASCII_ALPHA | "_") ~ (ASCII_ALPHANUMERIC | "_")* }
integer = @{ ASCII_DIGIT+ }
float = @{
ASCII_DIGIT+ ~ "." ~ ASCII_DIGIT+
}
boolean = {
"true"
| "false"
}
string_lit = {
"\"" ~ inner_str ~ "\""
}
inner_str = @{
(!"\"" ~ ("\\\"" | ANY))*
}
semicolon = _{ ";" }
static_path = {
identifier ~ ("::" ~ identifier)*
}
// ======================================================
// PROGRAM
// ======================================================
program = {
SOI ~ top_level* ~ EOI
}
// ======================================================
// MODIFIERS
// ======================================================
visibility = { "pub" ~ ("(" ~ static_path ~ ")")? }
mutable = { "mut" }
var = { "var" }
deref_px = { "*" }
// ======================================================
// ERROR RECOVERY
// ======================================================
unexpected_statement = {
(!(semicolon | "}" | statement) ~ ANY)+
}
// ======================================================
// GENERICS
// ======================================================
generic = { lifetime | (identifier ~ (":" ~ type_expr_param ~ ("+" ~ type_expr_param)*)?) }
generics = { "<" ~ (generic ~ ("," ~ generic)*)? ~ ">" }
// ======================================================
// TYPES
// ======================================================
tuple_type = {
"(" ~ (type_expr ~ ("," ~ type_expr)*)? ~ ")"
}
path_type = {
static_path ~ ("<" ~ (type_expr_param ~ ("," ~ type_expr_param)*)? ~ ">")?
}
ref_type = {
mutable? ~ lifetime? ~ "*"
}
type_expr = {
(path_type | tuple_type) ~ ref_type*
}
lifetime = {
"'" ~ identifier
}
type_expr_param = {
type_expr
| lifetime
}
// ======================================================
// LITERALS
// ======================================================
literal = {
float
| integer
| boolean
| string_lit
}
// ======================================================
// PATTERNS
// ======================================================
tuple_pattern = {
"(" ~ (identifier ~ ("," ~ identifier)*)? ~ ")"
}
named_tuple_pattern = {
static_path ~ "(" ~ (identifier ~ ("," ~ identifier)*)? ~ ")"
}
struct_pattern = {
static_path ~ "{" ~ (identifier ~ ("," ~ identifier)*)? ~ "}"
}
pattern = _{
named_tuple_pattern
| tuple_pattern
| struct_pattern
| literal
| static_path
}
// ======================================================
// ATTRIBUTES / META
// ======================================================
attribute = {
"#" ~ "[" ~ meta ~ "]"
}
mod_attribute = {
"#" ~ "!" ~ "[" ~ meta ~ "]"
}
meta = {
static_path ~ ("=" ~ literal | "(" ~ meta_list? ~ ")")?
}
meta_list = {
meta ~ ("," ~ meta)* ~ ","?
}
attributes = {
attribute*
}
// ======================================================
// DECLARATION HELPERS
// ======================================================
var_decl = {
(var | type_expr) ~ mutable? ~ pattern
}
field = {
visibility? ~ type_expr ~ identifier
}
field_list = {
field ~ ("," ~ field)* ~ ","?
}
param_list = {
var_decl ~ ("," ~ var_decl)* ~ ","?
}
// ======================================================
// IMPORTS / MODULES
// ======================================================
import = {
visibility? ~ "use" ~ "<" ~ static_path ~ ">" ~ semicolon
}
mod_package = {
visibility? ~ "mod" ~ identifier ~ semicolon
}
// ======================================================
// STRUCTS
// ======================================================
struct_decl = {
visibility? ~ "struct" ~ identifier ~ generics? ~ "{" ~ field_list? ~ "}"
}
// ======================================================
// ENUMS
// ======================================================
enum_named = {
identifier
}
enum_tuple = {
identifier ~ tuple_type
}
enum_struct = {
identifier ~ "{" ~ field_list ~ "}"
}
enum_field = _{
enum_tuple
| enum_struct
| enum_named
}
enum_fields = _{
enum_field ~ ("," ~ enum_field)* ~ ","?
}
enum_decl = {
visibility? ~ "enum" ~ identifier ~ generics? ~ "{" ~ enum_fields ~ "}"
}
// ======================================================
// CLASSES
// ======================================================
class_constructor = {
visibility? ~ "constructor" ~ generics? ~ "(" ~ param_list? ~ ")" ~ block
}
class_fields = {
class_field*
}
class_field = {
field ~ ("=" ~ expr)? ~ semicolon
}
self_param = {
"self" ~ mutable? ~ deref_px?
}
method = {
visibility? ~ type_expr ~ identifier ~ generics? ~ "(" ~ self_param? ~ ("," ~ param_list?)? ~ ")" ~ block
}
class_item = _{
impl_decl
| method
}
class_decl = {
visibility? ~ "class" ~ identifier ~ generics? ~ "{" ~ class_fields ~ class_constructor ~ class_item* ~ "}"
}
// ======================================================
// IMPL
// ======================================================
impl_for_decl = {
"impl" ~ generics? ~ type_expr ~ "for" ~ type_expr ~ "{" ~ method* ~ "}"
}
impl_decl = {
"impl" ~ generics? ~ type_expr ~ "{" ~ method* ~ "}"
}
// ======================================================
// Traits
// ======================================================
trait_requirements = { ":" ~ type_expr_param ~ ("+" ~ type_expr_param)* }
method_no_body = {
visibility? ~ type_expr ~ identifier ~ generics? ~ "(" ~ self_param? ~ ("," ~ param_list?)? ~ ")" ~ semicolon
}
trait_item = _{
method
| method_no_body
}
trait_decl = {
visibility? ~ "trait" ~ identifier ~ generics? ~ trait_requirements? ~ "{" ~ trait_item* ~ "}"
}
// ======================================================
// FUNCTIONS
// ======================================================
function_decl = {
visibility? ~ type_expr ~ identifier ~ "(" ~ param_list? ~ ")" ~ block
}
// ======================================================
// TOP LEVEL
// ======================================================
top_level = {
(attributes ~ (import | impl_for_decl | impl_decl | trait_decl | struct_decl | enum_decl | class_decl | mod_package | function_decl))
| mod_attribute
}
// ======================================================
// STATEMENTS
// ======================================================
block = {
"{" ~ (statement | unexpected_statement)* ~ "}"
}
statement = _{
expr_stmt
| match_stmt
| assign_statement
| compound_assign_statement
| if_stmt
| while_stmt
| c_for_stmt
| for_stmt
| return_stmt
| break_stmt
| continue_stmt
| block
| var_decl_statement
}
// ------------------------------------------------------
// BASIC STATEMENTS
// ------------------------------------------------------
expr_stmt = {
expr ~ semicolon
}
return_stmt = {
"return" ~ expr? ~ semicolon
}
break_stmt = {
"break" ~ semicolon
}
continue_stmt = {
"continue" ~ semicolon
}
var_decl_statement = {
var_decl ~ ("=" ~ expr)? ~ semicolon
}
assign_statement = {
expr ~ "=" ~ expr ~ semicolon
}
compound_assign = {
"+="
| "-="
| "/="
| "%="
| "&="
| "|="
| "^="
| "<<="
| ">>="
}
compound_assign_statement = {
expr ~ compound_assign ~ expr ~ semicolon
}
// ------------------------------------------------------
// CONDITIONALS
// ------------------------------------------------------
statement_branch = { "(" ~ expr ~ ")" ~ statement }
else_if = {
"else" ~ "if" ~ statement_branch
}
else_if_list = {
else_if*
}
if_stmt = {
"if" ~ statement_branch ~ else_if_list ~ ("else" ~ statement)?
}
// ------------------------------------------------------
// LOOPS
// ------------------------------------------------------
while_stmt = {
"while" ~ statement_branch
}
c_for_stmt = {
"for" ~ "(" ~ statement ~ expr ~ semicolon ~ statement ~ ")" ~ statement
}
for_stmt = {
"for" ~ "(" ~ mutable? ~ pattern ~ "in" ~ expr ~ ")" ~ statement
}
// ------------------------------------------------------
// MATCH
// ------------------------------------------------------
match_item = {
pattern ~ "=>" ~ block
}
match_stmt = {
"match" ~ "(" ~ expr ~ ")" ~ "{" ~ match_item* ~ "}"
}
// ======================================================
// EXPRESSIONS (Nested by Precedence, Low to High)
// ======================================================
// Base entry point for a pure expression
expr = { logical_or_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)*)? ~ ","? ~ "}" }
// ------------------------------------------------------
// 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* ~ ")"
| "[" ~ macro_inner_v* ~ "]"
| "{" ~ macro_inner_v* ~ "}"
| (!("(" | ")" | "[" | "]" | "{" | "}") ~ ANY)
}
macro_rules_call = { static_path ~ "!" ~ (("(" ~ macro_inner_v* ~ ")") | ("{" ~ macro_inner_v* ~ "}")) }
// (Assumed rules from your codebase)
tuple_index = { ASCII_DIGIT+ }