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 |
11 | 11 |
12 def _MapTreeForName(func, tree, name): | |
13 assert isinstance(name, str) | |
14 if not tree: | |
15 return [] | |
16 return [func(subtree) for subtree in tree \ | |
17 if isinstance(subtree, tuple) and subtree[0] == name] | |
18 | |
19 def _MapTreeForType(func, tree, type_to_map): | 12 def _MapTreeForType(func, tree, type_to_map): |
20 assert isinstance(type_to_map, type) | 13 assert isinstance(type_to_map, type) |
21 if not tree: | 14 if not tree: |
22 return [] | 15 return [] |
23 return [func(subtree) for subtree in tree if isinstance(subtree, type_to_map)] | 16 return [func(subtree) for subtree in tree if isinstance(subtree, type_to_map)] |
24 | 17 |
25 _FIXED_ARRAY_REGEXP = re.compile(r'\[[0-9]+\]') | 18 _FIXED_ARRAY_REGEXP = re.compile(r'\[[0-9]+\]') |
26 | 19 |
27 def _MapKind(kind): | 20 def _MapKind(kind): |
28 map_to_kind = {'bool': 'b', | 21 map_to_kind = {'bool': 'b', |
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
148 _MapTreeForType(InterfaceToDict, tree.definition_list, ast.Interface) | 141 _MapTreeForType(InterfaceToDict, tree.definition_list, ast.Interface) |
149 self.mojom['enums'] = \ | 142 self.mojom['enums'] = \ |
150 _MapTreeForType(_EnumToDict, tree.definition_list, ast.Enum) | 143 _MapTreeForType(_EnumToDict, tree.definition_list, ast.Enum) |
151 self.mojom['constants'] = \ | 144 self.mojom['constants'] = \ |
152 _MapTreeForType(_ConstToDict, tree.definition_list, ast.Const) | 145 _MapTreeForType(_ConstToDict, tree.definition_list, ast.Const) |
153 return self.mojom | 146 return self.mojom |
154 | 147 |
155 | 148 |
156 def Translate(tree, name): | 149 def Translate(tree, name): |
157 return _MojomBuilder().Build(tree, name) | 150 return _MojomBuilder().Build(tree, name) |
OLD | NEW |