| 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 _MapModule(tree, name): | |
| 124 mojom = {} | |
| 125 mojom['name'] = name | |
| 126 mojom['namespace'] = tree[1] | |
| 127 mojom['attributes'] = _AttributeListToDict(tree[2]) | |
| 128 mojom['structs'] = _MapTree(_MapStruct, tree[3], 'STRUCT') | |
| 129 mojom['interfaces'] = _MapTree(_MapInterface, tree[3], 'INTERFACE') | |
| 130 mojom['enums'] = _MapTree(_MapEnum, tree[3], 'ENUM') | |
| 131 mojom['constants'] = _MapTree(_MapConstant, tree[3], 'CONST') | |
| 132 return mojom | |
| 133 | |
| 134 def _MapImport(tree): | 123 def _MapImport(tree): |
| 135 import_item = {} | 124 import_item = {} |
| 136 import_item['filename'] = tree[1] | 125 import_item['filename'] = tree[1] |
| 137 return import_item | 126 return import_item |
| 138 | 127 |
| 139 | 128 |
| 140 class _MojomBuilder(object): | 129 class _MojomBuilder(object): |
| 141 def __init__(self): | 130 def __init__(self): |
| 142 self.mojom = {} | 131 self.mojom = {} |
| 143 | 132 |
| 144 def Build(self, tree, name): | 133 def Build(self, tree, name): |
| 145 modules = [_MapModule(item, name) for item in tree if item[0] == 'MODULE'] | 134 def MapModule(tree, name): |
| 135 assert tree[1] is None or tree[1][0] == 'IDENTIFIER' |
| 136 mojom = {} |
| 137 mojom['name'] = name |
| 138 mojom['namespace'] = '' if not tree[1] else tree[1][1] |
| 139 mojom['attributes'] = _AttributeListToDict(tree[2]) |
| 140 mojom['structs'] = _MapTree(_MapStruct, tree[3], 'STRUCT') |
| 141 mojom['interfaces'] = _MapTree(_MapInterface, tree[3], 'INTERFACE') |
| 142 mojom['enums'] = _MapTree(_MapEnum, tree[3], 'ENUM') |
| 143 mojom['constants'] = _MapTree(_MapConstant, tree[3], 'CONST') |
| 144 return mojom |
| 145 |
| 146 modules = [MapModule(item, name) for item in tree if item[0] == 'MODULE'] |
| 146 if len(modules) != 1: | 147 if len(modules) != 1: |
| 147 raise Exception('A mojom file must contain exactly 1 module.') | 148 raise Exception('A mojom file must contain exactly 1 module.') |
| 148 self.mojom = modules[0] | 149 self.mojom = modules[0] |
| 149 self.mojom['imports'] = _MapTree(_MapImport, tree, 'IMPORT') | 150 self.mojom['imports'] = _MapTree(_MapImport, tree, 'IMPORT') |
| 150 return self.mojom | 151 return self.mojom |
| 151 | 152 |
| 152 | 153 |
| 153 def Translate(tree, name): | 154 def Translate(tree, name): |
| 154 return _MojomBuilder().Build(tree, name) | 155 return _MojomBuilder().Build(tree, name) |
| OLD | NEW |