Example #1
0
def _skip_text_output_attribute():
    """Returns the IR for a [text_output: "Skip"] attribute."""
    result = ir_pb2.Attribute(name=ir_pb2.Word(text=attributes.TEXT_OUTPUT),
                              value=ir_pb2.AttributeValue(
                                  string_constant=ir_pb2.String(text="Skip")))
    _mark_as_synthetic(result)
    return result
Example #2
0
 def test_get_duplicate_attribute(self):
     type_def = ir_pb2.TypeDefinition(attribute=[
         ir_pb2.Attribute(value=ir_pb2.AttributeValue(
             expression=ir_pb2.Expression()),
                          name=ir_pb2.Word(text="phil")),
         ir_pb2.Attribute(value=ir_pb2.AttributeValue(
             expression=_parse_expression("true")),
                          name=ir_pb2.Word(text="bob")),
         ir_pb2.Attribute(value=ir_pb2.AttributeValue(
             expression=_parse_expression("false")),
                          name=ir_pb2.Word(text="bob")),
         ir_pb2.Attribute(value=ir_pb2.AttributeValue(
             expression=_parse_expression("false")),
                          name=ir_pb2.Word()),
     ])
     self.assertRaises(AssertionError, ir_util.get_attribute,
                       type_def.attribute, "bob")
Example #3
0
def _gather_default_attributes(obj, defaults):
  defaults = defaults.copy()
  for attr in obj.attribute:
    if attr.is_default:
      defaulted_attr = ir_pb2.Attribute()
      defaulted_attr.CopyFrom(attr)
      defaulted_attr.is_default = False
      defaults[attr.name.text] = defaulted_attr
  return {"defaults": defaults}
Example #4
0
def _construct_string_attribute(name, value, source_location):
  """Constructs a string Attribute with the given name and value."""
  attr_value = ir_pb2.AttributeValue(
      string_constant=ir_pb2.String(text=value,
                                    source_location=source_location),
      source_location=source_location)
  return ir_pb2.Attribute(name=ir_pb2.Word(text=name,
                                           source_location=source_location),
                          value=attr_value,
                          source_location=source_location)
Example #5
0
def _construct_boolean_attribute(name, value, source_location):
  """Constructs a boolean Attribute with the given name and value."""
  attr_value = ir_pb2.AttributeValue(
      expression=ir_pb2.Expression(
          boolean_constant=ir_pb2.BooleanConstant(
              value=value, source_location=source_location),
          type=ir_pb2.ExpressionType(boolean=ir_pb2.BooleanType(value=value)),
          source_location=source_location),
      source_location=source_location)
  return ir_pb2.Attribute(name=ir_pb2.Word(text=name,
                                           source_location=source_location),
                          value=attr_value,
                          source_location=source_location)
Example #6
0
def _construct_integer_attribute(name, value, source_location):
  """Constructs an integer Attribute with the given name and value."""
  attr_value = ir_pb2.AttributeValue(
      expression=ir_pb2.Expression(
          constant=ir_pb2.NumericConstant(value=str(value),
                                          source_location=source_location),
          type=ir_pb2.ExpressionType(
              integer=ir_pb2.IntegerType(modular_value=str(value),
                                         modulus="infinity",
                                         minimum_value=str(value),
                                         maximum_value=str(value))),
          source_location=source_location),
      source_location=source_location)
  return ir_pb2.Attribute(name=ir_pb2.Word(text=name),
                          value=attr_value,
                          source_location=source_location)
Example #7
0
 def test_get_boolean_attribute(self):
     type_def = ir_pb2.TypeDefinition(attribute=[
         ir_pb2.Attribute(value=ir_pb2.AttributeValue(
             expression=ir_pb2.Expression()),
                          name=ir_pb2.Word(text="phil")),
         ir_pb2.Attribute(value=ir_pb2.AttributeValue(
             expression=_parse_expression("false")),
                          name=ir_pb2.Word(text="bob"),
                          is_default=True),
         ir_pb2.Attribute(value=ir_pb2.AttributeValue(
             expression=_parse_expression("true")),
                          name=ir_pb2.Word(text="bob")),
         ir_pb2.Attribute(value=ir_pb2.AttributeValue(
             expression=_parse_expression("false")),
                          name=ir_pb2.Word(text="bob2")),
         ir_pb2.Attribute(value=ir_pb2.AttributeValue(
             expression=_parse_expression("true")),
                          name=ir_pb2.Word(text="bob2"),
                          is_default=True),
         ir_pb2.Attribute(value=ir_pb2.AttributeValue(
             expression=_parse_expression("false")),
                          name=ir_pb2.Word(text="bob3"),
                          is_default=True),
         ir_pb2.Attribute(value=ir_pb2.AttributeValue(
             expression=_parse_expression("false")),
                          name=ir_pb2.Word()),
     ])
     self.assertTrue(
         ir_util.get_boolean_attribute(type_def.attribute, "bob"))
     self.assertTrue(
         ir_util.get_boolean_attribute(type_def.attribute,
                                       "bob",
                                       default_value=False))
     self.assertFalse(
         ir_util.get_boolean_attribute(type_def.attribute, "bob2"))
     self.assertFalse(
         ir_util.get_boolean_attribute(type_def.attribute,
                                       "bob2",
                                       default_value=True))
     self.assertIsNone(
         ir_util.get_boolean_attribute(type_def.attribute, "Bob"))
     self.assertTrue(
         ir_util.get_boolean_attribute(type_def.attribute,
                                       "Bob",
                                       default_value=True))
     self.assertIsNone(
         ir_util.get_boolean_attribute(type_def.attribute, "bob3"))
