Ejemplo n.º 1
0
  def _ConsumeSingleByteString(self):
    """Consume one token of a string literal.

    String literals (whether bytes or text) can come in multiple adjacent
    tokens which are automatically concatenated, like in C or Python.  This
    method only consumes one token.

    Returns:
      The token parsed.
    Raises:
      ParseError: When the wrong format data is found.
    """
    text = self.token
    if len(text) < 1 or text[0] not in _QUOTES:
      raise self.ParseError('Expected string but found: %r' % (text,))

    if len(text) < 2 or text[-1] != text[0]:
      raise self.ParseError('String missing ending quote: %r' % (text,))

    try:
      result = text_encoding.CUnescape(text[1:-1])
    except ValueError as e:
      raise self.ParseError(str(e))
    self.NextToken()
    return result
Ejemplo n.º 2
0
    def _SetFieldType(self, field_proto, field_desc, package, scope):
        """Sets the field's type, cpp_type, message_type and enum_type.

    Args:
      field_proto: Data about the field in proto format.
      field_desc: The descriptor to modiy.
      package: The package the field's container is in.
      scope: Enclosing scope of available types.
    """
        if field_proto.type_name:
            desc = self._GetTypeFromScope(package, field_proto.type_name,
                                          scope)
        else:
            desc = None

        if not field_proto.HasField('type'):
            if isinstance(desc, descriptor.Descriptor):
                field_proto.type = descriptor.FieldDescriptor.TYPE_MESSAGE
            else:
                field_proto.type = descriptor.FieldDescriptor.TYPE_ENUM

        field_desc.cpp_type = descriptor.FieldDescriptor.ProtoTypeToCppProtoType(
            field_proto.type)

        if (field_proto.type == descriptor.FieldDescriptor.TYPE_MESSAGE
                or field_proto.type == descriptor.FieldDescriptor.TYPE_GROUP):
            field_desc.message_type = desc

        if field_proto.type == descriptor.FieldDescriptor.TYPE_ENUM:
            field_desc.enum_type = desc

        if field_proto.label == descriptor.FieldDescriptor.LABEL_REPEATED:
            field_desc.has_default_value = False
            field_desc.default_value = []
        elif field_proto.HasField('default_value'):
            field_desc.has_default_value = True
            if (field_proto.type == descriptor.FieldDescriptor.TYPE_DOUBLE or
                    field_proto.type == descriptor.FieldDescriptor.TYPE_FLOAT):
                field_desc.default_value = float(field_proto.default_value)
            elif field_proto.type == descriptor.FieldDescriptor.TYPE_STRING:
                field_desc.default_value = field_proto.default_value
            elif field_proto.type == descriptor.FieldDescriptor.TYPE_BOOL:
                field_desc.default_value = field_proto.default_value.lower(
                ) == 'true'
            elif field_proto.type == descriptor.FieldDescriptor.TYPE_ENUM:
                field_desc.default_value = field_desc.enum_type.values_by_name[
                    field_proto.default_value].number
            elif field_proto.type == descriptor.FieldDescriptor.TYPE_BYTES:
                field_desc.default_value = text_encoding.CUnescape(
                    field_proto.default_value)
            else:
                field_desc.default_value = int(field_proto.default_value)
        else:
            field_desc.has_default_value = False
            field_desc.default_value = None

        field_desc.type = field_proto.type
Ejemplo n.º 3
0
    def _ConsumeSingleByteString(self):
        """Consume one token of a string literal.

    String literals (whether bytes or text) can come in multiple adjacent
    tokens which are automatically concatenated, like in C or Python.  This
    method only consumes one token.
    """
        text = self.token
        if len(text) < 1 or text[0] not in ('\'', '"'):
            raise self._ParseError('Expected string.')

        if len(text) < 2 or text[-1] != text[0]:
            raise self._ParseError('String missing ending quote.')

        try:
            result = text_encoding.CUnescape(text[1:-1])
        except ValueError, e:
            raise self._ParseError(str(e))
Ejemplo n.º 4
0
 def testCUnescape(self):
     for escaped, escaped_utf8, unescaped in TEST_VALUES:
         self.assertEqual(unescaped, text_encoding.CUnescape(escaped))
         self.assertEqual(unescaped, text_encoding.CUnescape(escaped_utf8))