Class: SLF0::Token::Stream

Inherits:
Object
  • Object
show all
Defined in:
lib/slf0/token.rb

Direct Known Subclasses

XCActivityLog::Parser::S

Instance Method Summary collapse

Constructor Details

#initialize(tokens, class_deserializer:) ⇒ Stream

Returns a new instance of Stream.



38
39
40
41
# File 'lib/slf0/token.rb', line 38

def initialize(tokens, class_deserializer:)
  @tokens = tokens
  @class_deserializer = class_deserializer
end

Instance Method Details

#deserializer_for(class_ref_num) ⇒ Object



74
75
76
# File 'lib/slf0/token.rb', line 74

def deserializer_for(class_ref_num)
  @class_deserializer[class_ref_num].last
end

#double(&reason_blk) ⇒ Object



53
54
55
56
57
# File 'lib/slf0/token.rb', line 53

def double(&reason_blk)
  return if shift_nil?

  shift(Double, &reason_blk).value
end

#int(&reason_blk) ⇒ Object



43
44
45
# File 'lib/slf0/token.rb', line 43

def int(&reason_blk)
  shift(Int, &reason_blk).value
end

#object(&reason_blk) ⇒ Object



68
69
70
71
72
# 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) ⇒ Object



59
60
61
62
63
64
65
66
# 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) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
# 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) ⇒ Boolean

Returns:

  • (Boolean)


78
79
80
# File 'lib/slf0/token.rb', line 78

def shift_nil?(&reason_blk)
  shift(ObjectListNil, raise: false, &reason_blk)
end

#string(&reason_blk) ⇒ Object



47
48
49
50
51
# 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) ⇒ Object



96
97
98
# 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