Re writing
This commit is contained in:
+8
-134
@@ -2,7 +2,7 @@ WHITESPACE = _{ " " | "\t" | "\r" | "\n" }
|
||||
COMMENT = _{ "//" ~ (!"\n" ~ ANY)* }
|
||||
|
||||
program = { SOI ~ item* ~ EOI }
|
||||
item = _{ import_decl | function_decl | struct_decl | class_decl }
|
||||
item = _{ import_decl | function_decl | struct_decl }
|
||||
|
||||
// primitives
|
||||
identifier = @{ (ASCII_ALPHA | "_") ~ (ASCII_ALPHANUMERIC | "_")* }
|
||||
@@ -12,152 +12,26 @@ boolean = { "true" | "false" }
|
||||
string_lit = { "\"" ~ inner_str ~ "\"" }
|
||||
inner_str = @{ (!"\"" ~ ANY)* }
|
||||
|
||||
// ================= TYPE SCRIPT STYLE CHANGES =================
|
||||
// ================= TOP LEVEL =================
|
||||
|
||||
// import becomes TS-like (still identical structure)
|
||||
import_decl = { "import" ~ string_lit ~ ";" }
|
||||
|
||||
// struct stays, but feels like TS "type object shape"
|
||||
struct_decl = {
|
||||
struct_field = { identifier ~ ":" ~ type_expr ~ ";" }
|
||||
struct_decl = {
|
||||
"struct" ~ identifier ~ "{" ~ struct_field* ~ "}"
|
||||
}
|
||||
|
||||
// class already TS-like — just improved consistency
|
||||
class_decl = {
|
||||
"class" ~ identifier ~ "{" ~ class_member* ~ "}"
|
||||
}
|
||||
|
||||
class_member = _{ struct_field | function_decl }
|
||||
|
||||
// function keyword changed feel: fn → function
|
||||
param_list = { param ~ ("," ~ param)* ~ ","? }
|
||||
param = { identifier ~ ":" ~ type_expr }
|
||||
function_decl = {
|
||||
"function" ~ identifier ~ "(" ~ param_list? ~ ")" ~ ("->" ~ type_expr)? ~ block
|
||||
}
|
||||
|
||||
// struct fields: TS-style semicolon consistency (already good)
|
||||
struct_field = { identifier ~ ":" ~ type_expr ~ ";" }
|
||||
|
||||
// params: TS optional typing style feel (unchanged structure)
|
||||
param_list = { param ~ ("," ~ param)* ~ ","? }
|
||||
param = { identifier ~ ":" ~ type_expr }
|
||||
|
||||
// ================= STATEMENTS =================
|
||||
|
||||
block = { "{" ~ statement* ~ "}" }
|
||||
|
||||
statement = _{
|
||||
let_stmt
|
||||
| return_stmt
|
||||
| if_stmt
|
||||
| for_stmt
|
||||
| assign_stmt
|
||||
| expression_stmt
|
||||
}
|
||||
|
||||
// expression statements: TS allows no semicolon in real life,
|
||||
// but we keep your structure
|
||||
expression_stmt = { expression ~ ";" }
|
||||
|
||||
// assignment unchanged (JS/TS feel already)
|
||||
assign_stmt = { identifier ~ "=" ~ expression ~ ";" }
|
||||
|
||||
// let → const/let feel (still your rule, just TS naming)
|
||||
let_stmt = {
|
||||
"const" ~ identifier ~ (":" ~ type_expr)? ~ "=" ~ expression ~ ";"
|
||||
}
|
||||
|
||||
// return: TS-like (no change except feel consistency)
|
||||
return_stmt = { "return" ~ expression? ~ ";" }
|
||||
|
||||
// if: add TS-style parentheses (MAJOR feel upgrade, same structure)
|
||||
if_stmt = {
|
||||
"if" ~ "(" ~ expression ~ ")" ~ block ~ ("else" ~ block)?
|
||||
}
|
||||
|
||||
// for: TS-like "of"
|
||||
for_stmt = {
|
||||
"for" ~ identifier ~ "of" ~ expression ~ block
|
||||
}
|
||||
|
||||
// ================= TYPES (KEEP STRUCTURE, TS FEEL) =================
|
||||
|
||||
type_expr = { base_type ~ "?"? }
|
||||
base_type = _{ array_type | identifier }
|
||||
array_type = { "[" ~ type_expr ~ "]" }
|
||||
statement = _{ "idk" }
|
||||
|
||||
// ================= EXPRESSIONS =================
|
||||
|
||||
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_list? ~ "}"
|
||||
}
|
||||
|
||||
struct_init_list = {
|
||||
struct_init_field ~ ("," ~ struct_init_field)* ~ ","?
|
||||
}
|
||||
|
||||
struct_init_field = {
|
||||
identifier ~ ":" ~ expression
|
||||
}
|
||||
|
||||
array_literal = {
|
||||
"[" ~ (expression ~ ("," ~ expression)*)? ~ ","? ~ "]"
|
||||
}
|
||||
|
||||
// ================= CHAINING =================
|
||||
|
||||
term = { primary ~ (field_access | call_suffix)* }
|
||||
|
||||
field_access = { "." ~ identifier }
|
||||
|
||||
call_suffix = {
|
||||
"(" ~ (expression ~ ("," ~ expression)*)? ~ ","? ~ ")"
|
||||
}
|
||||
|
||||
// ================= PRIMARY =================
|
||||
|
||||
primary = _{
|
||||
struct_literal
|
||||
| array_literal
|
||||
| float
|
||||
| integer
|
||||
| string_lit
|
||||
| boolean
|
||||
| self_kw
|
||||
| null_kw
|
||||
| identifier
|
||||
}
|
||||
|
||||
self_kw = { "this" }
|
||||
null_kw = { "null" }
|
||||
type_expr = _{ identifier }
|
||||
|
||||
Reference in New Issue
Block a user