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 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
77 'kind': _MapKind(param.typename), | 77 'kind': _MapKind(param.typename), |
78 'ordinal': param.ordinal.value} | 78 'ordinal': param.ordinal.value} |
79 | 79 |
80 method = {'name': tree[1], | 80 method = {'name': tree[1], |
81 'parameters': map(ParameterToDict, tree[2]), | 81 'parameters': map(ParameterToDict, tree[2]), |
82 'ordinal': tree[3].value} | 82 'ordinal': tree[3].value} |
83 if tree[4]: | 83 if tree[4]: |
84 method['response_parameters'] = map(ParameterToDict, tree[4]) | 84 method['response_parameters'] = map(ParameterToDict, tree[4]) |
85 return method | 85 return method |
86 | 86 |
87 def _MapEnumField(tree): | |
88 return {'name': tree[1], | |
89 'value': tree[2]} | |
90 | |
91 def _MapStruct(tree): | 87 def _MapStruct(tree): |
92 struct = {} | 88 struct = {} |
93 struct['name'] = tree[1] | 89 struct['name'] = tree[1] |
94 struct['attributes'] = _AttributeListToDict(tree[2]) | 90 struct['attributes'] = _AttributeListToDict(tree[2]) |
95 struct['fields'] = _MapTree(_MapField, tree[3], 'FIELD') | 91 struct['fields'] = _MapTree(_MapField, tree[3], 'FIELD') |
96 struct['enums'] = _MapTree(_MapEnum, tree[3], 'ENUM') | 92 struct['enums'] = _MapTree(_MapEnum, tree[3], 'ENUM') |
97 struct['constants'] = _MapTree(_MapConstant, tree[3], 'CONST') | 93 struct['constants'] = _MapTree(_MapConstant, tree[3], 'CONST') |
98 return struct | 94 return struct |
99 | 95 |
100 def _MapInterface(tree): | 96 def _MapInterface(tree): |
101 interface = {} | 97 interface = {} |
102 interface['name'] = tree[1] | 98 interface['name'] = tree[1] |
103 interface['attributes'] = _AttributeListToDict(tree[2]) | 99 interface['attributes'] = _AttributeListToDict(tree[2]) |
104 interface['client'] = interface['attributes'].get('Client') | 100 interface['client'] = interface['attributes'].get('Client') |
105 interface['methods'] = _MapTree(_MapMethod, tree[3], 'METHOD') | 101 interface['methods'] = _MapTree(_MapMethod, tree[3], 'METHOD') |
106 interface['enums'] = _MapTree(_MapEnum, tree[3], 'ENUM') | 102 interface['enums'] = _MapTree(_MapEnum, tree[3], 'ENUM') |
107 interface['constants'] = _MapTree(_MapConstant, tree[3], 'CONST') | 103 interface['constants'] = _MapTree(_MapConstant, tree[3], 'CONST') |
108 return interface | 104 return interface |
109 | 105 |
110 def _MapEnum(tree): | 106 def _MapEnum(tree): |
| 107 def MapEnumField(tree): |
| 108 return {'name': tree[1], |
| 109 'value': tree[2]} |
| 110 |
111 enum = {} | 111 enum = {} |
112 enum['name'] = tree[1] | 112 enum['name'] = tree[1] |
113 enum['fields'] = _MapTree(_MapEnumField, tree[2], 'ENUM_FIELD') | 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): | 123 def _MapModule(tree, name): |
(...skipping 21 matching lines...) Expand all Loading... |
145 modules = [_MapModule(item, name) for item in tree if item[0] == 'MODULE'] | 145 modules = [_MapModule(item, name) for item in tree if item[0] == 'MODULE'] |
146 if len(modules) != 1: | 146 if len(modules) != 1: |
147 raise Exception('A mojom file must contain exactly 1 module.') | 147 raise Exception('A mojom file must contain exactly 1 module.') |
148 self.mojom = modules[0] | 148 self.mojom = modules[0] |
149 self.mojom['imports'] = _MapTree(_MapImport, tree, 'IMPORT') | 149 self.mojom['imports'] = _MapTree(_MapImport, tree, 'IMPORT') |
150 return self.mojom | 150 return self.mojom |
151 | 151 |
152 | 152 |
153 def Translate(tree, name): | 153 def Translate(tree, name): |
154 return _MojomBuilder().Build(tree, name) | 154 return _MojomBuilder().Build(tree, name) |
OLD | NEW |