Class: Prism::Serialize::Loader
- Inherits:
-
Object
- Object
- Prism::Serialize::Loader
- Defined in:
- lib/prism/serialize.rb
Overview
:nodoc:
Instance Attribute Summary collapse
-
#constant_pool ⇒ Object
readonly
Returns the value of attribute constant_pool.
-
#constant_pool_offset ⇒ Object
readonly
Returns the value of attribute constant_pool_offset.
-
#encoding ⇒ Object
readonly
Returns the value of attribute encoding.
-
#input ⇒ Object
readonly
Returns the value of attribute input.
-
#io ⇒ Object
readonly
Returns the value of attribute io.
-
#serialized ⇒ Object
readonly
Returns the value of attribute serialized.
-
#source ⇒ Object
readonly
Returns the value of attribute source.
-
#start_line ⇒ Object
readonly
Returns the value of attribute start_line.
Instance Method Summary collapse
-
#initialize(source, serialized) ⇒ Loader
constructor
A new instance of Loader.
- #load_comments ⇒ Object
- #load_encoding ⇒ Object
- #load_header ⇒ Object
- #load_line_offsets ⇒ Object
- #load_metadata ⇒ Object
- #load_nodes ⇒ Object
- #load_result ⇒ Object
- #load_start_line ⇒ Object
- #load_tokens ⇒ Object
- #load_tokens_result ⇒ Object
Constructor Details
#initialize(source, serialized) ⇒ Loader
Returns a new instance of Loader.
80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/prism/serialize.rb', line 80 def initialize(source, serialized) @encoding = Encoding::UTF_8 @input = source.source.dup raise unless serialized.encoding == Encoding::BINARY @serialized = serialized @io = FastStringIO.new(serialized) @constant_pool_offset = nil @constant_pool = nil @source = source define_load_node_lambdas unless RUBY_ENGINE == "ruby" end |
Instance Attribute Details
#constant_pool ⇒ Object (readonly)
Returns the value of attribute constant_pool.
77 78 79 |
# File 'lib/prism/serialize.rb', line 77 def constant_pool @constant_pool end |
#constant_pool_offset ⇒ Object (readonly)
Returns the value of attribute constant_pool_offset.
77 78 79 |
# File 'lib/prism/serialize.rb', line 77 def constant_pool_offset @constant_pool_offset end |
#encoding ⇒ Object (readonly)
Returns the value of attribute encoding.
76 77 78 |
# File 'lib/prism/serialize.rb', line 76 def encoding @encoding end |
#input ⇒ Object (readonly)
Returns the value of attribute input.
76 77 78 |
# File 'lib/prism/serialize.rb', line 76 def input @input end |
#io ⇒ Object (readonly)
Returns the value of attribute io.
76 77 78 |
# File 'lib/prism/serialize.rb', line 76 def io @io end |
#serialized ⇒ Object (readonly)
Returns the value of attribute serialized.
76 77 78 |
# File 'lib/prism/serialize.rb', line 76 def serialized @serialized end |
#source ⇒ Object (readonly)
Returns the value of attribute source.
77 78 79 |
# File 'lib/prism/serialize.rb', line 77 def source @source end |
#start_line ⇒ Object (readonly)
Returns the value of attribute start_line.
78 79 80 |
# File 'lib/prism/serialize.rb', line 78 def start_line @start_line end |
Instance Method Details
#load_comments ⇒ Object
118 119 120 121 122 123 124 125 |
# File 'lib/prism/serialize.rb', line 118 def load_comments Array.new(load_varuint) do case load_varuint when 0 then InlineComment.new(load_location_object) when 1 then EmbDocComment.new(load_location_object) end end end |
#load_encoding ⇒ Object
104 105 106 107 108 |
# File 'lib/prism/serialize.rb', line 104 def load_encoding @encoding = Encoding.find(io.read(load_varuint)) @input = input.force_encoding(@encoding).freeze @encoding end |
#load_header ⇒ Object
95 96 97 98 99 100 101 102 |
# File 'lib/prism/serialize.rb', line 95 def load_header raise "Invalid serialization" if io.read(5) != "PRISM" raise "Invalid serialization" if io.read(3).unpack("C3") != [MAJOR_VERSION, MINOR_VERSION, PATCH_VERSION] only_semantic_fields = io.getbyte unless only_semantic_fields == 0 raise "Invalid serialization (location fields must be included but are not)" end end |
#load_line_offsets ⇒ Object
114 115 116 |
# File 'lib/prism/serialize.rb', line 114 def load_line_offsets source.instance_variable_set :@offsets, Array.new(load_varuint) { load_varuint } end |
#load_metadata ⇒ Object
446 447 448 449 450 451 452 453 |
# File 'lib/prism/serialize.rb', line 446 def comments = load_comments magic_comments = Array.new(load_varuint) { MagicComment.new(load_location_object, load_location_object) } data_loc = load_optional_location_object errors = Array.new(load_varuint) { ParseError.new(DIAGNOSTIC_TYPES.fetch(load_varuint), , load_location_object, load_error_level) } warnings = Array.new(load_varuint) { ParseWarning.new(DIAGNOSTIC_TYPES.fetch(load_varuint), , load_location_object, load_warning_level) } [comments, magic_comments, data_loc, errors, warnings] end |
#load_nodes ⇒ Object
480 481 482 483 484 485 486 487 488 489 490 491 492 |
# File 'lib/prism/serialize.rb', line 480 def load_nodes load_header load_encoding load_start_line load_line_offsets comments, magic_comments, data_loc, errors, warnings = @constant_pool_offset = load_uint32 @constant_pool = Array.new(load_varuint, nil) [load_node, comments, magic_comments, data_loc, errors, warnings] end |
#load_result ⇒ Object
494 495 496 497 |
# File 'lib/prism/serialize.rb', line 494 def load_result node, comments, magic_comments, data_loc, errors, warnings = load_nodes ParseResult.new(node, comments, magic_comments, data_loc, errors, warnings, @source) end |
#load_start_line ⇒ Object
110 111 112 |
# File 'lib/prism/serialize.rb', line 110 def load_start_line source.instance_variable_set :@start_line, load_varsint end |
#load_tokens ⇒ Object
455 456 457 458 459 460 461 462 463 464 465 466 |
# File 'lib/prism/serialize.rb', line 455 def load_tokens tokens = [] while type = TOKEN_TYPES.fetch(load_varuint) start = load_varuint length = load_varuint lex_state = load_varuint location = Location.new(@source, start, length) tokens << [Token.new(source, type, location.slice, location), lex_state] end tokens end |
#load_tokens_result ⇒ Object
468 469 470 471 472 473 474 475 476 477 478 |
# File 'lib/prism/serialize.rb', line 468 def load_tokens_result tokens = load_tokens encoding = load_encoding load_start_line load_line_offsets comments, magic_comments, data_loc, errors, warnings = tokens.each { |token,| token.value.force_encoding(encoding) } raise "Expected to consume all bytes while deserializing" unless @io.eof? LexResult.new(tokens, comments, magic_comments, data_loc, errors, warnings, @source) end |