Skip to content

Commit cd63f48

Browse files
authored
Merge pull request #466 from aycabta/handle-square-bracket-after-dot-as-method
Handle "[]" and "[]=" after "." as method
2 parents 962e029 + dde8f6a commit cd63f48

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

lib/rdoc/ruby_lex.rb

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -739,6 +739,7 @@ def lex_int2
739739

740740
@OP.def_rule("[") do
741741
|op, io|
742+
text = nil
742743
@indent += 1
743744
if @lex_state == :EXPR_FNAME
744745
tk_c = TkfLBRACK
@@ -747,13 +748,25 @@ def lex_int2
747748
tk_c = TkLBRACK
748749
elsif @lex_state == :EXPR_ARG && @space_seen
749750
tk_c = TkLBRACK
751+
elsif @lex_state == :EXPR_DOT
752+
if peek(0) == "]"
753+
tk_c = TkIDENTIFIER
754+
getc
755+
if peek(0) == "="
756+
text = "[]="
757+
else
758+
text = "[]"
759+
end
760+
else
761+
tk_c = TkOp
762+
end
750763
else
751764
tk_c = TkfLBRACK
752765
end
753766
@lex_state = :EXPR_BEG
754767
end
755768
@indent_stack.push tk_c
756-
Token(tk_c)
769+
Token(tk_c, text)
757770
end
758771

759772
@OP.def_rule("{") do

test/test_rdoc_ruby_lex.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -490,5 +490,24 @@ def test_rational_imaginary_tokenize
490490
assert_equal expected, tokens
491491
end
492492

493+
def test_class_tokenize_square_bracket_as_method
494+
tokens = RDoc::RubyLex.tokenize "Array.[](1, 2)", nil
495+
496+
expected = [
497+
@TK::TkCONSTANT .new(0, 1, 0, "Array"),
498+
@TK::TkDOT .new(5, 1, 5, "."),
499+
@TK::TkIDENTIFIER.new(6, 1, 6, "[]"),
500+
@TK::TkfLPAREN .new(8, 1, 8, "("),
501+
@TK::TkINTEGER .new(9, 1, 9, "1"),
502+
@TK::TkCOMMA .new(10, 1, 10, ","),
503+
@TK::TkSPACE .new(11, 1, 11, " "),
504+
@TK::TkINTEGER .new(12, 1, 12, "2"),
505+
@TK::TkRPAREN .new(13, 1, 13, ")"),
506+
@TK::TkNL .new(14, 1, 14, "\n")
507+
]
508+
509+
assert_equal expected, tokens
510+
end
511+
493512
end
494513

0 commit comments

Comments
 (0)