summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/prism/ffi.rb25
1 files changed, 25 insertions, 0 deletions
diff --git a/lib/prism/ffi.rb b/lib/prism/ffi.rb
index cc7d94fb3f..ae13474c31 100644
--- a/lib/prism/ffi.rb
+++ b/lib/prism/ffi.rb
@@ -70,6 +70,7 @@ module Prism
"prism.h",
"pm_version",
"pm_parse_serialize",
+ "pm_parse_serialize_inline_comments",
"pm_lex_serialize",
"pm_parse_lex_serialize"
)
@@ -224,6 +225,30 @@ module Prism
end
end
+ # Mirror the Prism.parse_inline_comments API by using the serialization API.
+ def self.parse_inline_comments(code, filepath = nil)
+ LibRubyParser::PrismBuffer.with do |buffer|
+ metadata = [filepath.bytesize, filepath.b, 0].pack("LA*L") if filepath
+ LibRubyParser.pm_parse_serialize_inline_comments(code, code.bytesize, buffer.pointer, metadata)
+
+ source = Source.new(code)
+ loader = Serialize::Loader.new(source, buffer.read)
+
+ loader.load_header
+ loader.load_force_encoding
+ loader.load_comments
+ end
+ end
+
+ # Mirror the Prism.parse_file_inline_comments API by using the serialization
+ # API. This uses native strings instead of Ruby strings because it allows us
+ # to use mmap when it is available.
+ def self.parse_file_inline_comments(filepath)
+ LibRubyParser::PrismString.with(filepath) do |string|
+ parse_inline_comments(string.read, filepath)
+ end
+ end
+
# Mirror the Prism.parse_lex API by using the serialization API.
def self.parse_lex(code, filepath = nil)
LibRubyParser::PrismBuffer.with do |buffer|