Mojo: Mojom parser: Make |module| use an |identifier_wrapped|.

We should really get rid of the plain |identifier|, rename
|identifier_wrapped| to |identifier|, and make that rule produce an
ast.Identifier (say).

But that change will be more involved.

R=darin@chromium.org

Review URL: https://codereview.chromium.org/376023002

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@281963 0039d316-1c4b-4281-b951-d872f2087c98
parent 0f07bddf
......@@ -96,7 +96,7 @@ class Parser(object):
# Generator expects a module. If one wasn't specified insert one with an
# empty name.
if p[1][0] != 'MODULE':
p[0] = [('MODULE', '', None, p[1])]
p[0] = [('MODULE', None, None, p[1])]
else:
p[0] = [p[1]]
......@@ -106,8 +106,8 @@ class Parser(object):
p[0] = ('IMPORT', eval(p[2]))
def p_module(self, p):
"""module : attribute_section MODULE identifier LBRACE definition_list \
RBRACE"""
"""module : attribute_section MODULE identifier_wrapped LBRACE \
definition_list RBRACE"""
p[0] = ('MODULE', p[3], p[1], p[5])
def p_definition_list(self, p):
......@@ -315,6 +315,8 @@ class Parser(object):
"""identifier_wrapped : identifier"""
p[0] = ('IDENTIFIER', p[1])
# TODO(vtl): Make this produce a "wrapped" identifier (probably as an
# |ast.Identifier|, to be added) and get rid of identifier_wrapped.
def p_identifier(self, p):
"""identifier : NAME
| NAME DOT identifier"""
......
......@@ -120,17 +120,6 @@ def _MapConstant(tree):
constant['value'] = tree[3]
return constant
def _MapModule(tree, name):
mojom = {}
mojom['name'] = name
mojom['namespace'] = tree[1]
mojom['attributes'] = _AttributeListToDict(tree[2])
mojom['structs'] = _MapTree(_MapStruct, tree[3], 'STRUCT')
mojom['interfaces'] = _MapTree(_MapInterface, tree[3], 'INTERFACE')
mojom['enums'] = _MapTree(_MapEnum, tree[3], 'ENUM')
mojom['constants'] = _MapTree(_MapConstant, tree[3], 'CONST')
return mojom
def _MapImport(tree):
import_item = {}
import_item['filename'] = tree[1]
......@@ -142,7 +131,19 @@ class _MojomBuilder(object):
self.mojom = {}
def Build(self, tree, name):
modules = [_MapModule(item, name) for item in tree if item[0] == 'MODULE']
def MapModule(tree, name):
assert tree[1] is None or tree[1][0] == 'IDENTIFIER'
mojom = {}
mojom['name'] = name
mojom['namespace'] = '' if not tree[1] else tree[1][1]
mojom['attributes'] = _AttributeListToDict(tree[2])
mojom['structs'] = _MapTree(_MapStruct, tree[3], 'STRUCT')
mojom['interfaces'] = _MapTree(_MapInterface, tree[3], 'INTERFACE')
mojom['enums'] = _MapTree(_MapEnum, tree[3], 'ENUM')
mojom['constants'] = _MapTree(_MapConstant, tree[3], 'CONST')
return mojom
modules = [MapModule(item, name) for item in tree if item[0] == 'MODULE']
if len(modules) != 1:
raise Exception('A mojom file must contain exactly 1 module.')
self.mojom = modules[0]
......
......@@ -41,15 +41,15 @@ class ParserTest(unittest.TestCase):
module my_module {
}
"""
self.assertEquals(parser.Parse(source, "my_file.mojom"),
[("MODULE", "my_module", None, None)])
expected = [('MODULE', ('IDENTIFIER', 'my_module'), None, None)]
self.assertEquals(parser.Parse(source, "my_file.mojom"), expected)
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}\r\n"
self.assertEquals(parser.Parse(source, "my_file.mojom"),
[("MODULE", "my_module", None, None)])
expected = [('MODULE', ('IDENTIFIER', 'my_module'), None, None)]
self.assertEquals(parser.Parse(source, "my_file.mojom"), expected)
def testUnexpectedEOF(self):
"""Tests a "truncated" .mojom source."""
......@@ -141,7 +141,7 @@ class ParserTest(unittest.TestCase):
"""
expected = \
[('MODULE',
'my_module',
('IDENTIFIER', 'my_module'),
None,
[('STRUCT',
'MyStruct',
......@@ -161,7 +161,7 @@ class ParserTest(unittest.TestCase):
"""
expected = \
[('MODULE',
'',
None,
None,
[('STRUCT',
'MyStruct',
......@@ -219,7 +219,7 @@ class ParserTest(unittest.TestCase):
"""
expected = \
[('MODULE',
'my_module',
('IDENTIFIER', 'my_module'),
None,
[('ENUM',
'MyEnum1',
......@@ -281,7 +281,7 @@ class ParserTest(unittest.TestCase):
"""
expected = \
[('MODULE',
'my_module',
('IDENTIFIER', 'my_module'),
None,
[('STRUCT',
'MyStruct', None,
......@@ -330,7 +330,7 @@ class ParserTest(unittest.TestCase):
"""
expected = \
[('MODULE',
'my_module',
('IDENTIFIER', 'my_module'),
None,
[('STRUCT',
'MyStruct',
......@@ -427,7 +427,7 @@ class ParserTest(unittest.TestCase):
"""
expected = \
[('MODULE',
'my.mod',
('IDENTIFIER', 'my.mod'),
None,
[('STRUCT',
'MyStruct',
......@@ -451,7 +451,7 @@ class ParserTest(unittest.TestCase):
"""
expected = \
[('MODULE',
'',
None,
None,
[('STRUCT',
'MyStruct',
......@@ -513,7 +513,7 @@ class ParserTest(unittest.TestCase):
"""
expected = \
[('MODULE',
'',
None,
None,
[('STRUCT',
'MyStruct',
......@@ -555,7 +555,7 @@ class ParserTest(unittest.TestCase):
"""
expected = \
[('MODULE',
'',
None,
None,
[('STRUCT',
'MyStruct',
......@@ -573,7 +573,7 @@ class ParserTest(unittest.TestCase):
source = "struct MyStruct { int32[][] nested_array; };"
expected = \
[('MODULE',
'',
None,
None,
[('STRUCT',
'MyStruct',
......@@ -624,7 +624,7 @@ class ParserTest(unittest.TestCase):
source1 = "interface MyInterface { MyMethod(int32 a); };"
expected1 = \
[('MODULE',
'',
None,
None,
[('INTERFACE',
'MyInterface',
......@@ -644,7 +644,7 @@ class ParserTest(unittest.TestCase):
"""
expected2 = \
[('MODULE',
'',
None,
None,
[('INTERFACE',
'MyInterface',
......@@ -669,7 +669,7 @@ class ParserTest(unittest.TestCase):
"""
expected3 = \
[('MODULE',
'',
None,
None,
[('INTERFACE',
'MyInterface',
......@@ -720,7 +720,7 @@ class ParserTest(unittest.TestCase):
source1 = "[] struct MyStruct {};"
expected1 = \
[('MODULE',
'',
None,
None,
[('STRUCT',
'MyStruct',
......@@ -732,7 +732,7 @@ class ParserTest(unittest.TestCase):
source2 = "[MyAttribute=MyName] struct MyStruct {};"
expected2 = \
[('MODULE',
'',
None,
None,
[('STRUCT',
'MyStruct',
......@@ -744,7 +744,7 @@ class ParserTest(unittest.TestCase):
source3 = "[MyAttribute1 = \"hello\", MyAttribute2 = 5] struct MyStruct {};"
expected3 = \
[('MODULE',
'',
None,
None,
[('STRUCT',
'MyStruct',
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment