class method
Prism.parse_stream
parse_stream(stream, **options)
lib/prism/ffi.rb
View on GitHub ↗
# File tmp/rubies/ruby-4.0.0/lib/prism/ffi.rb, line 273
def parse_stream(stream, **options)
LibRubyParser::PrismBuffer.with do |buffer|
source = +""
callback = -> (string, size, _) {
raise "Expected size to be >= 0, got: #{size}" if size <= 0
if !(line = stream.gets(size - 1)).nil?
source << line
string.write_string("#{line}\x00", line.bytesize + 1)
end
}
eof_callback = -> (_) { stream.eof? }
# In the pm_serialize_parse_stream function it accepts a pointer to the
# IO object as a void* and then passes it through to the callback as the
# third argument, but it never touches it itself. As such, since we have
# access to the IO object already through the closure of the lambda, we
# can pass a null pointer here and not worry.
LibRubyParser.pm_serialize_parse_stream(buffer.pointer, nil, callback, eof_callback, dump_options(options))
Prism.load(source, buffer.read, options.fetch(:freeze, false))
end
end
Mirror the Prism.parse_stream API by using the serialization API.