roc-lang / roc

A fast, friendly, functional language.
https://roc-lang.org
Universal Permissive License v1.0
4.46k stars 313 forks source link

Print string literals with escape sequence #6923

Open basile-henry opened 3 months ago

basile-henry commented 3 months ago

The current strategy for printing string literals (in the REPL for example), seems to be: print the string, add some quotes around it (triple quotes when the need for multi-line is detected)

The problem is that it ends up creating invalid string literals, or at least string literals that don't correspond to the original string when it happens to be valid.

Concretely this is incorrect:

» "Problematic non-escaped prints: \r, \", \\, \$(not_interpolated)"

"""
, ", \, $(not_interpolated)nts:
""" : Str

I believe all the escaped characters should be printed as they are written in the source code when printing a string literal (not when printing the content of the string of course). There might be some exceptions when there are multiple ways to write a character:

There is probably an open question for \t which seems to be valid in string literals in its non-escape-sequence form, which I am personally not a fan of, as it's seems error prone.

basile-henry commented 3 months ago

Another fun one in the REPL:

» "\"\"\""

"""
"""
""" : Str