class method
Prism::Translation::Parser.new
new(builder = Prism::Translation::Parser::Builder.new, parser: Prism)
lib/prism/translation/parser.rb
View on GitHub ↗
# File tmp/rubies/ruby-4.0.0/lib/prism/translation/parser.rb, line 74
def initialize(builder = Prism::Translation::Parser::Builder.new, parser: Prism)
if !builder.is_a?(Prism::Translation::Parser::Builder)
warn(<<~MSG, uplevel: 1, category: :deprecated)
[deprecation]: The builder passed to `Prism::Translation::Parser.new` is not a \
`Prism::Translation::Parser::Builder` subclass. This will raise in the next major version.
MSG
end
@parser = parser
super(builder)
end
The builder argument is used to create the parser using our custom builder class by default.
By using the :parser keyword argument, you can translate in a way that is compatible with the Parser gem using any parser.
For example, in RuboCop for Ruby LSP, the following approach can be used to improve performance by reusing a pre-parsed Prism::ParseLexResult:
class PrismPreparsed def initialize(prism_result) @prism_result = prism_result end
Example
def parse_lex(source, **options) @prism_result end
end
prism_preparsed = PrismPreparsed.new(prism_result)
Prism::Translation::Ruby34.new(builder, parser: prism_preparsed)
In an object passed to the :parser keyword argument, the parse and parse_lex methods should be implemented as needed.