123456789_123456789_123456789_123456789_123456789_

Module: Prism

Relationships & Source Files
Namespace Children
Modules:
Classes:
Defined in: lib/prism.rb,
lib/prism/debug.rb,
lib/prism/desugar_compiler.rb,
lib/prism/ffi.rb,
lib/prism/lex_compat.rb,
lib/prism/node_ext.rb,
lib/prism/pack.rb,
lib/prism/parse_result.rb,
lib/prism/pattern.rb,
lib/prism/translation.rb,
lib/prism/parse_result/comments.rb,
lib/prism/parse_result/newlines.rb,
lib/prism/translation/parser.rb,
lib/prism/translation/parser33.rb,
lib/prism/translation/parser34.rb,
lib/prism/translation/ripper.rb,
lib/prism/translation/ruby_parser.rb,
lib/prism/translation/parser/compiler.rb,
lib/prism/translation/parser/lexer.rb,
lib/prism/translation/parser/rubocop.rb,
lib/prism/translation/ripper/sexp.rb

Overview

The Prism Ruby parser.

“Parsing Ruby is suddenly manageable!”

- You, hopefully

Constant Summary

Class Method Summary

Class Method Details

.dump(code, **options)

Mirror the dump API by using the serialization API.

[ GitHub ]

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

def dump(code, **options)
  LibRubyParser::PrismString.with_string(code) { |string| dump_common(string, options) }
end

.dump_common(string, options) (private)

This method is for internal use only.
[ GitHub ]

  
# File 'lib/prism/ffi.rb', line 307

def dump_common(string, options) # :nodoc:
  LibRubyParser::PrismBuffer.with do |buffer|
    LibRubyParser.pm_serialize_parse(buffer.pointer, string.pointer, string.length, dump_options(options))
    buffer.read
  end
end

.dump_file(filepath, **options)

Mirror the dump_file API by using the serialization API.

[ GitHub ]

  
# File 'lib/prism/ffi.rb', line 208

def dump_file(filepath, **options)
  options[:filepath] = filepath
  LibRubyParser::PrismString.with_file(filepath) { |string| dump_common(string, options) }
end

.dump_options(options) (private)

Convert the given options into a serialized options string.

[ GitHub ]

  
# File 'lib/prism/ffi.rb', line 380

def dump_options(options)
  template = +""
  values = []

  template << "L"
  if (filepath = options[:filepath])
    values.push(filepath.bytesize, filepath.b)
    template << "A*"
  else
    values << 0
  end

  template << "l"
  values << options.fetch(:line, 1)

  template << "L"
  if (encoding = options[:encoding])
    name = encoding.name
    values.push(name.bytesize, name.b)
    template << "A*"
  else
    values << 0
  end

  template << "C"
  values << (options.fetch(:frozen_string_literal, false) ? 1 : 0)

  template << "C"
  values << dump_options_command_line(options)

  template << "C"
  values << { nil => 0, "3.3.0" => 1, "3.4.0" => 0, "latest" => 0 }.fetch(options[:version])

  template << "L"
  if (scopes = options[:scopes])
    values << scopes.length

    scopes.each do |scope|
      template << "L"
      values << scope.length

      scope.each do |local|
        name = local.name
        template << "L"
        values << name.bytesize

        template << "A*"
        values << name.b
      end
    end
  else
    values << 0
  end

  values.pack(template)
end

.dump_options_command_line(options) (private)

Return the value that should be dumped for the command_line option.

Raises:

  • (ArgumentError)
[ GitHub ]

  
# File 'lib/prism/ffi.rb', line 362

def dump_options_command_line(options)
  command_line = options.fetch(:command_line, "")
  raise ArgumentError, "command_line must be a string" unless command_line.is_a?(String)

  command_line.each_char.inject(0) do |value, char|
    case char
    when "a" then value | 0b000001
    when "e" then value | 0b000010
    when "l" then value | 0b000100
    when "n" then value | 0b001000
    when "p" then value | 0b010000
    when "x" then value | 0b100000
    else raise ArgumentError, "invalid command_line option: #{char}"
    end
  end
end

.lex(code, **options)

Mirror the lex API by using the serialization API.

[ GitHub ]

  
# File 'lib/prism/ffi.rb', line 214

def lex(code, **options)
  LibRubyParser::PrismString.with_string(code) { |string| lex_common(string, code, options) }
end

.lex_common(string, code, options) (private)

This method is for internal use only.
[ GitHub ]

  
# File 'lib/prism/ffi.rb', line 314

def lex_common(string, code, options) # :nodoc:
  serialized = LibRubyParser::PrismBuffer.with do |buffer|
    LibRubyParser.pm_serialize_lex(buffer.pointer, string.pointer, string.length, dump_options(options))
    buffer.read
  end

  Serialize.load_tokens(Source.new(code), serialized)
end

.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 .parse.

[ GitHub ]

  
# File 'lib/prism.rb', line 47

def self.lex_compat(source, **options)
  LexCompat.new(source, **options).result # steep:ignore
end

.lex_file(filepath, **options)

Mirror the lex_file API by using the serialization API.

[ GitHub ]

  
# File 'lib/prism/ffi.rb', line 219

