Skip to content

Handle "[]" and "[]=" after "." as method #466

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 1, 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
15 changes: 14 additions & 1 deletion lib/rdoc/ruby_lex.rb
Original file line number Diff line number Diff line change
Expand Up @@ -737,6 +737,7 @@ def lex_int2

@OP.def_rule("[") do
|op, io|
text = nil
@indent += 1
if @lex_state == :EXPR_FNAME
tk_c = TkfLBRACK
Expand All @@ -745,13 +746,25 @@ def lex_int2
tk_c = TkLBRACK
elsif @lex_state == :EXPR_ARG && @space_seen
tk_c = TkLBRACK
elsif @lex_state == :EXPR_DOT
if peek(0) == "]"
tk_c = TkIDENTIFIER
getc
if peek(0) == "="
text = "[]="
else
text = "[]"
end
else
tk_c = TkOp
end
else
tk_c = TkfLBRACK
end
@lex_state = :EXPR_BEG
end
@indent_stack.push tk_c
Token(tk_c)
Token(tk_c, text)
end

@OP.def_rule("{") do
Expand Down
19 changes: 19 additions & 0 deletions test/test_rdoc_ruby_lex.rb
Original file line number Diff line number Diff line change
Expand Up @@ -435,5 +435,24 @@ def test_rational_imaginary_tokenize
assert_equal expected, tokens
end

def test_class_tokenize_square_bracket_as_method
tokens = RDoc::RubyLex.tokenize "Array.[](1, 2)", nil

expected = [
@TK::TkCONSTANT .new(0, 1, 0, "Array"),
@TK::TkDOT .new(5, 1, 5, "."),
@TK::TkIDENTIFIER.new(6, 1, 6, "[]"),
@TK::TkfLPAREN .new(8, 1, 8, "("),
@TK::TkINTEGER .new(9, 1, 9, "1"),
@TK::TkCOMMA .new(10, 1, 10, ","),
@TK::TkSPACE .new(11, 1, 11, " "),
@TK::TkINTEGER .new(12, 1, 12, "2"),
@TK::TkRPAREN .new(13, 1, 13, ")"),
@TK::TkNL .new(14, 1, 14, "\n")
]

assert_equal expected, tokens
end

end