Example #1
0
    def testValidAssociativeArrays(self):
        """Tests that we can parse valid associative array structures."""

        source1 = "struct MyStruct { map<string, uint8> data; };"
        expected1 = ast.Mojom(None, ast.ImportList(), [
            ast.Struct(
                'MyStruct', None,
                ast.StructBody(
                    [ast.StructField('data', None, 'uint8{string}', None)]))
        ])
        self.assertEquals(parser.Parse(source1, "my_file.mojom"), expected1)

        source2 = "interface MyInterface { MyMethod(map<string, uint8> a); };"
        expected2 = ast.Mojom(None, ast.ImportList(), [
            ast.Interface(
                'MyInterface', None,
                ast.InterfaceBody(
                    ast.Method(
                        'MyMethod', None,
                        ast.ParameterList(
                            ast.Parameter('a', None, 'uint8{string}')), None)))
        ])
        self.assertEquals(parser.Parse(source2, "my_file.mojom"), expected2)

        source3 = "struct MyStruct { map<string, array<uint8>> data; };"
        expected3 = ast.Mojom(None, ast.ImportList(), [
            ast.Struct(
                'MyStruct', None,
                ast.StructBody(
                    [ast.StructField('data', None, 'uint8[]{string}', None)]))
        ])
        self.assertEquals(parser.Parse(source3, "my_file.mojom"), expected3)
Example #2
0
  def testValidFixedSizeArray(self):
    """Tests parsing a fixed size array."""

    source = """\
        struct MyStruct {
          array<int32> normal_array;
          array<int32, 1> fixed_size_array_one_entry;
          array<int32, 10> fixed_size_array_ten_entries;
          array<array<array<int32, 1>>, 2> nested_arrays;
        };
        """
    expected = ast.Mojom(
        None,
        ast.ImportList(),
        [ast.Struct(
            'MyStruct',
            None,
            ast.StructBody(
                [ast.StructField('normal_array', None, None, 'int32[]', None),
                 ast.StructField('fixed_size_array_one_entry', None, None,
                                 'int32[1]', None),
                 ast.StructField('fixed_size_array_ten_entries', None, None,
                                 'int32[10]', None),
                 ast.StructField('nested_arrays', None, None,
                                 'int32[1][][2]', None)]))])
    self.assertEquals(parser.Parse(source, "my_file.mojom"), expected)
Example #3
0
  def testValidHandleTypes(self):
    """Tests (valid) handle types."""

    source = """\
        struct MyStruct {
          handle a;
          handle<data_pipe_consumer> b;
          handle <data_pipe_producer> c;
          handle < message_pipe > d;
          handle
            < shared_buffer
            > e;
        };
        """
    expected = ast.Mojom(
        None,
        ast.ImportList(),
        [ast.Struct(
            'MyStruct',
            None,
            ast.StructBody(
                [ast.StructField('a', None, None, 'handle', None),
                 ast.StructField('b', None, None, 'handle<data_pipe_consumer>',
                                 None),
                 ast.StructField('c', None, None, 'handle<data_pipe_producer>',
                                 None),
                 ast.StructField('d', None, None, 'handle<message_pipe>', None),
                 ast.StructField('e', None, None, 'handle<shared_buffer>',
                                 None)]))])
    self.assertEquals(parser.Parse(source, "my_file.mojom"), expected)
Example #4
0
  def testValidStructDefinitions(self):
    """Tests all types of definitions that can occur in a struct."""

    source = """\
        struct MyStruct {
          enum MyEnum { VALUE };
          const double kMyConst = 1.23;
          int32 a;
          SomeOtherStruct b;  // Invalidity detected at another stage.
        };
        """
    expected = ast.Mojom(
        None,
        ast.ImportList(),
        [ast.Struct(
            'MyStruct',
            None,
            ast.StructBody(
                [ast.Enum('MyEnum',
                          None,
                          ast.EnumValueList(
                              ast.EnumValue('VALUE', None, None))),
                 ast.Const('kMyConst', 'double', '1.23'),
                 ast.StructField('a', None, None, 'int32', None),
                 ast.StructField('b', None, None, 'SomeOtherStruct', None)]))])
    self.assertEquals(parser.Parse(source, "my_file.mojom"), expected)
Example #5
0
    def testSimpleStructWithoutModule(self):
        """Tests a simple struct without an explict module statement."""

        source = """\
        struct MyStruct {
          int32 a;
          double b;
        };
        """
        expected = ast.Mojom(None, ast.ImportList(), [
            ast.Struct(
                'MyStruct', None,
                ast.StructBody([
                    ast.StructField('a', None, 'int32', None),
                    ast.StructField('b', None, 'double', None)
                ]))
        ])
        self.assertEquals(parser.Parse(source, "my_file.mojom"), expected)
