From 72ced08517274dc6bf165f714e1c65985d7444bd Mon Sep 17 00:00:00 2001 From: Code Ass Date: Thu, 19 Oct 2017 22:39:55 +0900 Subject: [PATCH 1/2] Don't use lex_state emulation on Ruby 2.5 or later RDoc::RipperStateLex has InnerStateLex that is emulation layer about lex_state of parse.y, but after Ruby 2.5 or later, Ripper gives lex_state value as state method inside Ripper::Filter's callback. On this commit, uses the new feature of Ripper on Ruby 2.5 or later. --- lib/rdoc/parser/ripper_state_lex.rb | 32 ++++++++++++++++++++++------- 1 file changed, 25 insertions(+), 7 deletions(-) diff --git a/lib/rdoc/parser/ripper_state_lex.rb b/lib/rdoc/parser/ripper_state_lex.rb index c9a0f5a21e..0aceda2108 100644 --- a/lib/rdoc/parser/ripper_state_lex.rb +++ b/lib/rdoc/parser/ripper_state_lex.rb @@ -1,6 +1,9 @@ require 'ripper' class RDoc::RipperStateLex + # TODO: Remove this constants after Ruby 2.4 EOL + RIPPER_HAS_LEX_STATE = (RUBY_VERSION >= '2.5.0') + EXPR_NONE = 0 EXPR_BEG = 1 EXPR_END = 2 @@ -283,7 +286,22 @@ def each(&block) @callback = block parse end - end + end unless RIPPER_HAS_LEX_STATE + + class InnerStateLex < Ripper::Filter + def initialize(code) + super(code) + end + + def on_default(event, tok, data) + @callback.call({ :line_no => lineno, :char_no => column, :kind => event, :text => tok, :state => state}) + end + + def each(&block) + @callback = block + parse + end + end if RIPPER_HAS_LEX_STATE def get_squashed_tk if @buf.empty? @@ -297,10 +315,10 @@ def get_squashed_tk when :on_tstring_beg then tk = get_string_tk(tk) when :on_backtick then - if (EXPR_FNAME & tk[:state]) != 0 - @inner_lex.lex_state = EXPR_ARG + if ((EXPR_FNAME | EXPR_ENDFN) & tk[:state]) != 0 + @inner_lex.lex_state = EXPR_ARG unless RIPPER_HAS_LEX_STATE tk[:kind] = :on_ident - tk[:state] = @inner_lex.lex_state + tk[:state] = EXPR_ARG else tk = get_string_tk(tk) end @@ -310,7 +328,7 @@ def get_squashed_tk tk = get_embdoc_tk(tk) when :on_heredoc_beg then @heredoc_queue << retrieve_heredoc_info(tk) - @inner_lex.lex_state = EXPR_END + @inner_lex.lex_state = EXPR_END unless RIPPER_HAS_LEX_STATE when :on_nl, :on_ignored_nl, :on_comment, :on_heredoc_end then unless @heredoc_queue.empty? get_heredoc_tk(*@heredoc_queue.shift) @@ -541,9 +559,9 @@ def get_squashed_tk private def get_op_tk(tk) redefinable_operators = %w[! != !~ % & * ** + +@ - -@ / < << <= <=> == === =~ > >= >> [] []= ^ ` | ~] if redefinable_operators.include?(tk[:text]) and EXPR_ARG == tk[:state] then - @inner_lex.lex_state = EXPR_ARG + @inner_lex.lex_state = EXPR_ARG unless RIPPER_HAS_LEX_STATE + tk[:state] = EXPR_ARG tk[:kind] = :on_ident - tk[:state] = @inner_lex.lex_state elsif tk[:text] =~ /^[-+]$/ then tk_ahead = get_squashed_tk case tk_ahead[:kind] From ee9d50abd0271eeaa10b5de0fe45d4b117e51309 Mon Sep 17 00:00:00 2001 From: Code Ass Date: Wed, 25 Oct 2017 21:05:46 +0900 Subject: [PATCH 2/2] Use Ripper::Filter.method_defined?(:state) instead of RUBY_VERSION --- lib/rdoc/parser/ripper_state_lex.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/rdoc/parser/ripper_state_lex.rb b/lib/rdoc/parser/ripper_state_lex.rb index 0aceda2108..cd3d799cdf 100644 --- a/lib/rdoc/parser/ripper_state_lex.rb +++ b/lib/rdoc/parser/ripper_state_lex.rb @@ -2,7 +2,7 @@ class RDoc::RipperStateLex # TODO: Remove this constants after Ruby 2.4 EOL - RIPPER_HAS_LEX_STATE = (RUBY_VERSION >= '2.5.0') + RIPPER_HAS_LEX_STATE = Ripper::Filter.method_defined?(:state) EXPR_NONE = 0 EXPR_BEG = 1