def ValueFromMojom(self, value):
        """Translates a mojom_types_mojom.Value.

    Args:
      value: {mojom_types_mojom.Value} to be translated.

    Returns:
      {str|module.BuiltinValue|module.NamedValue} translated from the passed in
      mojom_value.
      If value is a literal value, a string is returned. If the literal value is
        a string literal value, the returned string is enclosed in double
        quotes. If the literal value is a boolean literal value then one of the
        strings 'true' or 'false' is returned. Otherwise the literal value is a
        numeric literal value and in this case the returned value is a Python
        string representation of the numeric value.
      If value is a built-in value, a module.BuiltinValue is returned.
      If value is a user defined reference, a module.NamedValue is returned.
    """
        if value.tag == mojom_types_mojom.Value.Tags.literal_value:
            if (value.literal_value.tag ==
                    mojom_types_mojom.LiteralValue.Tags.string_value):
                return '"%s"' % value.literal_value.data
            if (value.literal_value.tag ==
                    mojom_types_mojom.LiteralValue.Tags.bool_value):
                # The strings 'true' and 'false' are used to represent bool literals.
                return ('%s' % value.literal_value.data).lower()
            elif (value.literal_value.tag
                  == mojom_types_mojom.LiteralValue.Tags.float_value
                  or value.literal_value.tag
                  == mojom_types_mojom.LiteralValue.Tags.double_value):
                # Use the Python repr() function to get a string that accurately
                # represents the value of the floating point number.
                return repr(value.literal_value.data)
            return str(value.literal_value.data)
        elif value.tag == mojom_types_mojom.Value.Tags.builtin_value:
            mojom_to_builtin = {
                mojom_types_mojom.BuiltinConstantValue.DOUBLE_INFINITY:
                'double.INFINITY',
                mojom_types_mojom.BuiltinConstantValue.DOUBLE_NEGATIVE_INFINITY:
                'double.NEGATIVE_INFINITY',
                mojom_types_mojom.BuiltinConstantValue.DOUBLE_NAN:
                'double.NAN',
                mojom_types_mojom.BuiltinConstantValue.FLOAT_INFINITY:
                'float.INFINITY',
                mojom_types_mojom.BuiltinConstantValue.FLOAT_NEGATIVE_INFINITY:
                'float.NEGATIVE_INFINITY',
                mojom_types_mojom.BuiltinConstantValue.FLOAT_NAN: 'float.NAN',
            }
            return module.BuiltinValue(mojom_to_builtin[value.builtin_value])

        assert value.tag == mojom_types_mojom.Value.Tags.user_value_reference
        return self.UserDefinedFromValueKey(
            value.user_value_reference.value_key)
Esempio n. 2
0
def FixupExpression(module, value, scope, kind):
    """Translates an IDENTIFIER into a built-in value or structured NamedValue
     object."""
    if isinstance(value, tuple) and value[0] == 'IDENTIFIER':
        # Allow user defined values to shadow builtins.
        result = LookupValue(module.values, value[1], scope, kind)
        if result:
            if isinstance(result, tuple):
                raise Exception('Unable to resolve expression: %r' % value[1])
            return result
        if IsBuiltinValue(value[1]):
            return mojom.BuiltinValue(value[1])
    return value