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 19 matching lines...) Expand all Loading... |
30 'handle<data_pipe_consumer>': 'h:d:c', | 30 'handle<data_pipe_consumer>': 'h:d:c', |
31 'handle<data_pipe_producer>': 'h:d:p', | 31 'handle<data_pipe_producer>': 'h:d:p', |
32 'handle<message_pipe>': 'h:m', | 32 'handle<message_pipe>': 'h:m', |
33 'handle<shared_buffer>': 'h:s'} | 33 'handle<shared_buffer>': 'h:s'} |
34 if kind.endswith('[]'): | 34 if kind.endswith('[]'): |
35 return 'a:' + _MapKind(kind[0:len(kind)-2]) | 35 return 'a:' + _MapKind(kind[0:len(kind)-2]) |
36 if kind in map_to_kind: | 36 if kind in map_to_kind: |
37 return map_to_kind[kind] | 37 return map_to_kind[kind] |
38 return 'x:' + kind | 38 return 'x:' + kind |
39 | 39 |
| 40 def _MapAttributes(attributes): |
| 41 if not attributes: |
| 42 return {} |
| 43 return dict([(attribute[1], attribute[2]) |
| 44 for attribute in attributes if attribute[0] == 'ATTRIBUTE']) |
| 45 |
40 def _GetAttribute(attributes, name): | 46 def _GetAttribute(attributes, name): |
41 out = None | 47 out = None |
42 if attributes: | 48 if attributes: |
43 for attribute in attributes: | 49 for attribute in attributes: |
44 if attribute[0] == 'ATTRIBUTE' and attribute[1] == name: | 50 if attribute[0] == 'ATTRIBUTE' and attribute[1] == name: |
45 out = attribute[2] | 51 out = attribute[2] |
46 return out | 52 return out |
47 | 53 |
48 def _MapField(tree): | 54 def _MapField(tree): |
49 assert type(tree[3]) is ast.Ordinal | 55 assert type(tree[3]) is ast.Ordinal |
(...skipping 17 matching lines...) Expand all Loading... |
67 method['response_parameters'] = _MapTree(_MapParameter, tree[4], 'PARAM') | 73 method['response_parameters'] = _MapTree(_MapParameter, tree[4], 'PARAM') |
68 return method | 74 return method |
69 | 75 |
70 def _MapEnumField(tree): | 76 def _MapEnumField(tree): |
71 return {'name': tree[1], | 77 return {'name': tree[1], |
72 'value': tree[2]} | 78 'value': tree[2]} |
73 | 79 |
74 def _MapStruct(tree): | 80 def _MapStruct(tree): |
75 struct = {} | 81 struct = {} |
76 struct['name'] = tree[1] | 82 struct['name'] = tree[1] |
77 # TODO(darin): Add support for |attributes| | 83 struct['attributes'] = _MapAttributes(tree[2]) |
78 #struct['attributes'] = MapAttributes(tree[2]) | |
79 struct['fields'] = _MapTree(_MapField, tree[3], 'FIELD') | 84 struct['fields'] = _MapTree(_MapField, tree[3], 'FIELD') |
80 struct['enums'] = _MapTree(_MapEnum, tree[3], 'ENUM') | 85 struct['enums'] = _MapTree(_MapEnum, tree[3], 'ENUM') |
81 return struct | 86 return struct |
82 | 87 |
83 def _MapInterface(tree): | 88 def _MapInterface(tree): |
84 interface = {} | 89 interface = {} |
85 interface['name'] = tree[1] | 90 interface['name'] = tree[1] |
86 interface['peer'] = _GetAttribute(tree[2], 'Peer') | 91 interface['peer'] = _GetAttribute(tree[2], 'Peer') |
87 interface['methods'] = _MapTree(_MapMethod, tree[3], 'METHOD') | 92 interface['methods'] = _MapTree(_MapMethod, tree[3], 'METHOD') |
88 interface['enums'] = _MapTree(_MapEnum, tree[3], 'ENUM') | 93 interface['enums'] = _MapTree(_MapEnum, tree[3], 'ENUM') |
89 return interface | 94 return interface |
90 | 95 |
91 def _MapEnum(tree): | 96 def _MapEnum(tree): |
92 enum = {} | 97 enum = {} |
93 enum['name'] = tree[1] | 98 enum['name'] = tree[1] |
94 enum['fields'] = _MapTree(_MapEnumField, tree[2], 'ENUM_FIELD') | 99 enum['fields'] = _MapTree(_MapEnumField, tree[2], 'ENUM_FIELD') |
95 return enum | 100 return enum |
96 | 101 |
97 def _MapModule(tree, name): | 102 def _MapModule(tree, name): |
98 mojom = {} | 103 mojom = {} |
99 mojom['name'] = name | 104 mojom['name'] = name |
100 mojom['namespace'] = tree[1] | 105 mojom['namespace'] = tree[1] |
101 mojom['structs'] = _MapTree(_MapStruct, tree[2], 'STRUCT') | 106 mojom['attributes'] = _MapAttributes(tree[2]) |
102 mojom['interfaces'] = _MapTree(_MapInterface, tree[2], 'INTERFACE') | 107 mojom['structs'] = _MapTree(_MapStruct, tree[3], 'STRUCT') |
103 mojom['enums'] = _MapTree(_MapEnum, tree[2], 'ENUM') | 108 mojom['interfaces'] = _MapTree(_MapInterface, tree[3], 'INTERFACE') |
| 109 mojom['enums'] = _MapTree(_MapEnum, tree[3], 'ENUM') |
104 return mojom | 110 return mojom |
105 | 111 |
106 def _MapImport(tree): | 112 def _MapImport(tree): |
107 import_item = {} | 113 import_item = {} |
108 import_item['filename'] = tree[1] | 114 import_item['filename'] = tree[1] |
109 return import_item | 115 return import_item |
110 | 116 |
111 | 117 |
112 class _MojomBuilder(object): | 118 class _MojomBuilder(object): |
113 def __init__(self): | 119 def __init__(self): |
114 self.mojom = {} | 120 self.mojom = {} |
115 | 121 |
116 def Build(self, tree, name): | 122 def Build(self, tree, name): |
117 modules = [_MapModule(item, name) for item in tree if item[0] == 'MODULE'] | 123 modules = [_MapModule(item, name) for item in tree if item[0] == 'MODULE'] |
118 if len(modules) != 1: | 124 if len(modules) != 1: |
119 raise Exception('A mojom file must contain exactly 1 module.') | 125 raise Exception('A mojom file must contain exactly 1 module.') |
120 self.mojom = modules[0] | 126 self.mojom = modules[0] |
121 self.mojom['imports'] = _MapTree(_MapImport, tree, 'IMPORT') | 127 self.mojom['imports'] = _MapTree(_MapImport, tree, 'IMPORT') |
122 return self.mojom | 128 return self.mojom |
123 | 129 |
124 | 130 |
125 def Translate(tree, name): | 131 def Translate(tree, name): |
126 return _MojomBuilder().Build(tree, name) | 132 return _MojomBuilder().Build(tree, name) |
OLD | NEW |