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 = \
    [('MODULE',
        '',
        None,
        [('STRUCT',
        'MyStruct',
        None,
        [('FIELD', 'handle', 'a', ast.Ordinal(None), None),
         ('FIELD', 'handle<data_pipe_consumer>', 'b', ast.Ordinal(None), None),
         ('FIELD', 'handle<data_pipe_producer>', 'c', ast.Ordinal(None), None),
         ('FIELD', 'handle<message_pipe>', 'd', ast.Ordinal(None), None),
         ('FIELD', 'handle<shared_buffer>', 'e', ast.Ordinal(None), None)])])]
        self.assertEquals(parser.Parse(source, "my_file.mojom"), expected)
Exemple #2
0
    def testValidMethod(self):
        """Tests parsing method declarations."""

        source1 = "interface MyInterface { MyMethod(int32 a); };"
        expected1 = ast.Mojom(None, ast.ImportList(), [
            ast.Interface(
                'MyInterface', None,
                ast.InterfaceBody(
                    ast.Method(
                        'MyMethod', None,
                        ast.ParameterList(ast.Parameter('a', None, 'int32')),
                        None)))
        ])
        self.assertEquals(parser.Parse(source1, "my_file.mojom"), expected1)

        source2 = """\
        interface MyInterface {
          MyMethod1@0(int32 a@0, int64 b@1);
          MyMethod2@1() => ();
        };
        """
        expected2 = ast.Mojom(None, ast.ImportList(), [
            ast.Interface(
                'MyInterface', None,
                ast.InterfaceBody([
                    ast.Method(
                        'MyMethod1', ast.Ordinal(0),
                        ast.ParameterList([
                            ast.Parameter('a', ast.Ordinal(0), 'int32'),
                            ast.Parameter('b', ast.Ordinal(1), 'int64')
                        ]), None),
                    ast.Method('MyMethod2', ast.Ordinal(1),
                               ast.ParameterList(), ast.ParameterList())
                ]))
        ])
        self.assertEquals(parser.Parse(source2, "my_file.mojom"), expected2)

        source3 = """\
        interface MyInterface {
          MyMethod(string a) => (int32 a, bool b);
        };
        """
        expected3 = ast.Mojom(None, ast.ImportList(), [
            ast.Interface(
                'MyInterface', None,
                ast.InterfaceBody(
                    ast.Method(
                        'MyMethod', None,
                        ast.ParameterList(ast.Parameter('a', None, 'string')),
                        ast.ParameterList([
                            ast.Parameter('a', None, 'int32'),
                            ast.Parameter('b', None, 'bool')
                        ]))))
        ])
        self.assertEquals(parser.Parse(source3, "my_file.mojom"), expected3)
Exemple #3
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 = \
    [('MODULE',
        'my_module',
        [('STRUCT',
        'MyStruct',
        None,
        [('FIELD', 'int32', 'a0', ast.Ordinal(0), None),
         ('FIELD', 'int32', 'a1', ast.Ordinal(1), None),
         ('FIELD', 'int32', 'a2', ast.Ordinal(2), None),
         ('FIELD', 'int32', 'a9', ast.Ordinal(9), None),
         ('FIELD', 'int32', 'a10', ast.Ordinal(10), None),
         ('FIELD', 'int32', 'a11', ast.Ordinal(11), None),
         ('FIELD', 'int32', 'a29', ast.Ordinal(29), None),
         ('FIELD', 'int32', 'a1234567890', ast.Ordinal(1234567890), None)])])]
        self.assertEquals(parser.Parse(source, "my_file.mojom"), expected)
Exemple #4
0
    def testSimpleStructWithoutModule(self):
        """Tests a simple struct without an enclosing module."""
        source = """\
struct MyStruct {
  int32 a;
  double b;
};
"""
        expected = \
    [('MODULE',
        '',
        [('STRUCT',
        'MyStruct',
        None,
        [('FIELD', 'int32', 'a', ast.Ordinal(None), None),
         ('FIELD', 'double', 'b', ast.Ordinal(None), None)])])]
        self.assertEquals(parser.Parse(source, "my_file.mojom"), expected)
