OLD | NEW |
---|---|
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright 2014 The Chromium Authors. All rights reserved. | 2 # Copyright 2014 The Chromium Authors. All rights reserved. |
3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 | 5 |
6 """Translates parse tree to Mojom IR.""" | 6 """Translates parse tree to Mojom IR.""" |
7 | 7 |
8 | 8 |
9 import os.path | 9 import os.path |
10 import sys | 10 import sys |
(...skipping 23 matching lines...) Expand all Loading... | |
34 'handle<data_pipe_consumer>': 'h:d:c', | 34 'handle<data_pipe_consumer>': 'h:d:c', |
35 'handle<data_pipe_producer>': 'h:d:p', | 35 'handle<data_pipe_producer>': 'h:d:p', |
36 'handle<message_pipe>': 'h:m', | 36 'handle<message_pipe>': 'h:m', |
37 'handle<shared_buffer>': 'h:s'} | 37 'handle<shared_buffer>': 'h:s'} |
38 if kind.endswith('[]'): | 38 if kind.endswith('[]'): |
39 return 'a:' + MapKind(kind[0:len(kind)-2]) | 39 return 'a:' + MapKind(kind[0:len(kind)-2]) |
40 if kind in map_to_kind: | 40 if kind in map_to_kind: |
41 return map_to_kind[kind] | 41 return map_to_kind[kind] |
42 return 'x:' + kind | 42 return 'x:' + kind |
43 | 43 |
44 def MapAttributes(attributes): | |
45 def _AttributeValueToString(v): | |
qsr
2014/04/30 13:03:45
Not sure this is the best solution. If you think i
darin (slow to review)
2014/05/02 07:44:01
Do you think that would help us catch syntactic er
qsr
2014/05/05 12:40:05
Finally, I let the expression, and decided to use
| |
46 if isinstance(v, basestring): | |
47 return v | |
48 return ''.join(v[1]) | |
49 if not attributes: | |
50 return {} | |
51 return dict([(attribute[1], _AttributeValueToString(attribute[2])) | |
52 for attribute in attributes if attribute[0] == 'ATTRIBUTE']) | |
53 | |
44 def GetAttribute(attributes, name): | 54 def GetAttribute(attributes, name): |
45 out = None | 55 out = None |
46 if attributes: | 56 if attributes: |
47 for attribute in attributes: | 57 for attribute in attributes: |
48 if attribute[0] == 'ATTRIBUTE' and attribute[1] == name: | 58 if attribute[0] == 'ATTRIBUTE' and attribute[1] == name: |
49 out = attribute[2] | 59 out = attribute[2] |
50 return out | 60 return out |
51 | 61 |
52 def MapField(tree): | 62 def MapField(tree): |
53 assert type(tree[3]) is ast.Ordinal | 63 assert type(tree[3]) is ast.Ordinal |
(...skipping 17 matching lines...) Expand all Loading... | |
71 method['response_parameters'] = MapTree(MapParameter, tree[4], 'PARAM') | 81 method['response_parameters'] = MapTree(MapParameter, tree[4], 'PARAM') |
72 return method | 82 return method |
73 | 83 |
74 def MapEnumField(tree): | 84 def MapEnumField(tree): |
75 return {'name': tree[1], | 85 return {'name': tree[1], |
76 'value': tree[2]} | 86 'value': tree[2]} |
77 | 87 |
78 def MapStruct(tree): | 88 def MapStruct(tree): |
79 struct = {} | 89 struct = {} |
80 struct['name'] = tree[1] | 90 struct['name'] = tree[1] |
81 # TODO(darin): Add support for |attributes| | 91 struct['attributes'] = MapAttributes(tree[2]) |
82 #struct['attributes'] = MapAttributes(tree[2]) | |
83 struct['fields'] = MapTree(MapField, tree[3], 'FIELD') | 92 struct['fields'] = MapTree(MapField, tree[3], 'FIELD') |
84 struct['enums'] = MapTree(MapEnum, tree[3], 'ENUM') | 93 struct['enums'] = MapTree(MapEnum, tree[3], 'ENUM') |
85 return struct | 94 return struct |
86 | 95 |
87 def MapInterface(tree): | 96 def MapInterface(tree): |
88 interface = {} | 97 interface = {} |
89 interface['name'] = tree[1] | 98 interface['name'] = tree[1] |
90 interface['peer'] = GetAttribute(tree[2], 'Peer') | 99 interface['peer'] = GetAttribute(tree[2], 'Peer') |
91 interface['methods'] = MapTree(MapMethod, tree[3], 'METHOD') | 100 interface['methods'] = MapTree(MapMethod, tree[3], 'METHOD') |
92 interface['enums'] = MapTree(MapEnum, tree[3], 'ENUM') | 101 interface['enums'] = MapTree(MapEnum, tree[3], 'ENUM') |
93 return interface | 102 return interface |
94 | 103 |
95 def MapEnum(tree): | 104 def MapEnum(tree): |
96 enum = {} | 105 enum = {} |
97 enum['name'] = tree[1] | 106 enum['name'] = tree[1] |
98 enum['fields'] = MapTree(MapEnumField, tree[2], 'ENUM_FIELD') | 107 enum['fields'] = MapTree(MapEnumField, tree[2], 'ENUM_FIELD') |
99 return enum | 108 return enum |
100 | 109 |
101 def MapModule(tree, name): | 110 def MapModule(tree, name): |
102 mojom = {} | 111 mojom = {} |
103 mojom['name'] = name | 112 mojom['name'] = name |
104 mojom['namespace'] = tree[1] | 113 mojom['namespace'] = tree[1] |
105 mojom['structs'] = MapTree(MapStruct, tree[2], 'STRUCT') | 114 mojom['attributes'] = MapAttributes(tree[2]) |
106 mojom['interfaces'] = MapTree(MapInterface, tree[2], 'INTERFACE') | 115 mojom['structs'] = MapTree(MapStruct, tree[3], 'STRUCT') |
107 mojom['enums'] = MapTree(MapEnum, tree[2], 'ENUM') | 116 mojom['interfaces'] = MapTree(MapInterface, tree[3], 'INTERFACE') |
117 mojom['enums'] = MapTree(MapEnum, tree[3], 'ENUM') | |
108 return mojom | 118 return mojom |
109 | 119 |
110 def MapImport(tree): | 120 def MapImport(tree): |
111 import_item = {} | 121 import_item = {} |
112 import_item['filename'] = tree[1] | 122 import_item['filename'] = tree[1] |
113 return import_item | 123 return import_item |
114 | 124 |
115 | 125 |
116 class MojomBuilder(): | 126 class MojomBuilder(): |
117 def __init__(self): | 127 def __init__(self): |
(...skipping 18 matching lines...) Expand all Loading... | |
136 print("usage: %s filename" % (sys.argv[0])) | 146 print("usage: %s filename" % (sys.argv[0])) |
137 sys.exit(1) | 147 sys.exit(1) |
138 tree = eval(open(sys.argv[1]).read()) | 148 tree = eval(open(sys.argv[1]).read()) |
139 name = os.path.splitext(os.path.basename(sys.argv[1]))[0] | 149 name = os.path.splitext(os.path.basename(sys.argv[1]))[0] |
140 result = Translate(tree, name) | 150 result = Translate(tree, name) |
141 print(result) | 151 print(result) |
142 | 152 |
143 | 153 |
144 if __name__ == '__main__': | 154 if __name__ == '__main__': |
145 Main() | 155 Main() |
OLD | NEW |