62 lines
1.3 KiB
Plaintext
62 lines
1.3 KiB
Plaintext
WHITESPACE = _{ " " | "\t" | "\r" | "\n" }
|
|
COMMENT = _{ "//" ~ (!"\n" ~ ANY)* }
|
|
|
|
program = { SOI ~ top_level* ~ EOI }
|
|
|
|
// primitives
|
|
identifier = @{ (ASCII_ALPHA | "_") ~ (ASCII_ALPHANUMERIC | "_")* }
|
|
integer = @{ ASCII_DIGIT+ }
|
|
float = @{ ASCII_DIGIT+ ~ "." ~ ASCII_DIGIT+ }
|
|
boolean = { "true" | "false" }
|
|
string_lit = { "\"" ~ inner_str ~ "\"" }
|
|
inner_str = @{ (!"\"" ~ ANY)* }
|
|
|
|
// ================= TOP LEVEL =================
|
|
|
|
top_level = _{ import | function_decl | struct_decl }
|
|
|
|
export = { "export" }
|
|
|
|
import = { "import" ~ string_lit ~ ";" }
|
|
|
|
param_list = { param ~ ("," ~ param)* ~ ","? }
|
|
param = { identifier ~ ":" ~ type_expr }
|
|
|
|
struct_decl = {
|
|
export? ~ "struct" ~ identifier ~ "{" ~ param_list? ~ "}"
|
|
}
|
|
|
|
function_decl = {
|
|
export? ~ "function" ~ identifier ~ "(" ~ param_list? ~ ")" ~ (":" ~ type_expr)? ~ block
|
|
}
|
|
|
|
// ================= STATEMENTS =================
|
|
|
|
block = { "{" ~ statement_list ~ "}" }
|
|
|
|
statement_list = { statement* }
|
|
|
|
statement = _{ expr_stmt }
|
|
|
|
expr_stmt = { expr ~ ";" }
|
|
|
|
// ================= EXPRESSIONS =================
|
|
type_expr = { identifier }
|
|
|
|
primary = {
|
|
integer
|
|
| float
|
|
| boolean
|
|
| string_lit
|
|
| identifier
|
|
}
|
|
|
|
postfix = {
|
|
("." ~ identifier)
|
|
| ("(" ~ (expr ~ ("," ~ expr)*)? ~ ")")
|
|
}
|
|
|
|
expr = {
|
|
primary ~ postfix*
|
|
}
|