diff options
author | Kevin Newton <[email protected]> | 2024-03-07 15:24:43 -0500 |
---|---|---|
committer | git <[email protected]> | 2024-03-07 20:40:39 +0000 |
commit | ec159fc8ba17cb70e34a5b62c1ef804e393b7b2f () | |
tree | dba97733c5296ab09cf507b47dcbd6c327934328 /lib/prism/ffi.rb | |
parent | 76e11595e28e258f4a4187a6d3eaccc9ca752e10 (diff) |
[ruby/prism] Support parsing streams
https://.com/ruby/prism/commit/efdc2b7222
-rw-r--r-- | lib/prism/ffi.rb | 55 |
1 files changed, 45 insertions, 10 deletions
@@ -23,15 +23,21 @@ module Prism # size_t -> :size_t # void -> :void # - def self.resolve_type(type) type = type.strip - type.end_with?("*") ? :pointer : type.delete_prefix("const ").to_sym end # Read through the given header file and find the declaration of each of the # given functions. For each one, define a function with the same name and # signature as the C function. - def self.load_exported_functions_from(header, *functions) File.foreach(File.expand_path("../../include/#{header}", __dir__)) do |line| # We only want to attempt to load exported functions. next unless line.start_with?("PRISM_EXPORTED_FUNCTION ") @@ -55,24 +61,28 @@ module Prism # Resolve the type of the argument by dropping the name of the argument # first if it is present. - arg_types.map! { |type| resolve_type(type.sub(/\w+$/, "")) } # Attach the function using the FFI library. - attach_function name, arg_types, resolve_type(return_type) end # If we didn't find all of the functions, raise an error. raise "Could not find functions #{functions.inspect}" unless functions.empty? end load_exported_functions_from( "prism.h", "pm_version", "pm_serialize_parse", "pm_serialize_parse_comments", "pm_serialize_lex", "pm_serialize_parse_lex", - "pm_parse_success_p" ) load_exported_functions_from( @@ -81,7 +91,8 @@ module Prism "pm_buffer_init", "pm_buffer_value", "pm_buffer_length", - "pm_buffer_free" ) load_exported_functions_from( @@ -90,7 +101,8 @@ module Prism "pm_string_free", "pm_string_source", "pm_string_length", - "pm_string_sizeof" ) # This object represents a pm_buffer_t. We only use it as an opaque pointer, @@ -215,13 +227,36 @@ module Prism end # Mirror the Prism.parse_file 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 parse_file(filepath, **options) options[:filepath] = filepath LibRubyParser::PrismString.with_file(filepath) { |string| parse_common(string, string.read, options) } end # Mirror the Prism.parse_comments API by using the serialization API. def parse_comments(code, **options) LibRubyParser::PrismString.with_string(code) { |string| parse_comments_common(string, code, options) } |