module Prism::Serialize

def self.load_lex(input, serialized, freeze)

the structure of the serialized data.
The formatting of the source of this method is purposeful to illustrate

Deserialize the dumped output from a request to lex or lex_file.
def self.load_lex(input, serialized, freeze)
  source = Source.for(input)
  loader = Loader.new(source, serialized)
  tokens =         loader.load_tokens
  encoding =       loader.load_encoding
  start_line =     loader.load_varsint
  offsets =        loader.load_line_offsets(freeze)
  source.replace_start_line(start_line)
  source.replace_offsets(offsets)
  comments =       loader.load_comments(freeze)
  magic_comments = loader.load_magic_comments(freeze)
  data_loc =       loader.load_optional_location_object(freeze)
  errors =         loader.load_errors(encoding, freeze)
  warnings =       loader.load_warnings(encoding, freeze)
  raise unless     loader.eof?
  result = LexResult.new(tokens, comments, magic_comments, data_loc, errors, warnings, source)
  tokens.each do |token|
    token[0].value.force_encoding(encoding)
    if freeze
      token[0].deep_freeze
      token.freeze
    end
  end
  if freeze
    source.deep_freeze
    tokens.freeze
    result.freeze
  end
  result
end

def self.load_parse(input, serialized, freeze)

the structure of the serialized data.
The formatting of the source of this method is purposeful to illustrate

Deserialize the dumped output from a request to parse or parse_file.
def self.load_parse(input, serialized, freeze)
  input = input.dup
  source = Source.for(input)
  loader = Loader.new(source, serialized)
                   loader.load_header
  encoding =       loader.load_encoding
  start_line =     loader.load_varsint
  offsets =        loader.load_line_offsets(freeze)
  source.replace_start_line(start_line)
  source.replace_offsets(offsets)
  comments =       loader.load_comments(freeze)
  magic_comments = loader.load_magic_comments(freeze)
  data_loc =       loader.load_optional_location_object(freeze)
  errors =         loader.load_errors(encoding, freeze)
  warnings =       loader.load_warnings(encoding, freeze)
  cpool_base =     loader.load_uint32
  cpool_size =     loader.load_varuint
  constant_pool = ConstantPool.new(input, serialized, cpool_base, cpool_size)
  node =           loader.load_node(constant_pool, encoding, freeze)
                   loader.load_constant_pool(constant_pool)
  raise unless     loader.eof?
  result = ParseResult.new(node, comments, magic_comments, data_loc, errors, warnings, source)
  result.freeze if freeze
  input.force_encoding(encoding)
  # This is an extremely niche use-case where the file was marked as binary
  # but it contained UTF-8-encoded characters. In that case we will actually
  # put it back to UTF-8 to give the location APIs the best chance of being
  # correct.
  if !input.ascii_only? && input.encoding == Encoding::BINARY
    input.force_encoding(Encoding::UTF_8)
    input.force_encoding(Encoding::BINARY) unless input.valid_encoding?
  end
  if freeze
    input.freeze
    source.deep_freeze
  end
  result
end

def self.load_parse_comments(input, serialized, freeze)

the structure of the serialized data.
The formatting of the source of this method is purposeful to illustrate

parse_file_comments.
Deserialize the dumped output from a request to parse_comments or
def self.load_parse_comments(input, serialized, freeze)
  source = Source.for(input)
  loader = Loader.new(source, serialized)
               loader.load_header
               loader.load_encoding
  start_line = loader.load_varsint
  source.replace_start_line(start_line)
  result =     loader.load_comments(freeze)
  raise unless loader.eof?
  source.deep_freeze if freeze
  result
end

def self.load_parse_lex(input, serialized, freeze)

the structure of the serialized data.
The formatting of the source of this method is purposeful to illustrate

parse_lex_file.
Deserialize the dumped output from a request to parse_lex or
def self.load_parse_lex(input, serialized, freeze)
  source = Source.for(input)
  loader = Loader.new(source, serialized)
  tokens =         loader.load_tokens
                   loader.load_header
  encoding =       loader.load_encoding
  start_line =     loader.load_varsint
  offsets =        loader.load_line_offsets(freeze)
  source.replace_start_line(start_line)
  source.replace_offsets(offsets)
  comments =       loader.load_comments(freeze)
  magic_comments = loader.load_magic_comments(freeze)
  data_loc =       loader.load_optional_location_object(freeze)
  errors =         loader.load_errors(encoding, freeze)
  warnings =       loader.load_warnings(encoding, freeze)
  cpool_base =     loader.load_uint32
  cpool_size =     loader.load_varuint
  constant_pool = ConstantPool.new(input, serialized, cpool_base, cpool_size)
  node =           loader.load_node(constant_pool, encoding, freeze)
                   loader.load_constant_pool(constant_pool)
  raise unless     loader.eof?
  value = [node, tokens]
  result = ParseLexResult.new(value, comments, magic_comments, data_loc, errors, warnings, source)
  tokens.each do |token|
    token[0].value.force_encoding(encoding)
    if freeze
      token[0].deep_freeze
      token.freeze
    end
  end
  if freeze
    source.deep_freeze
    tokens.freeze
    value.freeze
    result.freeze
  end
  result
end