Skip to content

Commit 6c4a0bb

Browse files
authored
Merge pull request #20990 from github/tausbn/python-support-relaxed-exception-groups
Python: Add support for PEP-758 exception syntax
2 parents 8cb0f5f + 8c90c11 commit 6c4a0bb

File tree

11 files changed

+28143
-27761
lines changed

11 files changed

+28143
-27761
lines changed

python/extractor/tests/parser/exception_groups_new.expected

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Module: [1, 0] - [27, 0]
1+
Module: [1, 0] - [32, 0]
22
body: [
33
Try: [1, 0] - [1, 4]
44
body: [
@@ -153,4 +153,28 @@ Module: [1, 0] - [27, 0]
153153
]
154154
]
155155
finalbody: []
156+
Try: [28, 0] - [28, 4]
157+
body: [
158+
Pass: [29, 4] - [29, 8]
159+
]
160+
orelse: []
161+
handlers: [
162+
ExceptGroupStmt: [30, 0] - [31, 8]
163+
type:
164+
Tuple: [30, 8] - [30, 12]
165+
elts: [
166+
Name: [30, 8] - [30, 9]
167+
variable: Variable('x', None)
168+
ctx: Load
169+
Name: [30, 11] - [30, 12]
170+
variable: Variable('y', None)
171+
ctx: Load
172+
]
173+
ctx: Load
174+
name: None
175+
body: [
176+
Pass: [31, 4] - [31, 8]
177+
]
178+
]
179+
finalbody: []
156180
]

python/extractor/tests/parser/exception_groups_new.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,8 @@
2424
pass
2525
except *foo as e:
2626
pass
27+
28+
try:
29+
pass
30+
except* x, y:
31+
pass
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
Module: [1, 0] - [9, 0]
2+
body: [
3+
Try: [1, 0] - [1, 4]
4+
body: [
5+
Pass: [2, 4] - [2, 8]
6+
]
7+
orelse: []
8+
handlers: [
9+
ExceptStmt: [3, 0] - [3, 12]
10+
type:
11+
Tuple: [3, 7] - [3, 11]
12+
elts: [
13+
Name: [3, 7] - [3, 8]
14+
variable: Variable('a', None)
15+
ctx: Load
16+
Name: [3, 10] - [3, 11]
17+
variable: Variable('b', None)
18+
ctx: Load
19+
]
20+
ctx: Load
21+
name: None
22+
body: [
23+
Pass: [4, 4] - [4, 8]
24+
]
25+
ExceptStmt: [5, 0] - [5, 14]
26+
type:
27+
Tuple: [5, 8] - [5, 12]
28+
elts: [
29+
Name: [5, 8] - [5, 9]
30+
variable: Variable('c', None)
31+
ctx: Load
32+
Name: [5, 11] - [5, 12]
33+
variable: Variable('d', None)
34+
ctx: Load
35+
]
36+
ctx: Load
37+
parenthesised: True
38+
name: None
39+
body: [
40+
Pass: [6, 4] - [6, 8]
41+
]
42+
ExceptStmt: [7, 0] - [7, 19]
43+
type:
44+
Tuple: [7, 8] - [7, 12]
45+
elts: [
46+
Name: [7, 8] - [7, 9]
47+
variable: Variable('e', None)
48+
ctx: Load
49+
Name: [7, 11] - [7, 12]
50+
variable: Variable('f', None)
51+
ctx: Load
52+
]
53+
ctx: Load
54+
parenthesised: True
55+
name:
56+
Name: [7, 17] - [7, 18]
57+
variable: Variable('g', None)
58+
ctx: Store
59+
body: [
60+
Pass: [8, 4] - [8, 8]
61+
]
62+
]
63+
finalbody: []
64+
]
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
try:
2+
pass
3+
except a, b: # new, relaxed syntax
4+
pass
5+
except (c, d): # old syntax
6+
pass
7+
except (e, f) as g: # old syntax
8+
pass

python/extractor/tsg-python/python.tsg

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
(assignment !type) @assign
1313
{ let @assign.node = (ast-node @assign "Assign") }
1414

15-
[ (expression_list) (tuple) (tuple_pattern) (pattern_list) (index_expression_list) ] @tuple
15+
[ (expression_list) (tuple) (tuple_pattern) (pattern_list) (index_expression_list) (exception_list)] @tuple
1616
{ let @tuple.node = (ast-node @tuple "Tuple") }
1717

1818
(list_pattern) @list
@@ -3445,6 +3445,9 @@
34453445
(tuple element: (_) @elt) @parent
34463446

34473447
(tuple_pattern element: (_) @elt) @parent
3448+
3449+
; An exception list, as in `except A, B, C: ...`
3450+
(exception_list element: (_) @elt) @parent
34483451
]
34493452
{
34503453
edge @parent.node -> @elt.node
@@ -3480,6 +3483,7 @@
34803483
(parenthesized_expression inner: (_) @elt)
34813484
(set element: (_) @elt)
34823485
(match_sequence_pattern (_) @elt)
3486+
(exception_list element: (_) @elt)
34833487
] @seq
34843488
{
34853489
attr (@elt.node) _inherited_ctx = @seq.node
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Mark tree-sitter generated files
2+
src/grammar.json linguist-generated
3+
src/node-types.json linguist-generated
4+
src/parser.c linguist-generated

python/extractor/tsg-python/tsp/grammar.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -297,12 +297,21 @@ module.exports = grammar({
297297
)
298298
),
299299

300+
exception_list: $ => seq(
301+
field('element', $.expression),
302+
repeat1(
303+
seq(
304+
',',
305+
field('element', $.expression))
306+
)
307+
),
308+
300309
except_clause: $ => seq(
301310
'except',
302311
optional(seq(
303-
field('type', $.expression),
312+
field('type', choice($.expression, $.exception_list)),
304313
optional(seq(
305-
choice('as', ','),
314+
'as',
306315
field('alias', $.expression)
307316
))
308317
)),
@@ -314,7 +323,7 @@ module.exports = grammar({
314323
'except',
315324
'*',
316325
seq(
317-
field('type', $.expression),
326+
field('type', choice($.expression, $.exception_list)),
318327
optional(seq(
319328
'as',
320329
field('alias', $.expression)

python/extractor/tsg-python/tsp/src/grammar.json

Lines changed: 57 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

python/extractor/tsg-python/tsp/src/node-types.json

Lines changed: 24 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)