Example #8
0
 def test_get_integer_attribute(self):
     type_def = ir_pb2.TypeDefinition(attribute=[
         ir_pb2.Attribute(value=ir_pb2.AttributeValue(
             expression=ir_pb2.Expression(type=ir_pb2.ExpressionType(
                 integer=ir_pb2.IntegerType()))),
                          name=ir_pb2.Word(text="phil")),
         ir_pb2.Attribute(value=ir_pb2.AttributeValue(
             expression=ir_pb2.Expression(
                 constant=ir_pb2.NumericConstant(value="20"),
                 type=ir_pb2.ExpressionType(integer=ir_pb2.IntegerType(
                     modular_value="20", modulus="infinity")))),
                          name=ir_pb2.Word(text="bob"),
                          is_default=True),
         ir_pb2.Attribute(value=ir_pb2.AttributeValue(
             expression=ir_pb2.Expression(
                 constant=ir_pb2.NumericConstant(value="10"),
                 type=ir_pb2.ExpressionType(integer=ir_pb2.IntegerType(
                     modular_value="10", modulus="infinity")))),
                          name=ir_pb2.Word(text="bob")),
         ir_pb2.Attribute(value=ir_pb2.AttributeValue(
             expression=ir_pb2.Expression(
                 constant=ir_pb2.NumericConstant(value="5"),
                 type=ir_pb2.ExpressionType(integer=ir_pb2.IntegerType(
                     modular_value="5", modulus="infinity")))),
                          name=ir_pb2.Word(text="bob2")),
         ir_pb2.Attribute(value=ir_pb2.AttributeValue(
             expression=ir_pb2.Expression(
                 constant=ir_pb2.NumericConstant(value="0"),
                 type=ir_pb2.ExpressionType(integer=ir_pb2.IntegerType(
                     modular_value="0", modulus="infinity")))),
                          name=ir_pb2.Word(text="bob2"),
                          is_default=True),
         ir_pb2.Attribute(value=ir_pb2.AttributeValue(
             expression=ir_pb2.Expression(
                 constant=ir_pb2.NumericConstant(value="30"),
                 type=ir_pb2.ExpressionType(integer=ir_pb2.IntegerType(
                     modular_value="30", modulus="infinity")))),
                          name=ir_pb2.Word(text="bob3"),
                          is_default=True),
         ir_pb2.Attribute(value=ir_pb2.AttributeValue(
             expression=ir_pb2.Expression(function=ir_pb2.Function(
                 function=ir_pb2.Function.ADDITION,
                 args=[
                     ir_pb2.Expression(
                         constant=ir_pb2.NumericConstant(value="100"),
                         type=ir_pb2.ExpressionType(
                             integer=ir_pb2.IntegerType(
                                 modular_value="100", modulus="infinity"))),
                     ir_pb2.Expression(
                         constant=ir_pb2.NumericConstant(value="100"),
                         type=ir_pb2.ExpressionType(
                             integer=ir_pb2.IntegerType(
                                 modular_value="100", modulus="infinity")))
                 ]),
                                          type=ir_pb2.ExpressionType(
                                              integer=ir_pb2.IntegerType(
                                                  modular_value="200",
                                                  modulus="infinity")))),
                          name=ir_pb2.Word(text="bob4")),
         ir_pb2.Attribute(value=ir_pb2.AttributeValue(
             expression=ir_pb2.Expression(
                 constant=ir_pb2.NumericConstant(value="40"),
                 type=ir_pb2.ExpressionType(integer=ir_pb2.IntegerType(
                     modular_value="40", modulus="infinity")))),
                          name=ir_pb2.Word()),
     ])
     self.assertEqual(
         10, ir_util.get_integer_attribute(type_def.attribute, "bob"))
     self.assertEqual(
         5, ir_util.get_integer_attribute(type_def.attribute, "bob2"))
     self.assertIsNone(
         ir_util.get_integer_attribute(type_def.attribute, "Bob"))
     self.assertEqual(
         10,
         ir_util.get_integer_attribute(type_def.attribute,
                                       "Bob",
                                       default_value=10))
     self.assertIsNone(
         ir_util.get_integer_attribute(type_def.attribute, "bob3"))
     self.assertEqual(
         200, ir_util.get_integer_attribute(type_def.attribute, "bob4"))