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, line 196
def initialize(signature)
  @signature = signature
  if signature.size > 255
    msg = "Potential signature is longer than 255 characters (#{@signature.size}): #{@signature}"
    raise SignatureException, msg
  end

  @idx = 0
end

Public Instance Methods

nextchar() click to toggle source

Returns the next character from the signature.

# File lib/dbus/type.rb, line 207
def nextchar
  c = @signature[@idx]
  @idx += 1
  c
end
parse() click to toggle source

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

# File lib/dbus/type.rb, line 262
def parse
  @idx = 0
  ret = []
  while (c = nextchar)
    ret << parse_one(c)
  end
  ret.freeze
end
parse1() click to toggle source

Parse one {SingleCompleteType} @return [Type]

# File lib/dbus/type.rb, line 273
def parse1
  c = nextchar
  raise SignatureException, "Empty signature, expecting a Single Complete Type" if c.nil?

  t = parse_one(c)
  raise SignatureException, "Has more than a Single Complete Type: #{@signature}" unless nextchar.nil?

  t.freeze
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, line 216
def parse_one(char, for_array: false)
  res = nil
  case char
  when "a"
    res = Type.new(ARRAY)
    char = nextchar
    raise SignatureException, "Empty ARRAY in #{@signature}" if char.nil?

    child = parse_one(char, for_array: true)
    res << child
  when "("
    res = Type.new(STRUCT, abstract: true)
    while (char = nextchar) && char != ")"
      res << parse_one(char)
    end
    raise SignatureException, "STRUCT not closed in #{@signature}" if char.nil?
    raise SignatureException, "Empty STRUCT in #{@signature}" if res.members.empty?
  when "{"
    raise SignatureException, "DICT_ENTRY not an immediate child of an ARRAY" unless for_array

    res = Type.new(DICT_ENTRY, abstract: true)

    # key type, value type
    2.times do |i|
      char = nextchar
      raise SignatureException, "DICT_ENTRY not closed in #{@signature}" if char.nil?

      raise SignatureException, "DICT_ENTRY must have 2 subtypes, found #{i} in #{@signature}" if char == "}"

      res << parse_one(char)
    end

    # closing "}"
    char = nextchar
    raise SignatureException, "DICT_ENTRY not closed in #{@signature}" if char.nil?

    raise SignatureException, "DICT_ENTRY must have 2 subtypes, found 3 or more in #{@signature}" if char != "}"
  else
    res = Type.new(char)
  end
  res.members.freeze
  res
end