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 _MapMethod(tree): | 70 def _EnumToDict(enum): |
71 assert isinstance(tree[2], ast.ParameterList) | |
72 assert tree[3] is None or isinstance(tree[3], ast.Ordinal) | |
73 assert tree[4] is None or isinstance(tree[2], ast.ParameterList) | |
74 | |
75 def ParameterToDict(param): | |
76 assert isinstance(param, ast.Parameter) | |
77 return {'name': param.name, | |
78 'kind': _MapKind(param.typename), | |
79 'ordinal': param.ordinal.value if param.ordinal else None} | |
80 | |
81 method = {'name': tree[1], | |
82 'parameters': map(ParameterToDict, tree[2]), | |
83 'ordinal': tree[3].value if tree[3] else None} | |
84 if tree[4]: | |
85 method['response_parameters'] = map(ParameterToDict, tree[4]) | |
86 return method | |
87 | |
88 def _MapInterface(tree): | |
89 interface = {} | |
90 interface['name'] = tree[1] | |
91 interface['attributes'] = _AttributeListToDict(tree[2]) | |
92 interface['client'] = interface['attributes'].get('Client') | |
93 interface['methods'] = _MapTreeForName(_MapMethod, tree[3], 'METHOD') | |
94 interface['enums'] = _MapTreeForType(_MapEnum, tree[3], ast.Enum) | |
95 interface['constants'] = _MapTreeForType(_MapConstant, tree[3], ast.Const) | |
96 return interface | |
97 | |
98 def _MapEnum(enum): | |
99 def EnumValueToDict(enum_value): | 71 def EnumValueToDict(enum_value): |
100 assert isinstance(enum_value, ast.EnumValue) | 72 assert isinstance(enum_value, ast.EnumValue) |
101 return {'name': enum_value.name, | 73 return {'name': enum_value.name, |
102 'value': enum_value.value} | 74 'value': enum_value.value} |
103 | 75 |
104 assert isinstance(enum, ast.Enum) | 76 assert isinstance(enum, ast.Enum) |
105 return {'name': enum.name, | 77 return {'name': enum.name, |
106 'fields': map(EnumValueToDict, enum.enum_value_list)} | 78 'fields': map(EnumValueToDict, enum.enum_value_list)} |
107 | 79 |
108 def _MapConstant(const): | 80 def _ConstToDict(const): |
109 assert isinstance(const, ast.Const) | 81 assert isinstance(const, ast.Const) |
110 return {'name': const.name, | 82 return {'name': const.name, |
111 'kind': _MapKind(const.typename), | 83 'kind': _MapKind(const.typename), |
112 'value': const.value} | 84 'value': const.value} |
113 | 85 |
114 | 86 |
115 class _MojomBuilder(object): | 87 class _MojomBuilder(object): |
116 def __init__(self): | 88 def __init__(self): |
117 self.mojom = {} | 89 self.mojom = {} |
118 | 90 |
119 def Build(self, tree, name): | 91 def Build(self, tree, name): |
120 def StructToDict(struct): | 92 def StructToDict(struct): |
121 def StructFieldToDict(struct_field): | 93 def StructFieldToDict(struct_field): |
122 assert isinstance(struct_field, ast.StructField) | 94 assert isinstance(struct_field, ast.StructField) |
123 return {'name': struct_field.name, | 95 return {'name': struct_field.name, |
124 'kind': _MapKind(struct_field.typename), | 96 'kind': _MapKind(struct_field.typename), |
125 'ordinal': struct_field.ordinal.value \ | 97 'ordinal': struct_field.ordinal.value \ |
126 if struct_field.ordinal else None, | 98 if struct_field.ordinal else None, |
127 'default': struct_field.default_value} | 99 'default': struct_field.default_value} |
128 | 100 |
129 assert isinstance(struct, ast.Struct) | 101 assert isinstance(struct, ast.Struct) |
130 return {'name': struct.name, | 102 return {'name': struct.name, |
131 'attributes': _AttributeListToDict(struct.attribute_list), | 103 'attributes': _AttributeListToDict(struct.attribute_list), |
132 'fields': _MapTreeForType(StructFieldToDict, struct.body, | 104 'fields': _MapTreeForType(StructFieldToDict, struct.body, |
133 ast.StructField), | 105 ast.StructField), |
134 'enums': _MapTreeForType(_MapEnum, struct.body, ast.Enum), | 106 'enums': _MapTreeForType(_EnumToDict, struct.body, ast.Enum), |
135 'constants': _MapTreeForType(_MapConstant, struct.body, | 107 'constants': _MapTreeForType(_ConstToDict, struct.body, |
| 108 ast.Const)} |
| 109 |
| 110 def InterfaceToDict(interface): |
| 111 def MethodToDict(method): |
| 112 def ParameterToDict(param): |
| 113 assert isinstance(param, ast.Parameter) |
| 114 return {'name': param.name, |
| 115 'kind': _MapKind(param.typename), |
| 116 'ordinal': param.ordinal.value if param.ordinal else None} |
| 117 |
| 118 assert isinstance(method, ast.Method) |
| 119 rv = {'name': method.name, |
| 120 'parameters': map(ParameterToDict, method.parameter_list), |
| 121 'ordinal': method.ordinal.value if method.ordinal else None} |
| 122 if method.response_parameter_list is not None: |
| 123 rv['response_parameters'] = map(ParameterToDict, |
| 124 method.response_parameter_list) |
| 125 return rv |
| 126 |
| 127 assert isinstance(interface, ast.Interface) |
| 128 attributes = _AttributeListToDict(interface.attribute_list) |
| 129 return {'name': interface.name, |
| 130 'attributes': attributes, |
| 131 'client': attributes.get('Client'), |
| 132 'methods': _MapTreeForType(MethodToDict, interface.body, |
| 133 ast.Method), |
| 134 'enums': _MapTreeForType(_EnumToDict, interface.body, ast.Enum), |
| 135 'constants': _MapTreeForType(_ConstToDict, interface.body, |
136 ast.Const)} | 136 ast.Const)} |
137 | 137 |
138 assert isinstance(tree, ast.Mojom) | 138 assert isinstance(tree, ast.Mojom) |
139 self.mojom['name'] = name | 139 self.mojom['name'] = name |
140 self.mojom['namespace'] = tree.module.name[1] if tree.module else '' | 140 self.mojom['namespace'] = tree.module.name[1] if tree.module else '' |
141 self.mojom['imports'] = \ | 141 self.mojom['imports'] = \ |
142 [{'filename': imp.import_filename} for imp in tree.import_list] | 142 [{'filename': imp.import_filename} for imp in tree.import_list] |
143 self.mojom['attributes'] = \ | 143 self.mojom['attributes'] = \ |
144 _AttributeListToDict(tree.module.attribute_list) if tree.module else {} | 144 _AttributeListToDict(tree.module.attribute_list) if tree.module else {} |
145 self.mojom['structs'] = \ | 145 self.mojom['structs'] = \ |
146 _MapTreeForType(StructToDict, tree.definition_list, ast.Struct) | 146 _MapTreeForType(StructToDict, tree.definition_list, ast.Struct) |
147 self.mojom['interfaces'] = \ | 147 self.mojom['interfaces'] = \ |
148 _MapTreeForName(_MapInterface, tree.definition_list, 'INTERFACE') | 148 _MapTreeForType(InterfaceToDict, tree.definition_list, ast.Interface) |
149 self.mojom['enums'] = \ | 149 self.mojom['enums'] = \ |
150 _MapTreeForType(_MapEnum, tree.definition_list, ast.Enum) | 150 _MapTreeForType(_EnumToDict, tree.definition_list, ast.Enum) |
151 self.mojom['constants'] = \ | 151 self.mojom['constants'] = \ |
152 _MapTreeForType(_MapConstant, tree.definition_list, ast.Const) | 152 _MapTreeForType(_ConstToDict, tree.definition_list, ast.Const) |
153 return self.mojom | 153 return self.mojom |
154 | 154 |
155 | 155 |
156 def Translate(tree, name): | 156 def Translate(tree, name): |
157 return _MojomBuilder().Build(tree, name) | 157 return _MojomBuilder().Build(tree, name) |
OLD | NEW |