485 lines
8.8 KiB
Plaintext
485 lines
8.8 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
|
|
// ======================================================
|
|
|
|
export = { "public" }
|
|
mutable = { "mut" }
|
|
var = { "var" }
|
|
|
|
// ======================================================
|
|
// GENERICS
|
|
// ======================================================
|
|
generic = { identifier ~ (":" ~ static_path ~ ("+" ~ static_path)*)? }
|
|
generics = { "<" ~ (generic ~ ("," ~ generic)*)? ~ ">" }
|
|
|
|
// ======================================================
|
|
// TYPES
|
|
// ======================================================
|
|
|
|
tuple_type = {
|
|
"(" ~ (type_expr ~ ("," ~ type_expr)*)? ~ ")"
|
|
}
|
|
|
|
path_type = {
|
|
static_path ~ ("<" ~ (type_expr ~ ("," ~ type_expr)*)? ~ ">")?
|
|
}
|
|
|
|
ref_type = {
|
|
mutable? ~ "*"
|
|
}
|
|
|
|
type_expr = {
|
|
(path_type | tuple_type) ~ ref_type*
|
|
}
|
|
|
|
// ======================================================
|
|
// 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 = {
|
|
export? ~ type_expr ~ identifier
|
|
}
|
|
|
|
field_list = {
|
|
field ~ ("," ~ field)* ~ ","?
|
|
}
|
|
|
|
param_list = {
|
|
var_decl ~ ("," ~ var_decl)* ~ ","?
|
|
}
|
|
|
|
// ======================================================
|
|
// IMPORTS / MODULES
|
|
// ======================================================
|
|
|
|
import = {
|
|
"use" ~ "<" ~ static_path ~ ">" ~ semicolon
|
|
}
|
|
|
|
mod_package = {
|
|
"mod" ~ identifier ~ semicolon
|
|
}
|
|
|
|
// ======================================================
|
|
// STRUCTS
|
|
// ======================================================
|
|
|
|
struct_decl = {
|
|
export? ~ "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 = {
|
|
export? ~ "enum" ~ identifier ~ "{" ~ enum_fields ~ "}"
|
|
}
|
|
|
|
// ======================================================
|
|
// CLASSES
|
|
// ======================================================
|
|
|
|
class_constructor = {
|
|
export? ~ "constructor" ~ "(" ~ param_list? ~ ")" ~ block
|
|
}
|
|
|
|
class_fields = {
|
|
var_decl_statement*
|
|
}
|
|
|
|
self_param = {
|
|
"self" ~ mutable? ~ deref_px?
|
|
}
|
|
|
|
method = {
|
|
export? ~ type_expr ~ identifier ~ "(" ~ self_param? ~ param_list? ~ ")" ~ block
|
|
}
|
|
|
|
class_decl = {
|
|
export? ~ "class" ~ identifier ~ "{" ~ class_fields ~ class_constructor ~ method* ~ "}"
|
|
}
|
|
|
|
// ======================================================
|
|
// FUNCTIONS
|
|
// ======================================================
|
|
|
|
function_decl = {
|
|
export? ~ type_expr ~ identifier ~ "(" ~ param_list? ~ ")" ~ block
|
|
}
|
|
|
|
// ======================================================
|
|
// TOP LEVEL
|
|
// ======================================================
|
|
|
|
top_level = {
|
|
(attributes ~ (import | function_decl | struct_decl | enum_decl | class_decl | mod_package))
|
|
| mod_attribute
|
|
}
|
|
|
|
// ======================================================
|
|
// STATEMENTS
|
|
// ======================================================
|
|
|
|
block = {
|
|
"{" ~ statement_list ~ "}"
|
|
}
|
|
|
|
statement_list = {
|
|
statement*
|
|
}
|
|
|
|
statement = _{
|
|
expr_stmt
|
|
| match_stmt
|
|
| var_decl_statement
|
|
| assign_statement
|
|
| if_stmt
|
|
| while_stmt
|
|
| c_for_stmt
|
|
| for_stmt
|
|
| return_stmt
|
|
| break_stmt
|
|
| continue_stmt
|
|
| block
|
|
}
|
|
|
|
// ------------------------------------------------------
|
|
// 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
|
|
}
|
|
|
|
// ------------------------------------------------------
|
|
// CONDITIONALS
|
|
// ------------------------------------------------------
|
|
|
|
else_if = {
|
|
"else" ~ "if" ~ "(" ~ expr ~ ")" ~ statement
|
|
}
|
|
|
|
else_if_list = {
|
|
else_if*
|
|
}
|
|
|
|
if_stmt = {
|
|
"if" ~ "(" ~ expr ~ ")" ~ statement ~ else_if_list ~ ("else" ~ statement)?
|
|
}
|
|
|
|
// ------------------------------------------------------
|
|
// LOOPS
|
|
// ------------------------------------------------------
|
|
|
|
while_stmt = {
|
|
"while" ~ "(" ~ expr ~ ")" ~ statement
|
|
}
|
|
|
|
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
|
|
// ======================================================
|
|
|
|
// ------------------------------------------------------
|
|
// TUPLES
|
|
// ------------------------------------------------------
|
|
|
|
tuple = {
|
|
"(" ~ (expr ~ ("," ~ expr)*)? ~ ")"
|
|
}
|
|
|
|
// ------------------------------------------------------
|
|
// MACROS
|
|
// ------------------------------------------------------
|
|
|
|
macro_inner_v = {
|
|
"(" ~ macro_inner_v* ~ ")"
|
|
| (!("(" | ")") ~ ANY)
|
|
}
|
|
|
|
// ------------------------------------------------------
|
|
// PRIMARY EXPRESSIONS
|
|
// ------------------------------------------------------
|
|
|
|
primary = {
|
|
literal
|
|
| static_path
|
|
| tuple
|
|
}
|
|
|
|
// ------------------------------------------------------
|
|
// POSTFIX EXPRESSIONS
|
|
// ------------------------------------------------------
|
|
|
|
field_px = {
|
|
"." ~ identifier
|
|
}
|
|
|
|
call_px = {
|
|
"(" ~ (expr ~ ("," ~ expr)*)? ~ ")"
|
|
}
|
|
|
|
macro_call_px = {
|
|
"!" ~ "(" ~ macro_inner_v* ~ ")"
|
|
}
|
|
|
|
struct_field_decl = {
|
|
identifier ~ ":" ~ expr
|
|
}
|
|
|
|
struct_px = {
|
|
"{" ~ (struct_field_decl ~ ("," ~ struct_field_decl)*)? ~ ","? ~ "}"
|
|
}
|
|
|
|
index_px = {
|
|
"[" ~ expr ~ "]"
|
|
}
|
|
|
|
binary_px = {
|
|
bin_op ~ expr
|
|
}
|
|
|
|
postfix = {
|
|
field_px
|
|
| call_px
|
|
| macro_call_px
|
|
| struct_px
|
|
| index_px
|
|
| binary_px
|
|
}
|
|
|
|
// ------------------------------------------------------
|
|
// PREFIX EXPRESSIONS
|
|
// ------------------------------------------------------
|
|
|
|
deref_px = {
|
|
"*"
|
|
}
|
|
|
|
mut_ref_px = {
|
|
"&" ~ mutable
|
|
}
|
|
|
|
ref_px = {
|
|
"&"
|
|
}
|
|
|
|
new_px = {
|
|
"new"
|
|
}
|
|
|
|
not_px = {
|
|
"!"
|
|
}
|
|
|
|
prefix = {
|
|
deref_px
|
|
| mut_ref_px
|
|
| ref_px
|
|
| new_px
|
|
| not_px
|
|
}
|
|
|
|
prefix_list = {
|
|
prefix*
|
|
}
|
|
|
|
// ------------------------------------------------------
|
|
// OPERATORS
|
|
// ------------------------------------------------------
|
|
|
|
bin_op = {
|
|
"<="
|
|
| ">="
|
|
| "=="
|
|
| "!="
|
|
| "&&"
|
|
| "||"
|
|
| "+"
|
|
| "-"
|
|
| "*"
|
|
| "/"
|
|
| "%"
|
|
| "<"
|
|
| ">"
|
|
}
|
|
|
|
// ------------------------------------------------------
|
|
// EXPRESSIONS
|
|
// ------------------------------------------------------
|
|
|
|
expr = {
|
|
prefix_list ~ primary ~ postfix*
|
|
}
|