Example #6
0
  def testValidAssociatedKinds(self):
    """Tests parsing associated interfaces and requests."""
    source1 = """\
        struct MyStruct {
          associated MyInterface a;
          associated MyInterface& b;
          associated MyInterface? c;
          associated MyInterface&? d;
        };
        """
    expected1 = ast.Mojom(
        None,
        ast.ImportList(),
        [ast.Struct(
            'MyStruct',
            None,
            ast.StructBody(
                [ast.StructField('a', None, None,'asso<MyInterface>', None),
                 ast.StructField('b', None, None,'asso<MyInterface&>', None),
                 ast.StructField('c', None, None,'asso<MyInterface>?', None),
                 ast.StructField('d', None, None,'asso<MyInterface&>?',
                                 None)]))])
    self.assertEquals(parser.Parse(source1, "my_file.mojom"), expected1)

    source2 = """\
        interface MyInterface {
          MyMethod(associated A a) =>(associated B& b);
        };"""
    expected2 = ast.Mojom(
        None,
        ast.ImportList(),
        [ast.Interface(
            'MyInterface',
            None,
            ast.InterfaceBody(
                ast.Method(
                    'MyMethod',
                    None,
                    None,
                    ast.ParameterList(
                        ast.Parameter('a', None, None, 'asso<A>')),
                    ast.ParameterList(
                        ast.Parameter('b', None, None, 'asso<B&>')))))])
    self.assertEquals(parser.Parse(source2, "my_file.mojom"), expected2)
Example #7
0
    def testValidNestedArray(self):
        """Tests parsing a nested array."""

        source = "struct MyStruct { array<array<int32>> nested_array; };"
        expected = ast.Mojom(None, ast.ImportList(), [
            ast.Struct(
                'MyStruct', None,
                ast.StructBody(
                    ast.StructField('nested_array', None, 'int32[][]', None)))
        ])
        self.assertEquals(parser.Parse(source, "my_file.mojom"), expected)
Example #8
0
  def testSimpleStruct(self):
    """Tests a simple .mojom source that just defines a struct."""

    source = """\
        module my_module;

        struct MyStruct {
          int32 a;
          double b;
        };
        """
    expected = ast.Mojom(
        ast.Module(('IDENTIFIER', 'my_module'), None),
        ast.ImportList(),
        [ast.Struct(
            'MyStruct',
            None,
            ast.StructBody(
                [ast.StructField('a', None, None, 'int32', None),
                 ast.StructField('b', None, None, 'double', None)]))])
    self.assertEquals(parser.Parse(source, "my_file.mojom"), expected)
Example #9
0
    def testNestedNamespace(self):
        """Tests that "nested" namespaces work."""

        source = """\
        module my.mod;

        struct MyStruct {
          int32 a;
        };
        """
        expected = ast.Mojom(ast.Module(
            ('IDENTIFIER', 'my.mod'), None), ast.ImportList(), [
                ast.Struct(
                    'MyStruct', None,
                    ast.StructBody(ast.StructField('a', None, 'int32', None)))
            ])
        self.assertEquals(parser.Parse(source, "my_file.mojom"), expected)
Example #10
0
  def testConsts(self):
    """Tests some constants and struct members initialized with them."""

    source = """\
        module my_module;

        struct MyStruct {
          const int8 kNumber = -1;
          int8 number@0 = kNumber;
        };
        """
    expected = ast.Mojom(
        ast.Module(('IDENTIFIER', 'my_module'), None),
        ast.ImportList(),
        [ast.Struct(
            'MyStruct', None,
            ast.StructBody(
                [ast.Const('kNumber', 'int8', '-1'),
                 ast.StructField('number', None, ast.Ordinal(0), 'int8',
                                 ('IDENTIFIER', 'kNumber'))]))])
    self.assertEquals(parser.Parse(source, "my_file.mojom"), expected)
Example #11
0
  def testSimpleOrdinals(self):
    """Tests that (valid) ordinal values are scanned correctly."""

    source = """\
        module my_module {

        // This isn't actually valid .mojom, but the problem (missing ordinals)
        // should be handled at a different level.
        struct MyStruct {
          int32 a0@0;
          int32 a1@1;
          int32 a2@2;
          int32 a9@9;
          int32 a10 @10;
          int32 a11 @11;
          int32 a29 @29;
          int32 a1234567890 @1234567890;
        };

        }  // module my_module
        """
    expected = ast.Mojom(
        ast.Module(('IDENTIFIER', 'my_module'), None),
        ast.ImportList(),
        [ast.Struct(
            'MyStruct',
            None,
            ast.StructBody(
                [ast.StructField('a0', ast.Ordinal(0), 'int32', None),
                 ast.StructField('a1', ast.Ordinal(1), 'int32', None),
                 ast.StructField('a2', ast.Ordinal(2), 'int32', None),
                 ast.StructField('a9', ast.Ordinal(9), 'int32', None),
                 ast.StructField('a10', ast.Ordinal(10), 'int32', None),
                 ast.StructField('a11', ast.Ordinal(11), 'int32', None),
                 ast.StructField('a29', ast.Ordinal(29), 'int32', None),
                 ast.StructField('a1234567890', ast.Ordinal(1234567890),
                                 'int32', None)]))])
    self.assertEquals(parser.Parse(source, "my_file.mojom"), expected)
