lib/prism/ffi.rb in prism-0.15.1 vs lib/prism/ffi.rb in prism-0.16.0

- old
+ new

@@ -68,10 +68,11 @@ load_exported_functions_from( "prism.h", "pm_version", "pm_parse_serialize", + "pm_parse_serialize_comments", "pm_lex_serialize", "pm_parse_lex_serialize" ) load_exported_functions_from( @@ -219,9 +220,33 @@ # native strings instead of Ruby strings because it allows us to use mmap when # it is available. def self.parse_file(filepath) LibRubyParser::PrismString.with(filepath) do |string| parse(string.read, filepath) + end + end + + # Mirror the Prism.parse_comments API by using the serialization API. + def self.parse_comments(code, filepath = nil) + LibRubyParser::PrismBuffer.with do |buffer| + metadata = [filepath.bytesize, filepath.b, 0].pack("LA*L") if filepath + LibRubyParser.pm_parse_serialize_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_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_comments(filepath) + LibRubyParser::PrismString.with(filepath) do |string| + parse_comments(string.read, filepath) end end # Mirror the Prism.parse_lex API by using the serialization API. def self.parse_lex(code, filepath = nil)