Skip to content

Commit e2d1e17

Browse files
committed
Merge remote-tracking branch 'upstream/master' into support-encoding-keyword
2 parents 5925d92 + 71b01e3 commit e2d1e17

File tree

6 files changed

+496
-37
lines changed

6 files changed

+496
-37
lines changed

lib/rdoc/parser/ruby.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -752,6 +752,9 @@ def parse_class container, single, tk, comment
752752

753753
cls.line = line_no
754754

755+
# after end modifiers
756+
read_documentation_modifiers cls, RDoc::CLASS_MODIFIERS
757+
755758
cls
756759
end
757760

@@ -1311,6 +1314,9 @@ def parse_method(container, single, tk, comment)
13111314

13121315
meth.comment = comment
13131316

1317+
# after end modifiers
1318+
read_documentation_modifiers meth, RDoc::METHOD_MODIFIERS
1319+
13141320
@stats.add_method meth
13151321
end
13161322

@@ -1542,6 +1548,9 @@ def parse_module container, single, tk, comment
15421548
mod.add_comment comment, @top_level
15431549
parse_statements mod
15441550

1551+
# after end modifiers
1552+
read_documentation_modifiers mod, RDoc::CLASS_MODIFIERS
1553+
15451554
@stats.add_module mod
15461555
end
15471556

@@ -1715,7 +1724,6 @@ def parse_statements(container, single = NORMAL, current_method = nil,
17151724
when TkEND then
17161725
nest -= 1
17171726
if nest == 0 then
1718-
read_documentation_modifiers container, RDoc::CLASS_MODIFIERS
17191727
container.ongoing_visibility = save_visibility
17201728

17211729
parse_comment container, tk, comment unless comment.empty?

lib/rdoc/ruby_lex.rb

Lines changed: 35 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,10 @@ def token
378378
else
379379
tk = tk1
380380
end
381+
elsif (TkPLUS === tk or TkMINUS === tk) and peek(0) =~ /\d/ then
382+
tk1 = token
383+
set_token_position tk.seek, tk.line_no, tk.char_no
384+
tk = Token(tk1.class, tk.text + tk1.text)
381385
end
382386
# Tracer.off
383387
tk
@@ -450,15 +454,18 @@ def lex_init()
450454
proc{|op, io| @prev_char_no == 0 && peek(0) =~ /\s/}) do
451455
|op, io|
452456
@ltype = "="
453-
res = ''
454-
nil until getc == "\n"
457+
res = op
458+
until (ch = getc) == "\n" do
459+
res << ch
460+
end
461+
res << ch
455462

456463
until ( peek_equal?("=end") && peek(4) =~ /\s/ ) do
457464
(ch = getc)
458465
res << ch
459466
end
460467

461-
gets # consume =end
468+
res << gets # consume =end
462469

463470
@ltype = nil
464471
Token(TkRD_COMMENT, res)
@@ -602,6 +609,7 @@ def lex_init()
602609
Token(TkQUESTION)
603610
else
604611
@lex_state = :EXPR_END
612+
ch << getc if "\\" == ch
605613
Token(TkCHAR, "?#{ch}")
606614
end
607615
end
@@ -883,7 +891,8 @@ def lex_int2
883891
identify_quotation
884892
elsif peek(0) == '='
885893
getc
886-
Token(TkOPASGN, :%)
894+
@lex_state = :EXPR_BEG
895+
Token(TkOPASGN, '%')
887896
elsif @lex_state == :EXPR_ARG and @space_seen and peek(0) !~ /\s/
888897
identify_quotation
889898
else
@@ -985,7 +994,7 @@ def identify_identifier
985994

986995
ungetc
987996

988-
if (ch == "!" || ch == "?") && token[0,1] =~ /\w/ && peek(0) != "="
997+
if ((ch == "!" && peek(1) != "=") || ch == "?") && token[0,1] =~ /\w/
989998
token.concat getc
990999
end
9911000

@@ -1046,12 +1055,7 @@ def identify_identifier
10461055
@indent_stack.push token_c
10471056
end
10481057
else
1049-
if peek(0) == ':' and !peek_match?(/^::/)
1050-
token.concat getc
1051-
token_c = TkSYMBOL
1052-
else
1053-
token_c = TkIDENTIFIER
1054-
end
1058+
token_c = TkIDENTIFIER
10551059
end
10561060

10571061
elsif DEINDENT_CLAUSE.include?(token)
@@ -1063,13 +1067,17 @@ def identify_identifier
10631067
@lex_state = :EXPR_END
10641068
end
10651069
end
1070+
if token_c.ancestors.include?(TkId) and peek(0) == ':' and !peek_match?(/^::/)
1071+
token.concat getc
1072+
token_c = TkSYMBOL
1073+
end
10661074
return Token(token_c, token)
10671075
end
10681076
end
10691077

10701078
if @lex_state == :EXPR_FNAME
10711079
@lex_state = :EXPR_END
1072-
if peek(0) == '='
1080+
if peek(0) == '=' and peek(1) != '>'
10731081
token.concat getc
10741082
end
10751083
elsif @lex_state == :EXPR_BEG || @lex_state == :EXPR_DOT ||
@@ -1081,19 +1089,20 @@ def identify_identifier
10811089

