Class: BinaryParser::WhileList

Inherits:
LoopList
  • Object
show all
Defined in:
lib/binary_parser/while_list.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from LoopList

#[], #content_description, #each, #show, #size

Constructor Details

#initialize(definition, abstract_binary, parent_scope, name) ⇒ WhileList

Returns a new instance of WhileList.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/binary_parser/while_list.rb', line 6

def initialize(definition, abstract_binary, parent_scope, name)
  parsed_length = 0
  list, rest_binary = [], abstract_binary
  while continue?(definition, rest_binary, parent_scope, name)
    template = definition.klass.new(rest_binary, parent_scope)
    if template.structure_bit_length == 0
      raise ParsingError, "0 bit-length repetition happens. This means infinite loop."
    end
    parsed_length += template.structure_bit_length
    rest_binary = rest_binary.sub(:bit_index => template.structure_bit_length)
    list << template
  end
  @list, @bit_length = list, parsed_length
end

Instance Attribute Details

#bit_lengthObject (readonly)

Returns the value of attribute bit_length.



4
5
6
# File 'lib/binary_parser/while_list.rb', line 4

def bit_length
  @bit_length
end

Instance Method Details

#continue?(definition, rest_binary, parent_scope, name) ⇒ Boolean

Returns:

  • (Boolean)


21
22
23
24
25
26
27
28
29
# File 'lib/binary_parser/while_list.rb', line 21

def continue?(definition, rest_binary, parent_scope, name)
  definition.loop_condition.eval do |token|
    if token.nextbits_var?
      TemplateBase.new(rest_binary.sub(:bit_length => token.length)).to_i
    else
      parent_scope.token_eval(token, name)
    end
  end
end