| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
require "rbconfig" |
| 9 |
require "ffi" |
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
require "prism/serialize" if defined?(Ractor) |
| 14 |
|
| 15 |
module Prism |
| 16 |
module LibRubyParser |
| 17 |
extend FFI::Library |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
libprism_in_build = File.expand_path("../../build/libprism.#{RbConfig::CONFIG["SOEXT"]}", __dir__) |
| 22 |
libprism_in_libdir = "#{RbConfig::CONFIG["libdir"]}/prism/libprism.#{RbConfig::CONFIG["SOEXT"]}" |
| 23 |
|
| 24 |
if File.exist?(libprism_in_build) |
| 25 |
INCLUDE_DIR = File.expand_path("../../include", __dir__) |
| 26 |
ffi_lib libprism_in_build |
| 27 |
else |
| 28 |
INCLUDE_DIR = "#{RbConfig::CONFIG["libdir"]}/prism/include" |
| 29 |
ffi_lib libprism_in_libdir |
| 30 |
end |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
def self.resolve_type(type, callbacks) |
| 41 |
type = type.strip |
| 42 |
|
| 43 |
if !type.end_with?("*") |
| 44 |
type.delete_prefix("const ").to_sym |
| 45 |
else |
| 46 |
type = type.delete_suffix("*").rstrip |
| 47 |
callbacks.include?(type.to_sym) ? type.to_sym : :pointer |
| 48 |
end |
| 49 |
end |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
def self.load_exported_functions_from(header, *functions, callbacks) |
| 55 |
File.foreach("#{INCLUDE_DIR}/#{header}") do |line| |
| 56 |
|
| 57 |
next unless line.start_with?("PRISM_EXPORTED_FUNCTION ") |
| 58 |
|
| 59 |
|
| 60 |
next unless functions.any? { |function| line.include?(function) } |
| 61 |
|
| 62 |
|
| 63 |
unless /^PRISM_EXPORTED_FUNCTION (?<return_type>.+) (?<name>\w+)\((?<arg_types>.+)\);$/ =~ line |
| 64 |
raise "Could not parse #{line}" |
| 65 |
end |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
functions.delete(name) |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
arg_types = arg_types.split(",").map(&:strip) |
| 74 |
arg_types = [] if arg_types == %w[void] |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
arg_types.map! { |type| resolve_type(type.sub(/\w+$/, ""), callbacks) } |
| 79 |
|
| 80 |
|
| 81 |
attach_function name, arg_types, resolve_type(return_type, []) |
| 82 |
end |
| 83 |
|
| 84 |
|
| 85 |
raise "Could not find functions #{functions.inspect}" unless functions.empty? |
| 86 |
end |
| 87 |
|
| 88 |
callback :pm_parse_stream_fgets_t, [:pointer, :int, :pointer], :pointer |
| 89 |
callback :pm_parse_stream_feof_t, [:pointer], :int |
| 90 |
enum :pm_string_init_result_t, %i[PM_STRING_INIT_SUCCESS PM_STRING_INIT_ERROR_GENERIC PM_STRING_INIT_ERROR_DIRECTORY] |
| 91 |
enum :pm_string_query_t, [:PM_STRING_QUERY_ERROR, -1, :PM_STRING_QUERY_FALSE, :PM_STRING_QUERY_TRUE] |
| 92 |
|
| 93 |
load_exported_functions_from( |
| 94 |
"prism.h", |
| 95 |
"pm_version", |
| 96 |
"pm_serialize_parse", |
| 97 |
"pm_serialize_parse_stream", |
| 98 |
"pm_serialize_parse_comments", |
| 99 |
"pm_serialize_lex", |
| 100 |
"pm_serialize_parse_lex", |
| 101 |
"pm_parse_success_p", |
| 102 |
"pm_string_query_local", |
| 103 |
"pm_string_query_constant", |
| 104 |
"pm_string_query_method_name", |
| 105 |
[:pm_parse_stream_fgets_t, :pm_parse_stream_feof_t] |
| 106 |
) |
| 107 |
|
| 108 |
load_exported_functions_from( |
| 109 |
"prism/util/pm_buffer.h", |
| 110 |
"pm_buffer_sizeof", |
| 111 |
"pm_buffer_init", |
| 112 |
"pm_buffer_value", |
| 113 |
"pm_buffer_length", |
| 114 |
"pm_buffer_free", |
| 115 |
[] |
| 116 |
) |
| 117 |
|
| 118 |
load_exported_functions_from( |
| 119 |
"prism/util/pm_string.h", |
| 120 |
"pm_string_mapped_init", |
| 121 |
"pm_string_free", |
| 122 |
"pm_string_source", |
| 123 |
"pm_string_length", |
| 124 |
"pm_string_sizeof", |
| 125 |
[] |
| 126 |
) |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
class PrismBuffer |
| 131 |
SIZEOF = LibRubyParser.pm_buffer_sizeof |
| 132 |
|
| 133 |
attr_reader :pointer |
| 134 |
|
| 135 |
def initialize(pointer) |
| 136 |
@pointer = pointer |
| 137 |
end |
| 138 |
|
| 139 |
def value |
| 140 |
LibRubyParser.pm_buffer_value(pointer) |
| 141 |
end |
| 142 |
|
| 143 |
def length |
| 144 |
LibRubyParser.pm_buffer_length(pointer) |
| 145 |
end |
| 146 |
|
| 147 |
def read |
| 148 |
value.read_string(length) |
| 149 |
end |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
def self.with |
| 154 |
FFI::MemoryPointer.new(SIZEOF) do |pointer| |
| 155 |
raise unless LibRubyParser.pm_buffer_init(pointer) |
| 156 |
return yield new(pointer) |
| 157 |
ensure |
| 158 |
LibRubyParser.pm_buffer_free(pointer) |
| 159 |
end |
| 160 |
end |
| 161 |
end |
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
class PrismString |
| 166 |
SIZEOF = LibRubyParser.pm_string_sizeof |
| 167 |
|
| 168 |
PLATFORM_EXPECTS_UTF8 = |
| 169 |
RbConfig::CONFIG["host_os"].match?(/bccwin|cygwin|djgpp|mingw|mswin|wince|darwin/i) |
| 170 |
|
| 171 |
attr_reader :pointer, :length |
| 172 |
|
| 173 |
def initialize(pointer, length, from_string) |
| 174 |
@pointer = pointer |
| 175 |
@length = length |
| 176 |
@from_string = from_string |
| 177 |
end |
| 178 |
|
| 179 |
def read |
| 180 |
raise "should use the original String instead" if @from_string |
| 181 |
@pointer.read_string(@length) |
| 182 |
end |
| 183 |
|
| 184 |
|
| 185 |
def self.with_string(string) |
| 186 |
raise TypeError unless string.is_a?(String) |
| 187 |
|
| 188 |
length = string.bytesize |
| 189 |
|
| 190 |
FFI::MemoryPointer.new(:char, length + 1, false) do |pointer| |
| 191 |
pointer.write_string(string) |
| 192 |
|
| 193 |
pointer.put_char(length, 0) |
| 194 |
return yield new(pointer, length, true) |
| 195 |
end |
| 196 |
end |
| 197 |
|
| 198 |
|
| 199 |
def self.with_file(filepath) |
| 200 |
raise TypeError unless filepath.is_a?(String) |
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
if PLATFORM_EXPECTS_UTF8 && (encoding = filepath.encoding) != Encoding::ASCII_8BIT && encoding != Encoding::UTF_8 |
| 206 |
filepath = filepath.encode(Encoding::UTF_8) |
| 207 |
end |
| 208 |
|
| 209 |
FFI::MemoryPointer.new(SIZEOF) do |pm_string| |
| 210 |
case (result = LibRubyParser.pm_string_mapped_init(pm_string, filepath)) |
| 211 |
when :PM_STRING_INIT_SUCCESS |
| 212 |
pointer = LibRubyParser.pm_string_source(pm_string) |
| 213 |
length = LibRubyParser.pm_string_length(pm_string) |
| 214 |
return yield new(pointer, length, false) |
| 215 |
when :PM_STRING_INIT_ERROR_GENERIC |
| 216 |
raise SystemCallError.new(filepath, FFI.errno) |
| 217 |
when :PM_STRING_INIT_ERROR_DIRECTORY |
| 218 |
raise Errno::EISDIR.new(filepath) |
| 219 |
else |
| 220 |
raise "Unknown error initializing pm_string_t: #{result.inspect}" |
| 221 |
end |
| 222 |
ensure |
| 223 |
LibRubyParser.pm_string_free(pm_string) |
| 224 |
end |
| 225 |
end |
| 226 |
end |
| 227 |
end |
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
private_constant :LibRubyParser |
| 232 |
|
| 233 |
|
| 234 |
VERSION = LibRubyParser.pm_version.read_string.freeze |
| 235 |
|
| 236 |
class << self |
| 237 |
|
| 238 |
def dump(source, **options) |
| 239 |
LibRubyParser::PrismString.with_string(source) { |string| dump_common(string, options) } |
| 240 |
end |
| 241 |
|
| 242 |
|
| 243 |
def dump_file(filepath, **options) |
| 244 |
options[:filepath] = filepath |
| 245 |
LibRubyParser::PrismString.with_file(filepath) { |string| dump_common(string, options) } |
| 246 |
end |
| 247 |
|
| 248 |
|
| 249 |
def lex(code, **options) |
| 250 |
LibRubyParser::PrismString.with_string(code) { |string| lex_common(string, code, options) } |
| 251 |
end |
| 252 |
|
| 253 |
|
| 254 |
def lex_file(filepath, **options) |
| 255 |
options[:filepath] = filepath |
| 256 |
LibRubyParser::PrismString.with_file(filepath) { |string| lex_common(string, string.read, options) } |
| 257 |
end |
| 258 |
|
| 259 |
|
| 260 |
def parse(code, **options) |
| 261 |
LibRubyParser::PrismString.with_string(code) { |string| parse_common(string, code, options) } |
| 262 |
end |
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
def parse_file(filepath, **options) |
| 268 |
options[:filepath] = filepath |
| 269 |
LibRubyParser::PrismString.with_file(filepath) { |string| parse_common(string, string.read, options) } |
| 270 |
end |
| 271 |
|
| 272 |
|
| 273 |
def parse_stream(stream, **options) |
| 274 |
LibRubyParser::PrismBuffer.with do |buffer| |
| 275 |
source = +"" |
| 276 |
callback = -> (string, size, _) { |
| 277 |
raise "Expected size to be >= 0, got: #{size}" if size <= 0 |
| 278 |
|
| 279 |
if !(line = stream.gets(size - 1)).nil? |
| 280 |
source << line |
| 281 |
string.write_string("#{line}\x00", line.bytesize + 1) |
| 282 |
end |
| 283 |
} |
| 284 |
|
| 285 |
eof_callback = -> (_) { stream.eof? } |
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
LibRubyParser.pm_serialize_parse_stream(buffer.pointer, nil, callback, eof_callback, dump_options(options)) |
| 293 |
Prism.load(source, buffer.read, options.fetch(:freeze, false)) |
| 294 |
end |
| 295 |
end |
| 296 |
|
| 297 |
|
| 298 |
def parse_comments(code, **options) |
| 299 |
LibRubyParser::PrismString.with_string(code) { |string| parse_comments_common(string, code, options) } |
| 300 |
end |
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
def parse_file_comments(filepath, **options) |
| 306 |
options[:filepath] = filepath |
| 307 |
LibRubyParser::PrismString.with_file(filepath) { |string| parse_comments_common(string, string.read, options) } |
| 308 |
end |
| 309 |
|
| 310 |
|
| 311 |
def parse_lex(code, **options) |
| 312 |
LibRubyParser::PrismString.with_string(code) { |string| parse_lex_common(string, code, options) } |
| 313 |
end |
| 314 |
|
| 315 |
|
| 316 |
def parse_lex_file(filepath, **options) |
| 317 |
options[:filepath] = filepath |
| 318 |
LibRubyParser::PrismString.with_file(filepath) { |string| parse_lex_common(string, string.read, options) } |
| 319 |
end |
| 320 |
|
| 321 |
|
| 322 |
def parse_success?(code, **options) |
| 323 |
LibRubyParser::PrismString.with_string(code) { |string| parse_file_success_common(string, options) } |
| 324 |
end |
| 325 |
|
| 326 |
|
| 327 |
def parse_failure?(code, **options) |
| 328 |
!parse_success?(code, **options) |
| 329 |
end |
| 330 |
|
| 331 |
|
| 332 |
def parse_file_success?(filepath, **options) |
| 333 |
options[:filepath] = filepath |
| 334 |
LibRubyParser::PrismString.with_file(filepath) { |string| parse_file_success_common(string, options) } |
| 335 |
end |
| 336 |
|
| 337 |
|
| 338 |
def parse_file_failure?(filepath, **options) |
| 339 |
!parse_file_success?(filepath, **options) |
| 340 |
end |
| 341 |
|
| 342 |
|
| 343 |
def profile(source, **options) |
| 344 |
LibRubyParser::PrismString.with_string(source) do |string| |
| 345 |
LibRubyParser::PrismBuffer.with do |buffer| |
| 346 |
LibRubyParser.pm_serialize_parse(buffer.pointer, string.pointer, string.length, dump_options(options)) |
| 347 |
nil |
| 348 |
end |
| 349 |
end |
| 350 |
end |
| 351 |
|
| 352 |
|
| 353 |
def profile_file(filepath, **options) |
| 354 |
LibRubyParser::PrismString.with_file(filepath) do |string| |
| 355 |
LibRubyParser::PrismBuffer.with do |buffer| |
| 356 |
options[:filepath] = filepath |
| 357 |
LibRubyParser.pm_serialize_parse(buffer.pointer, string.pointer, string.length, dump_options(options)) |
| 358 |
nil |
| 359 |
end |
| 360 |
end |
| 361 |
end |
| 362 |
|
| 363 |
private |
| 364 |
|
| 365 |
def dump_common(string, options) |
| 366 |
LibRubyParser::PrismBuffer.with do |buffer| |
| 367 |
LibRubyParser.pm_serialize_parse(buffer.pointer, string.pointer, string.length, dump_options(options)) |
| 368 |
|
| 369 |
dumped = buffer.read |
| 370 |
dumped.freeze if options.fetch(:freeze, false) |
| 371 |
|
| 372 |
dumped |
| 373 |
end |
| 374 |
end |
| 375 |
|
| 376 |
def lex_common(string, code, options) |
| 377 |
LibRubyParser::PrismBuffer.with do |buffer| |
| 378 |
LibRubyParser.pm_serialize_lex(buffer.pointer, string.pointer, string.length, dump_options(options)) |
| 379 |
Serialize.load_lex(code, buffer.read, options.fetch(:freeze, false)) |
| 380 |
end |
| 381 |
end |
| 382 |
|
| 383 |
def parse_common(string, code, options) |
| 384 |
serialized = dump_common(string, options) |
| 385 |
Serialize.load_parse(code, serialized, options.fetch(:freeze, false)) |
| 386 |
end |
| 387 |
|
| 388 |
def parse_comments_common(string, code, options) |
| 389 |
LibRubyParser::PrismBuffer.with do |buffer| |
| 390 |
LibRubyParser.pm_serialize_parse_comments(buffer.pointer, string.pointer, string.length, dump_options(options)) |
| 391 |
Serialize.load_parse_comments(code, buffer.read, options.fetch(:freeze, false)) |
| 392 |
end |
| 393 |
end |
| 394 |
|
| 395 |
def parse_lex_common(string, code, options) |
| 396 |
LibRubyParser::PrismBuffer.with do |buffer| |
| 397 |
LibRubyParser.pm_serialize_parse_lex(buffer.pointer, string.pointer, string.length, dump_options(options)) |
| 398 |
Serialize.load_parse_lex(code, buffer.read, options.fetch(:freeze, false)) |
| 399 |
end |
| 400 |
end |
| 401 |
|
| 402 |
def parse_file_success_common(string, options) |
| 403 |
LibRubyParser.pm_parse_success_p(string.pointer, string.length, dump_options(options)) |
| 404 |
end |
| 405 |
|
| 406 |
|
| 407 |
def dump_options_command_line(options) |
| 408 |
command_line = options.fetch(:command_line, "") |
| 409 |
raise ArgumentError, "command_line must be a string" unless command_line.is_a?(String) |
| 410 |
|
| 411 |
command_line.each_char.inject(0) do |value, char| |
| 412 |
case char |
| 413 |
when "a" then value | 0b000001 |
| 414 |
when "e" then value | 0b000010 |
| 415 |
when "l" then value | 0b000100 |
| 416 |
when "n" then value | 0b001000 |
| 417 |
when "p" then value | 0b010000 |
| 418 |
when "x" then value | 0b100000 |
| 419 |
else raise ArgumentError, "invalid command_line option: #{char}" |
| 420 |
end |
| 421 |
end |
| 422 |
end |
| 423 |
|
| 424 |
|
| 425 |
def dump_options_version(version) |
| 426 |
current = version == "current" |
| 427 |
|
| 428 |
case current ? RUBY_VERSION : version |
| 429 |
when nil, "latest" |
| 430 |
0 |
| 431 |
when /\A3\.3(\.\d+)?\z/ |
| 432 |
1 |
| 433 |
when /\A3\.4(\.\d+)?\z/ |
| 434 |
2 |
| 435 |
when /\A3\.5(\.\d+)?\z/, /\A4\.0(\.\d+)?\z/ |
| 436 |
3 |
| 437 |
when /\A4\.1(\.\d+)?\z/ |
| 438 |
4 |
| 439 |
else |
| 440 |
if current |
| 441 |
raise CurrentVersionError, RUBY_VERSION |
| 442 |
else |
| 443 |
raise ArgumentError, "invalid version: #{version}" |
| 444 |
end |
| 445 |
end |
| 446 |
end |
| 447 |
|
| 448 |
|
| 449 |
def dump_options(options) |
| 450 |
template = +"" |
| 451 |
values = [] |
| 452 |
|
| 453 |
template << "L" |
| 454 |
if (filepath = options[:filepath]) |
| 455 |
values.push(filepath.bytesize, filepath.b) |
| 456 |
template << "A*" |
| 457 |
else |
| 458 |
values << 0 |
| 459 |
end |
| 460 |
|
| 461 |
template << "l" |
| 462 |
values << options.fetch(:line, 1) |
| 463 |
|
| 464 |
template << "L" |
| 465 |
if (encoding = options[:encoding]) |
| 466 |
name = encoding.is_a?(Encoding) ? encoding.name : encoding |
| 467 |
values.push(name.bytesize, name.b) |
| 468 |
template << "A*" |
| 469 |
else |
| 470 |
values << 0 |
| 471 |
end |
| 472 |
|
| 473 |
template << "C" |
| 474 |
values << (options.fetch(:frozen_string_literal, false) ? 1 : 0) |
| 475 |
|
| 476 |
template << "C" |
| 477 |
values << dump_options_command_line(options) |
| 478 |
|
| 479 |
template << "C" |
| 480 |
values << dump_options_version(options[:version]) |
| 481 |
|
| 482 |
template << "C" |
| 483 |
values << (options[:encoding] == false ? 1 : 0) |
| 484 |
|
| 485 |
template << "C" |
| 486 |
values << (options.fetch(:main_script, false) ? 1 : 0) |
| 487 |
|
| 488 |
template << "C" |
| 489 |
values << (options.fetch(:partial_script, false) ? 1 : 0) |
| 490 |
|
| 491 |
template << "C" |
| 492 |
values << (options.fetch(:freeze, false) ? 1 : 0) |
| 493 |
|
| 494 |
template << "L" |
| 495 |
if (scopes = options[:scopes]) |
| 496 |
values << scopes.length |
| 497 |
|
| 498 |
scopes.each do |scope| |
| 499 |
locals = nil |
| 500 |
forwarding = 0 |
| 501 |
|
| 502 |
case scope |
| 503 |
when Array |
| 504 |
locals = scope |
| 505 |
when Scope |
| 506 |
locals = scope.locals |
| 507 |
|
| 508 |
scope.forwarding.each do |forward| |
| 509 |
case forward |
| 510 |
when :* then forwarding |= 0x1 |
| 511 |
when :** then forwarding |= 0x2 |
| 512 |
when :& then forwarding |= 0x4 |
| 513 |
when :"..." then forwarding |= 0x8 |
| 514 |
else raise ArgumentError, "invalid forwarding value: #{forward}" |
| 515 |
end |
| 516 |
end |
| 517 |
else |
| 518 |
raise TypeError, "wrong argument type #{scope.class.inspect} (expected Array or Prism::Scope)" |
| 519 |
end |
| 520 |
|
| 521 |
template << "L" |
| 522 |
values << locals.length |
| 523 |
|
| 524 |
template << "C" |
| 525 |
values << forwarding |
| 526 |
|
| 527 |
locals.each do |local| |
| 528 |
name = local.name |
| 529 |
template << "L" |
| 530 |
values << name.bytesize |
| 531 |
|
| 532 |
template << "A*" |
| 533 |
values << name.b |
| 534 |
end |
| 535 |
end |
| 536 |
else |
| 537 |
values << 0 |
| 538 |
end |
| 539 |
|
| 540 |
values.pack(template) |
| 541 |
end |
| 542 |
end |
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
class StringQuery |
| 547 |
class << self |
| 548 |
|
| 549 |
def local?(string) |
| 550 |
query(LibRubyParser.pm_string_query_local(string, string.bytesize, string.encoding.name)) |
| 551 |
end |
| 552 |
|
| 553 |
|
| 554 |
def constant?(string) |
| 555 |
query(LibRubyParser.pm_string_query_constant(string, string.bytesize, string.encoding.name)) |
| 556 |
end |
| 557 |
|
| 558 |
|
| 559 |
def method_name?(string) |
| 560 |
query(LibRubyParser.pm_string_query_method_name(string, string.bytesize, string.encoding.name)) |
| 561 |
end |
| 562 |
|
| 563 |
private |
| 564 |
|
| 565 |
|
| 566 |
def query(result) |
| 567 |
case result |
| 568 |
when :PM_STRING_QUERY_ERROR |
| 569 |
raise ArgumentError, "Invalid or non ascii-compatible encoding" |
| 570 |
when :PM_STRING_QUERY_FALSE |
| 571 |
false |
| 572 |
when :PM_STRING_QUERY_TRUE |
| 573 |
true |
| 574 |
end |
| 575 |
end |
| 576 |
end |
| 577 |
end |
| 578 |
end |