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.



58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/prism/serialize.rb', line 58

def initialize(source, serialized)
  @encoding = Encoding::UTF_8

  @input = source.source.dup
  @serialized = serialized
  @io = StringIO.new(serialized)
  @io.set_encoding(Encoding::BINARY)

  @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.



55
56
57
# File 'lib/prism/serialize.rb', line 55

def constant_pool
  @constant_pool
end

#constant_pool_offsetObject (readonly)

Returns the value of attribute constant_pool_offset.



55
56
57
# File 'lib/prism/serialize.rb', line 55

def constant_pool_offset
  @constant_pool_offset
end

#encodingObject (readonly)

Returns the value of attribute encoding.



54
55
56
# File 'lib/prism/serialize.rb', line 54

def encoding
  @encoding
end

#inputObject (readonly)

Returns the value of attribute input.



54
55
56
# File 'lib/prism/serialize.rb', line 54

def input
  @input
end

#ioObject (readonly)

Returns the value of attribute io.



54
55
56
# File 'lib/prism/serialize.rb', line 54

def io
  @io
end

#serializedObject (readonly)

Returns the value of attribute serialized.



54
55
56
# File 'lib/prism/serialize.rb', line 54

def serialized
  @serialized
end

#sourceObject (readonly)

Returns the value of attribute source.



55
56
57
# File 'lib/prism/serialize.rb', line 55

def source
  @source
end

#start_lineObject (readonly)

Returns the value of attribute start_line.



56
57
58
# File 'lib/prism/serialize.rb', line 56

def start_line
  @start_line
end

Instance Method Details

#load_commentsObject



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

def load_comments
  load_varuint.times.map do
    case load_varuint
    when 0 then InlineComment.new(load_location)
    when 1 then EmbDocComment.new(load_location)
    when 2 then DATAComment.new(load_location)
    end
  end
end

#load_encodingObject



82
83
84
85
86
# File 'lib/prism/serialize.rb', line 82

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

#load_headerObject



73
74
75
76
77
78
79
80
# File 'lib/prism/serialize.rb', line 73

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.read(1).unpack1("C")
  unless only_semantic_fields == 0
    raise "Invalid serialization (location fields must be included but are not)"
  end
end

#load_metadataObject



102
103
104
105
106
107
108
109
# File 'lib/prism/serialize.rb', line 102

def 
  comments = load_comments
  magic_comments = load_varuint.times.map { MagicComment.new(load_location, load_location) }
  data_loc = load_optional_location
  errors = load_varuint.times.map { ParseError.new(load_embedded_string, load_location) }
  warnings = load_varuint.times.map { ParseWarning.new(load_embedded_string, load_location) }
  [comments, magic_comments, data_loc, errors, warnings]
end

#load_nodesObject



135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/prism/serialize.rb', line 135

def load_nodes
  load_header
  load_encoding
  load_start_line

  comments, magic_comments, data_loc, errors, warnings = 

  @constant_pool_offset = io.read(4).unpack1("L")
  @constant_pool = Array.new(load_varuint, nil)

  [load_node, comments, magic_comments, data_loc, errors, warnings]
end

#load_resultObject



148
149
150
151
# File 'lib/prism/serialize.rb', line 148

def load_result
  node, comments, magic_comments, data_loc, errors, warnings = load_nodes
  Prism::ParseResult.new(node, comments, magic_comments, data_loc, errors, warnings, @source)
end

#load_start_lineObject



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

def load_start_line
  source.start_line = load_varsint
end

#load_tokensObject



111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/prism/serialize.rb', line 111

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 << [Prism::Token.new(type, location.slice, location), lex_state]
  end

  tokens
end

#load_tokens_resultObject



124
125
126
127
128
129
130
131
132
133
# File 'lib/prism/serialize.rb', line 124

def load_tokens_result
  tokens = load_tokens
  encoding = load_encoding
  load_start_line
  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?
  Prism::ParseResult.new(tokens, comments, magic_comments, data_loc, errors, warnings, @source)
end