Example #12
0
 def p_struct_field(self, p):
   """struct_field : attribute_section typename NAME ordinal default SEMI"""
   p[0] = ast.StructField(p[3], p[1], p[4], p[2], p[5])
Example #13
0
  def testValidAttributes(self):
    """Tests parsing attributes (and attribute lists)."""

    # Note: We use structs because they have (optional) attribute lists.

    # Empty attribute list.
    source1 = "[] struct MyStruct {};"
    expected1 = ast.Mojom(
        None,
        ast.ImportList(),
        [ast.Struct('MyStruct', ast.AttributeList(), ast.StructBody())])
    self.assertEquals(parser.Parse(source1, "my_file.mojom"), expected1)

    # One-element attribute list, with name value.
    source2 = "[MyAttribute=MyName] struct MyStruct {};"
    expected2 = ast.Mojom(
        None,
        ast.ImportList(),
        [ast.Struct(
            'MyStruct',
            ast.AttributeList(ast.Attribute("MyAttribute", "MyName")),
            ast.StructBody())])
    self.assertEquals(parser.Parse(source2, "my_file.mojom"), expected2)

    # Two-element attribute list, with one string value and one integer value.
    source3 = "[MyAttribute1 = \"hello\", MyAttribute2 = 5] struct MyStruct {};"
    expected3 = ast.Mojom(
        None,
        ast.ImportList(),
        [ast.Struct(
            'MyStruct',
            ast.AttributeList([ast.Attribute("MyAttribute1", "hello"),
                               ast.Attribute("MyAttribute2", 5)]),
            ast.StructBody())])
    self.assertEquals(parser.Parse(source3, "my_file.mojom"), expected3)

    # Various places that attribute list is allowed.
    source4 = """\
        [Attr0=0] module my_module;

        [Attr1=1] struct MyStruct {
          [Attr2=2] int32 a;
        };
        [Attr3=3] union MyUnion {
          [Attr4=4] int32 a;
        };
        [Attr5=5] enum MyEnum {
          [Attr6=6] a
        };
        [Attr7=7] interface MyInterface {
          [Attr8=8] MyMethod([Attr9=9] int32 a) => ([Attr10=10] bool b);
        };
        """
    expected4 = ast.Mojom(
        ast.Module(('IDENTIFIER', 'my_module'),
                   ast.AttributeList([ast.Attribute("Attr0", 0)])),
        ast.ImportList(),
        [ast.Struct(
             'MyStruct',
             ast.AttributeList(ast.Attribute("Attr1", 1)),
             ast.StructBody(
                 ast.StructField(
                     'a', ast.AttributeList([ast.Attribute("Attr2", 2)]),
                     None, 'int32', None))),
         ast.Union(
             'MyUnion',
             ast.AttributeList(ast.Attribute("Attr3", 3)),
             ast.UnionBody(
                 ast.UnionField(
                     'a', ast.AttributeList([ast.Attribute("Attr4", 4)]), None,
                     'int32'))),
         ast.Enum(
             'MyEnum',
             ast.AttributeList(ast.Attribute("Attr5", 5)),
             ast.EnumValueList(
                 ast.EnumValue(
                     'VALUE', ast.AttributeList([ast.Attribute("Attr6", 6)]),
                     None))),
         ast.Interface(
            'MyInterface',
            ast.AttributeList(ast.Attribute("Attr7", 7)),
            ast.InterfaceBody(
                ast.Method(
                    'MyMethod',
                    ast.AttributeList(ast.Attribute("Attr8", 8)),
                    None,
                    ast.ParameterList(
                        ast.Parameter(
                            'a', ast.AttributeList([ast.Attribute("Attr9", 9)]),
                            None, 'int32')),
                    ast.ParameterList(
                        ast.Parameter(
                            'b',
                            ast.AttributeList([ast.Attribute("Attr10", 10)]),
                            None, 'bool')))))])
    self.assertEquals(parser.Parse(source4, "my_file.mojom"), expected4)
