Skip to content

Commit 790b385

Browse files
Earlopainmatzbot
authored andcommitted
[ruby/prism] Add a custom builder class for the parser translator
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
1 parent 2c3d241 commit 790b385

File tree

4 files changed

+21
-0
lines changed

4 files changed

+21
-0
lines changed

lib/prism/prism.gemspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ Gem::Specification.new do |spec|
9797
"lib/prism/translation/parser33.rb",
9898
"lib/prism/translation/parser34.rb",
9999
"lib/prism/translation/parser35.rb",
100+
"lib/prism/translation/parser/builder.rb",
100101
"lib/prism/translation/parser/compiler.rb",
101102
"lib/prism/translation/parser/lexer.rb",
102103
"lib/prism/translation/ripper.rb",

lib/prism/translation/parser.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ def initialize(message, level, reason, location)
3131
end
3232
end
3333

34+
# Create the parser with our custom builder class
35+
def initialize(builder = Parser::Builder.new)
36+
super
37+
end
38+
3439
Racc_debug_parser = false # :nodoc:
3540

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

350+
require_relative "parser/builder"
345351
require_relative "parser/compiler"
346352
require_relative "parser/lexer"
347353

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# frozen_string_literal: true
2+
3+
module Prism
4+
module Translation
5+
class Parser
6+
# A builder that knows how to convert more modern Ruby syntax
7+
# into whitequark/parser gem's syntax tree.
8+
class Builder < ::Parser::Builders::Default
9+
10+
end
11+
end
12+
end
13+
end

test/prism/ruby/parser_test.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
# First, opt in to every AST feature.
1818
Parser::Builders::Default.modernize
19+
Prism::Translation::Parser::Builder.modernize
1920

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

0 commit comments

Comments
 (0)