module Prism

The Prism Ruby parser.

“Parsing Ruby is suddenly manageable!”

- You, hopefully

Here we are reopening the prism module to provide methods on nodes that aren’t templated and are meant as convenience methods.

typed: ignore

Constants

BACKEND

The C extension is the default backend on CRuby.

VERSION

The version constant is set by reading the result of calling pm_version.

Public Class Methods

dump (source, **options)

Mirror the Prism.dump API by using the serialization API.

# File lib/prism/ffi.rb, line 217
def dump(source, **options)
  LibRubyParser::PrismString.with_string(source) { |string| dump_common(string, options) }
end
dump_file (filepath, **options)

Mirror the Prism.dump_file API by using the serialization API.

# File lib/prism/ffi.rb, line 222
def dump_file(filepath, **options)
  options[:filepath] = filepath
  LibRubyParser::PrismString.with_file(filepath) { |string| dump_common(string, options) }
end
lex (code, **options)

Mirror the Prism.lex API by using the serialization API.

# File lib/prism/ffi.rb, line 228
def lex(code, **options)
  LibRubyParser::PrismString.with_string(code) { |string| lex_common(string, code, options) }
end
Prism::lex_compat(source, **options) → LexCompat::Result

Returns a parse result whose value is an array of tokens that closely resembles the return value of Ripper::lex. The main difference is that the ‘:on_sp` token is not emitted.

For supported options, see Prism::parse.

# File lib/prism.rb, line 45
def self.lex_compat(source, **options)
  LexCompat.new(source, **options).result # steep:ignore
end
lex_file (filepath, **options)

Mirror the Prism.lex_file API by using the serialization API.

# File lib/prism/ffi.rb, line 233
def lex_file(filepath, **options)
  options[:filepath] = filepath
  LibRubyParser::PrismString.with_file(filepath) { |string| lex_common(string, string.read, options) }
end
Prism::lex_ripper(source) → Array

This lexes with the Ripper lex. It drops any space events but otherwise returns the same tokens. Raises SyntaxError if the syntax in source is invalid.

# File lib/prism.rb, line 55
def self.lex_ripper(source)
  LexRipper.new(source).result # steep:ignore
end
Prism::load(source, serialized) → ParseResult

Load the serialized AST using the source as a reference into a tree.

# File lib/prism.rb, line 63
def self.load(source, serialized)
  Serialize.load(source, serialized)
end
parse (code, **options)

Mirror the Prism.parse API by using the serialization API.

# File lib/prism/ffi.rb, line 239
def parse(code, **options)
  LibRubyParser::PrismString.with_string(code) { |string| parse_common(string, code, options) }
end
parse_comments (code, **options)

Mirror the Prism.parse_comments API by using the serialization API.

# File lib/prism/ffi.rb, line 275
def parse_comments(code, **options)
  LibRubyParser::PrismString.with_string(code) { |string| parse_comments_common(string, code, options) }
end
parse_failure? (code, **options)

Mirror the Prism.parse_failure? API by using the serialization API.

# File lib/prism/ffi.rb, line 304
def parse_failure?(code, **options)
  !parse_success?(code, **options)
end
parse_file (filepath, **options)

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.

# File lib/prism/ffi.rb, line 246
def parse_file(filepath, **options)
  options[:filepath] = filepath
  LibRubyParser::PrismString.with_file(filepath) { |string| parse_common(string, string.read, options) }
end
parse_file_comments (filepath, **options)

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.

# File lib/prism/ffi.rb, line 282
def parse_file_comments(filepath, **options)
  options[:filepath] = filepath
  LibRubyParser::PrismString.with_file(filepath) { |string| parse_comments_common(string, string.read, options) }
end
parse_file_failure? (filepath, **options)

Mirror the Prism.parse_file_failure? API by using the serialization API.

# File lib/prism/ffi.rb, line 315
def parse_file_failure?(filepath, **options)
  !parse_file_success?(filepath, **options)
end
parse_file_success? (filepath, **options)

Mirror the Prism.parse_file_success? API by using the serialization API.

# File lib/prism/ffi.rb, line 309
def parse_file_success?(filepath, **options)
  options[:filepath] = filepath
  LibRubyParser::PrismString.with_file(filepath) { |string| parse_file_success_common(string, options) }
end
parse_lex (code, **options)

Mirror the Prism.parse_lex API by using the serialization API.

# File lib/prism/ffi.rb, line 288
def parse_lex(code, **options)
  LibRubyParser::PrismString.with_string(code) { |string| parse_lex_common(string, code, options) }
end
parse_lex_file (filepath, **options)

Mirror the Prism.parse_lex_file API by using the serialization API.

# File lib/prism/ffi.rb, line 293
def parse_lex_file(filepath, **options)
  options[:filepath] = filepath
  LibRubyParser::PrismString.with_file(filepath) { |string| parse_lex_common(string, string.read, options) }
end
parse_stream (stream, **options)

Mirror the Prism.parse_stream API by using the serialization API.

# File lib/prism/ffi.rb, line 252
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
    }

    # 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, dump_options(options))
    Prism.load(source, buffer.read)
  end
end
parse_success? (code, **options)

Mirror the Prism.parse_success? API by using the serialization API.

# File lib/prism/ffi.rb, line 299
def parse_success?(code, **options)
  LibRubyParser::PrismString.with_string(code) { |string| parse_file_success_common(string, options) }
end
profile (source, **options)

Mirror the Prism.profile API by using the serialization API.

# File lib/prism/ffi.rb, line 320
def profile(source, **options)
  LibRubyParser::PrismString.with_string(source) do |string|
    LibRubyParser::PrismBuffer.with do |buffer|
      LibRubyParser.pm_serialize_parse(buffer.pointer, string.pointer, string.length, dump_options(options))
      nil
    end
  end
end
profile_file (filepath, **options)

Mirror the Prism.profile_file API by using the serialization API.

# File lib/prism/ffi.rb, line 330
def profile_file(filepath, **options)
  LibRubyParser::PrismString.with_file(filepath) do |string|
    LibRubyParser::PrismBuffer.with do |buffer|
      options[:filepath] = filepath
      LibRubyParser.pm_serialize_parse(buffer.pointer, string.pointer, string.length, dump_options(options))
      nil
    end
  end
end