Skip to content

Commit 48dd006

Browse files
committed
Add support for guarded branches, closes #258
1 parent 2c8d5e1 commit 48dd006

File tree

8 files changed

+80
-1
lines changed

8 files changed

+80
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
- Added support for properties in `gd2py`
1010
- Added support for `get():` property syntax
1111
- Added support for multiline arrays and dictionaries in `match` statement branches
12+
- Added support for guarded `match` branches
1213

1314
### Changed
1415
- Fixed support for `breakpoint` statement in formatter

gdtoolkit/formatter/function_statement.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from .context import Context, ExpressionContext
88
from .types import Outcome, FormattedLines
99
from .expression import format_expression
10+
from .expression_to_str import expression_to_str
1011
from .block import format_block, reconstruct_blank_lines_in_range
1112
from .statement_utils import format_simple_statement
1213
from .var_statement import format_var_statement
@@ -32,6 +33,7 @@ def format_func_statement(statement: Tree, context: Context) -> Outcome:
3233
"annotation": format_standalone_annotation,
3334
# fake statements:
3435
"match_branch": _format_match_branch,
36+
"guarded_match_branch": _format_guarded_match_branch,
3537
} # type: Dict[str, Callable]
3638
return handlers[statement.data](statement, context)
3739

@@ -106,6 +108,15 @@ def _format_match_branch(statement: Tree, context: Context) -> Outcome:
106108
return _format_branch(prefix, suffix, expr_position, statement, context)
107109

108110

111+
def _format_guarded_match_branch(statement: Tree, context: Context) -> Outcome:
112+
# TODO: fold pattern as well
113+
pattern_str = expression_to_str(statement.children[0].children[0])
114+
prefix = f"{pattern_str} when "
115+
suffix = ":"
116+
expr_position = 1
117+
return _format_branch(prefix, suffix, expr_position, statement, context)
118+
119+
109120
def _format_branch(
110121
prefix: str,
111122
suffix: str,

gdtoolkit/parser/gdscript.lark

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,8 @@ for_stmt: "for" NAME "in" expr ":" _func_suite
123123
for_stmt_typed: "for" _for_iterator_var_typed "in" expr ":" _func_suite
124124
match_stmt: "match" expr ":" _match_body
125125
_match_body: _NL _INDENT match_branch+ _DEDENT
126-
match_branch: pattern ":" _func_suite
126+
?match_branch: pattern ":" _func_suite
127+
| pattern "when" expr ":" _func_suite -> guarded_match_branch
127128

128129
pattern: list_pattern
129130
?list_pattern: test_pattern ("," test_pattern)*

tests/formatter/input-output-pairs/multiline_match_statements.in.gd

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ class X:
3737
pass
3838
[_,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]:
3939
pass
40+
[_,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1] when [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]:
41+
pass
4042
{'aaaaa', 'bbbbbbbbbbbbbbbbbbbbb': 1111, 'ccccccccccccccccccccc': 1111, 'ddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd': 1111}:
4143
pass
4244
[1,2,]:

tests/formatter/input-output-pairs/multiline_match_statements.out.gd

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,50 @@ class X:
291291
1
292292
]:
293293
pass
294+
[_, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1] when [
295+
1,
296+
1,
297+
1,
298+
1,
299+
1,
300+
1,
301+
1,
302+
1,
303+
1,
304+
1,
305+
1,
306+
1,
307+
1,
308+
1,
309+
1,
310+
1,
311+
1,
312+
1,
313+
1,
314+
1,
315+
1,
316+
1,
317+
1,
318+
1,
319+
1,
320+
1,
321+
1,
322+
1,
323+
1,
324+
1,
325+
1,
326+
1,
327+
1,
328+
1,
329+
1,
330+
1,
331+
1,
332+
1,
333+
1,
334+
1,
335+
1
336+
]:
337+
pass
294338
{
295339
"aaaaa",
296340
"bbbbbbbbbbbbbbbbbbbbb": 1111,

tests/formatter/input-output-pairs/simple_match_statements.in.gd

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ class X:
4545
pass
4646
1 if 1 else 2:
4747
pass
48+
2 when true:
49+
pass
50+
2 when 1 == 1:
51+
pass
52+
2 when 1.0 as int:
53+
pass
4854
_:
4955
pass
5056
match Vector3(1,1,1):

tests/formatter/input-output-pairs/simple_match_statements.out.gd

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ class X:
4545
pass
4646
1 if 1 else 2:
4747
pass
48+
2 when true:
49+
pass
50+
2 when 1 == 1:
51+
pass
52+
2 when 1.0 as int:
53+
pass
4854
_:
4955
pass
5056
match Vector3(1, 1, 1):

tests/valid-gd-scripts/match.gd

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,14 @@ func foo(x):
33
match x:
44
1:
55
pass
6+
5 when true:
7+
pass
8+
6 when 1 + 2 > 0:
9+
pass
10+
7 when y.aaa.bb == 0:
11+
pass
12+
8 when 1.0 as int:
13+
pass
614
# TODO: remove support for
715
# y.aaa:
816
# pass

0 commit comments

Comments
 (0)