Mojo: Mojom: Add AST types for imports and import lists.

R=davemoore@chromium.org

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

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@282184 0039d316-1c4b-4281-b951-d872f2087c98
parent 84763789
...@@ -88,6 +88,24 @@ class AttributeList(NodeListBase): ...@@ -88,6 +88,24 @@ class AttributeList(NodeListBase):
_list_item_type = Attribute _list_item_type = Attribute
class Import(NodeBase):
"""Represents an import statement."""
def __init__(self, import_filename, **kwargs):
assert isinstance(import_filename, str)
NodeBase.__init__(self, **kwargs)
self.import_filename = import_filename
def __eq__(self, other):
return self.import_filename == other.import_filename
class ImportList(NodeListBase):
"""Represents a list (i.e., sequence) of import statements."""
_list_item_type = Import
class Module(NodeBase): class Module(NodeBase):
"""Represents a module statement.""" """Represents a module statement."""
......
...@@ -87,26 +87,34 @@ class Parser(object): ...@@ -87,26 +87,34 @@ class Parser(object):
# See http://www.dabeaz.com/ply/ply.html#ply_nn25 for more details. # See http://www.dabeaz.com/ply/ply.html#ply_nn25 for more details.
# TODO(vtl): Get rid of this ('MODULE', ...) stuff and replace it with an # TODO(vtl): Get rid of this ('MODULE', ...) stuff and replace it with an
# ast.Mojom node. This will require putting the imports into a list (say # ast.Mojom node. (The ('IMPORT', ...) stuff is a hack until I do this and
# ast.ImportList). # update translate.py.)
# TODO(vtl): Get rid of the braces in the module "statement". (Consider # TODO(vtl): Get rid of the braces in the module "statement". (Consider
# renaming "module" -> "package".) # renaming "module" -> "package".)
def p_root(self, p): def p_root(self, p):
"""root : import root """root : import_list module LBRACE definition_list RBRACE
| module LBRACE definition_list RBRACE | import_list definition_list"""
| definition_list""" p[0] = [('IMPORT', import_statement.import_filename) \
if len(p) == 3: for import_statement in p[1]]
p[0] = p[2] if len(p) == 6:
p[0].insert(0, p[1]) p[0].append(('MODULE', p[2].name, p[2].attribute_list, p[4]))
elif len(p) == 5:
p[0] = [('MODULE', p[1].name, p[1].attribute_list, p[3])]
else: else:
p[0] = [('MODULE', None, None, p[1])] p[0].append(('MODULE', None, None, p[2]))
def p_import_list_1(self, p):
"""import_list : """
p[0] = ast.ImportList()
def p_import_list_2(self, p):
"""import_list : import_list import"""
p[0] = p[1]
p[0].Append(p[2])
def p_import(self, p): def p_import(self, p):
"""import : IMPORT STRING_LITERAL""" """import : IMPORT STRING_LITERAL"""
# 'eval' the literal to strip the quotes. # 'eval' the literal to strip the quotes.
p[0] = ('IMPORT', eval(p[2])) # TODO(vtl): This eval is dubious. We should unquote/unescape ourselves.
p[0] = ast.Import(eval(p[2]))
def p_module(self, p): def p_module(self, p):
"""module : attribute_section MODULE identifier_wrapped """ """module : attribute_section MODULE identifier_wrapped """
......
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