summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorKevin Newton <[email protected]>2025-01-12 20:06:47 -0500
committergit <[email protected]>2025-01-14 15:32:41 +0000
commitda93c9ae29d2575aa2eb3a3a0868b3ca7e489576 (patch)
tree8a8f8daa4a13d29638a0186d02b5dfe6567e4117 /lib
parent713f31872a42b75924d346ce5df3e567db074cc5 (diff)
[ruby/prism] Refactor serializer
https://github.com/ruby/prism/commit/8ab2532f09
Diffstat (limited to 'lib')
-rw-r--r--lib/prism.rb2
-rw-r--r--lib/prism/ffi.rb73
-rw-r--r--lib/prism/parse_result.rb24
3 files changed, 33 insertions, 66 deletions
diff --git a/lib/prism.rb b/lib/prism.rb
index 8024577fa3..6cae171f5e 100644
--- a/lib/prism.rb
+++ b/lib/prism.rb
@@ -63,7 +63,7 @@ module Prism
#
# Load the serialized AST using the source as a reference into a tree.
def self.load(source, serialized, freeze = false)
- Serialize.load(source, serialized, freeze)
+ Serialize.load_parse(source, serialized, freeze)
end
end
diff --git a/lib/prism/ffi.rb b/lib/prism/ffi.rb
index de11953a26..eda61b3ead 100644
--- a/lib/prism/ffi.rb
+++ b/lib/prism/ffi.rb
@@ -15,7 +15,8 @@ module Prism
# must align with the build shared library from make/rake.
libprism_in_build = File.expand_path("../../build/libprism.#{RbConfig::CONFIG["SOEXT"]}", __dir__)
libprism_in_libdir = "#{RbConfig::CONFIG["libdir"]}/prism/libprism.#{RbConfig::CONFIG["SOEXT"]}"
- if File.exist? libprism_in_build
+
+ if File.exist?(libprism_in_build)
INCLUDE_DIR = File.expand_path("../../include", __dir__)
ffi_lib libprism_in_build
else
@@ -363,86 +364,28 @@ module Prism
end
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
-
- freeze = options.fetch(:freeze, false)
- source = Source.for(code)
- result = Serialize.load_tokens(source, serialized, freeze)
-
- if freeze
- source.source.freeze
- source.offsets.freeze
- source.freeze
+ LibRubyParser::PrismBuffer.with do |buffer|
+ LibRubyParser.pm_serialize_lex(buffer.pointer, string.pointer, string.length, dump_options(options))
+ Serialize.load_lex(code, buffer.read, options.fetch(:freeze, false))
end
-
- result
end
def parse_common(string, code, options) # :nodoc:
serialized = dump_common(string, options)
- Prism.load(code, serialized, options.fetch(:freeze, false))
+ Serialize.load_parse(code, serialized, options.fetch(:freeze, false))
end
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.for(code)
- loader = Serialize::Loader.new(source, buffer.read)
-
- loader.load_header
- loader.load_encoding
- loader.load_start_line
-
- if (freeze = options.fetch(:freeze, false))
- source.source.freeze
- source.offsets.freeze
- source.freeze
- end
-
- loader.load_comments(freeze)
+ Serialize.load_parse_comments(code, buffer.read, options.fetch(:freeze, false))
end
end
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.for(code)
- loader = Serialize::Loader.new(source, buffer.read)
- freeze = options.fetch(:freeze, false)
-
- tokens = loader.load_tokens(false)
- node, comments, magic_comments, data_loc, errors, warnings = loader.load_nodes(freeze)
-
- tokens.each do |token,|
- token.value.force_encoding(loader.encoding)
-
- if freeze
- token.value.freeze
- token.location.freeze
- token.freeze
- end
- end
-
- value = [node, tokens]
- result = ParseLexResult.new(value, comments, magic_comments, data_loc, errors, warnings, source)
-
- if freeze
- source.source.freeze
- source.offsets.freeze
- source.freeze
- tokens.each(&:freeze)
- tokens.freeze
- value.freeze
- result.freeze
- end
-
- result
+ Serialize.load_parse_lex(code, buffer.read, options.fetch(:freeze, false))
end
end
diff --git a/lib/prism/parse_result.rb b/lib/prism/parse_result.rb
index 7aee20c9de..e76ea7e17e 100644
--- a/lib/prism/parse_result.rb
+++ b/lib/prism/parse_result.rb
@@ -48,6 +48,16 @@ module Prism
@offsets = offsets # set after parsing is done
end
+ # Replace the value of start_line with the given value.
+ def replace_start_line(start_line)
+ @start_line = start_line
+ end
+
+ # Replace the value of offsets with the given value.
+ def replace_offsets(offsets)
+ @offsets.replace(offsets)
+ end
+
# Returns the encoding of the source code, which is set by parameters to the
# parser or by the encoding magic comment.
def encoding
@@ -132,6 +142,13 @@ module Prism
code_units_offset(byte_offset, encoding) - code_units_offset(line_start(byte_offset), encoding)
end
+ # Freeze this object and the objects it contains.
+ def deep_freeze
+ source.freeze
+ offsets.freeze
+ freeze
+ end
+
private
# Binary search through the offsets to find the line number for the given
@@ -854,5 +871,12 @@ module Prism
location
super
end
+
+ # Freeze this object and the objects it contains.
+ def deep_freeze
+ value.freeze
+ location.freeze
+ freeze
+ end
end
end