Example #14
0
  def testValidDefaultValues(self):
    """Tests default values that are valid (to the parser)."""

    source = """\
        struct MyStruct {
          int16 a0 = 0;
          uint16 a1 = 0x0;
          uint16 a2 = 0x00;
          uint16 a3 = 0x01;
          uint16 a4 = 0xcd;
          int32 a5 = 12345;
          int64 a6 = -12345;
          int64 a7 = +12345;
          uint32 a8 = 0x12cd3;
          uint32 a9 = -0x12cD3;
          uint32 a10 = +0x12CD3;
          bool a11 = true;
          bool a12 = false;
          float a13 = 1.2345;
          float a14 = -1.2345;
          float a15 = +1.2345;
          float a16 = 123.;
          float a17 = .123;
          double a18 = 1.23E10;
          double a19 = 1.E-10;
          double a20 = .5E+10;
          double a21 = -1.23E10;
          double a22 = +.123E10;
        };
        """
    expected = ast.Mojom(
        None,
        ast.ImportList(),
        [ast.Struct(
            'MyStruct',
            None,
            ast.StructBody(
                [ast.StructField('a0', None, None, 'int16', '0'),
                 ast.StructField('a1', None, None, 'uint16', '0x0'),
                 ast.StructField('a2', None, None, 'uint16', '0x00'),
                 ast.StructField('a3', None, None, 'uint16', '0x01'),
                 ast.StructField('a4', None, None, 'uint16', '0xcd'),
                 ast.StructField('a5' , None, None, 'int32', '12345'),
                 ast.StructField('a6', None, None, 'int64', '-12345'),
                 ast.StructField('a7', None, None, 'int64', '+12345'),
                 ast.StructField('a8', None, None, 'uint32', '0x12cd3'),
                 ast.StructField('a9', None, None, 'uint32', '-0x12cD3'),
                 ast.StructField('a10', None, None, 'uint32', '+0x12CD3'),
                 ast.StructField('a11', None, None, 'bool', 'true'),
                 ast.StructField('a12', None, None, 'bool', 'false'),
                 ast.StructField('a13', None, None, 'float', '1.2345'),
                 ast.StructField('a14', None, None, 'float', '-1.2345'),
                 ast.StructField('a15', None, None, 'float', '+1.2345'),
                 ast.StructField('a16', None, None, 'float', '123.'),
                 ast.StructField('a17', None, None, 'float', '.123'),
                 ast.StructField('a18', None, None, 'double', '1.23E10'),
                 ast.StructField('a19', None, None, 'double', '1.E-10'),
                 ast.StructField('a20', None, None, 'double', '.5E+10'),
                 ast.StructField('a21', None, None, 'double', '-1.23E10'),
                 ast.StructField('a22', None, None, 'double', '+.123E10')]))])
    self.assertEquals(parser.Parse(source, "my_file.mojom"), expected)
Example #15
0
  def testValidNullableTypes(self):
    """Tests parsing nullable types."""

    source = """\
        struct MyStruct {
          int32? a;  // This is actually invalid, but handled at a different
                     // level.
          string? b;
          array<int32> ? c;
          array<string ? > ? d;
          array<array<int32>?>? e;
          array<int32, 1>? f;
          array<string?, 1>? g;
          some_struct? h;
          handle? i;
          handle<data_pipe_consumer>? j;
          handle<data_pipe_producer>? k;
          handle<message_pipe>? l;
          handle<shared_buffer>? m;
          some_interface&? n;
        };
        """
    expected = ast.Mojom(
        None,
        ast.ImportList(),
        [ast.Struct(
            'MyStruct',
            None,
            ast.StructBody(
                [ast.StructField('a', None, None,'int32?', None),
                 ast.StructField('b', None, None,'string?', None),
                 ast.StructField('c', None, None,'int32[]?', None),
                 ast.StructField('d', None, None,'string?[]?', None),
                 ast.StructField('e', None, None,'int32[]?[]?', None),
                 ast.StructField('f', None, None,'int32[1]?', None),
                 ast.StructField('g', None, None,'string?[1]?', None),
                 ast.StructField('h', None, None,'some_struct?', None),
                 ast.StructField('i', None, None,'handle?', None),
                 ast.StructField('j', None, None,'handle<data_pipe_consumer>?',
                                 None),
                 ast.StructField('k', None, None,'handle<data_pipe_producer>?',
                                 None),
                 ast.StructField('l', None, None,'handle<message_pipe>?', None),
                 ast.StructField('m', None, None,'handle<shared_buffer>?',
                                 None),
                 ast.StructField('n', None, None,'some_interface&?', None)]))])
    self.assertEquals(parser.Parse(source, "my_file.mojom"), expected)