Skip to content
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

Drop reimplementation of Ripper lex state #1118

Merged
merged 4 commits into from
Jul 6, 2024

Conversation

nevans
Copy link
Contributor

@nevans nevans commented Jun 6, 2024

This code was for ruby 2.4 compatibility, but rdoc dropped support for ruby 2.4 about three years ago, in f480b97. This code was almost half of the lines of code in rdoc/parser/ripper_state_lex.

This code was for ruby 2.4 compatibility, but rdoc dropped support for
ruby 2.4 about three years ago, in f480b97.  This code was almost half
of the lines of code in rdoc/parser/ripper_state_lex.
@nevans nevans force-pushed the drop-ruby-2.4-ripper-compatibility branch from 138acb9 to 5772f60 Compare June 6, 2024 17:50
Copy link
Member

@st0012 st0012 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is great, and I think we can go a bit further to remove other unused stuff:

diff --git a/lib/rdoc/parser/ripper_state_lex.rb b/lib/rdoc/parser/ripper_state_lex.rb
index 928f2011..80de0d0e 100644
--- a/lib/rdoc/parser/ripper_state_lex.rb
+++ b/lib/rdoc/parser/ripper_state_lex.rb
@@ -9,24 +9,11 @@ class RDoc::Parser::RipperStateLex
 
   Token = Struct.new(:line_no, :char_no, :kind, :text, :state)
 
-  EXPR_NONE = 0
-  EXPR_BEG = 1
   EXPR_END = 2
-  EXPR_ENDARG = 4
   EXPR_ENDFN = 8
   EXPR_ARG = 16
-  EXPR_CMDARG = 32
-  EXPR_MID = 64
   EXPR_FNAME = 128
-  EXPR_DOT = 256
-  EXPR_CLASS = 512
   EXPR_LABEL = 1024
-  EXPR_LABELED = 2048
-  EXPR_FITEM = 4096
-  EXPR_VALUE = EXPR_BEG
-  EXPR_BEG_ANY  =  (EXPR_BEG | EXPR_MID | EXPR_CLASS)
-  EXPR_ARG_ANY  =  (EXPR_ARG | EXPR_CMDARG)
-  EXPR_END_ANY  =  (EXPR_END | EXPR_ENDARG | EXPR_ENDFN)
 
   class InnerStateLex < Ripper::Filter
     def initialize(code)
@@ -53,7 +40,7 @@ class RDoc::Parser::RipperStateLex
     when :on_backtick then
       if (tk[:state] & (EXPR_FNAME | EXPR_ENDFN)) != 0
         tk[:kind] = :on_ident
-        tk[:state] = Ripper::Lexer.const_defined?(:State) ? Ripper::Lexer::State.new(EXPR_ARG) : EXPR_ARG
+        tk[:state] = Ripper::Lexer::State.new(EXPR_ARG)
       else
         tk = get_string_tk(tk)
       end
@@ -266,7 +253,7 @@ class RDoc::Parser::RipperStateLex
   private def get_op_tk(tk)
     redefinable_operators = %w[! != !~ % & * ** + +@ - -@ / < << <= <=> == === =~ > >= >> [] []= ^ ` | ~]
     if redefinable_operators.include?(tk[:text]) and tk[:state] == EXPR_ARG then
-      tk[:state] = Ripper::Lexer.const_defined?(:State) ? Ripper::Lexer::State.new(EXPR_ARG) : EXPR_ARG
+      tk[:state] = Ripper::Lexer::State.new(EXPR_ARG)
       tk[:kind] = :on_ident
     elsif tk[:text] =~ /^[-+]$/ then
       tk_ahead = get_squashed_tk

Ripper::Lexer::State exists in Ruby 2.5 already, and the current RDoc requires Ruby 2.6 so it's safe to assume it's defined.

@nevans
Copy link
Contributor Author

nevans commented Jun 6, 2024

Ah, good point. Yeah. I haven't previously done more than tinker with Ripper and I'm only just starting to familiarize myself with how this part of rdoc is working, so I hadn't looked at it deeply enough to find those. I'll add your patch to the PR when I get a moment.

@nevans
Copy link
Contributor Author

nevans commented Jun 6, 2024

I was actually only in this code to see if I could figure out how much work #885 would be... and I was reading through this old code for quite a while before I got to the unless RIPPER_HAS_LEX_STATE at the bottom... I was thinking to myself, wow, is rdoc re-implementing ruby's entire lex state algorithm inside its parser?! Only when I got to the bottom of that class did I realize, yes, that's exactly what it was doing (as a polyfill). Thus this PR, to save future code readers some time. 😉

nevans and others added 2 commits June 7, 2024 10:59
This was mostly copied from the diff in @st0012's PR comment.  The
remaining constants have been updated to get their value directly from
Ripper.

Co-authored-by: Stan Lo <[email protected]>
Since this is only used from outside RipperStateLex, there's no longer
any benefit to using the indirect reference rather than just going
straight to Ripper.
@nevans nevans requested a review from st0012 June 7, 2024 19:06
Copy link

@martinemde martinemde left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code looks right to me from a non-maintainer's perspective. I support merging this as long as the maintainers also approve and checks pass. Maybe we should merge the Ruby-core test workflow #1115 first so the rest of these can be revalidated under that environment.

Copy link

@simi simi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

Copy link
Member

@st0012 st0012 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the cleanup!

@st0012 st0012 merged commit dd8c216 into ruby:master Jul 6, 2024
22 checks passed
matzbot pushed a commit to ruby/ruby that referenced this pull request Jul 15, 2024
(ruby/rdoc#1118)

* Drop reimplementation of Ripper lex state

This code was for ruby 2.4 compatibility, but rdoc dropped support for
ruby 2.4 about three years ago, in f480b970c.  This code was almost half
of the lines of code in rdoc/parser/ripper_state_lex.

* Remove unused Ripper constants and const_defined?

This was mostly copied from the diff in @st0012's PR comment.  The
remaining constants have been updated to get their value directly from
Ripper.

Co-authored-by: Stan Lo <[email protected]>

* Use Ripper::EXPR_LABEL directly

Since this is only used from outside RipperStateLex, there's no longer
any benefit to using the indirect reference rather than just going
straight to Ripper.

---------

ruby/rdoc@dd8c216263

Co-authored-by: Stan Lo <[email protected]>
@st0012 st0012 added this to the v6.8.0 milestone Oct 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

4 participants