Skip to content

Commit f17a85f

Browse files
committed
Merge branch 'fix-escape-in-string' of github.com:aycabta/rdoc into fix-escape-in-string
2 parents 0c47b30 + e104cd1 commit f17a85f

File tree

3 files changed

+160
-4
lines changed

3 files changed

+160
-4
lines changed

lib/rdoc/ruby_lex.rb

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1233,13 +1233,14 @@ def identify_string(ltype, quoted = ltype, type = nil)
12331233
ungetc
12341234
end
12351235
elsif ch == '\\'
1236-
if %w[' /].include? @ltype then
1236+
case @ltype
1237+
when "'" then
12371238
case ch = getc
1238-
when "\\", "\n", "'"
1239-
when @ltype
1239+
when "'", '\\' then
12401240
str << ch
12411241
else
1242-
ungetc
1242+
str << '\\'
1243+
str << ch
12431244
end
12441245
else
12451246
str << read_escape

test/test_rdoc_markup_to_html.rb

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,66 @@ def test_accept_verbatim_pipe
469469
assert_equal expected, @to.res.join
470470
end
471471

472+
def test_accept_verbatim_escape_in_string
473+
code = <<-'RUBY'
474+
def foo
475+
[
476+
'\\',
477+
'\'',
478+
"'",
479+
"\'\"\`",
480+
"\#",
481+
"\#{}",
482+
"#",
483+
"#{}",
484+
/'"/,
485+
/\'\"/,
486+
/\//,
487+
/\\/,
488+
/\#/,
489+
/\#{}/,
490+
/#/,
491+
/#{}/
492+
]
493+
end
494+
def bar
495+
end
496+
RUBY
497+
verb = @RM::Verbatim.new(*code.split(/(?<=\n)/))
498+
499+
@to.start_accepting
500+
@to.accept_verbatim verb
501+
502+
expected = <<-'EXPECTED'
503+
504+
<pre class="ruby"><span class="ruby-keyword">def</span> <span class="ruby-identifier">foo</span>
505+
[
506+
<span class="ruby-string">&#39;\\&#39;</span>,
507+
<span class="ruby-string">&#39;\&#39;&#39;</span>,
508+
<span class="ruby-string">&quot;&#39;&quot;</span>,
509+
<span class="ruby-string">&quot;\&#39;\&quot;\`&quot;</span>,
510+
<span class="ruby-string">&quot;\#&quot;</span>,
511+
<span class="ruby-string">&quot;\#{}&quot;</span>,
512+
<span class="ruby-string">&quot;#&quot;</span>,
513+
<span class="ruby-node">&quot;#{}&quot;</span>,
514+
<span class="ruby-regexp">/&#39;&quot;/</span>,
515+
<span class="ruby-regexp">/\&#39;\&quot;/</span>,
516+
<span class="ruby-regexp">/\//</span>,
517+
<span class="ruby-regexp">/\\/</span>,
518+
<span class="ruby-regexp">/\#/</span>,
519+
<span class="ruby-regexp">/\#{}/</span>,
520+
<span class="ruby-regexp">/#/</span>,
521+
<span class="ruby-regexp">/#{}/</span>
522+
]
523+
<span class="ruby-keyword">end</span>
524+
<span class="ruby-keyword">def</span> <span class="ruby-identifier">bar</span>
525+
<span class="ruby-keyword">end</span>
526+
</pre>
527+
EXPECTED
528+
529+
assert_equal expected, @to.res.join
530+
end
531+
472532
def test_accept_verbatim_ruby
473533
verb = @RM::Verbatim.new("1 + 1\n")
474534
verb.format = :ruby

test/test_rdoc_ruby_lex.rb

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,101 @@ def test_class_tokenize_string
350350
assert_equal expected, tokens
351351
end
352352

353+
def test_class_tokenize_string_with_escape
354+
tokens = RDoc::RubyLex.tokenize <<'RUBY', nil
355+
[
356+
'\\',
357+
'\'',
358+
"'",
359+
"\'\"\`",
360+
"\#",
361+
"\#{}",
362+
"#",
363+
"#{}",
364+
/'"/,
365+
/\'\"/,
366+
/\//,
367+
/\\/,
368+
/\#/,
369+
/\#{}/,
370+
/#/,
371+
/#{}/
372+
]
373+
RUBY
374+
375+
expected = [
376+
@TK::TkLBRACK .new( 0, 1, 0, "["),
377+
@TK::TkNL .new( 1, 1, 1, "\n"),
378+
@TK::TkSPACE .new( 2, 2, 0, " "),
379+
@TK::TkSTRING .new( 4, 2, 2, "'\\\\'"),
380+
@TK::TkCOMMA .new( 8, 2, 6, ","),
381+
@TK::TkNL .new( 9, 2, 2, "\n"),
382+
@TK::TkSPACE .new( 10, 3, 0, " "),
383+
@TK::TkSTRING .new( 12, 3, 2, "'\\''"),
384+
@TK::TkCOMMA .new( 16, 3, 6, ","),
385+
@TK::TkNL .new( 17, 3, 10, "\n"),
386+
@TK::TkSPACE .new( 18, 4, 0, " "),
387+
@TK::TkSTRING .new( 20, 4, 2, "\"'\""),
388+
@TK::TkCOMMA .new( 23, 4, 5, ","),
389+
@TK::TkNL .new( 24, 4, 18, "\n"),
390+
@TK::TkSPACE .new( 25, 5, 0, " "),
391+
@TK::TkSTRING .new( 27, 5, 2, "\"\\'\\\"\\`\""),
392+
@TK::TkCOMMA .new( 35, 5, 10, ","),
393+
@TK::TkNL .new( 36, 5, 25, "\n"),
394+
@TK::TkSPACE .new( 37, 6, 0, " "),
395+
@TK::TkSTRING .new( 39, 6, 2, "\"\\#\""),
396+
@TK::TkCOMMA .new( 43, 6, 6, ","),
397+
@TK::TkNL .new( 44, 6, 37, "\n"),
398+
@TK::TkSPACE .new( 45, 7, 0, " "),
399+
@TK::TkSTRING .new( 47, 7, 2, "\"\\\#{}\""),
400+
@TK::TkCOMMA .new( 53, 7, 8, ","),
401+
@TK::TkNL .new( 54, 7, 45, "\n"),
402+
@TK::TkSPACE .new( 55, 8, 0, " "),
403+
@TK::TkSTRING .new( 57, 8, 2, "\"#\""),
404+
@TK::TkCOMMA .new( 60, 8, 5, ","),
405+
@TK::TkNL .new( 61, 8, 55, "\n"),
406+
@TK::TkSPACE .new( 62, 9, 0, " "),
407+
@TK::TkDSTRING.new( 64, 9, 2, "\"\#{}\""),
408+
@TK::TkCOMMA .new( 69, 9, 7, ","),
409+
@TK::TkNL .new( 70, 9, 62, "\n"),
410+
@TK::TkSPACE .new( 71, 10, 0, " "),
411+
@TK::TkREGEXP .new( 73, 10, 2, "/'\"/"),
412+
@TK::TkCOMMA .new( 77, 10, 6, ","),
413+
@TK::TkNL .new( 78, 10, 71, "\n"),
414+
@TK::TkSPACE .new( 79, 11, 0, " "),
415+
@TK::TkREGEXP .new( 81, 11, 2, "/\\'\\\"/"),
416+
@TK::TkCOMMA .new( 87, 11, 8, ","),
417+
@TK::TkNL .new( 88, 11, 79, "\n"),
418+
@TK::TkSPACE .new( 89, 12, 0, " "),
419+
@TK::TkREGEXP .new( 91, 12, 2, "/\\//"),
420+
@TK::TkCOMMA .new( 95, 12, 6, ","),
421+
@TK::TkNL .new( 96, 12, 89, "\n"),
422+
@TK::TkSPACE .new( 97, 13, 0, " "),
423+
@TK::TkREGEXP .new( 99, 13, 2, "/\\\\/"),
424+
@TK::TkCOMMA .new(103, 13, 6, ","),
425+
@TK::TkNL .new(104, 13, 97, "\n"),
426+
@TK::TkSPACE .new(105, 14, 0, " "),
427+
@TK::TkREGEXP .new(107, 14, 2, "/\\#/"),
428+
@TK::TkCOMMA .new(111, 14, 6, ","),
429+
@TK::TkNL .new(112, 14, 105, "\n"),
430+
@TK::TkSPACE .new(113, 15, 0, " "),
431+
@TK::TkREGEXP .new(115, 15, 2, "/\\\#{}/"),
432+
@TK::TkCOMMA .new(121, 15, 8, ","),
433+
@TK::TkNL .new(122, 15, 113, "\n"),
434+
@TK::TkSPACE .new(123, 16, 0, " "),
435+
@TK::TkREGEXP .new(125, 16, 2, "/#/"),
436+
@TK::TkCOMMA .new(128, 16, 5, ","),
437+
@TK::TkNL .new(129, 16, 123, "\n"),
438+
@TK::TkSPACE .new(130, 17, 0, " "),
439+
@TK::TkDREGEXP.new(132, 17, 2, "/\#{}/"),
440+
@TK::TkNL .new(137, 17, 7, "\n"),
441+
@TK::TkRBRACK .new(138, 18, 0, "]"),
442+
@TK::TkNL .new(139, 18, 138, "\n")
443+
]
444+
445+
assert_equal expected, tokens
446+
end
447+
353448
def test_class_tokenize_string_escape
354449
tokens = RDoc::RubyLex.tokenize '"\\n"', nil
355450
assert_equal @TK::TkSTRING.new( 0, 1, 0, "\"\\n\""), tokens.first

0 commit comments

Comments
 (0)