Organized grammar
This commit is contained in:
+342
-80
@@ -1,59 +1,115 @@
|
|||||||
|
// ======================================================
|
||||||
|
// LEXICAL
|
||||||
|
// ======================================================
|
||||||
|
|
||||||
WHITESPACE = _{ " " | "\t" | "\r" | "\n" }
|
WHITESPACE = _{ " " | "\t" | "\r" | "\n" }
|
||||||
COMMENT = _{ "//" ~ (!"\n" ~ ANY)* }
|
COMMENT = _{ "//" ~ (!"\n" ~ ANY)* }
|
||||||
|
|
||||||
program = { SOI ~ top_level* ~ EOI }
|
// ------------------------------------------------------
|
||||||
|
// PRIMITIVES
|
||||||
|
// ------------------------------------------------------
|
||||||
|
|
||||||
// primitives
|
|
||||||
identifier = @{ (ASCII_ALPHA | "_") ~ (ASCII_ALPHANUMERIC | "_")* }
|
identifier = @{ (ASCII_ALPHA | "_") ~ (ASCII_ALPHANUMERIC | "_")* }
|
||||||
integer = @{ ASCII_DIGIT+ }
|
|
||||||
float = @{ ASCII_DIGIT+ ~ "." ~ ASCII_DIGIT+ }
|
|
||||||
boolean = { "true" | "false" }
|
|
||||||
string_lit = { "\"" ~ inner_str ~ "\"" }
|
|
||||||
inner_str = @{ (!"\"" ~ ("\\\"" | ANY))* }
|
|
||||||
static_path = { identifier ~ ("::" ~ identifier)* }
|
|
||||||
|
|
||||||
// pattern
|
integer = @{ ASCII_DIGIT+ }
|
||||||
tuple_pattern = { "(" ~ (identifier ~ ("," ~ identifier)*)? ~ ")" }
|
|
||||||
named_tuple_pattern = { static_path ~ "(" ~ (identifier ~ ("," ~ identifier)*)? ~ ")" }
|
float = @{
|
||||||
struct_pattern = { static_path ~ "{" ~ (identifier ~ ("," ~ identifier)*)? ~ "}" }
|
ASCII_DIGIT+ ~ "." ~ ASCII_DIGIT+
|
||||||
pattern = _{ named_tuple_pattern | tuple_pattern | struct_pattern | literal | static_path }
|
}
|
||||||
|
|
||||||
|
boolean = {
|
||||||
|
"true"
|
||||||
|
| "false"
|
||||||
|
}
|
||||||
|
|
||||||
|
string_lit = {
|
||||||
|
"\"" ~ inner_str ~ "\""
|
||||||
|
}
|
||||||
|
|
||||||
|
inner_str = @{
|
||||||
|
(!"\"" ~ ("\\\"" | ANY))*
|
||||||
|
}
|
||||||
|
|
||||||
|
semicolon = _{ ";" }
|
||||||
|
|
||||||
|
static_path = {
|
||||||
|
identifier ~ ("::" ~ identifier)*
|
||||||
|
}
|
||||||
|
|
||||||
|
// ======================================================
|
||||||
|
// PROGRAM
|
||||||
|
// ======================================================
|
||||||
|
|
||||||
|
program = {
|
||||||
|
SOI ~ top_level* ~ EOI
|
||||||
|
}
|
||||||
|
|
||||||
|
// ======================================================
|
||||||
|
// MODIFIERS
|
||||||
|
// ======================================================
|
||||||
|
|
||||||
export = { "public" }
|
export = { "public" }
|
||||||
mutable = { "mut" }
|
mutable = { "mut" }
|
||||||
var = { "var" }
|
var = { "var" }
|
||||||
var_decl = { (var | type_expr) ~ mutable? ~ pattern }
|
|
||||||
semicolon = _{ ";" }
|
|
||||||
|
|
||||||
// ================= CLASS =================
|
// ======================================================
|
||||||
class_constructor = {
|
// TYPES
|
||||||
export? ~ "constructor" ~ "(" ~ param_list? ~ ")" ~ block
|
// ======================================================
|
||||||
|
|
||||||
|
tuple_type = {
|
||||||
|
"(" ~ (type_expr ~ ("," ~ type_expr)*)? ~ ")"
|
||||||
}
|
}
|
||||||
|
|
||||||
class_fields = { var_decl_statement* }
|
path_type = {
|
||||||
|
static_path ~ ("<" ~ (type_expr ~ ("," ~ type_expr)*)? ~ ">")?
|
||||||
self_param = { "self" ~ mutable? ~ deref_px? }
|
|
||||||
|
|
||||||
method = {
|
|
||||||
export? ~ type_expr ~ identifier ~ "(" ~ self_param? ~ param_list? ~ ")" ~ block
|
|
||||||
}
|
}
|
||||||
|
|
||||||
class_decl = {
|
ref_type = {
|
||||||
export? ~ "class" ~ identifier ~ "{" ~ class_fields ~ class_constructor ~ method* ~ "}"
|
mutable? ~ "*"
|
||||||
}
|
}
|
||||||
|
|
||||||
// ================= TOP LEVEL =================
|
type_expr = {
|
||||||
|
(path_type | tuple_type) ~ ref_type*
|
||||||
|
}
|
||||||
|
|
||||||
enum_named = { identifier }
|
// ======================================================
|
||||||
enum_tuple = { identifier ~ tuple_type }
|
// LITERALS
|
||||||
enum_struct = { identifier ~ "{" ~ field_list ~ "}" }
|
// ======================================================
|
||||||
|
|
||||||
enum_field = _{ enum_tuple | enum_struct | enum_named }
|
literal = {
|
||||||
|
float
|
||||||
|
| integer
|
||||||
|
| boolean
|
||||||
|
| string_lit
|
||||||
|
}
|
||||||
|
|
||||||
enum_fields = _{ enum_field ~ ("," ~ enum_field)* ~ ","? }
|
// ======================================================
|
||||||
|
// PATTERNS
|
||||||
|
// ======================================================
|
||||||
|
|
||||||
enum_decl = { export? ~ "enum" ~ identifier ~ "{" ~ enum_fields ~ "}" }
|
tuple_pattern = {
|
||||||
|
"(" ~ (identifier ~ ("," ~ identifier)*)? ~ ")"
|
||||||
|
}
|
||||||
|
|
||||||
top_level = { (attributes ~ (import | function_decl | struct_decl | enum_decl | class_decl | mod_package)) | mod_attribute }
|
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 = {
|
attribute = {
|
||||||
"#" ~ "[" ~ meta ~ "]"
|
"#" ~ "[" ~ meta ~ "]"
|
||||||
@@ -71,30 +127,132 @@ meta_list = {
|
|||||||
meta ~ ("," ~ meta)* ~ ","?
|
meta ~ ("," ~ meta)* ~ ","?
|
||||||
}
|
}
|
||||||
|
|
||||||
attributes = { attribute* }
|
attributes = {
|
||||||
|
attribute*
|
||||||
|
}
|
||||||
|
|
||||||
import = { "use" ~ "<" ~ static_path ~ ">" ~ semicolon }
|
// ======================================================
|
||||||
|
// DECLARATION HELPERS
|
||||||
|
// ======================================================
|
||||||
|
|
||||||
mod_package = { "mod" ~ identifier ~ semicolon }
|
var_decl = {
|
||||||
|
(var | type_expr) ~ mutable? ~ pattern
|
||||||
|
}
|
||||||
|
|
||||||
field_list = { field ~ ("," ~ field)* ~ ","? }
|
field = {
|
||||||
field = { export? ~ type_expr ~ identifier }
|
export? ~ type_expr ~ identifier
|
||||||
|
}
|
||||||
|
|
||||||
param_list = { var_decl ~ ("," ~ var_decl)* ~ ","? }
|
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 = {
|
struct_decl = {
|
||||||
export? ~ "struct" ~ identifier ~ "{" ~ field_list? ~ "}"
|
export? ~ "struct" ~ identifier ~ "{" ~ 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 = {
|
function_decl = {
|
||||||
export? ~ type_expr ~ identifier ~ "(" ~ param_list? ~ ")" ~ block
|
export? ~ type_expr ~ identifier ~ "(" ~ param_list? ~ ")" ~ block
|
||||||
}
|
}
|
||||||
|
|
||||||
// ================= STATEMENTS =================
|
// ======================================================
|
||||||
|
// TOP LEVEL
|
||||||
|
// ======================================================
|
||||||
|
|
||||||
block = { "{" ~ statement_list ~ "}" }
|
top_level = {
|
||||||
|
(attributes ~ (import | function_decl | struct_decl | enum_decl | class_decl | mod_package))
|
||||||
|
| mod_attribute
|
||||||
|
}
|
||||||
|
|
||||||
statement_list = { statement* }
|
// ======================================================
|
||||||
|
// STATEMENTS
|
||||||
|
// ======================================================
|
||||||
|
|
||||||
|
block = {
|
||||||
|
"{" ~ statement_list ~ "}"
|
||||||
|
}
|
||||||
|
|
||||||
|
statement_list = {
|
||||||
|
statement*
|
||||||
|
}
|
||||||
|
|
||||||
statement = _{
|
statement = _{
|
||||||
expr_stmt
|
expr_stmt
|
||||||
@@ -111,28 +269,54 @@ statement = _{
|
|||||||
| block
|
| block
|
||||||
}
|
}
|
||||||
|
|
||||||
// --- basic statements ---
|
// ------------------------------------------------------
|
||||||
|
// BASIC STATEMENTS
|
||||||
|
// ------------------------------------------------------
|
||||||
|
|
||||||
expr_stmt = { expr ~ semicolon }
|
expr_stmt = {
|
||||||
|
expr ~ semicolon
|
||||||
|
}
|
||||||
|
|
||||||
return_stmt = { "return" ~ expr? ~ semicolon }
|
return_stmt = {
|
||||||
|
"return" ~ expr? ~ semicolon
|
||||||
|
}
|
||||||
|
|
||||||
break_stmt = { "break" ~ semicolon }
|
break_stmt = {
|
||||||
|
"break" ~ semicolon
|
||||||
|
}
|
||||||
|
|
||||||
continue_stmt = { "continue" ~ semicolon }
|
continue_stmt = {
|
||||||
|
"continue" ~ semicolon
|
||||||
|
}
|
||||||
|
|
||||||
var_decl_statement = { var_decl ~ ("=" ~ expr)? ~ semicolon }
|
var_decl_statement = {
|
||||||
|
var_decl ~ ("=" ~ expr)? ~ semicolon
|
||||||
|
}
|
||||||
|
|
||||||
assign_statement = { expr ~ "=" ~ expr ~ semicolon }
|
assign_statement = {
|
||||||
|
expr ~ "=" ~ expr ~ semicolon
|
||||||
|
}
|
||||||
|
|
||||||
else_if = { "else" ~ "if" ~ "(" ~ expr ~ ")" ~ statement }
|
// ------------------------------------------------------
|
||||||
|
// CONDITIONALS
|
||||||
|
// ------------------------------------------------------
|
||||||
|
|
||||||
else_if_list = { else_if* }
|
else_if = {
|
||||||
|
"else" ~ "if" ~ "(" ~ expr ~ ")" ~ statement
|
||||||
|
}
|
||||||
|
|
||||||
|
else_if_list = {
|
||||||
|
else_if*
|
||||||
|
}
|
||||||
|
|
||||||
if_stmt = {
|
if_stmt = {
|
||||||
"if" ~ "(" ~ expr ~ ")" ~ statement ~ else_if_list ~ ("else" ~ statement)?
|
"if" ~ "(" ~ expr ~ ")" ~ statement ~ else_if_list ~ ("else" ~ statement)?
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ------------------------------------------------------
|
||||||
|
// LOOPS
|
||||||
|
// ------------------------------------------------------
|
||||||
|
|
||||||
while_stmt = {
|
while_stmt = {
|
||||||
"while" ~ "(" ~ expr ~ ")" ~ statement
|
"while" ~ "(" ~ expr ~ ")" ~ statement
|
||||||
}
|
}
|
||||||
@@ -145,31 +329,42 @@ for_stmt = {
|
|||||||
"for" ~ "(" ~ mutable? ~ pattern ~ "in" ~ expr ~ ")" ~ statement
|
"for" ~ "(" ~ mutable? ~ pattern ~ "in" ~ expr ~ ")" ~ statement
|
||||||
}
|
}
|
||||||
|
|
||||||
match_item = { pattern ~ "=>" ~ block }
|
// ------------------------------------------------------
|
||||||
|
// MATCH
|
||||||
|
// ------------------------------------------------------
|
||||||
|
|
||||||
|
match_item = {
|
||||||
|
pattern ~ "=>" ~ block
|
||||||
|
}
|
||||||
|
|
||||||
match_stmt = {
|
match_stmt = {
|
||||||
"match" ~ "(" ~ expr ~ ")" ~ "{" ~ match_item* ~ "}"
|
"match" ~ "(" ~ expr ~ ")" ~ "{" ~ match_item* ~ "}"
|
||||||
}
|
}
|
||||||
|
|
||||||
// ================= EXPRESSIONS =================
|
// ======================================================
|
||||||
tuple_type = { "(" ~ (type_expr ~ ("," ~ type_expr)*)? ~ ")" }
|
// EXPRESSIONS
|
||||||
path_type = { static_path ~ ("<" ~ (type_expr ~ ("," ~ type_expr)*)? ~ ">")? }
|
// ======================================================
|
||||||
ref_type = { mutable? ~ "*" }
|
|
||||||
type_expr = { (path_type | tuple_type) ~ ref_type* }
|
// ------------------------------------------------------
|
||||||
|
// TUPLES
|
||||||
|
// ------------------------------------------------------
|
||||||
|
|
||||||
|
tuple = {
|
||||||
|
"(" ~ (expr ~ ("," ~ expr)*)? ~ ")"
|
||||||
|
}
|
||||||
|
|
||||||
|
// ------------------------------------------------------
|
||||||
|
// MACROS
|
||||||
|
// ------------------------------------------------------
|
||||||
|
|
||||||
macro_inner_v = {
|
macro_inner_v = {
|
||||||
"(" ~ macro_inner_v* ~ ")"
|
"(" ~ macro_inner_v* ~ ")"
|
||||||
| (!("(" | ")") ~ ANY)
|
| (!("(" | ")") ~ ANY)
|
||||||
}
|
}
|
||||||
|
|
||||||
tuple = { "(" ~ (expr ~ ("," ~ expr)*)? ~ ")" }
|
// ------------------------------------------------------
|
||||||
|
// PRIMARY EXPRESSIONS
|
||||||
literal = {
|
// ------------------------------------------------------
|
||||||
float
|
|
||||||
| integer
|
|
||||||
| boolean
|
|
||||||
| string_lit
|
|
||||||
}
|
|
||||||
|
|
||||||
primary = {
|
primary = {
|
||||||
literal
|
literal
|
||||||
@@ -177,6 +372,38 @@ primary = {
|
|||||||
| tuple
|
| 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 = {
|
postfix = {
|
||||||
field_px
|
field_px
|
||||||
| call_px
|
| call_px
|
||||||
@@ -186,14 +413,29 @@ postfix = {
|
|||||||
| binary_px
|
| binary_px
|
||||||
}
|
}
|
||||||
|
|
||||||
field_px = { "." ~ identifier }
|
// ------------------------------------------------------
|
||||||
call_px = { "(" ~ (expr ~ ("," ~ expr)*)? ~ ")" }
|
// PREFIX EXPRESSIONS
|
||||||
macro_call_px = { "!" ~ "(" ~ macro_inner_v* ~ ")" }
|
// ------------------------------------------------------
|
||||||
struct_px = { "{" ~ (struct_field_decl ~ ("," ~ struct_field_decl)*)? ~ ","? ~ "}" }
|
|
||||||
index_px = { "[" ~ expr ~ "]" }
|
deref_px = {
|
||||||
binary_px = { bin_op ~ expr }
|
"*"
|
||||||
bin_op = { "<=" | ">=" | "==" | "!=" | "&&" | "||" | "+" | "-" | "*" | "/" | "%" | "<" | ">" }
|
}
|
||||||
struct_field_decl = { identifier ~ ":" ~ expr }
|
|
||||||
|
mut_ref_px = {
|
||||||
|
"&" ~ mutable
|
||||||
|
}
|
||||||
|
|
||||||
|
ref_px = {
|
||||||
|
"&"
|
||||||
|
}
|
||||||
|
|
||||||
|
new_px = {
|
||||||
|
"new"
|
||||||
|
}
|
||||||
|
|
||||||
|
not_px = {
|
||||||
|
"!"
|
||||||
|
}
|
||||||
|
|
||||||
prefix = {
|
prefix = {
|
||||||
deref_px
|
deref_px
|
||||||
@@ -203,13 +445,33 @@ prefix = {
|
|||||||
| not_px
|
| not_px
|
||||||
}
|
}
|
||||||
|
|
||||||
deref_px = { "*" }
|
prefix_list = {
|
||||||
mut_ref_px = { "&" ~ mutable }
|
prefix*
|
||||||
ref_px = { "&" }
|
}
|
||||||
new_px = { "new" }
|
|
||||||
not_px = { "!" }
|
|
||||||
|
|
||||||
prefix_list = { prefix* }
|
// ------------------------------------------------------
|
||||||
|
// OPERATORS
|
||||||
|
// ------------------------------------------------------
|
||||||
|
|
||||||
|
bin_op = {
|
||||||
|
"<="
|
||||||
|
| ">="
|
||||||
|
| "=="
|
||||||
|
| "!="
|
||||||
|
| "&&"
|
||||||
|
| "||"
|
||||||
|
| "+"
|
||||||
|
| "-"
|
||||||
|
| "*"
|
||||||
|
| "/"
|
||||||
|
| "%"
|
||||||
|
| "<"
|
||||||
|
| ">"
|
||||||
|
}
|
||||||
|
|
||||||
|
// ------------------------------------------------------
|
||||||
|
// EXPRESSIONS
|
||||||
|
// ------------------------------------------------------
|
||||||
|
|
||||||
expr = {
|
expr = {
|
||||||
prefix_list ~ primary ~ postfix*
|
prefix_list ~ primary ~ postfix*
|
||||||
|
|||||||
Reference in New Issue
Block a user