Exemple #1
0
  def testValidImports(self):
    """Tests parsing import statements."""

    # One import (no module statement).
    source1 = "import \"somedir/my.mojom\";"
    expected1 = ast.Mojom(
        None,
        ast.ImportList(ast.Import("somedir/my.mojom")),
        [])
    self.assertEquals(parser.Parse(source1, "my_file.mojom"), expected1)

    # Two imports (no module statement).
    source2 = """\
        import "somedir/my1.mojom";
        import "somedir/my2.mojom";
        """
    expected2 = ast.Mojom(
        None,
        ast.ImportList([ast.Import("somedir/my1.mojom"),
                        ast.Import("somedir/my2.mojom")]),
        [])
    self.assertEquals(parser.Parse(source2, "my_file.mojom"), expected2)

    # Imports with module statement.
    source3 = """\
        module my_module;
        import "somedir/my1.mojom";
        import "somedir/my2.mojom";
        """
    expected3 = ast.Mojom(
        ast.Module(('IDENTIFIER', 'my_module'), None),
        ast.ImportList([ast.Import("somedir/my1.mojom"),
                        ast.Import("somedir/my2.mojom")]),
        [])
    self.assertEquals(parser.Parse(source3, "my_file.mojom"), expected3)
Exemple #2
0
  def testEnums(self):
    """Tests that enum statements are correctly parsed."""

    source = """\
        module my_module {
        enum MyEnum1 { VALUE1, VALUE2 };  // No trailing comma.
        enum MyEnum2 {
          VALUE1 = -1,
          VALUE2 = 0,
          VALUE3 = + 987,  // Check that space is allowed.
          VALUE4 = 0xAF12,
          VALUE5 = -0x09bcd,
          VALUE6 = VALUE5,
          VALUE7,  // Leave trailing comma.
        };
        }  // my_module
        """
    expected = ast.Mojom(
        ast.Module(('IDENTIFIER', 'my_module'), None),
        ast.ImportList(),
        [ast.Enum(
            'MyEnum1',
            ast.EnumValueList([ast.EnumValue('VALUE1', None),
                               ast.EnumValue('VALUE2', None)])),
         ast.Enum(
            'MyEnum2',
            ast.EnumValueList([ast.EnumValue('VALUE1', '-1'),
                               ast.EnumValue('VALUE2', '0'),
                               ast.EnumValue('VALUE3', '+987'),
                               ast.EnumValue('VALUE4', '0xAF12'),
                               ast.EnumValue('VALUE5', '-0x09bcd'),
                               ast.EnumValue('VALUE6', ('IDENTIFIER',
                                                        'VALUE5')),
                               ast.EnumValue('VALUE7', None)]))])
    self.assertEquals(parser.Parse(source, "my_file.mojom"), expected)
Exemple #3
0
    def testSourceWithCrLfs(self):
        """Tests a .mojom source with CR-LFs instead of LFs."""

        source = "// This is a comment.\r\n\r\nmodule my_module;\r\n"
        expected = ast.Mojom(ast.Module(('IDENTIFIER', 'my_module'), None),
                             ast.ImportList(), [])
        self.assertEquals(parser.Parse(source, "my_file.mojom"), expected)
Exemple #4
0
    def testTrivialValidSource(self):
        """Tests a trivial, but valid, .mojom source."""

        source = """\
        // This is a comment.

        module my_module;
        """
        expected = ast.Mojom(ast.Module(('IDENTIFIER', 'my_module'), None),
                             ast.ImportList(), [])
        self.assertEquals(parser.Parse(source, "my_file.mojom"), expected)
Exemple #5
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)
Exemple #6
0
  def testUnionWithMapMember(self):
    """Test that map members are accepted."""
    source = """\
        module my_module;

        union MyUnion {
          map<int32, string> m;
        };
        """
    expected = ast.Mojom(
        ast.Module(('IDENTIFIER', 'my_module'), None),
        ast.ImportList(),
        [ast.Union(
          'MyUnion',
          None,
          ast.UnionBody([
            ast.UnionField('m', None, None, 'string{int32}')
            ]))])
    actual = parser.Parse(source, "my_file.mojom")
    self.assertEquals(actual, expected)
Exemple #7
0
  def testUnionWithArrayMember(self):
    """Test that array members are accepted."""
    source = """\
        module my_module;

        union MyUnion {
          array<int32> a;
        };
        """
    expected = ast.Mojom(
        ast.Module(('IDENTIFIER', 'my_module'), None),
        ast.ImportList(),
        [ast.Union(
          'MyUnion',
          None,
          ast.UnionBody([
            ast.UnionField('a', None, None, 'int32[]')
            ]))])
    actual = parser.Parse(source, "my_file.mojom")
    self.assertEquals(actual, expected)
Exemple #8
0
  def testUnionWithStructMembers(self):
    """Test that struct members are accepted."""
    source = """\
        module my_module;

        union MyUnion {
          SomeStruct s;
        };
        """
    expected = ast.Mojom(
        ast.Module(('IDENTIFIER', 'my_module'), None),
        ast.ImportList(),
        [ast.Union(
          'MyUnion',
          None,
          ast.UnionBody([
            ast.UnionField('s', None, None, 'SomeStruct')
            ]))])
    actual = parser.Parse(source, "my_file.mojom")
    self.assertEquals(actual, expected)
Exemple #9
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)
Exemple #10
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)
Exemple #11
0
  def testUnionWithOrdinals(self):
    """Test that ordinals are assigned to fields."""
    source = """\
        module my_module;

        union MyUnion {
          int32 a @10;
          double b @30;
        };
        """
    expected = ast.Mojom(
        ast.Module(('IDENTIFIER', 'my_module'), None),
        ast.ImportList(),
        [ast.Union(
          'MyUnion',
          None,
          ast.UnionBody([
            ast.UnionField('a', None, ast.Ordinal(10), 'int32'),
            ast.UnionField('b', None, ast.Ordinal(30), 'double')
            ]))])
    actual = parser.Parse(source, "my_file.mojom")
    self.assertEquals(actual, expected)
Exemple #12
0
  def testSimpleUnion(self):
    """Tests a simple .mojom source that just defines a union."""
    source = """\
        module my_module;

        union MyUnion {
          int32 a;
          double b;
        };
        """
    expected = ast.Mojom(
        ast.Module(('IDENTIFIER', 'my_module'), None),
        ast.ImportList(),
        [ast.Union(
          'MyUnion',
          None,
          ast.UnionBody([
            ast.UnionField('a', None, None, 'int32'),
            ast.UnionField('b', None, None, 'double')
            ]))])
    actual = parser.Parse(source, "my_file.mojom")
    self.assertEquals(actual, expected)
Exemple #13
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)
Exemple #14
0
 def p_module(self, p):
   """module : attribute_section MODULE identifier_wrapped SEMI"""
   p[0] = ast.Module(p[3], p[1], filename=self.filename, lineno=p.lineno(2))
Exemple #15
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)