Skip to content

Commit 54aa7dc

Browse files
committed
Refactor serializer
1 parent 8e6a93b commit 54aa7dc

File tree

7 files changed

+279
-249
lines changed

7 files changed

+279
-249
lines changed

lib/prism.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ def self.lex_ripper(source)
6363
#
6464
# Load the serialized AST using the source as a reference into a tree.
6565
def self.load(source, serialized, freeze = false)
66-
Serialize.load(source, serialized, freeze)
66+
Serialize.load_parse(source, serialized, freeze)
6767
end
6868
end
6969

lib/prism/ffi.rb

Lines changed: 8 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ module LibRubyParser # :nodoc:
1515
# must align with the build shared library from make/rake.
1616
libprism_in_build = File.expand_path("../../build/libprism.#{RbConfig::CONFIG["SOEXT"]}", __dir__)
1717
libprism_in_libdir = "#{RbConfig::CONFIG["libdir"]}/prism/libprism.#{RbConfig::CONFIG["SOEXT"]}"
18-
if File.exist? libprism_in_build
18+
19+
if File.exist?(libprism_in_build)
1920
INCLUDE_DIR = File.expand_path("../../include", __dir__)
2021
ffi_lib libprism_in_build
2122
else
@@ -363,86 +364,28 @@ def dump_common(string, options) # :nodoc:
363364
end
364365

365366
def lex_common(string, code, options) # :nodoc:
366-
serialized =
367-
LibRubyParser::PrismBuffer.with do |buffer|
368-
LibRubyParser.pm_serialize_lex(buffer.pointer, string.pointer, string.length, dump_options(options))
369-
buffer.read
370-
end
371-
372-
freeze = options.fetch(:freeze, false)
373-
source = Source.for(code)
374-
result = Serialize.load_tokens(source, serialized, freeze)
375-
376-
if freeze
377-
source.source.freeze
378-
source.offsets.freeze
379-
source.freeze
367+
LibRubyParser::PrismBuffer.with do |buffer|
368+
LibRubyParser.pm_serialize_lex(buffer.pointer, string.pointer, string.length, dump_options(options))
369+
Serialize.load_lex(code, buffer.read, options.fetch(:freeze, false))
380370
end
381-
382-
result
383371
end
384372

385373
def parse_common(string, code, options) # :nodoc:
386374
serialized = dump_common(string, options)
387-
Prism.load(code, serialized, options.fetch(:freeze, false))
375+
Serialize.load_parse(code, serialized, options.fetch(:freeze, false))
388376
end
389377

390378
def parse_comments_common(string, code, options) # :nodoc:
391379
LibRubyParser::PrismBuffer.with do |buffer|
392380
LibRubyParser.pm_serialize_parse_comments(buffer.pointer, string.pointer, string.length, dump_options(options))
393-
394-
source = Source.for(code)
395-
loader = Serialize::Loader.new(source, buffer.read)
396-
397-
loader.load_header
398-
loader.load_encoding
399-
loader.load_start_line
400-
401-
if (freeze = options.fetch(:freeze, false))
402-
source.source.freeze
403-
source.offsets.freeze
404-
source.freeze
405-
end
406-
407-
loader.load_comments(freeze)
381+
Serialize.load_parse_comments(code, buffer.read, options.fetch(:freeze, false))
408382
end
409383
end
410384

411385
def parse_lex_common(string, code, options) # :nodoc:
412386
LibRubyParser::PrismBuffer.with do |buffer|
413387
LibRubyParser.pm_serialize_parse_lex(buffer.pointer, string.pointer, string.length, dump_options(options))
414-
415-
source = Source.for(code)
416-
loader = Serialize::Loader.new(source, buffer.read)
417-
freeze = options.fetch(:freeze, false)
418-
419-
tokens = loader.load_tokens(false)
420-
node, comments, magic_comments, data_loc, errors, warnings = loader.load_nodes(freeze)
421-
422-
tokens.each do |token,|
423-
token.value.force_encoding(loader.encoding)
424-
425-
if freeze
426-
token.value.freeze
427-
token.location.freeze
428-
token.freeze
429-
end
430-
end
431-
432-
value = [node, tokens]
433-
result = ParseLexResult.new(value, comments, magic_comments, data_loc, errors, warnings, source)
434-
435-
if freeze
436-
source.source.freeze
437-
source.offsets.freeze
438-
source.freeze
439-
tokens.each(&:freeze)
440-
tokens.freeze
441-
value.freeze
442-
result.freeze
443-
end
444-
445-
result
388+
Serialize.load_parse_lex(code, buffer.read, options.fetch(:freeze, false))
446389
end
447390
end
448391

lib/prism/parse_result.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,16 @@ def initialize(source, start_line = 1, offsets = [])
4848
@offsets = offsets # set after parsing is done
4949
end
5050

51+
# Replace the value of start_line with the given value.
52+
def replace_start_line(start_line)
53+
@start_line = start_line
54+
end
55+
56+
# Replace the value of offsets with the given value.
57+
def replace_offsets(offsets)
58+
@offsets.replace(offsets)
59+
end
60+
5161
# Returns the encoding of the source code, which is set by parameters to the
5262
# parser or by the encoding magic comment.
5363
def encoding

rbi/prism/parse_result.rbi

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ class Prism::Source
1313
sig { params(source: String, start_line: Integer, offsets: T::Array[Integer]).void }
1414
def initialize(source, start_line = 1, offsets = []); end
1515

16+
sig { params(start_line: Integer).void }
17+
def replace_start_line(start_line); end
18+
19+
sig { params(offsets: T::Array[Integer]).void }
20+
def replace_offsets(offsets); end
21+
1622
sig { returns(Encoding) }
1723
def encoding; end
1824

sig/prism/parse_result.rbs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ module Prism
99
attr_reader offsets: Array[Integer]
1010

1111
def initialize: (String source, ?Integer start_line, ?Array[Integer] offsets) -> void
12+
def replace_start_line: (Integer start_line) -> void
13+
def replace_offsets: (Array[Integer] offsets) -> void
1214
def encoding: () -> Encoding
1315
def lines: () -> Array[String]
1416
def slice: (Integer byte_offset, Integer length) -> String

sig/prism/serialize.rbs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
module Prism
22
module Serialize
3-
def self.load: (String, String) -> ParseResult
4-
def self.load_tokens: (Source, String) -> LexResult
3+
def self.load_parse: (String, String, bool) -> ParseResult
4+
def self.load_lex: (String, String, bool) -> LexResult
5+
def self.load_parse_comments: (String, String, bool) -> Array[comment]
6+
def self.load_parse_lex: (String, String, bool) -> ParseLexResult
57
end
68
end

0 commit comments

Comments
 (0)