Skip to content

Commit

Permalink
Fix support for r-strings
Browse files Browse the repository at this point in the history
  • Loading branch information
Scony committed Dec 11, 2023
1 parent 09af174 commit 9c4fa1c
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 5 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## [4.2.2] 2023-12-11

### Changed
- Fixed support for r-strings

## [4.2.1] 2023-12-10

### Added
Expand Down
12 changes: 12 additions & 0 deletions gdtoolkit/formatter/expression_to_str.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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]
Expand All @@ -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:]))
9 changes: 5 additions & 4 deletions gdtoolkit/parser/gdscript.lark
Original file line number Diff line number Diff line change
Expand Up @@ -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: /("(?!"").*?(?<!\\)(\\\\)*?"|'(?!'').*?(?<!\\)(\\\\)*?')/i
string: LONG_STRING | REGULAR_STRING
rstring: LONG_RSTRING | REGULAR_RSTRING
LONG_STRING: /""".*?(?<!\\)(\\\\)*?"""/is | /'''.*?(?<!\\)(\\\\)*?'''/is
LONG_RSTRING: /r""".*?(?<!\\)(\\\\)*?"""/is | /r'''.*?(?<!\\)(\\\\)*?'''/is
REGULAR_STRING: /("(?!"").*?(?<!\\)(\\\\)*?"|'(?!'').*?(?<!\\)(\\\\)*?')/i
REGULAR_RSTRING: /(r"(?!"").*?(?<!\\)(\\\\)*?"|r'(?!'').*?(?<!\\)(\\\\)*?')/i

_NL: ( /\r?\n[\t ]*/ | COMMENT )+
COMMENT: /#[^\n]*/
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

setup(
name="gdtoolkit",
version="4.2.1",
version="4.2.2",
description="Independent set of tools for working with GDScript - parser, linter and formatter",
keywords=["GODOT", "GDSCRIPT", "PARSER", "LINTER", "FORMATTER"],
url="https://github.com/Scony/godot-gdscript-toolkit",
Expand Down
5 changes: 5 additions & 0 deletions tests/valid-gd-scripts/rstrings.gd
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,8 @@ func foo():
print(r"\" ' \ \\")
print(r"""aaa""")
print(r'''bbb''')

func corner_case(r):
for x in r:
pass
var rx = 1

0 comments on commit 9c4fa1c

Please sign in to comment.