unify catch fallback value sources

This commit is contained in:
2026-07-22 00:37:48 +02:00
parent 17508ff751
commit 5f343ad2d3
12 changed files with 1048397 additions and 324314 deletions
+14 -7
View File
@@ -33,6 +33,8 @@ module.exports = grammar({
[$.array_type, $.expression],
[$.array_type, $.array_literal],
[$.expression_statement, $.parenthesized_expression],
[$.assignment_statement, $.expression_statement],
[$.labeled_block, $.expression],
[$.block, $.tuple_literal, $.anonymous_record_literal],
[$.field_initializer, $.expression],
[$.capture_list, $.expression],
@@ -310,8 +312,8 @@ module.exports = grammar({
field('value', $._value),
),
break_statement: $ => seq('break', optional(seq(':', field('label', $.identifier)))),
continue_statement: $ => seq('continue', optional(seq(':', field('label', $.identifier)))),
break_statement: $ => prec.right(seq('break', optional(seq(':', field('label', $.identifier))))),
continue_statement: $ => prec.right(seq('continue', optional(seq(':', field('label', $.identifier))))),
defer_statement: $ => choice(
seq('defer', repeat($._newline), field('body', $.statement)),
@@ -468,7 +470,7 @@ module.exports = grammar({
binary_expression: $ => choice(
prec.left(PREC.RANGE, seq(field('left', $.expression), field('operator', choice('..', '..=')), repeat($._newline), field('right', $.expression))),
prec.left(PREC.FALLBACK, seq(field('left', $.expression), field('operator', choice('orelse', 'catch')), repeat($._newline), field('right', $.expression))),
prec.left(PREC.FALLBACK, seq(field('left', $.expression), field('operator', 'orelse'), repeat($._newline), field('right', $.expression))),
prec.left(PREC.OR, seq(field('left', $.expression), 'or', repeat($._newline), field('right', $.expression))),
prec.left(PREC.AND, seq(field('left', $.expression), 'and', repeat($._newline), field('right', $.expression))),
prec.left(PREC.COMPARE, seq(field('left', $.expression), field('operator', choice('==', '!=', '<', '<=', '>', '>=')), repeat($._newline), field('right', $.expression))),
@@ -481,11 +483,16 @@ module.exports = grammar({
catch_expression: $ => prec.left(PREC.FALLBACK, seq(
field('value', $.expression),
'catch',
'|',
field('name', choice($.identifier, $.sink)),
'|',
optional(field('capture', $.error_capture)),
repeat($._newline),
field('body', $.block),
field('fallback', choice(
$.block,
$.if_statement,
$.while_statement,
$.for_statement,
$.match_statement,
$.expression,
)),
)),
unary_expression: $ => prec(PREC.PREFIX, seq(
+120 -97
View File
@@ -2072,72 +2072,80 @@
]
},
"break_statement": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "break"
},
{
"type": "CHOICE",
"members": [
{
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": ":"
},
{
"type": "FIELD",
"name": "label",
"content": {
"type": "SYMBOL",
"name": "identifier"
"type": "PREC_RIGHT",
"value": 0,
"content": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "break"
},
{
"type": "CHOICE",
"members": [
{
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": ":"
},
{
"type": "FIELD",
"name": "label",
"content": {
"type": "SYMBOL",
"name": "identifier"
}
}
}
]
},
{
"type": "BLANK"
}
]
}
]
]
},
{
"type": "BLANK"
}
]
}
]
}
},
"continue_statement": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "continue"
},
{
"type": "CHOICE",
"members": [
{
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": ":"
},
{
"type": "FIELD",
"name": "label",
"content": {
"type": "SYMBOL",
"name": "identifier"
"type": "PREC_RIGHT",
"value": 0,
"content": {
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": "continue"
},
{
"type": "CHOICE",
"members": [
{
"type": "SEQ",
"members": [
{
"type": "STRING",
"value": ":"
},
{
"type": "FIELD",
"name": "label",
"content": {
"type": "SYMBOL",
"name": "identifier"
}
}
}
]
},
{
"type": "BLANK"
}
]
}
]
]
},
{
"type": "BLANK"
}
]
}
]
}
},
"defer_statement": {
"type": "CHOICE",
@@ -3299,17 +3307,8 @@
"type": "FIELD",
"name": "operator",
"content": {
"type": "CHOICE",
"members": [
{
"type": "STRING",
"value": "orelse"
},
{
"type": "STRING",
"value": "catch"
}
]
"type": "STRING",
"value": "orelse"
}
},
{
@@ -3692,29 +3691,20 @@
"value": "catch"
},
{
"type": "STRING",
"value": "|"
},
{
"type": "FIELD",
"name": "name",
"content": {
"type": "CHOICE",
"members": [
{
"type": "CHOICE",
"members": [
{
"type": "FIELD",
"name": "capture",
"content": {
"type": "SYMBOL",
"name": "identifier"
},
{
"type": "SYMBOL",
"name": "sink"
"name": "error_capture"
}
]
}
},
{
"type": "STRING",
"value": "|"
},
{
"type": "BLANK"
}
]
},
{
"type": "REPEAT",
@@ -3725,10 +3715,35 @@
},
{
"type": "FIELD",
"name": "body",
"name": "fallback",
"content": {
"type": "SYMBOL",
"name": "block"
"type": "CHOICE",
"members": [
{
"type": "SYMBOL",
"name": "block"
},
{
"type": "SYMBOL",
"name": "if_statement"
},
{
"type": "SYMBOL",
"name": "while_statement"
},
{
"type": "SYMBOL",
"name": "for_statement"
},
{
"type": "SYMBOL",
"name": "match_statement"
},
{
"type": "SYMBOL",
"name": "expression"
}
]
}
}
]
@@ -5481,6 +5496,14 @@
"expression_statement",
"parenthesized_expression"
],
[
"assignment_statement",
"expression_statement"
],
[
"labeled_block",
"expression"
],
[
"block",
"tuple_literal",
+26 -14
View File
@@ -290,10 +290,6 @@
"type": ">>",
"named": false
},
{
"type": "catch",
"named": false
},
{
"type": "orelse",
"named": false
@@ -436,26 +432,42 @@
"type": "catch_expression",
"named": true,
"fields": {
"body": {
"capture": {
"multiple": false,
"required": false,
"types": [
{
"type": "error_capture",
"named": true
}
]
},
"fallback": {
"multiple": false,
"required": true,
"types": [
{
"type": "block",
"named": true
}
]
},
"name": {
"multiple": false,
"required": true,
"types": [
},
{
"type": "identifier",
"type": "expression",
"named": true
},
{
"type": "sink",
"type": "for_statement",
"named": true
},
{
"type": "if_statement",
"named": true
},
{
"type": "match_statement",
"named": true
},
{
"type": "while_statement",
"named": true
}
]
+1047997 -324168
View File
File diff suppressed because it is too large Load Diff
+110
View File
@@ -0,0 +1,110 @@
==================
Catch value sources
==================
recover func() void {
a :: fail() catch |err| handle(err)
b :: fail() catch |err| match err {
.bad: 1
else: 2
}
c :: fail() catch { yield 3 }
d :: fail() catch if true { yield 4 } else { yield 5 }
}
---
(source_file
(function_declaration
(identifier)
(parameter_list)
(type
(builtin_type))
(block
(statement
(constant_declaration
(identifier)
(expression
(catch_expression
(expression
(call_expression
(expression
(identifier))
(argument_list)))
(error_capture
(identifier))
(expression
(call_expression
(expression
(identifier))
(argument_list
(expression
(identifier)))))))))
(statement
(constant_declaration
(identifier)
(expression
(catch_expression
(expression
(call_expression
(expression
(identifier))
(argument_list)))
(error_capture
(identifier))
(match_statement
(expression
(identifier))
(match_arm
(expression
(enum_literal
(identifier)))
(statement
(expression_statement
(expression
(integer)))))
(match_arm
(statement
(expression_statement
(expression
(integer))))))))))
(statement
(constant_declaration
(identifier)
(expression
(catch_expression
(expression
(call_expression
(expression
(identifier))
(argument_list)))
(block
(statement
(yield_statement
(expression
(integer)))))))))
(statement
(constant_declaration
(identifier)
(expression
(catch_expression
(expression
(call_expression
(expression
(identifier))
(argument_list)))
(if_statement
(expression
(boolean))
(statement
(block
(statement
(yield_statement
(expression
(integer))))))
(statement
(block
(statement
(yield_statement
(expression
(integer))))))))))))))