Module: Prism
Overview
The Prism Ruby parser.
“Parsing Ruby is suddenly manageable!”
- You, hopefullyConstant Summary
- 
    BACKEND =
    
 # File 'lib/prism/ffi.rb', line 10:FFI 
- 
    VERSION =
    # File 'lib/prism/ffi.rb', line 178The version constant is set by reading the result of calling pm_version. LibRubyParser.pm_version.read_string 
Class Method Summary
- 
    
      .dump(code, **options)  
    
    Mirror the dumpAPI by using the serialization API.
- 
    
      .dump_file(filepath, **options)  
    
    Mirror the dump_fileAPI by using the serialization API.
- 
    
      .lex(code, **options)  
    
    Mirror the lexAPI by using the serialization API.
- 
    
      .lex_compat(source, **options)  ⇒ ParseResult 
    
    Returns a parse result whose value is an array of tokens that closely resembles the return value of Ripper.lex.
- 
    
      .lex_file(filepath, **options)  
    
    Mirror the lex_fileAPI by using the serialization API.
- 
    
      .lex_ripper(source)  ⇒ Array 
    
    This lexes with the Ripper lex. 
- 
    
      .load(source, serialized)  ⇒ ParseResult 
    
    Load the serialized AST using the source as a reference into a tree. 
- 
    
      .parse(code, **options)  
    
    Mirror the parseAPI by using the serialization API.
- 
    
      .parse_comments(code, **options)  
    
    Mirror the parse_commentsAPI by using the serialization API.
- 
    
      .parse_failure?(source, **options)  ⇒ Boolean 
    
    Returns true if the source parses with errors. 
- 
    
      .parse_file(filepath, **options)  
    
    Mirror the parse_fileAPI by using the serialization API.
- 
    
      .parse_file_comments(filepath, **options)  
    
    Mirror the parse_file_commentsAPI by using the serialization API.
- 
    
      .parse_file_failure?(filepath, **options)  ⇒ Boolean 
    
    Returns true if the file at filepath parses with errors. 
- 
    
      .parse_file_success?(filepath, **options)  ⇒ Boolean 
    
    Mirror the parse_file_success?API by using the serialization API.
- 
    
      .parse_lex(code, **options)  
    
    Mirror the parse_lexAPI by using the serialization API.
- 
    
      .parse_lex_file(filepath, **options)  
    
    Mirror the parse_lex_fileAPI by using the serialization API.
- 
    
      .parse_success?(code, **options)  ⇒ Boolean 
    
    Mirror the parse_success?API by using the serialization API.
- 
    
      .dump_options(options)  
    
    private
    Convert the given options into a serialized options string. 
Class Method Details
.dump(code, **options)
Mirror the dump API by using the serialization API.
# File 'lib/prism/ffi.rb', line 182
def dump(code, **) LibRubyParser::PrismBuffer.with do |buffer| LibRubyParser.pm_serialize_parse(buffer.pointer, code, code.bytesize, ()) buffer.read end end
.dump_file(filepath, **options)
Mirror the dump_file API by using the serialization API.
# File 'lib/prism/ffi.rb', line 190
def dump_file(filepath, **) LibRubyParser::PrismString.with(filepath) do |string| dump(string.read, **, filepath: filepath) end end
.dump_options(options) (private)
Convert the given options into a serialized options string.
# File 'lib/prism/ffi.rb', line 287
def () template = +"" values = [] template << "L" if (filepath = [:filepath]) values.push(filepath.bytesize, filepath.b) template << "A*" else values << 0 end template << "L" values << .fetch(:line, 1) template << "L" if (encoding = [:encoding]) name = encoding.name values.push(name.bytesize, name.b) template << "A*" else values << 0 end template << "C" values << (.fetch(:frozen_string_literal, false) ? 1 : 0) template << "C" values << (.fetch(:verbose, true) ? 0 : 1) template << "L" if (scopes = [: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
.lex(code, **options)
Mirror the lex API by using the serialization API.
# File 'lib/prism/ffi.rb', line 197
def lex(code, **) LibRubyParser::PrismBuffer.with do |buffer| LibRubyParser.pm_serialize_lex(buffer.pointer, code, code.bytesize, ()) Serialize.load_tokens(Source.new(code), buffer.read) end end
.lex_compat(source, **options) ⇒ ParseResult
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.
.lex_file(filepath, **options)
Mirror the lex_file API by using the serialization API.
# File 'lib/prism/ffi.rb', line 205
def lex_file(filepath, **) LibRubyParser::PrismString.with(filepath) do |string| lex(string.read, **, filepath: filepath) end 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.
.load(source, serialized) ⇒ ParseResult
Load the serialized AST using the source as a reference into a tree.
# File 'lib/prism.rb', line 64
def self.load(source, serialized) Serialize.load(source, serialized) end
.parse(code, **options)
Mirror the parse API by using the serialization API.
.parse_comments(code, **options)
Mirror the parse_comments API by using the serialization API.
# File 'lib/prism/ffi.rb', line 226
def parse_comments(code, **) LibRubyParser::PrismBuffer.with do |buffer| LibRubyParser.pm_serialize_parse_comments(buffer.pointer, code, code.bytesize, ()) 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_failure?(source, **options)  ⇒ Boolean   
Returns true if the source parses with errors.
# File 'lib/prism.rb', line 72
def self.parse_failure?(source, **) !parse_success?(source, **) 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.
# File 'lib/prism/ffi.rb', line 219
def parse_file(filepath, **) LibRubyParser::PrismString.with(filepath) do |string| parse(string.read, **, filepath: filepath) end 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.
# File 'lib/prism/ffi.rb', line 243
def parse_file_comments(filepath, **) LibRubyParser::PrismString.with(filepath) do |string| parse_comments(string.read, **, filepath: filepath) end end
    .parse_file_failure?(filepath, **options)  ⇒ Boolean   
Returns true if the file at filepath parses with errors.
# File 'lib/prism.rb', line 80
def self.parse_file_failure?(filepath, **) !parse_file_success?(filepath, **) end
    .parse_file_success?(filepath, **options)  ⇒ Boolean 
  
Mirror the parse_file_success? API by using the serialization API.
# File 'lib/prism/ffi.rb', line 278
def parse_file_success?(filepath, **) LibRubyParser::PrismString.with(filepath) do |string| parse_success?(string.read, **, filepath: filepath) end end
.parse_lex(code, **options)
Mirror the parse_lex API by using the serialization API.
# File 'lib/prism/ffi.rb', line 250
def parse_lex(code, **) LibRubyParser::PrismBuffer.with do |buffer| LibRubyParser.pm_serialize_parse_lex(buffer.pointer, code, code.bytesize, ()) 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) } ParseResult.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.
# File 'lib/prism/ffi.rb', line 266
def parse_lex_file(filepath, **) LibRubyParser::PrismString.with(filepath) do |string| parse_lex(string.read, **, filepath: filepath) end end
    .parse_success?(code, **options)  ⇒ Boolean 
  
Mirror the parse_success? API by using the serialization API.
# File 'lib/prism/ffi.rb', line 273
def parse_success?(code, **) LibRubyParser.pm_parse_success_p(code, code.bytesize, ()) end