From 9a4ab515a1295436e556106857bd9df31fb0531c Mon Sep 17 00:00:00 2001 From: Pawel Lampe Date: Sun, 10 Dec 2023 18:10:29 +0100 Subject: [PATCH] Add support for r-strings, closes #257 --- CHANGELOG.md | 1 + gdtoolkit/formatter/expression_to_str.py | 1 + gdtoolkit/formatter/expression_utils.py | 1 + gdtoolkit/formatter/safety_checks.py | 4 ++++ gdtoolkit/parser/gdscript.lark | 5 ++++- .../formatter/input-output-pairs/rstrings.in.gd | 17 +++++++++++++++++ .../input-output-pairs/rstrings.out.gd | 17 +++++++++++++++++ tests/valid-gd-scripts/rstrings.gd | 5 +++++ 8 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 tests/formatter/input-output-pairs/rstrings.in.gd create mode 100644 tests/formatter/input-output-pairs/rstrings.out.gd create mode 100644 tests/valid-gd-scripts/rstrings.gd diff --git a/CHANGELOG.md b/CHANGELOG.md index 5aedfd4a..c4b4b325 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ ### Added - Added support for multiline patterns within `match` statement branches + - Added support for r-strings ## [4.2.0] 2023-11-30 diff --git a/gdtoolkit/formatter/expression_to_str.py b/gdtoolkit/formatter/expression_to_str.py index 31aebcc0..d0be50f0 100644 --- a/gdtoolkit/formatter/expression_to_str.py +++ b/gdtoolkit/formatter/expression_to_str.py @@ -77,6 +77,7 @@ def expression_to_str(expression: Node) -> str: "c_dict_element": _dict_element_to_str, "eq_dict_element": _dict_element_to_str, "string": lambda e: expression_to_str(e.children[0]), + "rstring": lambda e: f"r{expression_to_str(e.children[0])}", "get_node": lambda e: f"${expression_to_str(e.children[0])}", "path": lambda e: "".join([name_token.value for name_token in e.children]), "node_path": lambda e: f"^{expression_to_str(e.children[0])}", diff --git a/gdtoolkit/formatter/expression_utils.py b/gdtoolkit/formatter/expression_utils.py index 786c8538..b147ad84 100644 --- a/gdtoolkit/formatter/expression_utils.py +++ b/gdtoolkit/formatter/expression_utils.py @@ -17,6 +17,7 @@ def is_foldable(expression: Node) -> bool: return True return not isinstance(expression, Token) and expression.data not in [ "string", + "rstring", "get_node", "node_path", "string_name", diff --git a/gdtoolkit/formatter/safety_checks.py b/gdtoolkit/formatter/safety_checks.py index 528d6feb..8facc758 100644 --- a/gdtoolkit/formatter/safety_checks.py +++ b/gdtoolkit/formatter/safety_checks.py @@ -69,6 +69,10 @@ def string(self, args): string_token = args[0] return expression_to_str(string_token) + def rstring(self, args): + string_token = args[0] + return expression_to_str(string_token) + def par_pattern(self, args): return args[0] if len(args) > 0 else args diff --git a/gdtoolkit/parser/gdscript.lark b/gdtoolkit/parser/gdscript.lark index 82a78492..a163a4bb 100644 --- a/gdtoolkit/parser/gdscript.lark +++ b/gdtoolkit/parser/gdscript.lark @@ -253,6 +253,7 @@ _simple_lambda_stmt: single_lambda_stmt (";" single_lambda_stmt)* [";"] | expr_stmt ?literal: NUMBER | string + | rstring | get_node | "^" string -> node_path | "&" string -> string_name @@ -281,7 +282,9 @@ DECIMAL: INT "." INT? | "." INT INT: DIGIT (DIGIT | "_")* DIGIT: "0".."9" -string: LONG_STRING | REGULAR_STRING +rstring: "r" _string +string: _string +_string: LONG_STRING | REGULAR_STRING REGULAR_STRING: /("(?!"").*?(?