From 9c4fa1cd596149d71e9d867416f3bb7b3a2fed3e Mon Sep 17 00:00:00 2001 From: Pawel Lampe Date: Mon, 11 Dec 2023 22:45:25 +0100 Subject: [PATCH] Fix support for r-strings --- CHANGELOG.md | 5 +++++ gdtoolkit/formatter/expression_to_str.py | 12 ++++++++++++ gdtoolkit/parser/gdscript.lark | 9 +++++---- setup.py | 2 +- tests/valid-gd-scripts/rstrings.gd | 5 +++++ 5 files changed, 28 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 13033ea8..111a6f21 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # Changelog +## [4.2.2] 2023-12-11 + +### Changed + - Fixed support for r-strings + ## [4.2.1] 2023-12-10 ### Added diff --git a/gdtoolkit/formatter/expression_to_str.py b/gdtoolkit/formatter/expression_to_str.py index d0be50f0..75450b98 100644 --- a/gdtoolkit/formatter/expression_to_str.py +++ b/gdtoolkit/formatter/expression_to_str.py @@ -20,7 +20,9 @@ def expression_to_str(expression: Node) -> str: if isinstance(expression, Token): token_handlers = { "LONG_STRING": _long_string_to_str, + "LONG_RSTRING": _long_rstring_to_str, "REGULAR_STRING": _regular_string_to_str, + "REGULAR_RSTRING": _regular_rstring_to_str, } if expression.type in token_handlers: return token_handlers[expression.type](expression) @@ -317,6 +319,11 @@ def _long_string_to_str(string: Token) -> str: return actual_string +def _long_rstring_to_str(rstring: Token) -> str: + actual_string = rstring.value + return _long_string_to_str(Token("LONG_STRING", actual_string[1:])) + + def _regular_string_to_str(string: Token) -> str: actual_string = string.value actual_string_data = actual_string[1:-1] @@ -330,3 +337,8 @@ def _regular_string_to_str(string: Token) -> str: actual_string_data = actual_string_data.replace('\\"', '"') actual_string_data = actual_string_data.replace("'", "\\'") return "{}{}{}".format(target, actual_string_data, target) # pylint: disable=W1308 + + +def _regular_rstring_to_str(rstring: Token) -> str: + actual_string = rstring.value + return _regular_string_to_str(Token("REGULAR_STRING", actual_string[1:])) diff --git a/gdtoolkit/parser/gdscript.lark b/gdtoolkit/parser/gdscript.lark index a163a4bb..e5ce8390 100644 --- a/gdtoolkit/parser/gdscript.lark +++ b/gdtoolkit/parser/gdscript.lark @@ -282,11 +282,12 @@ DECIMAL: INT "." INT? | "." INT INT: DIGIT (DIGIT | "_")* DIGIT: "0".."9" -rstring: "r" _string -string: _string -_string: LONG_STRING | REGULAR_STRING -REGULAR_STRING: /("(?!"").*?(?