class SLF0::Token::Stream

Public Class Methods

new(tokens, class_deserializer:) click to toggle source
# File lib/slf0/token.rb, line 38
def initialize(tokens, class_deserializer:)
  @tokens = tokens
  @class_deserializer = class_deserializer
end

Public Instance Methods

deserializer_for(class_ref_num) click to toggle source
# File lib/slf0/token.rb, line 74
def deserializer_for(class_ref_num)
  @class_deserializer[class_ref_num].last
end
double(&reason_blk) click to toggle source
# File lib/slf0/token.rb, line 53
def double(&reason_blk)
  return if shift_nil?

  shift(Double, &reason_blk).value
end
int(&reason_blk) click to toggle source
# File lib/slf0/token.rb, line 43
def int(&reason_blk)
  shift(Int, &reason_blk).value
end
object(&reason_blk) click to toggle source
# File lib/slf0/token.rb, line 68
def object(&reason_blk)
  return if shift_nil?(&reason_blk)

  deserializer_for(shift(ClassNameRef, &reason_blk).value)[self]
end
object_list(&reason_blk) click to toggle source
# File lib/slf0/token.rb, line 59
def object_list(&reason_blk)
  return if shift_nil?(&reason_blk)

  length = shift(ObjectList, &reason_blk).length
  Array.new(length) do
    object { reason_blk && "object #{length} in object list for #{reason_blk&.call}" }
  end
end
shift(cls, raise: true, &reason_blk) click to toggle source
# File lib/slf0/token.rb, line 82
def shift(cls, raise: true, &reason_blk)
  unless (token = @tokens.shift)
    raise 'no more tokens'
  end

  unless token.is_a?(cls)
    unexpected_token!(cls, token, &reason_blk) if raise

    @tokens.unshift(token)
    return
  end
  token
end
shift_nil?(&reason_blk) click to toggle source
# File lib/slf0/token.rb, line 78
def shift_nil?(&reason_blk)
  shift(ObjectListNil, raise: false, &reason_blk)
end
string(&reason_blk) click to toggle source
# File lib/slf0/token.rb, line 47
def string(&reason_blk)
  return if shift_nil?(&reason_blk)

  shift(String, &reason_blk).value
end
unexpected_token!(expected_class, token, &reason_blk) click to toggle source
# File lib/slf0/token.rb, line 96
def unexpected_token!(expected_class, token, &reason_blk)
  raise "expected #{expected_class} got #{token.inspect} for #{reason_blk&.call}"
end