Skip to content

Commit

Permalink
Add support for r-strings, closes #257
Browse files Browse the repository at this point in the history
  • Loading branch information
Scony committed Dec 10, 2023
1 parent f261487 commit 9a4ab51
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions gdtoolkit/formatter/expression_to_str.py
Original file line number Diff line number Diff line change
Expand Up @@ -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])}",
Expand Down
1 change: 1 addition & 0 deletions gdtoolkit/formatter/expression_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
4 changes: 4 additions & 0 deletions gdtoolkit/formatter/safety_checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
5 changes: 4 additions & 1 deletion gdtoolkit/parser/gdscript.lark
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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: /("(?!"").*?(?<!\\)(\\\\)*?"|'(?!'').*?(?<!\\)(\\\\)*?')/i
LONG_STRING: /""".*?(?<!\\)(\\\\)*?"""/is | /'''.*?(?<!\\)(\\\\)*?'''/is

Expand Down
17 changes: 17 additions & 0 deletions tests/formatter/input-output-pairs/rstrings.in.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
var x = r'x'

var x2 = r'x "'
var x3 = r'x " " \''
var x4 = r"x '"
var x5 = r"x ' ' \""

var x6 = r'x \''
var x7 = r"x \""
var x8 = r'x \' "'

var x9 = r'''x'''
var x10 = r'''x "'''

var x11 = r"""abc " def ' ghi"""

var x12 = r'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
17 changes: 17 additions & 0 deletions tests/formatter/input-output-pairs/rstrings.out.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
var x = r"x"

var x2 = r'x "'
var x3 = r'x " " \''
var x4 = r"x '"
var x5 = r"x ' ' \""

var x6 = r"x '"
var x7 = r'x "'
var x8 = r"x ' \""

var x9 = r"x"
var x10 = r'x "'

var x11 = r"""abc " def ' ghi"""

var x12 = r"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
5 changes: 5 additions & 0 deletions tests/valid-gd-scripts/rstrings.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
func foo():
print(r'" \' \ \\')
print(r"\" ' \ \\")
print(r"""aaa""")
print(r'''bbb''')

0 comments on commit 9a4ab51

Please sign in to comment.