def lex_file(filepath, **options)
  options[:filepath] = filepath
  LibRubyParser::PrismString.with_file(filepath) { |string| lex_common(string, string.read, options) }
end

.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.

[ GitHub ]

  
# File 'lib/prism.rb', line 57

def self.lex_ripper(source)
  LexRipper.new(source).result # steep:ignore
end

.load(source, serialized) ⇒ ParseResult

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

[ GitHub ]

  
# File 'lib/prism.rb', line 65

def self.load(source, serialized)
  Serialize.load(source, serialized)
end

.parse(code, **options)

Mirror the parse API by using the serialization API.

[ GitHub ]

  
# File 'lib/prism/ffi.rb', line 225

def parse(code, **options)
  LibRubyParser::PrismString.with_string(code) { |string| parse_common(string, code, options) }
end

.parse_comments(code, **options)

Mirror the parse_comments API by using the serialization API.

[ GitHub ]

  
# File 'lib/prism/ffi.rb', line 261

def parse_comments(code, **options)
  LibRubyParser::PrismString.with_string(code) { |string| parse_comments_common(string, code, options) }
end

.parse_comments_common(string, code, options) (private)

This method is for internal use only.
[ GitHub ]

  
# File 'lib/prism/ffi.rb', line 328

def parse_comments_common(string, code, options) # :nodoc:
  LibRubyParser::PrismBuffer.with do |buffer|
    LibRubyParser.pm_serialize_parse_comments(buffer.pointer, string.pointer, string.length, dump_options(options))

    source = Source.new(code)
    loader = Serialize::Loader.new(source, buffer.read)

    loader.load_header
    loader.load_encoding
    loader.load_start_line
    loader.load_comments
  end
end

.parse_common(string, code, options) (private)

This method is for internal use only.
[ GitHub ]

  
# File 'lib/prism/ffi.rb', line 323

def parse_common(string, code, options) # :nodoc:
  serialized = dump_common(string, options)
  Prism.load(code, serialized)
end

.parse_failure?(code, **options) ⇒ Boolean

Mirror the parse_failure? API by using the serialization API.

[ GitHub ]

  
# File 'lib/prism/ffi.rb', line 290

def parse_failure?(code, **options)
  !parse_success?(code, **options)
end

.parse_file(filepath, **options)

Mirror the 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.

[ GitHub ]

  
# File 'lib/prism/ffi.rb', line 232

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 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.

[ GitHub ]

  
# File 'lib/prism/ffi.rb', line 268

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) ⇒ Boolean

Mirror the parse_file_failure? API by using the serialization API.

[ GitHub ]

  
# File 'lib/prism/ffi.rb', line 301

def parse_file_failure?(filepath, **options)
  !parse_file_success?(filepath, **options)
end

.parse_file_success?(filepath, **options) ⇒ Boolean

Mirror the parse_file_success? API by using the serialization API.

[ GitHub ]

  
# File 'lib/prism/ffi.rb', line 295

def parse_file_success?(filepath, **options)
  options[:filepath] = filepath
  LibRubyParser::PrismString.with_file(filepath) { |string| parse_file_success_common(string, options) }
end

.parse_file_success_common(string, options) (private)

This method is for internal use only.
[ GitHub ]

  
# File 'lib/prism/ffi.rb', line 357

def parse_file_success_common(string, options) # :nodoc:
  LibRubyParser.pm_parse_success_p(string.pointer, string.length, dump_options(options))
end

.parse_lex(code, **options)

Mirror the parse_lex API by using the serialization API.

[ GitHub ]

  
# File 'lib/prism/ffi.rb', line 274

def parse_lex(code, **options)
  LibRubyParser::PrismString.with_string(code) { |string| parse_lex_common(string, code, options) }
end

.parse_lex_common(string, code, options) (private)

This method is for internal use only.
[ GitHub ]

  
# File 'lib/prism/ffi.rb', line 342

def parse_lex_common(string, code, options) # :nodoc:
  LibRubyParser::PrismBuffer.with do |buffer|
    LibRubyParser.pm_serialize_parse_lex(buffer.pointer, string.pointer, string.length, dump_options(options))

    source = Source.new(code)
    loader = Serialize::Loader.new(source, buffer.read)

    tokens = loader.load_tokens
    node, comments, magic_comments, data_loc, errors, warnings = loader.load_nodes
    tokens.each { |token,| token.value.force_encoding(loader.encoding) }

    ParseLexResult.new([node, tokens], comments, magic_comments, data_loc, errors, warnings, source)
  end
end

.parse_lex_file(filepath, **options)

Mirror the parse_lex_file API by using the serialization API.

[ GitHub ]

  
# File 'lib/prism/ffi.rb', line 279

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 parse_stream API by using the serialization API.

[ GitHub ]

  
# File 'lib/prism/ffi.rb', line 238

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) ⇒ Boolean

Mirror the parse_success? API by using the serialization API.

[ GitHub ]

  
# File 'lib/prism/ffi.rb', line 285

def parse_success?(code, **options)
  LibRubyParser::PrismString.with_string(code) { |string| parse_file_success_common(string, options) }
end