-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Fix ASTNode#to_s to use escape sequence correctly #5842
Fix ASTNode#to_s to use escape sequence correctly #5842
Conversation
Wouldn't it be better to implement the escape logic from #5841 in a class method so it can be reused here? |
@straight-shoota Good idea. I'll fix this PR after #5841 is merged. |
e4a7fd6
to
0fb38bd
Compare
Updated. Escape logic is only in |
@@ -499,9 +499,9 @@ module Crystal | |||
@str << '`' | |||
case exp | |||
when StringLiteral | |||
@str << exp.value.inspect[1..-2] | |||
@str << exp.value.inspect_unquoted.gsub('`', "\\`") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
inspect_unquoted
will escape \"
which is not wrong but also not needed here. Not sure if this should be changed in this PR as well.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes. backtick operator must need escaping backtick and null character. However, it also accepts all string escape sequences, so I use inspect_unquoted
.
src/compiler/crystal/syntax/to_s.cr
Outdated
@@ -968,9 +968,9 @@ module Crystal | |||
@str << "/" | |||
case exp = node.value | |||
when StringLiteral | |||
@str << exp.value.gsub('/', "\\/") | |||
@str << escape_regex exp.value |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Regex.append_source(exp.value, @str)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes.
src/compiler/crystal/syntax/to_s.cr
Outdated
when StringInterpolation | ||
visit_interpolation exp, &.gsub('/', "\\/") | ||
visit_interpolation(exp) { |s| escape_regex s } | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ditto
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think no ditto here.
src/compiler/crystal/syntax/to_s.cr
Outdated
when StringInterpolation | ||
visit_interpolation exp, &.gsub('/', "\\/") | ||
visit_interpolation(exp) { |s| escape_regex s } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not use Regex.append_source
directly? visit_interpolation
simply appends to @str
. You can do that in the block as well and don't need the string builder. Just make sure the block returns nil
or empty string.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You are trick star!! Yeah!
crystal-lang#5842 (comment) It is super tricky way. But @straight-shoota said, so it is right way also.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great work!
@makenowjust this need a rebase 👍 |
ASTNode#to_s against StringInterpolation, RegexLiteral, and Call of backtick operator is buggy, it is missing to use escape sequence. For example `pp "#{1}\0"` cannot be compiled by current compiler.
crystal-lang#5842 (comment) It is super tricky way. But @straight-shoota said, so it is right way also.
3f1f6f1
to
9282181
Compare
@sdogruyol Thank you. |
* Fix ASTNode#to_s to use escape sequence correctly ASTNode#to_s against StringInterpolation, RegexLiteral, and Call of backtick operator is buggy, it is missing to use escape sequence. For example `pp "#{1}\0"` cannot be compiled by current compiler. * Regex: make append_source as class method for RegexLiteral#to_s * Use Regex.append_source directly * Remove escape_regex and use Regex.append_source directly for all crystal-lang#5842 (comment) It is super tricky way. But @straight-shoota said, so it is right way also.
ASTNode#to_s
againstStringInterpolation
,RegexLiteral
, andCall
of backtick operator is buggy, it is missing to use escape sequence.For example
pp "#{1}\0"
cannot be compiled by current compiler:Because
ASTNode#to_s
against"#{1}\0"
yields"#{1}
and real null character.Thank you.