OLD | NEW |
1 # Copyright 2014 The Chromium Authors. All rights reserved. | 1 # Copyright 2014 The Chromium Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 """Translates parse tree to Mojom IR.""" | 5 """Translates parse tree to Mojom IR.""" |
6 | 6 |
7 | 7 |
8 import ast | 8 import ast |
9 import re | 9 import re |
10 | 10 |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
113 enum['fields'] = _MapTree(MapEnumField, tree[2], 'ENUM_VALUE') | 113 enum['fields'] = _MapTree(MapEnumField, tree[2], 'ENUM_VALUE') |
114 return enum | 114 return enum |
115 | 115 |
116 def _MapConstant(tree): | 116 def _MapConstant(tree): |
117 constant = {} | 117 constant = {} |
118 constant['name'] = tree[2] | 118 constant['name'] = tree[2] |
119 constant['kind'] = _MapKind(tree[1]) | 119 constant['kind'] = _MapKind(tree[1]) |
120 constant['value'] = tree[3] | 120 constant['value'] = tree[3] |
121 return constant | 121 return constant |
122 | 122 |
123 def _MapImport(tree): | |
124 import_item = {} | |
125 import_item['filename'] = tree[1] | |
126 return import_item | |
127 | |
128 | 123 |
129 class _MojomBuilder(object): | 124 class _MojomBuilder(object): |
130 def __init__(self): | 125 def __init__(self): |
131 self.mojom = {} | 126 self.mojom = {} |
132 | 127 |
133 def Build(self, tree, name): | 128 def Build(self, tree, name): |
134 def MapModule(tree, name): | 129 assert isinstance(tree, ast.Mojom) |
135 assert tree[1] is None or tree[1][0] == 'IDENTIFIER' | 130 self.mojom['name'] = name |
136 mojom = {} | 131 self.mojom['namespace'] = tree.module.name[1] if tree.module else '' |
137 mojom['name'] = name | 132 self.mojom['imports'] = \ |
138 mojom['namespace'] = '' if not tree[1] else tree[1][1] | 133 [{'filename': imp.import_filename} for imp in tree.import_list] |
139 mojom['attributes'] = _AttributeListToDict(tree[2]) | 134 self.mojom['attributes'] = \ |
140 mojom['structs'] = _MapTree(_MapStruct, tree[3], 'STRUCT') | 135 _AttributeListToDict(tree.module.attribute_list) if tree.module else {} |
141 mojom['interfaces'] = _MapTree(_MapInterface, tree[3], 'INTERFACE') | 136 self.mojom['structs'] = _MapTree(_MapStruct, tree.definition_list, 'STRUCT') |
142 mojom['enums'] = _MapTree(_MapEnum, tree[3], 'ENUM') | 137 self.mojom['interfaces'] = \ |
143 mojom['constants'] = _MapTree(_MapConstant, tree[3], 'CONST') | 138 _MapTree(_MapInterface, tree.definition_list, 'INTERFACE') |
144 return mojom | 139 self.mojom['enums'] = _MapTree(_MapEnum, tree.definition_list, 'ENUM') |
145 | 140 self.mojom['constants'] = \ |
146 modules = [MapModule(item, name) for item in tree if item[0] == 'MODULE'] | 141 _MapTree(_MapConstant, tree.definition_list, 'CONST') |
147 if len(modules) != 1: | |
148 raise Exception('A mojom file must contain exactly 1 module.') | |
149 self.mojom = modules[0] | |
150 self.mojom['imports'] = _MapTree(_MapImport, tree, 'IMPORT') | |
151 return self.mojom | 142 return self.mojom |
152 | 143 |
153 | 144 |
154 def Translate(tree, name): | 145 def Translate(tree, name): |
155 return _MojomBuilder().Build(tree, name) | 146 return _MojomBuilder().Build(tree, name) |
OLD | NEW |