summaryrefslogtreecommitdiff
path: root/lib/prism
diff options
context:
space:
mode:
authorKevin Newton <[email protected]>2023-11-02 14:01:20 -0400
committerKevin Newton <[email protected]>2023-11-03 10:13:49 -0400
commit05f5c545d232554b6ffb183d6948ad37f46df53b ()
tree43ab5b181b7fb31e5421a9adda59f822e0d65c45 /lib/prism
parentca7297efd389eca792c706326d1af138acf5a4f6 (diff)
[ruby/prism] Wire up options through the FFI API
https://.com/ruby/prism/commit/f0aa8ad93b
-rw-r--r--lib/prism/debug.rb8
-rw-r--r--lib/prism/ffi.rb198
-rw-r--r--lib/prism/lex_compat.rb8
3 files changed, 128 insertions, 86 deletions
@@ -187,13 +187,5 @@ module Prism
def self.newlines(source)
Prism.parse(source).source.offsets
end
-
- # :call-seq:
- # Debug::parse_serialize_file(filepath) -> dumped
- #
- # For the given file, parse the AST and dump it to a string.
- def self.parse_serialize_file(filepath)
- parse_serialize_file_metadata(filepath, [filepath.bytesize, filepath.b, 0].pack("LA*L"))
- end
end
end
@@ -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
@@ -594,11 +594,11 @@ module Prism
private_constant :Heredoc
- attr_reader :source, :filepath
- def initialize(source, filepath = "")
@source = source
- @filepath = filepath || ""
end
def result
@@ -607,7 +607,7 @@ module Prism
state = :default
heredoc_stack = [[]]
- result = Prism.lex(source, filepath: @filepath)
result_value = result.value
previous_state = nil