module Prism
def self.lex_compat(source, **options)
`:on_sp` token is not emitted.
resembles the return value of Ripper::lex. The main difference is that the
Returns a parse result whose value is an array of tokens that closely
Prism::lex_compat(source, **options) -> ParseResult
:call-seq:
def self.lex_compat(source, **options) LexCompat.new(source, **options).result end
def self.lex_ripper(source)
returns the same tokens. Raises SyntaxError if the syntax in source is
This lexes with the Ripper lex. It drops any space events but otherwise
Prism::lex_ripper(source) -> Array
:call-seq:
def self.lex_ripper(source) LexRipper.new(source).result end
def self.load(source, serialized)
Prism::load(source, serialized) -> ParseResult
:call-seq:
def self.load(source, serialized) Serialize.load(source, serialized) end
def self.parse_failure?(source, **options)
Prism::parse_failure?(source, **options) -> bool
:call-seq:
def self.parse_failure?(source, **options) !parse_success?(source, **options) end
def self.parse_file_failure?(filepath, **options)
Prism::parse_file_failure?(filepath, **options) -> bool
:call-seq:
def self.parse_file_failure?(filepath, **options) !parse_file_success?(filepath, **options) end
def dump(code, **options)
def dump(code, **options) LibRubyParser::PrismBuffer.with do |buffer| LibRubyParser.pm_serialize_parse(buffer.pointer, code, code.bytesize, dump_options(options)) buffer.read end end
def dump_file(filepath, **options)
def dump_file(filepath, **options) LibRubyParser::PrismString.with(filepath) do |string| dump(string.read, **options, filepath: filepath) end end
def dump_options(options)
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 << { nil => 0, "3.3.0" => 1, "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
def lex(code, **options)
def lex(code, **options) LibRubyParser::PrismBuffer.with do |buffer| LibRubyParser.pm_serialize_lex(buffer.pointer, code, code.bytesize, dump_options(options)) Serialize.load_tokens(Source.new(code), buffer.read) end end
def lex_file(filepath, **options)
def lex_file(filepath, **options) LibRubyParser::PrismString.with(filepath) do |string| lex(string.read, **options, filepath: filepath) end end
def parse(code, **options)
def parse(code, **options) Prism.load(code, dump(code, **options)) end
def parse_comments(code, **options)
def parse_comments(code, **options) LibRubyParser::PrismBuffer.with do |buffer| LibRubyParser.pm_serialize_parse_comments(buffer.pointer, code, code.bytesize, 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
def parse_file(filepath, **options)
native strings instead of Ruby strings because it allows us to use mmap when
Mirror the Prism.parse_file API by using the serialization API. This uses
def parse_file(filepath, **options) LibRubyParser::PrismString.with(filepath) do |string| parse(string.read, **options, filepath: filepath) end end
def parse_file_comments(filepath, **options)
API. This uses native strings instead of Ruby strings because it allows us
Mirror the Prism.parse_file_comments API by using the serialization
def parse_file_comments(filepath, **options) LibRubyParser::PrismString.with(filepath) do |string| parse_comments(string.read, **options, filepath: filepath) end end
def parse_file_success?(filepath, **options)
def parse_file_success?(filepath, **options) LibRubyParser::PrismString.with(filepath) do |string| parse_success?(string.read, **options, filepath: filepath) end end
def parse_lex(code, **options)
def parse_lex(code, **options) LibRubyParser::PrismBuffer.with do |buffer| LibRubyParser.pm_serialize_parse_lex(buffer.pointer, code, code.bytesize, 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) } ParseResult.new([node, tokens], comments, magic_comments, data_loc, errors, warnings, source) end end
def parse_lex_file(filepath, **options)
def parse_lex_file(filepath, **options) LibRubyParser::PrismString.with(filepath) do |string| parse_lex(string.read, **options, filepath: filepath) end end
def parse_success?(code, **options)
def parse_success?(code, **options) LibRubyParser.pm_parse_success_p(code, code.bytesize, dump_options(options)) end