Skip to content

Ripper translation layer relies on upstream Ripper and is not fully compatible #3838

@eregon

Description

@eregon

https://github.com/ruby/prism/blob/main/docs/ripper_translation.md mentions

require "prism/translation/ripper/shim"

However that does not work:

$ ruby -v -rprism -rprism/translation/ripper/shim -e 'p Ripper.lex("1 + 2")'
ruby 4.0.0 (2025-12-25 revision 553f1675f3) +PRISM [x86_64-linux]
/home/eregon/.rubies/ruby-4.0.0/lib/ruby/4.0.0/prism/translation/ripper/shim.rb:5: warning: already initialized constant Ripper
/home/eregon/.rubies/ruby-4.0.0/lib/ruby/4.0.0/prism/parse_result.rb:776:in 'Prism::LexResult#initialize': stack level too deep (SystemStackError)
	from /home/eregon/.rubies/ruby-4.0.0/lib/ruby/4.0.0/prism/lex_compat.rb:631:in 'Prism.lex'
	from /home/eregon/.rubies/ruby-4.0.0/lib/ruby/4.0.0/prism/lex_compat.rb:631:in 'Prism::LexCompat#result'
	from /home/eregon/.rubies/ruby-4.0.0/lib/ruby/4.0.0/prism.rb:69:in 'Prism.lex_compat'
	from /home/eregon/.rubies/ruby-4.0.0/lib/ruby/4.0.0/prism/translation/ripper.rb:74:in 'Prism::Translation::Ripper.lex'
	from /home/eregon/.rubies/ruby-4.0.0/lib/ruby/4.0.0/prism/lex_compat.rb:639:in 'Prism::LexCompat#result'
	from /home/eregon/.rubies/ruby-4.0.0/lib/ruby/4.0.0/prism.rb:69:in 'Prism.lex_compat'
	from /home/eregon/.rubies/ruby-4.0.0/lib/ruby/4.0.0/prism/translation/ripper.rb:74:in 'Prism::Translation::Ripper.lex'
	from /home/eregon/.rubies/ruby-4.0.0/lib/ruby/4.0.0/prism/lex_compat.rb:639:in 'Prism::LexCompat#result'
	 ... 7850 levels...
	from /home/eregon/.rubies/ruby-4.0.0/lib/ruby/4.0.0/prism/lex_compat.rb:639:in 'Prism::LexCompat#result'
	from /home/eregon/.rubies/ruby-4.0.0/lib/ruby/4.0.0/prism.rb:69:in 'Prism.lex_compat'
	from /home/eregon/.rubies/ruby-4.0.0/lib/ruby/4.0.0/prism/translation/ripper.rb:74:in 'Prism::Translation::Ripper.lex'
	from -e:1:in '<main>'

Also while

$ ruby -rprism -rprism/translation/ripper -e 'p Prism::Translation::Ripper.lex("1 + 2"); puts $".grep(/ripper/)'
[[[1, 0], :on_int, "1", END], [[1, 2], :on_op, "+", BEG], [[1, 4], :on_int, "2", END]]
/home/eregon/.rubies/ruby-4.0.0/lib/ruby/4.0.0/x86_64-linux/ripper.so
/home/eregon/.rubies/ruby-4.0.0/lib/ruby/4.0.0/ripper/core.rb
/home/eregon/.rubies/ruby-4.0.0/lib/ruby/4.0.0/ripper/lexer.rb
/home/eregon/.rubies/ruby-4.0.0/lib/ruby/4.0.0/ripper/filter.rb
/home/eregon/.rubies/ruby-4.0.0/lib/ruby/4.0.0/ripper/sexp.rb
/home/eregon/.rubies/ruby-4.0.0/lib/ruby/4.0.0/ripper.rb
/home/eregon/.rubies/ruby-4.0.0/lib/ruby/4.0.0/prism/translation/ripper.rb

works it relies on upstream Ripper.
Which means it does not work without another implementation of Ripper/without parse.y (and there is no much point to it if you need to load the original Ripper anyway).

It also returns a different result than standard Ripper:

$ ruby -rripper -e 'p Ripper.lex("1 + 2")'                        
[[[1, 0], :on_int, "1", END], [[1, 1], :on_sp, " ", END], [[1, 2], :on_op, "+", BEG], [[1, 3], :on_sp, " ", BEG], [[1, 4], :on_int, "2", END]]

(the :on_sp are missing in Prism::Translation::Ripper)

For context we'd like to use Prism::Translation::Ripper to implement Ripper on TruffleRuby, since the ripper extension is causing endless problems, every single time we update the Ruby version and along with it the ripper extension: truffleruby/truffleruby#3481

Which of these 3 issues do you think could be solved?
@andrykonchin mostly worked through the first 2 issues on a branch at some point, would fixes for that be welcome in Prism?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions