styler icon indicating copy to clipboard operation
styler copied to clipboard

Styler throws "not parseable" error on parseable script using unprintable ASCII character literal "\1"

Open MichaelChirico opened this issue 3 years ago • 5 comments

styler::style_text(R'( "\1" )')
Error in `value[[3L]]()`:
! Styling resulted in code that isn't parsable. This should not happen. Please file a bug report on GitHub (https://github.com/r-lib/styler/issues) using a reprex.
Run `rlang::last_error()` to see where the error occurred.

Of course, this is parseable:

parse(text = R'( "\1" )')
# expression("\1")

MichaelChirico avatar Aug 27 '22 06:08 MichaelChirico

Oh, I think this is because of the R bug (https://bugs.r-project.org/show_bug.cgi?id=18323; fixed in devel, and maybe 4.2.1?); lintr has this helper as a workaround:

https://github.com/r-lib/lintr/blob/7d8c2c4891961d0a99d52d42c513ecb9fe851bea/R/get_source_expressions.R#L686-L688

MichaelChirico avatar Aug 27 '22 06:08 MichaelChirico

Careful:

r"{(\1\2)}"
#> [1] "(\\1\\2)"
"(\1\2)"
#> [1] "(\001\002)"

Created on 2022-09-07 by the reprex package (v2.0.1)

krlmlr avatar Sep 07 '22 11:09 krlmlr

Sorry I can't follow. @MichaelChirico if you want this fixed, I am afraid you have to make a PR (or @krlmlr can do).

lorenzwalthert avatar Oct 22 '22 14:10 lorenzwalthert

Kirill, to clarify, I'm using raw strings to avoid two-layer-escaping -- your latter example is the code that causes issues for styler. Look at the getParseData output for it -- the column numbers are wrong (on 4.2.0 and earlier, at least). lintr's workaround corrects the column numbers.

MichaelChirico avatar Oct 22 '22 14:10 MichaelChirico

We can work around, or accept that you need recent R to style these literals. I don't have a strong preference.

krlmlr avatar Oct 23 '22 10:10 krlmlr