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_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.
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_pool ⇒ Object (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_offset ⇒ Object (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 |
#encoding ⇒ Object (readonly)
Returns the value of attribute encoding.
54 55 56 |
# File 'lib/prism/serialize.rb', line 54 def encoding @encoding end |
#input ⇒ Object (readonly)
Returns the value of attribute input.
54 55 56 |
# File 'lib/prism/serialize.rb', line 54 def input @input end |
#io ⇒ Object (readonly)
Returns the value of attribute io.
54 55 56 |
# File 'lib/prism/serialize.rb', line 54 def io @io end |
#serialized ⇒ Object (readonly)
Returns the value of attribute serialized.
54 55 56 |
# File 'lib/prism/serialize.rb', line 54 def serialized @serialized end |
#source ⇒ Object (readonly)
Returns the value of attribute source.
55 56 57 |
# File 'lib/prism/serialize.rb', line 55 def source @source end |
#start_line ⇒ Object (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_comments ⇒ Object
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_encoding ⇒ Object
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_header ⇒ Object
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_metadata ⇒ Object
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_location) } warnings = load_varuint.times.map { ParseWarning.new(, load_location) } [comments, magic_comments, data_loc, errors, warnings] end |
#load_nodes ⇒ Object
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_result ⇒ Object
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_line ⇒ Object
88 89 90 |
# File 'lib/prism/serialize.rb', line 88 def load_start_line source.start_line = load_varsint end |
#load_tokens ⇒ Object
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_result ⇒ Object
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 |