| 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 sys | 10 import sys |
| 10 | 11 |
| 11 | 12 |
| 12 def MapKind(kind): | 13 def MapKind(kind): |
| 13 map_to_kind = { 'bool': 'b', | 14 map_to_kind = { 'bool': 'b', |
| 14 'int8': 'i8', | 15 'int8': 'i8', |
| 15 'int16': 'i16', | 16 'int16': 'i16', |
| 16 'int32': 'i32', | 17 'int32': 'i32', |
| 17 'int64': 'i64', | 18 'int64': 'i64', |
| 18 'uint8': 'u8', | 19 'uint8': 'u8', |
| (...skipping 10 matching lines...) Expand all Loading... |
| 29 return map_to_kind[kind] | 30 return map_to_kind[kind] |
| 30 return 'x:' + kind | 31 return 'x:' + kind |
| 31 | 32 |
| 32 | 33 |
| 33 def MapOrdinal(ordinal): | 34 def MapOrdinal(ordinal): |
| 34 if ordinal == None: | 35 if ordinal == None: |
| 35 return None; | 36 return None; |
| 36 return int(ordinal[1:]) # Strip leading '@' | 37 return int(ordinal[1:]) # Strip leading '@' |
| 37 | 38 |
| 38 | 39 |
| 40 def MapAttributes(attributes): |
| 41 out = {} |
| 42 for attribute in attributes: |
| 43 if attribute[0] == 'ATTRIBUTE': |
| 44 out[attribute[1]] = attribute[2] |
| 45 return out |
| 46 |
| 47 |
| 39 def MapFields(fields): | 48 def MapFields(fields): |
| 40 out = [] | 49 out = [] |
| 41 for field in fields: | 50 for field in fields: |
| 42 if field[0] == 'FIELD': | 51 if field[0] == 'FIELD': |
| 43 out.append({'name': field[2], | 52 out.append({'name': field[2], |
| 44 'kind': MapKind(field[1]), | 53 'kind': MapKind(field[1]), |
| 45 'ordinal': MapOrdinal(field[3])}) | 54 'ordinal': MapOrdinal(field[3])}) |
| 46 return out | 55 return out |
| 47 | 56 |
| 48 | 57 |
| (...skipping 18 matching lines...) Expand all Loading... |
| 67 | 76 |
| 68 | 77 |
| 69 class MojomBuilder(): | 78 class MojomBuilder(): |
| 70 | 79 |
| 71 def __init__(self): | 80 def __init__(self): |
| 72 self.mojom = {} | 81 self.mojom = {} |
| 73 | 82 |
| 74 def AddStruct(self, name, attributes, fields): | 83 def AddStruct(self, name, attributes, fields): |
| 75 struct = {} | 84 struct = {} |
| 76 struct['name'] = name | 85 struct['name'] = name |
| 86 # TODO(darin): Add support for |attributes| |
| 87 #struct['attributes'] = MapAttributes(attributes) |
| 77 struct['fields'] = MapFields(fields) | 88 struct['fields'] = MapFields(fields) |
| 78 self.mojom['structs'].append(struct) | 89 self.mojom['structs'].append(struct) |
| 79 # TODO(darin): Add support for |attributes| | |
| 80 | 90 |
| 81 def AddInterface(self, name, attributes, methods): | 91 def AddInterface(self, name, attributes, methods): |
| 82 interface = {} | 92 interface = {} |
| 83 interface['name'] = name | 93 interface['name'] = name |
| 94 interface['attributes'] = MapAttributes(attributes) |
| 84 interface['methods'] = MapMethods(methods) | 95 interface['methods'] = MapMethods(methods) |
| 85 self.mojom['interfaces'].append(interface) | 96 self.mojom['interfaces'].append(interface) |
| 86 # TODO(darin): Add support for |attributes| | |
| 87 | 97 |
| 88 def AddModule(self, name, namespace, contents): | 98 def AddModule(self, name, namespace, contents): |
| 89 self.mojom['name'] = name | 99 self.mojom['name'] = name |
| 90 self.mojom['namespace'] = namespace | 100 self.mojom['namespace'] = namespace |
| 91 self.mojom['structs'] = [] | 101 self.mojom['structs'] = [] |
| 92 self.mojom['interfaces'] = [] | 102 self.mojom['interfaces'] = [] |
| 93 for item in contents: | 103 for item in contents: |
| 94 if item[0] == 'STRUCT': | 104 if item[0] == 'STRUCT': |
| 95 self.AddStruct(name=item[1], attributes=item[2], fields=item[3]) | 105 self.AddStruct(name=item[1], attributes=item[2], fields=item[3]) |
| 96 elif item[0] == 'INTERFACE': | 106 elif item[0] == 'INTERFACE': |
| (...skipping 14 matching lines...) Expand all Loading... |
| 111 print("usage: %s filename" % (sys.argv[0])) | 121 print("usage: %s filename" % (sys.argv[0])) |
| 112 sys.exit(1) | 122 sys.exit(1) |
| 113 tree = eval(open(sys.argv[1]).read()) | 123 tree = eval(open(sys.argv[1]).read()) |
| 114 name = os.path.splitext(os.path.basename(sys.argv[1]))[0] | 124 name = os.path.splitext(os.path.basename(sys.argv[1]))[0] |
| 115 result = Translate(tree, name) | 125 result = Translate(tree, name) |
| 116 print(result) | 126 print(result) |
| 117 | 127 |
| 118 | 128 |
| 119 if __name__ == '__main__': | 129 if __name__ == '__main__': |
| 120 Main() | 130 Main() |
| OLD | NEW |