Skip to content

Commit

Permalink
Add support for guarded branches, closes #258
Browse files Browse the repository at this point in the history
  • Loading branch information
Scony committed Apr 29, 2024
1 parent 2c8d5e1 commit 48dd006
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
- Added support for properties in `gd2py`
- Added support for `get():` property syntax
- Added support for multiline arrays and dictionaries in `match` statement branches
- Added support for guarded `match` branches

### Changed
- Fixed support for `breakpoint` statement in formatter
Expand Down
11 changes: 11 additions & 0 deletions gdtoolkit/formatter/function_statement.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from .context import Context, ExpressionContext
from .types import Outcome, FormattedLines
from .expression import format_expression
from .expression_to_str import expression_to_str
from .block import format_block, reconstruct_blank_lines_in_range
from .statement_utils import format_simple_statement
from .var_statement import format_var_statement
Expand All @@ -32,6 +33,7 @@ def format_func_statement(statement: Tree, context: Context) -> Outcome:
"annotation": format_standalone_annotation,
# fake statements:
"match_branch": _format_match_branch,
"guarded_match_branch": _format_guarded_match_branch,
} # type: Dict[str, Callable]
return handlers[statement.data](statement, context)

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


def _format_guarded_match_branch(statement: Tree, context: Context) -> Outcome:
# TODO: fold pattern as well
pattern_str = expression_to_str(statement.children[0].children[0])
prefix = f"{pattern_str} when "
suffix = ":"
expr_position = 1
return _format_branch(prefix, suffix, expr_position, statement, context)


def _format_branch(
prefix: str,
suffix: str,
Expand Down
3 changes: 2 additions & 1 deletion gdtoolkit/parser/gdscript.lark
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,8 @@ for_stmt: "for" NAME "in" expr ":" _func_suite
for_stmt_typed: "for" _for_iterator_var_typed "in" expr ":" _func_suite
match_stmt: "match" expr ":" _match_body
_match_body: _NL _INDENT match_branch+ _DEDENT
match_branch: pattern ":" _func_suite
?match_branch: pattern ":" _func_suite
| pattern "when" expr ":" _func_suite -> guarded_match_branch

pattern: list_pattern
?list_pattern: test_pattern ("," test_pattern)*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ class X:
pass
[_,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]:
pass
[_,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]:
pass
{'aaaaa', 'bbbbbbbbbbbbbbbbbbbbb': 1111, 'ccccccccccccccccccccc': 1111, 'ddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd': 1111}:
pass
[1,2,]:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,50 @@ class X:
1
]:
pass
[_, 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
]:
pass
{
"aaaaa",
"bbbbbbbbbbbbbbbbbbbbb": 1111,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ class X:
pass
1 if 1 else 2:
pass
2 when true:
pass
2 when 1 == 1:
pass
2 when 1.0 as int:
pass
_:
pass
match Vector3(1,1,1):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ class X:
pass
1 if 1 else 2:
pass
2 when true:
pass
2 when 1 == 1:
pass
2 when 1.0 as int:
pass
_:
pass
match Vector3(1, 1, 1):
Expand Down
8 changes: 8 additions & 0 deletions tests/valid-gd-scripts/match.gd
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@ func foo(x):
match x:
1:
pass
5 when true:
pass
6 when 1 + 2 > 0:
pass
7 when y.aaa.bb == 0:
pass
8 when 1.0 as int:
pass
# TODO: remove support for
# y.aaa:
# pass
Expand Down

0 comments on commit 48dd006

Please sign in to comment.