Skip to content

Commit b080e60

Browse files
committed
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.
1 parent 28dc4ff commit b080e60

File tree

4 files changed

+21
-0
lines changed

4 files changed

+21
-0
lines changed

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
def version # :nodoc:
@@ -313,6 +318,7 @@ def convert_for_prism(version)
313318
end
314319
end
315320

321+
require_relative "parser/builder"
316322
require_relative "parser/compiler"
317323
require_relative "parser/lexer"
318324

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

prism.gemspec

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

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)