Skip to content

Commit

Permalink
[ruby/prism] Add a custom builder class for the parser translator
Browse files Browse the repository at this point in the history
I want to add new node types to the parser translator, for example `itblock`. The bulk of the work is already done by prism itself. In the `parser`
builder, this would be a 5-line change at most but we don't control that here.

Instead, we can add our own builder and either overwrite the few methods we need,
or just inline the complete builder. I'm not sure yet which would be better.

`rubocop-ast` uses its own builder for `parser`. For this to correctly work, it must explicitly choose to extend the
prism builder and use it, same as it currently chooses to use a different parser when prism is used.

I'd like to enforce that the builder for prism extends its custom one since it will lead to
some pretty weird issues otherwise. But first, I'd like to change `rubocop-ast` to make use of this.

ruby/prism@b080e608a8
  • Loading branch information
Earlopain authored and matzbot committed Feb 25, 2025
1 parent 2c3d241 commit 790b385
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/prism/prism.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ Gem::Specification.new do |spec|
"lib/prism/translation/parser33.rb",
"lib/prism/translation/parser34.rb",
"lib/prism/translation/parser35.rb",
"lib/prism/translation/parser/builder.rb",
"lib/prism/translation/parser/compiler.rb",
"lib/prism/translation/parser/lexer.rb",
"lib/prism/translation/ripper.rb",
Expand Down
6 changes: 6 additions & 0 deletions lib/prism/translation/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ def initialize(message, level, reason, location)
end
end

# Create the parser with our custom builder class
def initialize(builder = Parser::Builder.new)
super
end

Racc_debug_parser = false # :nodoc:

# By using the `:parser` keyword argument, you can translate in a way that is compatible with
Expand Down Expand Up @@ -342,6 +347,7 @@ def convert_for_prism(version)
end
end

require_relative "parser/builder"
require_relative "parser/compiler"
require_relative "parser/lexer"

Expand Down
13 changes: 13 additions & 0 deletions lib/prism/translation/parser/builder.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

module Prism
module Translation
class Parser
# A builder that knows how to convert more modern Ruby syntax
# into whitequark/parser gem's syntax tree.
class Builder < ::Parser::Builders::Default

end
end
end
end
1 change: 1 addition & 0 deletions test/prism/ruby/parser_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

# First, opt in to every AST feature.
Parser::Builders::Default.modernize
Prism::Translation::Parser::Builder.modernize

# The parser gem rejects some strings that would most likely lead to errors
# in consumers due to encoding problems. RuboCop however monkey-patches this
Expand Down

0 comments on commit 790b385

Please sign in to comment.