comptime expandable match statements

This commit is contained in:
2026-07-17 14:29:50 +02:00
parent 1f25e6cd1d
commit 97f1c06057
22 changed files with 120821 additions and 119318 deletions
+17 -3
View File
@@ -339,7 +339,7 @@ module.exports = grammar({
),
for_statement: $ => seq(
optional('inline'),
optional('expand'),
'for',
repeat($._newline),
field('iterable', $.expression),
@@ -364,16 +364,30 @@ module.exports = grammar({
'}',
),
match_arm: $ => seq(
match_arm: $ => choice(seq(
field('pattern', choice('else', commaSep1($, $.expression))),
optional($.match_capture),
':',
repeat($._newline),
field('body', $._branch_body),
),
), seq(
'expand',
$.expand_match_capture,
':',
repeat($._newline),
field('body', $._branch_body),
)),
match_capture: $ => seq('|', optional('@'), field('name', choice($.identifier, $.sink)), '|'),
expand_match_capture: $ => seq(
'|',
optional('@'),
field('value', choice($.identifier, $.sink)),
optional(seq(',', field('tag', choice($.identifier, $.sink)))),
'|',
),
_branch_body: $ => $.statement,
_value: $ => choice(
+1 -1
View File
@@ -72,7 +72,7 @@
"if"
"while"
"for"
"inline"
"expand"
"break"
"continue"
"defer"
+182 -69
View File
@@ -2402,7 +2402,7 @@
"members": [
{
"type": "STRING",
"value": "inline"
"value": "expand"
},
{
"type": "BLANK"
@@ -2593,90 +2593,127 @@
]
},
"match_arm": {
"type": "SEQ",
"type": "CHOICE",
"members": [
{
"type": "FIELD",
"name": "pattern",
"content": {
"type": "CHOICE",
"members": [
{
"type": "STRING",
"value": "else"
},
{
"type": "SEQ",
"type": "SEQ",
"members": [
{
"type": "FIELD",
"name": "pattern",
"content": {
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "expression"
"type": "STRING",
"value": "else"
},
{
"type": "REPEAT",
"content": {
"type": "SEQ",
"members": [
{
"type": "REPEAT",
"content": {
"type": "SYMBOL",
"name": "_newline"
}
},
{
"type": "STRING",
"value": ","
},
{
"type": "REPEAT",
"content": {
"type": "SYMBOL",
"name": "_newline"
}
},
{
"type": "SYMBOL",
"name": "expression"
"type": "SEQ",
"members": [
{
"type": "SYMBOL",
"name": "expression"
},
{
"type": "REPEAT",
"content": {
"type": "SEQ",
"members": [
{
"type": "REPEAT",
"content": {
"type": "SYMBOL",
"name": "_newline"
}
},
{
"type": "STRING",
"value": ","
},
{
"type": "REPEAT",
"content": {
"type": "SYMBOL",
"name": "_newline"
}
},
{
"type": "SYMBOL",
"name": "expression"
}
]
}
]
}
}
]
}
]
}
]
}
},
{
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "match_capture"
},
{
"type": "BLANK"
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "match_capture"
},
{
"type": "BLANK"
}
]
},
{
"type": "STRING",
"value": ":"
},
{
"type": "REPEAT",
"content": {
"type": "SYMBOL",
"name": "_newline"
}
},
{
"type": "FIELD",
"name": "body",
"content": {
"type": "SYMBOL",
"name": "_branch_body"
}
}
]
},
{
"type": "STRING",
"value": ":"
},
{
"type": "REPEAT",
"content": {
"type": "SYMBOL",
"name": "_newline"
}
},
{
"type": "FIELD",
"name": "body",
"content": {
"type": "SYMBOL",
"name": "_branch_body"
}
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "expand"
},
{
"type": "SYMBOL",
"name": "expand_match_capture"
},
{
"type": "STRING",
"value": ":"
},
{
"type": "REPEAT",
"content": {
"type": "SYMBOL",
"name": "_newline"
}
},
{
"type": "FIELD",
"name": "body",
"content": {
"type": "SYMBOL",
"name": "_branch_body"
}
}
]
}
]
},
@@ -2722,6 +2759,82 @@
}
]
},
"expand_match_capture": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "|"
},
{
"type": "CHOICE",
"members": [
{
"type": "STRING",
"value": "@"
},
{
"type": "BLANK"
}
]
},
{
"type": "FIELD",
"name": "value",
"content": {
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "identifier"
},
{
"type": "SYMBOL",
"name": "sink"
}
]
}
},
{
"type": "CHOICE",
"members": [
{
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": ","
},
{
"type": "FIELD",
"name": "tag",
"content": {
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "identifier"
},
{
"type": "SYMBOL",
"name": "sink"
}
]
}
}
]
},
{
"type": "BLANK"
}
]
},
{
"type": "STRING",
"value": "|"
}
]
},
"_branch_body": {
"type": "SYMBOL",
"name": "statement"
+43 -5
View File
@@ -658,6 +658,40 @@
]
}
},
{
"type": "expand_match_capture",
"named": true,
"fields": {
"tag": {
"multiple": false,
"required": false,
"types": [
{
"type": "identifier",
"named": true
},
{
"type": "sink",
"named": true
}
]
},
"value": {
"multiple": false,
"required": true,
"types": [
{
"type": "identifier",
"named": true
},
{
"type": "sink",
"named": true
}
]
}
}
},
{
"type": "expression",
"named": true,
@@ -1429,7 +1463,7 @@
},
"pattern": {
"multiple": true,
"required": true,
"required": false,
"types": [
{
"type": ",",
@@ -1450,6 +1484,10 @@
"multiple": false,
"required": false,
"types": [
{
"type": "expand_match_capture",
"named": true
},
{
"type": "match_capture",
"named": true
@@ -2575,6 +2613,10 @@
"type": "escape_sequence",
"named": true
},
{
"type": "expand",
"named": false
},
{
"type": "f32",
"named": false
@@ -2635,10 +2677,6 @@
"type": "import",
"named": false
},
{
"type": "inline",
"named": false
},
{
"type": "int",
"named": false
+119691 -119134
View File
File diff suppressed because it is too large Load Diff
+107 -2
View File
@@ -279,7 +279,7 @@ choose func() i32 {
(identifier)))))))
==================
Tuples and inline for
Tuples and expand for
==================
Pair :: struct { i32, []u8 }
@@ -289,7 +289,7 @@ main func() void {
singleton :: {1,}
empty :: {}
_ = pair.0
inline for singleton |value| {
expand for singleton |value| {
_ = value
}
}
@@ -365,3 +365,108 @@ main func() void {
(sink))
(expression
(identifier))))))))))
==================
Expanded match arms
==================
Kind :: enum { one, two }
Value :: union(enum) { number i32, empty void }
test func(kind Kind, value Value) void {
match kind {
.one: {}
expand |tag|: _ = tag
}
match value {
expand |@payload, tag|: {
_ = payload
_ = tag
}
}
}
---
(source_file
(type_declaration
(identifier)
(enum_type
(enum_body
(enum_member
(identifier))
(enum_member
(identifier)))))
(type_declaration
(identifier)
(union_type
(record_body
(record_field
(identifier)
(type
(builtin_type)))
(record_field
(identifier)
(type
(builtin_type))))))
(function_declaration
(identifier)
(parameter_list
(parameter
(identifier)
(type
(named_type
(qualified_identifier
(identifier)))))
(parameter
(identifier)
(type
(named_type
(qualified_identifier
(identifier))))))
(type
(builtin_type))
(block
(statement
(match_statement
(expression
(identifier))
(match_arm
(expression
(enum_literal
(identifier)))
(statement
(expression_statement
(expression
(tuple_literal)))))
(match_arm
(expand_match_capture
(identifier))
(statement
(assignment_statement
(expression
(sink))
(expression
(identifier)))))))
(statement
(match_statement
(expression
(identifier))
(match_arm
(expand_match_capture
(identifier)
(identifier))
(statement
(block
(statement
(assignment_statement
(expression
(sink))
(expression
(identifier))))
(statement
(assignment_statement
(expression
(sink))
(expression
(identifier))))))))))))