From 080cbf2091c56bde5483a206bd64990ff74a6e3c Mon Sep 17 00:00:00 2001 From: Code Ass Date: Fri, 11 Aug 2017 19:08:54 +0900 Subject: [PATCH 1/2] Support safe navigation operator The safe navigation operator is an operator from the name, but it's a variety of dot token for method calling. So TkSAFENAV class inherited Token class instead of TkOp class. --- lib/rdoc/ruby_lex.rb | 6 +++--- lib/rdoc/ruby_token.rb | 1 + 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/lib/rdoc/ruby_lex.rb b/lib/rdoc/ruby_lex.rb index 7abcc37777..e1124818ac 100644 --- a/lib/rdoc/ruby_lex.rb +++ b/lib/rdoc/ruby_lex.rb @@ -668,16 +668,16 @@ def lex_init() end end - @OP.def_rule(".") do + @OP.def_rules(".", "&.") do |op, io| @lex_state = :EXPR_BEG if peek(0) =~ /[0-9]/ ungetc identify_number else - # for "obj.if" etc. + # for "obj.if" or "obj&.if" etc. @lex_state = :EXPR_DOT - Token(TkDOT) + Token(op) end end diff --git a/lib/rdoc/ruby_token.rb b/lib/rdoc/ruby_token.rb index 79df84aadb..98b62e8e93 100644 --- a/lib/rdoc/ruby_token.rb +++ b/lib/rdoc/ruby_token.rb @@ -401,6 +401,7 @@ def Token(token, value = nil) [:TkASSIGN, Token, "="], [:TkDOT, Token, "."], + [:TkSAFENAV, Token, "&."], [:TkLPAREN, Token, "("], #(exp) [:TkLBRACK, Token, "["], #[arry] [:TkLBRACE, Token, "{"], #{hash} From 9ad8761ed35ea197d6a7db4046b21304de066827 Mon Sep 17 00:00:00 2001 From: Code Ass Date: Fri, 11 Aug 2017 19:25:37 +0900 Subject: [PATCH 2/2] Add test_class_tokenize_safe_nav_operator --- test/test_rdoc_ruby_lex.rb | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/test/test_rdoc_ruby_lex.rb b/test/test_rdoc_ruby_lex.rb index d8a920f42c..012ac0a672 100644 --- a/test/test_rdoc_ruby_lex.rb +++ b/test/test_rdoc_ruby_lex.rb @@ -132,6 +132,19 @@ def test_class_tokenize_double_colon_is_not_hash_symbol assert_equal expected, tokens end + def test_class_tokenize_safe_nav_operator + tokens = RDoc::RubyLex.tokenize 'receiver&.meth', nil + + expected = [ + @TK::TkIDENTIFIER.new( 0, 1, 0, "receiver"), + @TK::TkSAFENAV .new( 8, 1, 8, "&."), + @TK::TkIDENTIFIER.new(10, 1, 10, "meth"), + @TK::TkNL .new(14, 1, 14, "\n"), + ] + + assert_equal expected, tokens + end + def test_class_tokenize_hash_rocket tokens = RDoc::RubyLex.tokenize '{ :class => "foo" }', nil