Class: Prism::LibRubyParser::PrismSource

Inherits:
Object
  • Object
show all
Defined in:
lib/prism/ffi.rb

Overview

This object represents source code to be parsed. For strings it wraps a pointer directly; for files it uses a pm_source_t under the hood.

Constant Summary collapse

PLATFORM_EXPECTS_UTF8 =

:nodoc:

RbConfig::CONFIG["host_os"].match?(/cygwin|mingw|mswin|darwin/i)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pointer, length, from_string) ⇒ PrismSource

Returns a new instance of PrismSource.



194
195
196
197
198
# File 'lib/prism/ffi.rb', line 194

def initialize(pointer, length, from_string)
  @pointer = pointer
  @length = length
  @from_string = from_string
end

Instance Attribute Details

#length ⇒ Object (readonly)

Returns the value of attribute length.



192
193
194
# File 'lib/prism/ffi.rb', line 192

def length
  @length
end

#pointer ⇒ Object (readonly)

Returns the value of attribute pointer.



192
193
194
# File 'lib/prism/ffi.rb', line 192

def pointer
  @pointer
end

Class Method Details

.with_file(filepath, options) ⇒ Object

Yields a PrismSource to the given block, backed by a pm_source_t.



220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
# File 'lib/prism/ffi.rb', line 220

def self.with_file(filepath, options)
  # File.path applies the same coercion and validation as the C
  # extension's rb_get_path: it accepts to_path/to_str objects and
  # rejects paths containing a NUL byte, which would otherwise truncate
  # the path when it is handed to C as a NUL-terminated string.
  filepath = File.path(filepath)
  options[:filepath] = filepath

  # On Windows and Mac, it's expected that filepaths will be encoded in
  # UTF-8. If they are not, we need to convert them to UTF-8 before
  # passing them into pm_source_mapped_new.
  if PLATFORM_EXPECTS_UTF8 && (encoding = filepath.encoding) != Encoding::ASCII_8BIT && encoding != Encoding::UTF_8
    filepath = filepath.encode(Encoding::UTF_8)
  end

  FFI::MemoryPointer.new(:int) do |result_ptr|
    pm_source = LibRubyParser.pm_source_mapped_new(filepath, 0, result_ptr)

    case SOURCE_INIT_RESULT[result_ptr.read_int]
    when :PM_SOURCE_INIT_SUCCESS
      pointer = LibRubyParser.pm_source_source(pm_source)
      length = LibRubyParser.pm_source_length(pm_source)
      return yield new(pointer, length, false)
    when :PM_SOURCE_INIT_ERROR_GENERIC
      raise SystemCallError.new(filepath, FFI.errno)
    when :PM_SOURCE_INIT_ERROR_DIRECTORY
      raise Errno::EISDIR.new(filepath)
    when :PM_SOURCE_INIT_ERROR_NON_REGULAR
      # Fall back to reading the file through Ruby IO for non-regular
      # files (pipes, character devices, etc.)
      return with_string(File.read(filepath)) { |string| yield string }
    else
      raise "Unknown error initializing pm_source_t: #{result_ptr.read_int}"
    end
  ensure
    LibRubyParser.pm_source_free(pm_source) if pm_source && !pm_source.null?
  end
end

.with_string(string) ⇒ Object

Yields a PrismSource backed by the given string to the block.

Raises:

  • (TypeError)


206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/prism/ffi.rb', line 206

def self.with_string(string)
  raise TypeError unless string.is_a?(String)

  length = string.bytesize
  # + 1 to never get an address of 0, which pm_parser_init() asserts
  FFI::MemoryPointer.new(:char, length + 1, false) do |pointer|
    pointer.write_string(string)
    # since we have the extra byte we might as well \0-terminate
    pointer.put_char(length, 0)
    return yield new(pointer, length, true)
  end
end

Instance Method Details

#read ⇒ Object



200
201
202
203
# File 'lib/prism/ffi.rb', line 200

def read
  raise "should use the original String instead" if @from_string
  @pointer.read_string(@length)
end