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