Skip to content

Fix complex condition in for/while/until #480

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

Merged
merged 2 commits into from
Aug 8, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 3 additions & 9 deletions lib/rdoc/parser/ruby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2022,31 +2022,25 @@ def scan
def skip_optional_do_after_expression
skip_tkspace false
tk = get_tk
end_token = get_end_token tk

b_nest = 0
nest = 0
@scanner.continue = false

loop do
case tk
when TkSEMICOLON then
when TkSEMICOLON, TkNL then
break if b_nest.zero?
when TkLPAREN, TkfLPAREN then
nest += 1
when TkRPAREN then
nest -= 1
when TkBEGIN then
b_nest += 1
when TkEND then
b_nest -= 1
when TkDO
break if nest.zero?
when end_token then
if end_token == TkRPAREN
nest -= 1
break if @scanner.lex_state == :EXPR_END and nest.zero?
else
break unless @scanner.continue
end
when nil then
break
end
Expand Down
32 changes: 32 additions & 0 deletions test/test_rdoc_parser_ruby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2446,6 +2446,38 @@ class DateTime < Date
assert_equal :private, date_time_now.visibility, date_time_now.full_name
end

def test_parse_statements_complex_condition_in_for
util_parser <<RUBY
class Foo
def blah()
for i in (k)...n do
end
for i in (k)...n
end
end
end
RUBY

expected = <<EXPTECTED
<span class="ruby-keyword">def</span> <span class="ruby-identifier">blah</span>()
<span class="ruby-keyword">for</span> <span class="ruby-identifier">i</span> <span class="ruby-keyword">in</span> (<span class="ruby-identifier">k</span>)<span class="ruby-operator">...</span><span class="ruby-identifier">n</span> <span class="ruby-keyword">do</span>
<span class="ruby-keyword">end</span>
<span class="ruby-keyword">for</span> <span class="ruby-identifier">i</span> <span class="ruby-keyword">in</span> (<span class="ruby-identifier">k</span>)<span class="ruby-operator">...</span><span class="ruby-identifier">n</span>
<span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span>
EXPTECTED
expected = expected.rstrip

@parser.scan

foo = @top_level.classes.first
assert_equal 'Foo', foo.full_name

blah = foo.method_list.first
markup_code = blah.markup_code.sub(/^.*\n/, '')
assert_equal markup_code, expected
end

def test_parse_require_dynamic_string
content = <<-RUBY
prefix = 'path'
Expand Down