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 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
60 return 'x:' + kind | 60 return 'x:' + kind |
61 | 61 |
62 def _AttributeListToDict(attribute_list): | 62 def _AttributeListToDict(attribute_list): |
63 if attribute_list is None: | 63 if attribute_list is None: |
64 return {} | 64 return {} |
65 assert isinstance(attribute_list, ast.AttributeList) | 65 assert isinstance(attribute_list, ast.AttributeList) |
66 # TODO(vtl): Check for duplicate keys here. | 66 # TODO(vtl): Check for duplicate keys here. |
67 return dict([(attribute.key, attribute.value) | 67 return dict([(attribute.key, attribute.value) |
68 for attribute in attribute_list]) | 68 for attribute in attribute_list]) |
69 | 69 |
70 def _MapField(tree): | |
71 assert tree[3] is None or isinstance(tree[3], ast.Ordinal) | |
72 return {'name': tree[2], | |
73 'kind': _MapKind(tree[1]), | |
74 'ordinal': tree[3].value if tree[3] else None, | |
75 'default': tree[4]} | |
76 | |
77 def _MapMethod(tree): | 70 def _MapMethod(tree): |
78 assert isinstance(tree[2], ast.ParameterList) | 71 assert isinstance(tree[2], ast.ParameterList) |
79 assert tree[3] is None or isinstance(tree[3], ast.Ordinal) | 72 assert tree[3] is None or isinstance(tree[3], ast.Ordinal) |
80 assert tree[4] is None or isinstance(tree[2], ast.ParameterList) | 73 assert tree[4] is None or isinstance(tree[2], ast.ParameterList) |
81 | 74 |
82 def ParameterToDict(param): | 75 def ParameterToDict(param): |
83 assert isinstance(param, ast.Parameter) | 76 assert isinstance(param, ast.Parameter) |
84 return {'name': param.name, | 77 return {'name': param.name, |
85 'kind': _MapKind(param.typename), | 78 'kind': _MapKind(param.typename), |
86 'ordinal': param.ordinal.value if param.ordinal else None} | 79 'ordinal': param.ordinal.value if param.ordinal else None} |
87 | 80 |
88 method = {'name': tree[1], | 81 method = {'name': tree[1], |
89 'parameters': map(ParameterToDict, tree[2]), | 82 'parameters': map(ParameterToDict, tree[2]), |
90 'ordinal': tree[3].value if tree[3] else None} | 83 'ordinal': tree[3].value if tree[3] else None} |
91 if tree[4]: | 84 if tree[4]: |
92 method['response_parameters'] = map(ParameterToDict, tree[4]) | 85 method['response_parameters'] = map(ParameterToDict, tree[4]) |
93 return method | 86 return method |
94 | 87 |
95 def _MapStruct(tree): | 88 def _MapStruct(tree): |
| 89 def StructFieldToDict(struct_field): |
| 90 assert isinstance(struct_field, ast.StructField) |
| 91 return {'name': struct_field.name, |
| 92 'kind': _MapKind(struct_field.typename), |
| 93 'ordinal': struct_field.ordinal.value \ |
| 94 if struct_field.ordinal else None, |
| 95 'default': struct_field.default_value} |
| 96 |
96 struct = {} | 97 struct = {} |
97 struct['name'] = tree[1] | 98 struct['name'] = tree[1] |
98 struct['attributes'] = _AttributeListToDict(tree[2]) | 99 struct['attributes'] = _AttributeListToDict(tree[2]) |
99 struct['fields'] = _MapTreeForName(_MapField, tree[3], 'FIELD') | 100 struct['fields'] = _MapTreeForType(StructFieldToDict, tree[3], |
| 101 ast.StructField) |
100 struct['enums'] = _MapTreeForType(_MapEnum, tree[3], ast.Enum) | 102 struct['enums'] = _MapTreeForType(_MapEnum, tree[3], ast.Enum) |
101 struct['constants'] = _MapTreeForType(_MapConstant, tree[3], ast.Const) | 103 struct['constants'] = _MapTreeForType(_MapConstant, tree[3], ast.Const) |
102 return struct | 104 return struct |
103 | 105 |
104 def _MapInterface(tree): | 106 def _MapInterface(tree): |
105 interface = {} | 107 interface = {} |
106 interface['name'] = tree[1] | 108 interface['name'] = tree[1] |
107 interface['attributes'] = _AttributeListToDict(tree[2]) | 109 interface['attributes'] = _AttributeListToDict(tree[2]) |
108 interface['client'] = interface['attributes'].get('Client') | 110 interface['client'] = interface['attributes'].get('Client') |
109 interface['methods'] = _MapTreeForName(_MapMethod, tree[3], 'METHOD') | 111 interface['methods'] = _MapTreeForName(_MapMethod, tree[3], 'METHOD') |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
146 _MapTreeForName(_MapInterface, tree.definition_list, 'INTERFACE') | 148 _MapTreeForName(_MapInterface, tree.definition_list, 'INTERFACE') |
147 self.mojom['enums'] = \ | 149 self.mojom['enums'] = \ |
148 _MapTreeForType(_MapEnum, tree.definition_list, ast.Enum) | 150 _MapTreeForType(_MapEnum, tree.definition_list, ast.Enum) |
149 self.mojom['constants'] = \ | 151 self.mojom['constants'] = \ |
150 _MapTreeForType(_MapConstant, tree.definition_list, ast.Const) | 152 _MapTreeForType(_MapConstant, tree.definition_list, ast.Const) |
151 return self.mojom | 153 return self.mojom |
152 | 154 |
153 | 155 |
154 def Translate(tree, name): | 156 def Translate(tree, name): |
155 return _MojomBuilder().Build(tree, name) | 157 return _MojomBuilder().Build(tree, name) |
OLD | NEW |