Skip to content

Commit 9c4fa1c

Browse files
committed
Fix support for r-strings
1 parent 09af174 commit 9c4fa1c

File tree

5 files changed

+28
-5
lines changed

5 files changed

+28
-5
lines changed

CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Changelog
22

3+
## [4.2.2] 2023-12-11
4+
5+
### Changed
6+
- Fixed support for r-strings
7+
38
## [4.2.1] 2023-12-10
49

510
### Added

gdtoolkit/formatter/expression_to_str.py

+12
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ def expression_to_str(expression: Node) -> str:
2020
if isinstance(expression, Token):
2121
token_handlers = {
2222
"LONG_STRING": _long_string_to_str,
23+
"LONG_RSTRING": _long_rstring_to_str,
2324
"REGULAR_STRING": _regular_string_to_str,
25+
"REGULAR_RSTRING": _regular_rstring_to_str,
2426
}
2527
if expression.type in token_handlers:
2628
return token_handlers[expression.type](expression)
@@ -317,6 +319,11 @@ def _long_string_to_str(string: Token) -> str:
317319
return actual_string
318320

319321

322+
def _long_rstring_to_str(rstring: Token) -> str:
323+
actual_string = rstring.value
324+
return _long_string_to_str(Token("LONG_STRING", actual_string[1:]))
325+
326+
320327
def _regular_string_to_str(string: Token) -> str:
321328
actual_string = string.value
322329
actual_string_data = actual_string[1:-1]
@@ -330,3 +337,8 @@ def _regular_string_to_str(string: Token) -> str:
330337
actual_string_data = actual_string_data.replace('\\"', '"')
331338
actual_string_data = actual_string_data.replace("'", "\\'")
332339
return "{}{}{}".format(target, actual_string_data, target) # pylint: disable=W1308
340+
341+
342+
def _regular_rstring_to_str(rstring: Token) -> str:
343+
actual_string = rstring.value
344+
return _regular_string_to_str(Token("REGULAR_STRING", actual_string[1:]))

gdtoolkit/parser/gdscript.lark

+5-4
Original file line numberDiff line numberDiff line change
@@ -282,11 +282,12 @@ DECIMAL: INT "." INT? | "." INT
282282
INT: DIGIT (DIGIT | "_")*
283283
DIGIT: "0".."9"
284284

285-
rstring: "r" _string
286-
string: _string
287-
_string: LONG_STRING | REGULAR_STRING
288-
REGULAR_STRING: /("(?!"").*?(?<!\\)(\\\\)*?"|'(?!'').*?(?<!\\)(\\\\)*?')/i
285+
string: LONG_STRING | REGULAR_STRING
286+
rstring: LONG_RSTRING | REGULAR_RSTRING
289287
LONG_STRING: /""".*?(?<!\\)(\\\\)*?"""/is | /'''.*?(?<!\\)(\\\\)*?'''/is
288+
LONG_RSTRING: /r""".*?(?<!\\)(\\\\)*?"""/is | /r'''.*?(?<!\\)(\\\\)*?'''/is
289+
REGULAR_STRING: /("(?!"").*?(?<!\\)(\\\\)*?"|'(?!'').*?(?<!\\)(\\\\)*?')/i
290+
REGULAR_RSTRING: /(r"(?!"").*?(?<!\\)(\\\\)*?"|r'(?!'').*?(?<!\\)(\\\\)*?')/i
290291

291292
_NL: ( /\r?\n[\t ]*/ | COMMENT )+
292293
COMMENT: /#[^\n]*/

setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
setup(
55
name="gdtoolkit",
6-
version="4.2.1",
6+
version="4.2.2",
77
description="Independent set of tools for working with GDScript - parser, linter and formatter",
88
keywords=["GODOT", "GDSCRIPT", "PARSER", "LINTER", "FORMATTER"],
99
url="https://github.com/Scony/godot-gdscript-toolkit",

tests/valid-gd-scripts/rstrings.gd

+5
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,8 @@ func foo():
33
print(r"\" ' \ \\")
44
print(r"""aaa""")
55
print(r'''bbb''')
6+
7+
func corner_case(r):
8+
for x in r:
9+
pass
10+
var rx = 1

0 commit comments

Comments
 (0)