Skip to content

Commit

Permalink
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.
  • Loading branch information
Earlopain committed Jan 21, 2025
1 parent 28dc4ff commit 5d95e53
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lib/prism/translation/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ def initialize(message, level, reason, location)
end
end

def initialize(builder = Parser::Builder.new)
super
end

Racc_debug_parser = false # :nodoc:

def version # :nodoc:
Expand Down Expand Up @@ -313,6 +317,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 5d95e53

Please sign in to comment.