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): ...@@ -96,7 +96,7 @@ class Parser(object):
# Generator expects a module. If one wasn't specified insert one with an # Generator expects a module. If one wasn't specified insert one with an
# empty name. # empty name.
if p[1][0] != 'MODULE': if p[1][0] != 'MODULE':
p[0] = [('MODULE', '', None, p[1])] p[0] = [('MODULE', None, None, p[1])]
else: else:
p[0] = [p[1]] p[0] = [p[1]]
...@@ -106,8 +106,8 @@ class Parser(object): ...@@ -106,8 +106,8 @@ class Parser(object):
p[0] = ('IMPORT', eval(p[2])) p[0] = ('IMPORT', eval(p[2]))
def p_module(self, p): def p_module(self, p):
"""module : attribute_section MODULE identifier LBRACE definition_list \ """module : attribute_section MODULE identifier_wrapped LBRACE \
RBRACE""" definition_list RBRACE"""
p[0] = ('MODULE', p[3], p[1], p[5]) p[0] = ('MODULE', p[3], p[1], p[5])
def p_definition_list(self, p): def p_definition_list(self, p):
...@@ -315,6 +315,8 @@ class Parser(object): ...@@ -315,6 +315,8 @@ class Parser(object):
"""identifier_wrapped : identifier""" """identifier_wrapped : identifier"""
p[0] = ('IDENTIFIER', p[1]) 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): def p_identifier(self, p):
"""identifier : NAME """identifier : NAME
| NAME DOT identifier""" | NAME DOT identifier"""
......
...@@ -120,17 +120,6 @@ def _MapConstant(tree): ...@@ -120,17 +120,6 @@ def _MapConstant(tree):
constant['value'] = tree[3] constant['value'] = tree[3]
return constant 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): def _MapImport(tree):
import_item = {} import_item = {}
import_item['filename'] = tree[1] import_item['filename'] = tree[1]
...@@ -142,7 +131,19 @@ class _MojomBuilder(object): ...@@ -142,7 +131,19 @@ class _MojomBuilder(object):
self.mojom = {} self.mojom = {}
def Build(self, tree, name): 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: if len(modules) != 1:
raise Exception('A mojom file must contain exactly 1 module.') raise Exception('A mojom file must contain exactly 1 module.')
self.mojom = modules[0] self.mojom = modules[0]
......
...@@ -41,15 +41,15 @@ class ParserTest(unittest.TestCase): ...@@ -41,15 +41,15 @@ class ParserTest(unittest.TestCase):
module my_module { module my_module {
} }
""" """
self.assertEquals(parser.Parse(source, "my_file.mojom"), expected = [('MODULE', ('IDENTIFIER', 'my_module'), None, None)]
[("MODULE", "my_module", None, None)]) self.assertEquals(parser.Parse(source, "my_file.mojom"), expected)
def testSourceWithCrLfs(self): def testSourceWithCrLfs(self):
"""Tests a .mojom source with CR-LFs instead of LFs.""" """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" source = "// This is a comment.\r\n\r\nmodule my_module {\r\n}\r\n"
self.assertEquals(parser.Parse(source, "my_file.mojom"), expected = [('MODULE', ('IDENTIFIER', 'my_module'), None, None)]
[("MODULE", "my_module", None, None)]) self.assertEquals(parser.Parse(source, "my_file.mojom"), expected)
def testUnexpectedEOF(self): def testUnexpectedEOF(self):
"""Tests a "truncated" .mojom source.""" """Tests a "truncated" .mojom source."""
...@@ -141,7 +141,7 @@ class ParserTest(unittest.TestCase): ...@@ -141,7 +141,7 @@ class ParserTest(unittest.TestCase):
""" """
expected = \ expected = \
[('MODULE', [('MODULE',
'my_module', ('IDENTIFIER', 'my_module'),
None, None,
[('STRUCT', [('STRUCT',
'MyStruct', 'MyStruct',
...@@ -161,7 +161,7 @@ class ParserTest(unittest.TestCase): ...@@ -161,7 +161,7 @@ class ParserTest(unittest.TestCase):
""" """
expected = \ expected = \
[('MODULE', [('MODULE',
'', None,
None, None,
[('STRUCT', [('STRUCT',
'MyStruct', 'MyStruct',
...@@ -219,7 +219,7 @@ class ParserTest(unittest.TestCase): ...@@ -219,7 +219,7 @@ class ParserTest(unittest.TestCase):
""" """
expected = \ expected = \
[('MODULE', [('MODULE',
'my_module', ('IDENTIFIER', 'my_module'),
None, None,
[('ENUM', [('ENUM',
'MyEnum1', 'MyEnum1',
...@@ -281,7 +281,7 @@ class ParserTest(unittest.TestCase): ...@@ -281,7 +281,7 @@ class ParserTest(unittest.TestCase):
""" """
expected = \ expected = \
[('MODULE', [('MODULE',
'my_module', ('IDENTIFIER', 'my_module'),
None, None,
[('STRUCT', [('STRUCT',
'MyStruct', None, 'MyStruct', None,
...@@ -330,7 +330,7 @@ class ParserTest(unittest.TestCase): ...@@ -330,7 +330,7 @@ class ParserTest(unittest.TestCase):
""" """
expected = \ expected = \
[('MODULE', [('MODULE',
'my_module', ('IDENTIFIER', 'my_module'),
None, None,
[('STRUCT', [('STRUCT',
'MyStruct', 'MyStruct',
...@@ -427,7 +427,7 @@ class ParserTest(unittest.TestCase): ...@@ -427,7 +427,7 @@ class ParserTest(unittest.TestCase):
""" """
expected = \ expected = \
[('MODULE', [('MODULE',
'my.mod', ('IDENTIFIER', 'my.mod'),
None, None,
[('STRUCT', [('STRUCT',
'MyStruct', 'MyStruct',
...@@ -451,7 +451,7 @@ class ParserTest(unittest.TestCase): ...@@ -451,7 +451,7 @@ class ParserTest(unittest.TestCase):
""" """
expected = \ expected = \
[('MODULE', [('MODULE',
'', None,
None, None,
[('STRUCT', [('STRUCT',
'MyStruct', 'MyStruct',
...@@ -513,7 +513,7 @@ class ParserTest(unittest.TestCase): ...@@ -513,7 +513,7 @@ class ParserTest(unittest.TestCase):
""" """
expected = \ expected = \
[('MODULE', [('MODULE',
'', None,
None, None,
[('STRUCT', [('STRUCT',
'MyStruct', 'MyStruct',
...@@ -555,7 +555,7 @@ class ParserTest(unittest.TestCase): ...@@ -555,7 +555,7 @@ class ParserTest(unittest.TestCase):
""" """
expected = \ expected = \
[('MODULE', [('MODULE',
'', None,
None, None,
[('STRUCT', [('STRUCT',
'MyStruct', 'MyStruct',
...@@ -573,7 +573,7 @@ class ParserTest(unittest.TestCase): ...@@ -573,7 +573,7 @@ class ParserTest(unittest.TestCase):
source = "struct MyStruct { int32[][] nested_array; };" source = "struct MyStruct { int32[][] nested_array; };"
expected = \ expected = \
[('MODULE', [('MODULE',
'', None,
None, None,
[('STRUCT', [('STRUCT',
'MyStruct', 'MyStruct',
...@@ -624,7 +624,7 @@ class ParserTest(unittest.TestCase): ...@@ -624,7 +624,7 @@ class ParserTest(unittest.TestCase):
source1 = "interface MyInterface { MyMethod(int32 a); };" source1 = "interface MyInterface { MyMethod(int32 a); };"
expected1 = \ expected1 = \
[('MODULE', [('MODULE',
'', None,
None, None,
[('INTERFACE', [('INTERFACE',
'MyInterface', 'MyInterface',
...@@ -644,7 +644,7 @@ class ParserTest(unittest.TestCase): ...@@ -644,7 +644,7 @@ class ParserTest(unittest.TestCase):
""" """
expected2 = \ expected2 = \
[('MODULE', [('MODULE',
'', None,
None, None,
[('INTERFACE', [('INTERFACE',
'MyInterface', 'MyInterface',
...@@ -669,7 +669,7 @@ class ParserTest(unittest.TestCase): ...@@ -669,7 +669,7 @@ class ParserTest(unittest.TestCase):
""" """
expected3 = \ expected3 = \
[('MODULE', [('MODULE',
'', None,
None, None,
[('INTERFACE', [('INTERFACE',
'MyInterface', 'MyInterface',
...@@ -720,7 +720,7 @@ class ParserTest(unittest.TestCase): ...@@ -720,7 +720,7 @@ class ParserTest(unittest.TestCase):
source1 = "[] struct MyStruct {};" source1 = "[] struct MyStruct {};"
expected1 = \ expected1 = \
[('MODULE', [('MODULE',
'', None,
None, None,
[('STRUCT', [('STRUCT',
'MyStruct', 'MyStruct',
...@@ -732,7 +732,7 @@ class ParserTest(unittest.TestCase): ...@@ -732,7 +732,7 @@ class ParserTest(unittest.TestCase):
source2 = "[MyAttribute=MyName] struct MyStruct {};" source2 = "[MyAttribute=MyName] struct MyStruct {};"
expected2 = \ expected2 = \
[('MODULE', [('MODULE',
'', None,
None, None,
[('STRUCT', [('STRUCT',
'MyStruct', 'MyStruct',
...@@ -744,7 +744,7 @@ class ParserTest(unittest.TestCase): ...@@ -744,7 +744,7 @@ class ParserTest(unittest.TestCase):
source3 = "[MyAttribute1 = \"hello\", MyAttribute2 = 5] struct MyStruct {};" source3 = "[MyAttribute1 = \"hello\", MyAttribute2 = 5] struct MyStruct {};"
expected3 = \ expected3 = \
[('MODULE', [('MODULE',
'', None,
None, None,
[('STRUCT', [('STRUCT',
'MyStruct', '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