@@ -33,6 +33,35 @@ def initialize(message, level, reason, location)
33
33
34
34
Racc_debug_parser = false # :nodoc:
35
35
36
+ # By using the `:parser` keyword argument, you can translate in a way that is compatible with
37
+ # the Parser gem using any parser.
38
+ #
39
+ # For example, in RuboCop for Ruby LSP, the following approach can be used to improve performance
40
+ # by reusing a pre-parsed `Prism::ParseLexResult`:
41
+ #
42
+ # class PrismPreparsed
43
+ # def initialize(prism_result)
44
+ # @prism_result = prism_result
45
+ # end
46
+ #
47
+ # def parse_lex(source, **options)
48
+ # @prism_result
49
+ # end
50
+ # end
51
+ #
52
+ # prism_proxy = ParserProxy.new(prism_result)
53
+ #
54
+ # Prism::Translation::Ruby34.new(builder, parser: prism_proxy)
55
+ #
56
+ # In an object passed to the `:parser` keyword argument, the `parse` and `parse_lex` methods
57
+ # should be implemented as needed.
58
+ #
59
+ def initialize ( builder = ::Parser ::Builders ::Default . new , parser : Prism )
60
+ @parser = parser
61
+
62
+ super ( builder )
63
+ end
64
+
36
65
def version # :nodoc:
37
66
34
38
67
end
@@ -51,7 +80,7 @@ def parse(source_buffer)
51
80
source = source_buffer . source
52
81
53
82
offset_cache = build_offset_cache ( source )
54
- result = unwrap ( Prism . parse ( source , **prism_options ) , offset_cache )
83
+ result = unwrap ( @parser . parse ( source , **prism_options ) , offset_cache )
55
84
56
85
build_ast ( result . value , offset_cache )
57
86
ensure
@@ -64,7 +93,7 @@ def parse_with_comments(source_buffer)
64
93
source = source_buffer . source
65
94
66
95
offset_cache = build_offset_cache ( source )
67
- result = unwrap ( Prism . parse ( source , **prism_options ) , offset_cache )
96
+ result = unwrap ( @parser . parse ( source , **prism_options ) , offset_cache )
68
97
69
98
[
70
99
build_ast ( result . value , offset_cache ) ,
@@ -83,7 +112,7 @@ def tokenize(source_buffer, recover = false)
83
112
offset_cache = build_offset_cache ( source )
84
113
result =
85
114
begin
86
- unwrap ( Prism . parse_lex ( source , **prism_options ) , offset_cache )
115
+ unwrap ( @parser . parse_lex ( source , **prism_options ) , offset_cache )
87
116
rescue ::Parser ::SyntaxError
88
117
raise if !recover
89
118
end
0 commit comments