Mojo: Mojom: Add an AST node type for the module "statement".

R=davemoore@chromium.org

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

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@282125 0039d316-1c4b-4281-b951-d872f2087c98
parent 78ed4ef8
...@@ -83,6 +83,22 @@ class AttributeList(NodeListBase): ...@@ -83,6 +83,22 @@ class AttributeList(NodeListBase):
_list_item_type = Attribute _list_item_type = Attribute
class Module(NodeBase):
"""Represents a module statement."""
def __init__(self, name, attribute_list, **kwargs):
# |name| is either none or a "wrapped identifier".
assert name is None or isinstance(name, tuple)
assert attribute_list is None or isinstance(attribute_list, AttributeList)
NodeBase.__init__(self, **kwargs)
self.name = name
self.attribute_list = attribute_list
def __eq__(self, other):
return self.name == other.name and \
self.attribute_list == other.attribute_list
class Ordinal(NodeBase): class Ordinal(NodeBase):
"""Represents an ordinal value labeling, e.g., a struct field.""" """Represents an ordinal value labeling, e.g., a struct field."""
......
...@@ -86,19 +86,22 @@ class Parser(object): ...@@ -86,19 +86,22 @@ 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
# ast.Mojom node. This will require putting the imports into a list (say
# ast.ImportList).
# TODO(vtl): Get rid of the braces in the module "statement". (Consider
# renaming "module" -> "package".)
def p_root(self, p): def p_root(self, p):
"""root : import root """root : import root
| module | module LBRACE definition_list RBRACE
| definition_list""" | definition_list"""
if len(p) > 2: if len(p) == 3:
p[0] = _ListFromConcat(p[1], p[2]) p[0] = p[2]
p[0].insert(0, p[1])
elif len(p) == 5:
p[0] = [('MODULE', p[1].name, p[1].attribute_list, p[3])]
else: else:
# Generator expects a module. If one wasn't specified insert one with an
# empty name.
if p[1][0] != 'MODULE':
p[0] = [('MODULE', None, None, p[1])] p[0] = [('MODULE', None, None, p[1])]
else:
p[0] = [p[1]]
def p_import(self, p): def p_import(self, p):
"""import : IMPORT STRING_LITERAL""" """import : IMPORT STRING_LITERAL"""
...@@ -106,9 +109,8 @@ class Parser(object): ...@@ -106,9 +109,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_wrapped LBRACE \ """module : attribute_section MODULE identifier_wrapped """
definition_list RBRACE""" p[0] = ast.Module(p[3], p[1], filename=self.filename, lineno=p.lineno(2))
p[0] = ('MODULE', p[3], p[1], p[5])
def p_definition_list(self, p): def p_definition_list(self, p):
"""definition_list : definition definition_list """definition_list : definition definition_list
...@@ -123,10 +125,12 @@ class Parser(object): ...@@ -123,10 +125,12 @@ class Parser(object):
| const""" | const"""
p[0] = p[1] p[0] = p[1]
def p_attribute_section(self, p): def p_attribute_section_1(self, p):
"""attribute_section : LBRACKET attribute_list RBRACKET """attribute_section : """
| """ p[0] = None
if len(p) > 3:
def p_attribute_section_2(self, p):
"""attribute_section : LBRACKET attribute_list RBRACKET"""
p[0] = p[2] p[0] = p[2]
def p_attribute_list_1(self, p): def p_attribute_list_1(self, p):
......
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