Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Odd number of reverse solidus \ characters aren't allowed at the end of a raw string. #90350

Closed
Lielay9 opened this issue Apr 7, 2024 · 2 comments · Fixed by godotengine/godot-docs#9207

Comments

@Lielay9
Copy link
Contributor

Lielay9 commented Apr 7, 2024

Tested versions

v4.3.dev5.official [89f70e9]

System information

Godot v4.3.dev5 - Windows 10.0.22631 - GLES3 (Compatibility) - NVIDIA GeForce RTX 4090 (NVIDIA; 31.0.15.3699) - AMD Ryzen 9 7950X 16-Core Processor (32 Threads)

Issue description

Odd number of reverse solidus \ characters aren't allowed at the end of a raw string.

The following is valid syntax but results in "Unterminated string." - error:

r"\"

The following is valid syntax and works:

r"\\"

Bonus: The following is valid syntax and works, but is highlighted wrong:

r"
\\"

All steps also apply for multiline raw strings.

Steps to reproduce

Write:

r"\"

in a gdscript file.

Minimal reproduction project (MRP)

Tiny mrp.
raw_strings.zip

@dalexeev
Copy link
Member

dalexeev commented Apr 7, 2024

This is intentional. You cannot represent a string with an odd number of trailing backslashes using a raw string literal, because \" is escaped to \" and \\ is escaped to \\. This is for convenience and is consistent with Python behavior. Otherwise, the very first quote that matches the initial one would be a terminal quote, which would be much more inconvenient.

In those rare cases when you need it, you can use concatenation with a regular string literal. This will not affect runtime, since constant expressions are resolved at compile time.

The following is valid syntax and works, but is highlighted wrong

This is a known bug. The highlighter in general does not work very well in the presence of line breaks, since the context is completely lost, with the exception of color regions. Specifically for raw strings, we can fix the highlighting if we use separate color regions for regular and raw strings.

@Lielay9
Copy link
Contributor Author

Lielay9 commented Apr 7, 2024

Ah, my bad. Didn't know the definition of a "raw string" differs between languages. Moreover the behavior is documented in the GDScript reference, I just wasn't smart enough to read past the two preceding contradictory clauses:

Raw string literals always encode the string as it appears in the source code. This is especially useful for regular expressions. Raw strings do not process escape sequences, but you can "escape" a quote or backslash (they replace themselves).

So the behavior is both intended and documented, hence this could be closed. Albeit the wording of the documentation could be amended.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants