# class DBus::Type::Parser

# D-Bus type parser class¶ ↑

Helper class to parse a type signature in the protocol. @api private

### Public Class Methods

new(signature) click to toggle source

Create a new parser for the given signature. @param signature [Signature]

        # File lib/dbus/type.rb
    197 def initialize(signature)
    198   @signature = signature
    199   if signature.size > 255
    200     msg = "Potential signature is longer than 255 characters (#{@signature.size}): #{@signature}"
    201     raise SignatureException, msg
    202   end
    203
    204   @idx = 0
    205 end

### Public Instance Methods

nextchar() click to toggle source

Returns the next character from the signature.

        # File lib/dbus/type.rb
    208 def nextchar
    209   c = @signature[@idx]
    210   @idx += 1
    211   c
    212 end

parse() click to toggle source

Parse the entire signature, return a `DBus::Type` object. @return [Array<Type>]

        # File lib/dbus/type.rb
    263 def parse
    264   @idx = 0
    265   ret = []
    266   while (c = nextchar)
    267     ret << parse_one(c)
    268   end
    269   ret.freeze
    270 end

parse1() click to toggle source

Parse one {SingleCompleteType} @return [Type]

        # File lib/dbus/type.rb
    274 def parse1
    275   c = nextchar
    276   raise SignatureException, "Empty signature, expecting a Single Complete Type" if c.nil?
    277
    278   t = parse_one(c)
    279   raise SignatureException, "Has more than a Single Complete Type: #{@signature}" unless nextchar.nil?
    280
    281   t.freeze
    282 end

parse_one(char, for_array: false) click to toggle source

Parse one character char of the signature. @param for_array [Boolean] are we parsing an immediate child of an ARRAY @return [Type]

        # File lib/dbus/type.rb
    217 def parse_one(char, for_array: false)
    218   res = nil
    219   case char
    220   when "a"
    221     res = Type.new(ARRAY)
    222     char = nextchar
    223     raise SignatureException, "Empty ARRAY in #{@signature}" if char.nil?
    224
    225     child = parse_one(char, for_array: true)
    226     res << child
    227   when "("
    228     res = Type.new(STRUCT, abstract: true)
    229     while (char = nextchar) && char != ")"
    230       res << parse_one(char)
    231     end
    232     raise SignatureException, "STRUCT not closed in #{@signature}" if char.nil?
    233     raise SignatureException, "Empty STRUCT in #{@signature}" if res.members.empty?
    234   when "{"
    235     raise SignatureException, "DICT_ENTRY not an immediate child of an ARRAY" unless for_array
    236
    237     res = Type.new(DICT_ENTRY, abstract: true)
    238
    239     # key type, value type
    240     2.times do |i|
    241       char = nextchar
    242       raise SignatureException, "DICT_ENTRY not closed in #{@signature}" if char.nil?
    243
    244       raise SignatureException, "DICT_ENTRY must have 2 subtypes, found #{i} in #{@signature}" if char == "}"
    245
    246       res << parse_one(char)
    247     end
    248
    249     # closing "}"
    250     char = nextchar
    251     raise SignatureException, "DICT_ENTRY not closed in #{@signature}" if char.nil?
    252
    253     raise SignatureException, "DICT_ENTRY must have 2 subtypes, found 3 or more in #{@signature}" if char != "}"
    254   else
    255     res = Type.new(char)
    256   end
    257   res.members.freeze
    258   res
    259 end