frontend: align source grammar and reject empty enum payload lists

This commit is contained in:
dev committed 2026-10-08 14:14:45 +03:00
1 parent 48b9b42fed
commit 4c9c5626c9
44 files changed
+501 -33

No files matched your search

+54 -26
View File
@@ -5,17 +5,23 @@
-------------------
- `//` starts a comment that runs to the end of the line.
- Whitespace and line breaks separate tokens and are otherwise insignificant.
- Keywords: module struct enum fn effects trusted from let var zero
- Keywords: module struct enum type fn effects trusted from let var zero
if else while for in loop break continue return trap match select
scope shield lock as cancelled ref mut move unchecked or and not
true false recv send.
true false.
- import is contextual after the module header; recv/send are contextual
inside select. They remain identifiers elsewhere. A lone `_` is not
an identifier; it is a wildcard/discard binding. `_name` is an identifier.
- as! and as% are single tokens: `as` immediately followed by `!` or `%`,
without whitespace.
- Every construct has one canonical spelling (spec 15 §1).
*)
module = "module" path { module_import } { item } ;
module_import = "import" path ";" ; (* contextual word, spec 15 §13/§14 *)
item = type_decl | fn_decl ;
path = ident { "." ident } ;
path = ident { "." member } ; (* module/effect paths allow keywords after `.` *)
name_path = ident { "." ident } ; (* qualified types and enum patterns *)
type_decl = struct_decl | enum_decl ;
struct_decl = "struct" ident [ type_params ] "{" [ field { "," field } [ "," ] ] "}" ;
@@ -23,28 +29,29 @@ field = ident ":" type ;
enum_decl = "enum" ident [ type_params ] "{" variant { "," variant } [ "," ] "}" ;
variant = ident [ "(" type { "," type } ")" ] ;
fn_decl = "fn" ident [ type_params ] "(" [ param { "," param } ] ")" "->" type [ "from" ident ]
"effects" "[" [ path { "," path } ] "]"
fn_decl = "fn" ident [ type_params ] "(" [ param { "," param } [ "," ] ] ")" "->" type [ "from" ident ]
"effects" "[" [ path { "," path } [ "," ] ] "]"
block ; (* `trusted` is reserved and rejected, E7006 *)
param = ident ":" type ;
type_params = "<" type_param { "," type_param } ">" ;
type_param = ident [ ":" ( property { "+" property } | function_contract ) ] ;
function_contract = "fn" "(" [ type { "," type } ] ")" "->" type
"effects" "[" [ path { "," path } ] "]" ;
function_contract = "fn" "(" [ type { "," type } [ "," ] ] ")" "->" type
"effects" "[" [ path { "," path } [ "," ] ] "]" ;
property = "Copy" | "Send" | "Share" | "Freeze" | "Local" ;
type_args = "<" generic_arg { "," generic_arg } ">" ;
generic_arg = type | "fn" path [ application ] ; (* static function selection; spec 15 §18 *)
application = "::" type_args ; (* explicit; no inference, spec 15 §15 *)
type = int_type | "bool" | "void" | "never" | path
| path type_args
type = int_type | "bool" | "void" | "never" | name_path
| name_path type_args
| "[" type ";" int_lit "]"
| wrapper "<" type ">"
| "cap" "<" path ">"
| "file" "<" ( "read" | "write" | "append" ) ">" ;
(* `>>` closes two type arguments: mut<file<write>> *)
wrapper = "ref" | "mut" | "slice" | "slice_mut" | "own" | "own_array"
| "task" | "sender" | "receiver" | "mutex" | "buffer" ;
| "task" | "sender" | "receiver" | "mutex" | "buffer"
| "island" | "frozen" | "arc" | "iref" | "atomic" ;
int_type = "u8" | "u16" | "u32" | "u64" | "u128" | "usize"
| "i8" | "i16" | "i32" | "i64" | "i128" | "isize" ;
(* `region`, `listener`, `stream`, `io_error`, `alloc_error`,
@@ -59,6 +66,18 @@ int_type = "u8" | "u16" | "u32" | "u64" | "u128" | "usize"
No outer postfix/cast/binary continuation is consumed here; parenthesize
the construct for continuation. Nested expr contexts are unrestricted.
Other block_stmt forms end at their final `}`. Whitespace is irrelevant. *)
(* Other statements parse an expression first: the next `=` selects assignment,
`;` selects an expression statement, and `}` selects a tail. Lowering checks
that an assignment target is a place (E7006 invalid_assignment). This ordered
choice, with the block rule above, has no LL(k) bound on the common prefix.
In if/while conditions, match scrutinees, for bounds and lock places, a
following `{` starts the body rather than a struct literal. Parenthesized
expressions, call arguments and index expressions restore ordinary parsing.
Struct literals require an uppercase ASCII initial in the final name.
At a dot step only channel.make<T> and buffer.new<T> consume `<` as a type
argument; that choice commits to a typed call, with no comparison fallback.
Other expression `<` tokens are comparisons; generic calls use `::<...>`.
Arm bodies starting with `{` likewise consume the whole block first. *)
block = "{" { statement } [ expr ] "}" ; (* optional tail expression: the block's value *)
statement = "let" binding [ ":" type ] "=" expr ";"
| "var" ident ":" type "=" ( expr | "zero" ) ";"
@@ -67,7 +86,9 @@ statement = "let" binding [ ":" type ] "=" expr ";"
| "break" ";" | "continue" ";" | "return" [ expr ] ";" | "trap" ";"
| expr ";"
| block_stmt ;
binding = ident | "(" [ ( ident | "_" ) { "," ( ident | "_" ) } ] ")" ; (* complete unpack; pairs also split_at/compare_exchange *)
binding = ident | binds ;
binds = "(" [ bind { "," bind } [ "," ] ] ")" ;
bind = ident | "_" ; (* complete unpack; pairs also split_at/compare_exchange *)
block_stmt = if_expr | match_expr | select_stmt | block
| "while" expr block
| "for" ident "in" expr ".." expr block
@@ -77,15 +98,17 @@ block_stmt = if_expr | match_expr | select_stmt | block
| "lock" "ref" place "as" ident block "cancelled" block ;
if_expr = "if" expr block [ "else" ( if_expr | block ) ] ;
match_expr = "match" expr "{" { arm } "}" ;
arm = pattern "=>" ( block | expr "," ) ;
pattern = "_" | int_lit (* integer matches only; `_` is required there *)
| path [ application ] "::" ident [ "(" ident { "," ident } ")" ] (* enum variant: Shape::Rect(w, h); no `_` in enum matches *)
| ident [ "(" ident { "," ident } ")" ] ; (* outcome of a fallible operation: ok(n), closed *)
match_expr = "match" expr "{" { arm } [ final_arm ] "}" ;
arm = pattern "=>" ( block [ "," ] | expr "," ) ;
final_arm = pattern "=>" expr ; (* final non-block arm may omit its comma *)
pattern = "_" | [ "-" ] int_lit (* integer matches only; `_` is required there *)
| name_path [ application ] "::" ident [ payload_binds ] (* exhaustive enum variants; §6 *)
| ident [ binds ] ; (* outcome of a fallible operation: ok(n), closed *)
payload_binds = "(" bind { "," bind } [ "," ] ")" ; (* enum payload list MUST NOT be empty *)
select_stmt = "select" "{" { select_arm } "cancelled" [ "(" ident { "," ident } ")" ] "=>" ( block | expr "," ) "}" ;
select_arm = "recv" "(" ident ")" "{" { arm } "}"
| "send" "(" ident "," expr ")" "{" { arm } "}" ;
select_stmt = "select" "{" { select_arm } "cancelled" [ "(" [ ident { "," ident } [ "," ] ] ")" ] "=>" ( block [ "," ] | expr [ "," ] ) "}" ;
select_arm = "recv" "(" ident ")" "{" { arm } [ final_arm ] "}"
| "send" "(" ident "," expr ")" "{" { arm } [ final_arm ] "}" ;
expr = or_expr ;
or_expr = and_expr { "or" ( and_expr | "trap" ) } ; (* `e or trap`: the only fallible shorthand *)
@@ -100,9 +123,12 @@ unary = ( "-" | "not" | "*" ) unary
| ( "ref" | "mut" ) [ "unchecked" ] place
| "move" ident
| postfix ;
postfix = primary { application | "." member | "[" expr [ ".." expr ] "]" | call_args } ;
postfix = primary { application | "." member [ typed_call_args ] | "[" expr [ ".." expr ] "]" | call_args } ;
member = ident | keyword ; (* after `.` a keyword is a name: task.cancelled *)
call_args = [ "<" type ">" ] "(" [ expr { "," expr } ] ")" ;
call_args = "(" [ expr { "," expr } [ "," ] ] ")" ;
typed_call_args = "<" type ">" call_args ; (* only channel.make / buffer.new at the dot step *)
(* A variant may be followed only by a non-empty payload call;
payload-free variants MUST be bare. Payload arity is checked. *)
primary = int_lit | "true" | "false" | string
| "struct" "." "unpack" (* contextual built-in call, §16 *)
| ident | path [ application ] "{" [ field_init { "," field_init } [ "," ] ] "}" (* struct literal; uppercase final component *)
@@ -112,11 +138,13 @@ primary = int_lit | "true" | "false" | string
| "(" expr ")"
| if_expr | match_expr | block ;
field_init = ident ":" expr ;
borrow_call = "buffer" "." "view" "(" ident ")"
| ( "island" | "frozen" | "arc" ) "." "at" "(" expr "," expr ")" ;
place = ident { "." ident | "[" expr [ ".." expr ] "]" } | "*" unary ;
borrow_call = name_path [ application ] call_args ; (* only declared `from` calls (§19), buffer.view and graph .at (§17) *)
place = ( ident | "(" place ")" ) { "." member | "[" expr [ ".." expr ] "]" } | "*" unary ;
int_lit = digit { digit | "_" } [ int_type ] | "0x" hexdigit { hexdigit | "_" } [ int_type ] ;
int_lit = digit { digit | "_" } [ int_type ]
| "0x" hexdigit { hexdigit | "_" } [ int_type ]
| "0b" bindigit { bindigit | "_" } [ int_type ] ;
bindigit = "0" | "1" ;
string = 'b"' { char | escape } '"' ; (* byte string; escapes \n \r \t \0 \\ \" \xHH *)
ident = ( letter | "_" ) { letter | digit | "_" } ;
ident = ( letter | "_" ) { letter | digit | "_" } ; (* excluding keywords and lone `_` *)
keyword = ? any keyword listed under Lexical conventions ? ;