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 | 9 |
10 | 10 |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
76 def _MapEnumField(tree): | 76 def _MapEnumField(tree): |
77 return {'name': tree[1], | 77 return {'name': tree[1], |
78 'value': tree[2]} | 78 'value': tree[2]} |
79 | 79 |
80 def _MapStruct(tree): | 80 def _MapStruct(tree): |
81 struct = {} | 81 struct = {} |
82 struct['name'] = tree[1] | 82 struct['name'] = tree[1] |
83 struct['attributes'] = _MapAttributes(tree[2]) | 83 struct['attributes'] = _MapAttributes(tree[2]) |
84 struct['fields'] = _MapTree(_MapField, tree[3], 'FIELD') | 84 struct['fields'] = _MapTree(_MapField, tree[3], 'FIELD') |
85 struct['enums'] = _MapTree(_MapEnum, tree[3], 'ENUM') | 85 struct['enums'] = _MapTree(_MapEnum, tree[3], 'ENUM') |
| 86 struct['constants'] = _MapTree(_MapConstant, tree[3], 'CONST') |
86 return struct | 87 return struct |
87 | 88 |
88 def _MapInterface(tree): | 89 def _MapInterface(tree): |
89 interface = {} | 90 interface = {} |
90 interface['name'] = tree[1] | 91 interface['name'] = tree[1] |
91 interface['peer'] = _GetAttribute(tree[2], 'Peer') | 92 interface['peer'] = _GetAttribute(tree[2], 'Peer') |
92 interface['methods'] = _MapTree(_MapMethod, tree[3], 'METHOD') | 93 interface['methods'] = _MapTree(_MapMethod, tree[3], 'METHOD') |
93 interface['enums'] = _MapTree(_MapEnum, tree[3], 'ENUM') | 94 interface['enums'] = _MapTree(_MapEnum, tree[3], 'ENUM') |
| 95 interface['constants'] = _MapTree(_MapConstant, tree[3], 'CONST') |
94 return interface | 96 return interface |
95 | 97 |
96 def _MapEnum(tree): | 98 def _MapEnum(tree): |
97 enum = {} | 99 enum = {} |
98 enum['name'] = tree[1] | 100 enum['name'] = tree[1] |
99 enum['fields'] = _MapTree(_MapEnumField, tree[2], 'ENUM_FIELD') | 101 enum['fields'] = _MapTree(_MapEnumField, tree[2], 'ENUM_FIELD') |
100 return enum | 102 return enum |
101 | 103 |
| 104 def _MapConstant(tree): |
| 105 constant = {} |
| 106 constant['name'] = tree[2] |
| 107 constant['kind'] = _MapKind(tree[1]) |
| 108 constant['value'] = tree[3] |
| 109 return constant |
| 110 |
102 def _MapModule(tree, name): | 111 def _MapModule(tree, name): |
103 mojom = {} | 112 mojom = {} |
104 mojom['name'] = name | 113 mojom['name'] = name |
105 mojom['namespace'] = tree[1] | 114 mojom['namespace'] = tree[1] |
106 mojom['attributes'] = _MapAttributes(tree[2]) | 115 mojom['attributes'] = _MapAttributes(tree[2]) |
107 mojom['structs'] = _MapTree(_MapStruct, tree[3], 'STRUCT') | 116 mojom['structs'] = _MapTree(_MapStruct, tree[3], 'STRUCT') |
108 mojom['interfaces'] = _MapTree(_MapInterface, tree[3], 'INTERFACE') | 117 mojom['interfaces'] = _MapTree(_MapInterface, tree[3], 'INTERFACE') |
109 mojom['enums'] = _MapTree(_MapEnum, tree[3], 'ENUM') | 118 mojom['enums'] = _MapTree(_MapEnum, tree[3], 'ENUM') |
| 119 mojom['constants'] = _MapTree(_MapConstant, tree[3], 'CONST') |
110 return mojom | 120 return mojom |
111 | 121 |
112 def _MapImport(tree): | 122 def _MapImport(tree): |
113 import_item = {} | 123 import_item = {} |
114 import_item['filename'] = tree[1] | 124 import_item['filename'] = tree[1] |
115 return import_item | 125 return import_item |
116 | 126 |
117 | 127 |
118 class _MojomBuilder(object): | 128 class _MojomBuilder(object): |
119 def __init__(self): | 129 def __init__(self): |
120 self.mojom = {} | 130 self.mojom = {} |
121 | 131 |
122 def Build(self, tree, name): | 132 def Build(self, tree, name): |
123 modules = [_MapModule(item, name) for item in tree if item[0] == 'MODULE'] | 133 modules = [_MapModule(item, name) for item in tree if item[0] == 'MODULE'] |
124 if len(modules) != 1: | 134 if len(modules) != 1: |
125 raise Exception('A mojom file must contain exactly 1 module.') | 135 raise Exception('A mojom file must contain exactly 1 module.') |
126 self.mojom = modules[0] | 136 self.mojom = modules[0] |
127 self.mojom['imports'] = _MapTree(_MapImport, tree, 'IMPORT') | 137 self.mojom['imports'] = _MapTree(_MapImport, tree, 'IMPORT') |
128 return self.mojom | 138 return self.mojom |
129 | 139 |
130 | 140 |
131 def Translate(tree, name): | 141 def Translate(tree, name): |
132 return _MojomBuilder().Build(tree, name) | 142 return _MojomBuilder().Build(tree, name) |
OLD | NEW |