diff options
author | Kevin Newton <[email protected]> | 2023-11-02 14:01:20 -0400 |
---|---|---|
committer | Kevin Newton <[email protected]> | 2023-11-03 10:13:49 -0400 |
commit | 05f5c545d232554b6ffb183d6948ad37f46df53b () | |
tree | 43ab5b181b7fb31e5421a9adda59f822e0d65c45 /lib/prism/ffi.rb | |
parent | ca7297efd389eca792c706326d1af138acf5a4f6 (diff) |
[ruby/prism] Wire up options through the FFI API
https://.com/ruby/prism/commit/f0aa8ad93b
-rw-r--r-- | lib/prism/ffi.rb | 198 |
1 files changed, 124 insertions, 74 deletions
@@ -167,15 +167,6 @@ module Prism end end end - - # Dump the given source into a serialized format. - def self.dump_internal(source, source_size, filepath) - PrismBuffer.with do |buffer| - metadata = [filepath.bytesize, filepath.b, 0].pack("LA*L") if filepath - pm_parse_serialize(source, source_size, buffer.pointer, metadata) - buffer.read - end - end end # Mark the LibRubyParser module as private as it should only be called through @@ -185,93 +176,152 @@ module Prism # The version constant is set by reading the result of calling pm_version. VERSION = LibRubyParser.pm_version.read_string - # Mirror the Prism.dump API by using the serialization API. - def self.dump(code, filepath = nil) - LibRubyParser.dump_internal(code, code.bytesize, filepath) - end - # Mirror the Prism.dump_file API by using the serialization API. - def self.dump_file(filepath) - LibRubyParser::PrismString.with(filepath) do |string| - LibRubyParser.dump_internal(string.source, string.length, filepath) end - end - # Mirror the Prism.lex API by using the serialization API. - def self.lex(code, filepath = nil) - LibRubyParser::PrismBuffer.with do |buffer| - LibRubyParser.pm_lex_serialize(code, code.bytesize, filepath, buffer.pointer) - Serialize.load_tokens(Source.new(code), buffer.read) end - end - # Mirror the Prism.lex_file API by using the serialization API. - def self.lex_file(filepath) - LibRubyParser::PrismString.with(filepath) do |string| - lex(string.read, filepath) end - end - # Mirror the Prism.parse API by using the serialization API. - def self.parse(code, filepath = nil) - Prism.load(code, dump(code, filepath)) - 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 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) - LibRubyParser::PrismBuffer.with do |buffer| - metadata = [filepath.bytesize, filepath.b, 0].pack("LA*L") if filepath - LibRubyParser.pm_parse_lex_serialize(code, code.bytesize, buffer.pointer, metadata) - source = Source.new(code) - loader = Serialize::Loader.new(source, buffer.read) - tokens = loader.load_tokens - node, comments, magic_comments, errors, warnings = loader.load_nodes - tokens.each { |token,| token.value.force_encoding(loader.encoding) } - ParseResult.new([node, tokens], comments, magic_comments, errors, warnings, source) end - end - # Mirror the Prism.parse_lex_file API by using the serialization API. - def self.parse_lex_file(filepath) - LibRubyParser::PrismString.with(filepath) do |string| - parse_lex(string.read, filepath) end end end |