10821090
if token[0, 1] =~ /[A-Z]/
10831091
if token[-1] =~ /[!?]/
1084-
return Token(TkIDENTIFIER, token)
1092+
token_c = TkIDENTIFIER
10851093
else
1086-
return Token(TkCONSTANT, token)
1094+
token_c = TkCONSTANT
10871095
end
10881096
elsif token[token.size - 1, 1] =~ /[!?]/
1089-
return Token(TkFID, token)
1097+
token_c = TkFID
10901098
else
1091-
if peek(0) == ':' and !peek_match?(/^::/)
1092-
token.concat getc
1093-
return Token(TkSYMBOL, token)
1094-
else
1095-
return Token(TkIDENTIFIER, token)
1096-
end
1099+
token_c = TkIDENTIFIER
1100+
end
1101+
if peek(0) == ':' and !peek_match?(/^::/)
1102+
token.concat getc
1103+
return Token(TkSYMBOL, token)
1104+
else
1105+
return Token(token_c, token)
10971106
end
10981107
end
10991108

@@ -1132,7 +1141,7 @@ def identify_here_document(op)
11321141
indent: indent,
11331142
started: false
11341143
}
1135-
@lex_state = :EXPR_BEG
1144+
@lex_state = :EXPR_END
11361145
Token(RDoc::RubyLex::TkHEREDOCBEG, start_token)
11371146
end
11381147

@@ -1331,13 +1340,13 @@ def identify_string(ltype, quoted = ltype, type = nil)
13311340
ungetc
13321341
end
13331342
elsif ch == '\\'
1334-
if %w[' /].include? @ltype then
1343+
case @ltype
1344+
when "'" then
13351345
case ch = getc
1336-
when "\n", "'"
1337-
when @ltype
1346+
when "'", '\\' then
13381347
str << ch
13391348
else
1340-
ungetc
1349+
str << ch
13411350
end
13421351
else
13431352
str << read_escape

lib/rdoc/token_stream.rb

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,18 @@ def self.to_html token_stream
4444
when RDoc::RubyToken::TkVal then 'ruby-value'
4545
end
4646

47-
text = CGI.escapeHTML t.text
47+
comment_with_nl = false
48+
case t
49+
when RDoc::RubyToken::TkRD_COMMENT, RDoc::RubyToken::TkHEREDOCEND
50+
comment_with_nl = true if t.text =~ /\n$/
51+
text = t.text.rstrip
52+
else
53+
text = t.text
54+
end
55+
text = CGI.escapeHTML text
4856

4957
if style then
50-
"<span class=\"#{style}\">#{text}</span>"
58+
"<span class=\"#{style}\">#{text}</span>#{"\n" if comment_with_nl}"
5159
else
5260
text
5361
end

test/test_rdoc_markup_to_html.rb

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,66 @@ def test_accept_verbatim_pipe
485485
assert_equal expected, @to.res.join
486486
end
487487

488+
def test_accept_verbatim_escape_in_string
489+
code = <<-'RUBY'
490+
def foo
491+
[
492+
'\\',
493+
'\'',
494+
"'",
495+
"\'\"\`",
496+
"\#",
497+
"\#{}",
498+
"#",
499+
"#{}",
500+
/'"/,
501+
/\'\"/,
502+
/\//,
503+
/\\/,
504+
/\#/,
505+
/\#{}/,
506+
/#/,
507+
/#{}/
508+
]
509+
end
510+
def bar
511+
end
512+
RUBY
513+
verb = @RM::Verbatim.new(*code.split(/(?<=\n)/))
514+
515+
@to.start_accepting
516+
@to.accept_verbatim verb
517+
518+
expected = <<-'EXPECTED'
519+
520+
<pre class="ruby"><span class="ruby-keyword">def</span> <span class="ruby-identifier">foo</span>
521+
[
522+
<span class="ruby-string">&#39;\\&#39;</span>,
523+
<span class="ruby-string">&#39;\&#39;&#39;</span>,
524+
<span class="ruby-string">&quot;&#39;&quot;</span>,
525+
<span class="ruby-string">&quot;\&#39;\&quot;\`&quot;</span>,
526+
<span class="ruby-string">&quot;\#&quot;</span>,
527+
<span class="ruby-string">&quot;\#{}&quot;</span>,
528+
<span class="ruby-string">&quot;#&quot;</span>,
529+
<span class="ruby-node">&quot;#{}&quot;</span>,
530+
<span class="ruby-regexp">/&#39;&quot;/</span>,
531+
<span class="ruby-regexp">/\&#39;\&quot;/</span>,
532+
<span class="ruby-regexp">/\//</span>,
533+
<span class="ruby-regexp">/\\/</span>,
534+
<span class="ruby-regexp">/\#/</span>,
535+
<span class="ruby-regexp">/\#{}/</span>,
536+
<span class="ruby-regexp">/#/</span>,
537+
<span class="ruby-regexp">/#{}/</span>
538+
]
539+
<span class="ruby-keyword">end</span>
540+
<span class="ruby-keyword">def</span> <span class="ruby-identifier">bar</span>
541+
<span class="ruby-keyword">end</span>
542+
</pre>
543+
EXPECTED
544+
545+
assert_equal expected, @to.res.join
546+
end
547+
488548
def test_accept_verbatim_ruby
489549
verb = @RM::Verbatim.new("1 + 1\n")
490550
verb.format = :ruby

0 commit comments

Comments
 (0)