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

Fix parsing of "\\\n" escaped sequences in various literals. #539

Conversation

iliabylich
Copy link
Collaborator

Closes #537

Introduced Literal#supports_line_continuation_via_slash? method that returns true/false depending on the type of the literal. I wasn't able to find any correlation between interpolation and escaping rules. Looks like that's a completely new separate property of the literal. Ruby ❤️

Added spec that verifies that "\\\n" gets properly escaped for all kinds of literals. @whitequark Please let me know if I should convert it from acceptance table to a list of plain assertions.

test data

$ cat test.rb
def `(str); str; end

p 'a\
b'

p <<'HERE'
  a\
  b
HERE

p %q{a\
b}

p "a\
b"

p <<"HERE"
  a\
  b
HERE

p %{a\
b}

p %Q{a\
b}

p %w{a\
b}

p %W{a\
b}

p %i{a\
b}
p %I{a\
b}

p :'a\
b'

p %s{a\
b}

p :"a\
b"

p /a\
b/

p %r{a\
b}

p %x{a\
b}

p `a\
b`

p <<`HERE`
a\
b
HERE

ruby -e

$ ruby test.rb
"a\\\nb"
"  a\\\n  b\n"
"a\\\nb"
"ab"
"  a  b\n"
"ab"
"ab"
["a\nb"]
["a\nb"]
[:"a\nb"]
[:"a\nb"]
:"a\\\nb"
:"a\\\nb"
:ab
/ab/
/ab/
"ab"
"ab"
"ab\n"

bin/ruby-parse

$ bin/ruby-parse test.rb
warning: parser/current is loading parser/ruby26, which recognizes
warning: 2.6.0-dev-compliant syntax, but you are running 2.6.0.
warning: please see https://github.com/whitequark/parser#compatibility-with-ruby-mri.
(begin
  (def :`
    (args
      (arg :str))
    (lvar :str))
  (send nil :p
    (dstr
      (str "a\\\n")
      (str "b")))
  (send nil :p
    (dstr
      (str "  a\\\n")
      (str "  b\n")))
  (send nil :p
    (dstr
      (str "a\\\n")
      (str "b")))
  (send nil :p
    (str "ab"))
  (send nil :p
    (str "  a  b\n"))
  (send nil :p
    (str "ab"))
  (send nil :p
    (str "ab"))
  (send nil :p
    (array
      (str "a\nb")))
  (send nil :p
    (array
      (str "a\nb")))
  (send nil :p
    (array
      (sym :"a\nb")))
  (send nil :p
    (array
      (sym :"a\nb")))
  (send nil :p
    (dsym
      (str "a\\\n")
      (str "b")))
  (send nil :p
    (dsym
      (str "a\\\n")
      (str "b")))
  (send nil :p
    (sym :ab))
  (send nil :p
    (regexp
      (str "ab")
      (regopt)))
  (send nil :p
    (regexp
      (str "ab")
      (regopt)))
  (send nil :p
    (xstr
      (str "ab")))
  (send nil :p
    (xstr
      (str "ab")))
  (send nil :p
    (xstr
      (str "ab\n"))))

lib/parser/lexer/literal.rb Outdated Show resolved Hide resolved
@iliabylich iliabylich force-pushed the fix-slash-slash-n-escaping-in-literals branch from 41cb059 to 1e76123 Compare December 26, 2018 22:33
@iliabylich iliabylich merged commit ed352d6 into whitequark:master Dec 29, 2018
@iliabylich iliabylich deleted the fix-slash-slash-n-escaping-in-literals branch December 29, 2018 13:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants