Class: Prism::Serialize::Loader

Inherits:
Object
  • Object
show all
Defined in:
lib/prism/serialize.rb

Overview

:nodoc:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, serialized) ⇒ Loader

Returns a new instance of Loader.



91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/prism/serialize.rb', line 91

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_poolObject (readonly)

Returns the value of attribute constant_pool.



88
89
90
# File 'lib/prism/serialize.rb', line 88

def constant_pool
  @constant_pool
end

#constant_pool_offsetObject (readonly)

Returns the value of attribute constant_pool_offset.



88
89
90
# File 'lib/prism/serialize.rb', line 88

def constant_pool_offset
  @constant_pool_offset
end

#encodingObject (readonly)

Returns the value of attribute encoding.



87
88
89
# File 'lib/prism/serialize.rb', line 87

def encoding
  @encoding
end

#inputObject (readonly)

Returns the value of attribute input.



87
88
89
# File 'lib/prism/serialize.rb', line 87

def input
  @input
end

#ioObject (readonly)

Returns the value of attribute io.



87
88
89
# File 'lib/prism/serialize.rb', line 87

def io
  @io
end

#serializedObject (readonly)

Returns the value of attribute serialized.



87
88
89
# File 'lib/prism/serialize.rb', line 87

def serialized
  @serialized
end

#sourceObject (readonly)

Returns the value of attribute source.



88
89
90
# File 'lib/prism/serialize.rb', line 88

def source
  @source
end

#start_lineObject (readonly)

Returns the value of attribute start_line.



89
90
91
# File 'lib/prism/serialize.rb', line 89

def start_line
  @start_line
end

Instance Method Details

#load_commentsObject



129
130
131
132
133
134
135
136
# File 'lib/prism/serialize.rb', line 129

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_encodingObject



115
116
117
118
119
# File 'lib/prism/serialize.rb', line 115

def load_encoding
  @encoding = Encoding.find(io.read(load_varuint))
  @input = input.force_encoding(@encoding).freeze
  @encoding
end

#load_headerObject



106
107
108
109
110
111
112
113
# File 'lib/prism/serialize.rb', line 106

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_offsetsObject



125
126
127
# File 'lib/prism/serialize.rb', line 125

def load_line_offsets
  source.instance_variable_set :@offsets, Array.new(load_varuint) { load_varuint }
end

#load_metadataObject



462
463
464
465
466
467
468
469
# File 'lib/prism/serialize.rb', line 462

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_embedded_string, load_location_object, load_error_level) }
  warnings = Array.new(load_varuint) { ParseWarning.new(DIAGNOSTIC_TYPES.fetch(load_varuint), load_embedded_string, load_location_object, load_warning_level) }
  [comments, magic_comments, data_loc, errors, warnings]
end

#load_nodesObject



496
497
498
499
500
501
502
503
504
505
506
507
508
# File 'lib/prism/serialize.rb', line 496

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_resultObject



510
511
512
513
# File 'lib/prism/serialize.rb', line 510

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_lineObject



121
122
123
# File 'lib/prism/serialize.rb', line 121

def load_start_line
  source.instance_variable_set :@start_line, load_varsint
end

#load_tokensObject



471
472
473
474
475
476
477
478
479
480
481
482
# File 'lib/prism/serialize.rb', line 471

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_resultObject



484
485
486
487
488
489
490
491
492
493
494
# File 'lib/prism/serialize.rb', line 484

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