Skip to content

Commit 00734c1

Browse files
committed
Merge remote-tracking branch 'upstream/master' into support-quoted-symbol-for-json-style
2 parents b57c241 + 6a81c8a commit 00734c1

File tree

4 files changed

+382
-23
lines changed

4 files changed

+382
-23
lines changed

lib/rdoc/ruby_lex.rb

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,10 @@ def token
379379
else
380380
tk = tk1
381381
end
382+
elsif (TkPLUS === tk or TkMINUS === tk) and peek(0) =~ /\d/ then
383+
tk1 = token
384+
set_token_position tk.seek, tk.line_no, tk.char_no
385+
tk = Token(tk1.class, tk.text + tk1.text)
382386
end
383387
@after_question = false if @after_question and !(TkQUESTION === tk)
384388

@@ -887,7 +891,8 @@ def lex_int2
887891
identify_quotation
888892
elsif peek(0) == '='
889893
getc
890-
Token(TkOPASGN, :%)
894+
@lex_state = :EXPR_BEG
895+
Token(TkOPASGN, '%')
891896
elsif @lex_state == :EXPR_ARG and @space_seen and peek(0) !~ /\s/
892897
identify_quotation
893898
else
@@ -989,7 +994,7 @@ def identify_identifier
989994

990995
ungetc
991996

992-
if (ch == "!" || ch == "?") && token[0,1] =~ /\w/ && peek(0) != "="
997+
if ((ch == "!" && peek(1) != "=") || ch == "?") && token[0,1] =~ /\w/
993998
token.concat getc
994999
end
9951000

@@ -1050,12 +1055,7 @@ def identify_identifier
10501055
@indent_stack.push token_c
10511056
end
10521057
else
1053-
if peek(0) == ':' and !peek_match?(/^::/)
1054-
token.concat getc
1055-
token_c = TkSYMBOL
1056-
else
1057-
token_c = TkIDENTIFIER
1058-
end
1058+
token_c = TkIDENTIFIER
10591059
end
10601060

10611061
elsif DEINDENT_CLAUSE.include?(token)
@@ -1067,13 +1067,17 @@ def identify_identifier
10671067
@lex_state = :EXPR_END
10681068
end
10691069
end
1070+
if token_c.ancestors.include?(TkId) and peek(0) == ':' and !peek_match?(/^::/)
1071+
token.concat getc
1072+
token_c = TkSYMBOL
1073+
end
10701074
return Token(token_c, token)
10711075
end
10721076
end
10731077

10741078
if @lex_state == :EXPR_FNAME
10751079
@lex_state = :EXPR_END
1076-
if peek(0) == '='
1080+
if peek(0) == '=' and peek(1) != '>'
10771081
token.concat getc
10781082
end
10791083
elsif @lex_state == :EXPR_BEG || @lex_state == :EXPR_DOT ||
@@ -1085,19 +1089,20 @@ def identify_identifier
10851089

10861090
if token[0, 1] =~ /[A-Z]/
10871091
if token[-1] =~ /[!?]/
1088-
return Token(TkIDENTIFIER, token)
1092+
token_c = TkIDENTIFIER
10891093
else
1090-
return Token(TkCONSTANT, token)
1094+
token_c = TkCONSTANT
10911095
end
10921096
elsif token[token.size - 1, 1] =~ /[!?]/
1093-
return Token(TkFID, token)
1097+
token_c = TkFID
10941098
else
1095-
if peek(0) == ':' and !peek_match?(/^::/)
1096-
token.concat getc
1097-
return Token(TkSYMBOL, token)
1098-
else
1099-
return Token(TkIDENTIFIER, token)
1100-
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)
11011106
end
11021107
end
11031108

@@ -1136,7 +1141,7 @@ def identify_here_document(op)
11361141
indent: indent,
11371142
started: false
11381143
}
1139-
@lex_state = :EXPR_BEG
1144+
@lex_state = :EXPR_END
11401145
Token(RDoc::RubyLex::TkHEREDOCBEG, start_token)
11411146
end
11421147

@@ -1335,13 +1340,14 @@ def identify_string(ltype, quoted = ltype, type = nil)
13351340
ungetc
13361341
end
13371342
elsif ch == '\\'
1338-
if %w[' /].include? @ltype then
1343+
case @ltype
1344+
when "'" then
13391345
case ch = getc
1340-
when "\n", "'"
1341-
when @ltype
1346+
when "'", '\\' then
13421347
str << ch
13431348
else
1344-
ungetc
1349+
str << '\\'
1350+
str << ch
13451351
end
13461352
else
13471353
str << read_escape

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

test/test_rdoc_parser_ruby.rb

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2499,6 +2499,35 @@ def blah()
24992499
assert_equal markup_code, expected
25002500
end
25012501

2502+
def test_parse_statements_postfix_if_after_heredocbeg
2503+
@filename = 'file.rb'
2504+
util_parser <<RUBY
2505+
class Foo
2506+
def blah()
2507+
<<~EOM if true
2508+
EOM
2509+
end
2510+
end
2511+
RUBY
2512+
2513+
expected = <<EXPTECTED
2514+
<span class="ruby-keyword">def</span> <span class="ruby-identifier">blah</span>()
2515+
<span class="ruby-identifier">&lt;&lt;~EOM</span> <span class="ruby-keyword">if</span> <span class="ruby-keyword">true</span>
2516+
<span class="ruby-value"></span><span class="ruby-identifier"> EOM
2517+
</span> <span class="ruby-keyword">end</span>
2518+
EXPTECTED
2519+
expected = expected.rstrip
2520+
2521+
@parser.scan
2522+
2523+
foo = @top_level.classes.first
2524+
assert_equal 'Foo', foo.full_name
2525+
2526+
blah = foo.method_list.first
2527+
markup_code = blah.markup_code.sub(/^.*\n/, '')
2528+
assert_equal markup_code, expected
2529+
end
2530+
25022531
def test_parse_require_dynamic_string
25032532
content = <<-RUBY
25042533
prefix = 'path'

0 commit comments

Comments
 (0)