From 2ef7c1c1846b3958ac5d9b11fa4203c424f91637 Mon Sep 17 00:00:00 2001 From: Klesti Selimaj Date: Mon, 13 Apr 2026 14:32:15 +0200 Subject: [PATCH] Small update --- src/parser/grammar.pest | 107 +++++++++++++++++++++++++++++----------- 1 file changed, 77 insertions(+), 30 deletions(-) diff --git a/src/parser/grammar.pest b/src/parser/grammar.pest index fdf0ac9..bb18fae 100644 --- a/src/parser/grammar.pest +++ b/src/parser/grammar.pest @@ -1,10 +1,11 @@ WHITESPACE = _{ " " | "\t" | "\r" | "\n" } COMMENT = _{ "//" ~ (!"\n" ~ ANY)* } -program = { SOI ~ (import_decl | function_decl | struct_decl | class_decl)* ~ EOI } +program = { SOI ~ item* ~ EOI } +item = _{ import_decl | function_decl | struct_decl | class_decl } // primitives -identifier = @{ ASCII_ALPHA ~ (ASCII_ALPHANUMERIC | "_")* } +identifier = @{ (ASCII_ALPHA | "_") ~ (ASCII_ALPHANUMERIC | "_")* } integer = @{ ASCII_DIGIT+ } float = @{ ASCII_DIGIT+ ~ "." ~ ASCII_DIGIT+ } boolean = { "true" | "false" } @@ -12,9 +13,11 @@ string_lit = { "\"" ~ inner_str ~ "\"" } inner_str = @{ (!"\"" ~ ANY)* } // top level -import_decl = { "import" ~ string_lit } -struct_decl = { "struct" ~ identifier ~ "{" ~ struct_field* ~ "}" } -class_decl = { "class" ~ identifier ~ "{" ~ (struct_field | function_decl)* ~ "}" } +import_decl = { "import" ~ string_lit } +struct_decl = { "struct" ~ identifier ~ "{" ~ struct_field* ~ "}" } +class_decl = { "class" ~ identifier ~ "{" ~ class_member* ~ "}" } +class_member = _{ struct_field | function_decl } + function_decl = { "fn" ~ identifier ~ "(" ~ param_list? ~ ")" ~ ("->" ~ type_expr)? ~ block } struct_field = { identifier ~ ":" ~ type_expr } @@ -23,7 +26,14 @@ param = { identifier ~ ":" ~ type_expr } // statements block = { "{" ~ statement* ~ "}" } -statement = _{ let_stmt | return_stmt | if_stmt | for_stmt | assign_stmt | expression } +statement = _{ + let_stmt + | return_stmt + | if_stmt + | for_stmt + | assign_stmt + | expression +} assign_stmt = { identifier ~ "=" ~ expression } let_stmt = { "let" ~ identifier ~ (":" ~ type_expr)? ~ "=" ~ expression } @@ -31,29 +41,49 @@ return_stmt = { "return" ~ expression? } if_stmt = { "if" ~ expression ~ block ~ ("else" ~ block)? } for_stmt = { "for" ~ identifier ~ "in" ~ expression ~ block } -// types — optional is a suffix, not a recursive wrapper +// types type_expr = { base_type ~ "?"? } base_type = _{ array_type | identifier } array_type = { "[" ~ type_expr ~ "]" } -// expressions -expression = { term ~ (bin_op ~ term)* } -bin_op = _{ add | sub | mul | div | eq | neq | lte | gte | lt | gt | and | or } -add = { "+" } -sub = { "-" } -mul = { "*" } -div = { "/" } -eq = { "==" } -neq = { "!=" } -lte = { "<=" } -gte = { ">=" } -lt = { "<" } -gt = { ">" } -and = { "&&" } -or = { "||" } +// expressions (still simple, no precedence yet) +expression = { term ~ (bin_op ~ term)* } +bin_op = _{ + eq + | neq + | lte + | gte + | lt + | gt + | and + | or + | add + | sub + | mul + | div +} + +add = { "+" } +sub = { "-" } +mul = { "*" } +div = { "/" } +eq = { "==" } +neq = { "!=" } +lte = { "<=" } +gte = { ">=" } +lt = { "<" } +gt = { ">" } +and = { "&&" } +or = { "||" } + +// literals struct_literal = { - identifier ~ "{" ~ (struct_init_field ~ ("," ~ struct_init_field)*)? ~ "}" + identifier ~ "{" ~ struct_init_list? ~ "}" +} + +struct_init_list = { + struct_init_field ~ ("," ~ struct_init_field)* ~ ","? } struct_init_field = { @@ -61,12 +91,29 @@ struct_init_field = { } array_literal = { - "[" ~ (expression ~ ("," ~ expression)*)? ~ "]" + "[" ~ (expression ~ ("," ~ expression)*)? ~ ","? ~ "]" } -term = { primary ~ (field_access | call_suffix)* } -field_access = { "." ~ identifier } -call_suffix = { "(" ~ (expression ~ ("," ~ expression)*)? ~ ")" } -primary = _{ struct_literal | array_literal | float | integer | string_lit | boolean | self_kw | null_kw | identifier } -self_kw = { "self" } -null_kw = { "null" } +// expressions continuation +term = { primary ~ (field_access | call_suffix)* } + +field_access = { "." ~ identifier } + +call_suffix = { + "(" ~ (expression ~ ("," ~ expression)*)? ~ ","? ~ ")" +} + +primary = _{ + struct_literal + | array_literal + | float + | integer + | string_lit + | boolean + | self_kw + | null_kw + | identifier +} + +self_kw = { "self" } +null_kw = { "null" }