Exemple #5
0
 def p_ordinal_2(self, p):
     """ordinal : ORDINAL"""
     value = int(p[1][1:])
     if value > _MAX_ORDINAL_VALUE:
         raise ParseError(self.filename,
                          "Ordinal value %d too large:" % value,
                          lineno=p.lineno(1),
                          snippet=self._GetSnippet(p.lineno(1)))
     p[0] = ast.Ordinal(value, filename=self.filename, lineno=p.lineno(1))
Exemple #6
0
    def testSimpleStruct(self):
        """Tests a simple .mojom source that just defines a struct."""
        source = """\
module my_module {

struct MyStruct {
  int32 a;
  double b;
};

}  // module my_module
"""
        expected = \
    [('MODULE',
        'my_module',
        [('STRUCT',
        'MyStruct',
        None,
        [('FIELD', 'int32', 'a', ast.Ordinal(None), None),
         ('FIELD', 'double', 'b', ast.Ordinal(None), None)])])]
        self.assertEquals(parser.Parse(source, "my_file.mojom"), expected)
Exemple #7
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 #8
0
    def testNestedNamespace(self):
        """Tests nested namespaces work."""
        source = """\
module my.mod {

struct MyStruct {
  int32 a;
};

}  // module my.mod
"""
        expected = \
    [('MODULE',
        'my.mod',
        [('STRUCT',
        'MyStruct',
        None,
        [('FIELD', 'int32', 'a', ast.Ordinal(None), None)])])]
        self.assertEquals(parser.Parse(source, "my_file.mojom"), 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 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)
    def testConst(self):
        """Tests some constants and struct memebers initialized with them."""
        source = """\
module my_module {

struct MyStruct {
  const int8 kNumber = -1;
  int8 number@0 = kNumber;
};

}  // my_module
"""
        expected = \
    [('MODULE',
        'my_module',
        None,
        [('STRUCT',
        'MyStruct', None,
        [('CONST', 'int8', 'kNumber', '-1'),
         ('FIELD', 'int8', 'number',
            ast.Ordinal(0), ('IDENTIFIER', 'kNumber'))])])]
        self.assertEquals(parser.Parse(source, "my_file.mojom"), expected)
    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 = \
    [('MODULE',
        '',
        None,
        [('STRUCT',
        'MyStruct',
        None,
        [('FIELD', 'int16', 'a0', ast.Ordinal(None), '0'),
         ('FIELD', 'uint16', 'a1', ast.Ordinal(None), '0x0'),
         ('FIELD', 'uint16', 'a2', ast.Ordinal(None), '0x00'),
         ('FIELD', 'uint16', 'a3', ast.Ordinal(None), '0x01'),
         ('FIELD', 'uint16', 'a4', ast.Ordinal(None), '0xcd'),
         ('FIELD', 'int32', 'a5' , ast.Ordinal(None), '12345'),
         ('FIELD', 'int64', 'a6', ast.Ordinal(None), '-12345'),
         ('FIELD', 'int64', 'a7', ast.Ordinal(None), '+12345'),
         ('FIELD', 'uint32', 'a8', ast.Ordinal(None), '0x12cd3'),
         ('FIELD', 'uint32', 'a9', ast.Ordinal(None), '-0x12cD3'),
         ('FIELD', 'uint32', 'a10', ast.Ordinal(None), '+0x12CD3'),
         ('FIELD', 'bool', 'a11', ast.Ordinal(None), 'true'),
         ('FIELD', 'bool', 'a12', ast.Ordinal(None), 'false'),
         ('FIELD', 'float', 'a13', ast.Ordinal(None), '1.2345'),
         ('FIELD', 'float', 'a14', ast.Ordinal(None), '-1.2345'),
         ('FIELD', 'float', 'a15', ast.Ordinal(None), '+1.2345'),
         ('FIELD', 'float', 'a16', ast.Ordinal(None), '123.'),
         ('FIELD', 'float', 'a17', ast.Ordinal(None), '.123'),
         ('FIELD', 'double', 'a18', ast.Ordinal(None), '1.23E10'),
         ('FIELD', 'double', 'a19', ast.Ordinal(None), '1.E-10'),
         ('FIELD', 'double', 'a20', ast.Ordinal(None), '.5E+10'),
         ('FIELD', 'double', 'a21', ast.Ordinal(None), '-1.23E10'),
         ('FIELD', 'double', 'a22', ast.Ordinal(None), '+.123E10')])])]
        self.assertEquals(parser.Parse(source, "my_file.mojom"), expected)