From c201dbc0ada03b985e297d22d6dfa24b7bac12ce Mon Sep 17 00:00:00 2001 From: Kevin Newton Date: Fri, 27 Oct 2023 13:55:48 -0400 Subject: [ruby/prism] Prism.parse_inline_comments https://github.com/ruby/prism/commit/5b72f84480 --- lib/prism/ffi.rb | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) (limited to 'lib') 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| -- cgit v1.2.3