OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright 2013 The Chromium Authors. All rights reserved. | 2 # Copyright 2013 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 """Translate parse tree to Mojom IR""" | 6 """Translate parse tree to Mojom IR""" |
7 | 7 |
8 | 8 |
9 import os | 9 import os |
10 import sys | 10 import sys |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
70 | 70 |
71 def MapMethods(methods): | 71 def MapMethods(methods): |
72 out = [] | 72 out = [] |
73 for method in methods: | 73 for method in methods: |
74 if method[0] == 'METHOD': | 74 if method[0] == 'METHOD': |
75 out.append({'name': method[1], | 75 out.append({'name': method[1], |
76 'parameters': MapParameters(method[2]), | 76 'parameters': MapParameters(method[2]), |
77 'ordinal': MapOrdinal(method[3])}) | 77 'ordinal': MapOrdinal(method[3])}) |
78 return out | 78 return out |
79 | 79 |
| 80 |
80 def MapEnumFields(fields): | 81 def MapEnumFields(fields): |
81 out = [] | 82 out = [] |
82 for field in fields: | 83 for field in fields: |
83 if field[0] == 'ENUM_FIELD': | 84 if field[0] == 'ENUM_FIELD': |
84 out.append({'name': field[1], | 85 out.append({'name': field[1], |
85 'value': field[2]}) | 86 'value': field[2]}) |
86 return out | 87 return out |
87 | 88 |
| 89 |
| 90 def MapEnums(enums): |
| 91 out = [] |
| 92 for enum in enums: |
| 93 if enum[0] == 'ENUM': |
| 94 out.append({'name': enum[1], |
| 95 'fields': MapEnumFields(enum[2])}) |
| 96 return out |
| 97 |
| 98 |
88 class MojomBuilder(): | 99 class MojomBuilder(): |
89 | 100 |
90 def __init__(self): | 101 def __init__(self): |
91 self.mojom = {} | 102 self.mojom = {} |
92 | 103 |
93 def AddStruct(self, name, attributes, fields): | 104 def AddStruct(self, name, attributes, body): |
94 struct = {} | 105 struct = {} |
95 struct['name'] = name | 106 struct['name'] = name |
96 # TODO(darin): Add support for |attributes| | 107 # TODO(darin): Add support for |attributes| |
97 #struct['attributes'] = MapAttributes(attributes) | 108 #struct['attributes'] = MapAttributes(attributes) |
98 struct['fields'] = MapFields(fields) | 109 struct['fields'] = MapFields(body) |
| 110 struct['enums'] = MapEnums(body) |
99 self.mojom['structs'].append(struct) | 111 self.mojom['structs'].append(struct) |
100 | 112 |
101 def AddInterface(self, name, attributes, methods): | 113 def AddInterface(self, name, attributes, body): |
102 interface = {} | 114 interface = {} |
103 interface['name'] = name | 115 interface['name'] = name |
104 interface['peer'] = GetAttribute(attributes, 'Peer') | 116 interface['peer'] = GetAttribute(attributes, 'Peer') |
105 interface['methods'] = MapMethods(methods) | 117 interface['methods'] = MapMethods(body) |
| 118 interface['enums'] = MapEnums(body) |
106 self.mojom['interfaces'].append(interface) | 119 self.mojom['interfaces'].append(interface) |
107 | 120 |
108 def AddEnum(self, name, fields): | 121 def AddEnum(self, name, fields): |
109 # TODO(mpcomplete): add support for specifying enums as types. Right now | 122 # TODO(mpcomplete): add support for specifying enums as types. Right now |
110 # we just use int32. | 123 # we just use int32. |
111 enum = {} | 124 enum = {} |
112 enum['name'] = name | 125 enum['name'] = name |
113 enum['fields'] = MapEnumFields(fields) | 126 enum['fields'] = MapEnumFields(fields) |
114 self.mojom['enums'].append(enum) | 127 self.mojom['enums'].append(enum) |
115 | 128 |
116 def AddModule(self, name, namespace, contents): | 129 def AddModule(self, name, namespace, contents): |
117 self.mojom['name'] = name | 130 self.mojom['name'] = name |
118 self.mojom['namespace'] = namespace | 131 self.mojom['namespace'] = namespace |
119 self.mojom['structs'] = [] | 132 self.mojom['structs'] = [] |
120 self.mojom['interfaces'] = [] | 133 self.mojom['interfaces'] = [] |
121 self.mojom['enums'] = [] | 134 self.mojom['enums'] = [] |
122 for item in contents: | 135 for item in contents: |
123 if item[0] == 'STRUCT': | 136 if item[0] == 'STRUCT': |
124 self.AddStruct(name=item[1], attributes=item[2], fields=item[3]) | 137 self.AddStruct(name=item[1], attributes=item[2], body=item[3]) |
125 elif item[0] == 'INTERFACE': | 138 elif item[0] == 'INTERFACE': |
126 self.AddInterface(name=item[1], attributes=item[2], methods=item[3]) | 139 self.AddInterface(name=item[1], attributes=item[2], body=item[3]) |
127 elif item[0] == 'ENUM': | 140 elif item[0] == 'ENUM': |
128 self.AddEnum(name=item[1], fields=item[2]) | 141 self.AddEnum(name=item[1], fields=item[2]) |
129 | 142 |
130 def Build(self, tree, name): | 143 def Build(self, tree, name): |
131 if tree[0] == 'MODULE': | 144 if tree[0] == 'MODULE': |
132 self.AddModule(name=name, namespace=tree[1], contents=tree[2]) | 145 self.AddModule(name=name, namespace=tree[1], contents=tree[2]) |
133 return self.mojom | 146 return self.mojom |
134 | 147 |
135 | 148 |
136 def Translate(tree, name): | 149 def Translate(tree, name): |
137 return MojomBuilder().Build(tree, name) | 150 return MojomBuilder().Build(tree, name) |
138 | 151 |
139 | 152 |
140 def Main(): | 153 def Main(): |
141 if len(sys.argv) < 2: | 154 if len(sys.argv) < 2: |
142 print("usage: %s filename" % (sys.argv[0])) | 155 print("usage: %s filename" % (sys.argv[0])) |
143 sys.exit(1) | 156 sys.exit(1) |
144 tree = eval(open(sys.argv[1]).read()) | 157 tree = eval(open(sys.argv[1]).read()) |
145 name = os.path.splitext(os.path.basename(sys.argv[1]))[0] | 158 name = os.path.splitext(os.path.basename(sys.argv[1]))[0] |
146 result = Translate(tree, name) | 159 result = Translate(tree, name) |
147 print(result) | 160 print(result) |
148 | 161 |
149 | 162 |
150 if __name__ == '__main__': | 163 if __name__ == '__main__': |
151 Main() | 164 Main() |
OLD | NEW |