Skip to content

Commit

Permalink
Refactor serializer
Browse files Browse the repository at this point in the history
  • Loading branch information
kddnewton committed Jan 13, 2025
1 parent 8e6a93b commit 54aa7dc
Show file tree
Hide file tree
Showing 7 changed files with 279 additions and 249 deletions.
2 changes: 1 addition & 1 deletion lib/prism.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def self.lex_ripper(source)
#
# 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

Expand Down
73 changes: 8 additions & 65 deletions lib/prism/ffi.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ module LibRubyParser # :nodoc:
# 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
Expand Down Expand Up @@ -363,86 +364,28 @@ def dump_common(string, options) # :nodoc:
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

Expand Down
10 changes: 10 additions & 0 deletions lib/prism/parse_result.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,16 @@ def initialize(source, start_line = 1, offsets = [])
@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
Expand Down
6 changes: 6 additions & 0 deletions rbi/prism/parse_result.rbi
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ class Prism::Source
sig { params(source: String, start_line: Integer, offsets: T::Array[Integer]).void }
def initialize(source, start_line = 1, offsets = []); end

sig { params(start_line: Integer).void }
def replace_start_line(start_line); end

sig { params(offsets: T::Array[Integer]).void }
def replace_offsets(offsets); end

sig { returns(Encoding) }
def encoding; end

Expand Down
2 changes: 2 additions & 0 deletions sig/prism/parse_result.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ module Prism
attr_reader offsets: Array[Integer]

def initialize: (String source, ?Integer start_line, ?Array[Integer] offsets) -> void
def replace_start_line: (Integer start_line) -> void
def replace_offsets: (Array[Integer] offsets) -> void
def encoding: () -> Encoding
def lines: () -> Array[String]
def slice: (Integer byte_offset, Integer length) -> String
Expand Down
6 changes: 4 additions & 2 deletions sig/prism/serialize.rbs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
module Prism
module Serialize
def self.load: (String, String) -> ParseResult
def self.load_tokens: (Source, String) -> LexResult
def self.load_parse: (String, String, bool) -> ParseResult
def self.load_lex: (String, String, bool) -> LexResult
def self.load_parse_comments: (String, String, bool) -> Array[comment]
def self.load_parse_lex: (String, String, bool) -> ParseLexResult
end
end
Loading

0 comments on commit 54aa7dc

Please sign in to comment.