| OLD | NEW |
| (Empty) |
| 1 #!/usr/bin/env python | |
| 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 | |
| 4 # found in the LICENSE file. | |
| 5 | |
| 6 """Translate parse tree to Mojom IR""" | |
| 7 | |
| 8 | |
| 9 import os | |
| 10 import sys | |
| 11 | |
| 12 | |
| 13 def MapKind(kind): | |
| 14 map_to_kind = { 'bool': 'b', | |
| 15 'int8': 'i8', | |
| 16 'int16': 'i16', | |
| 17 'int32': 'i32', | |
| 18 'int64': 'i64', | |
| 19 'uint8': 'u8', | |
| 20 'uint16': 'u16', | |
| 21 'uint32': 'u32', | |
| 22 'uint64': 'u64', | |
| 23 'float': 'f', | |
| 24 'double': 'd', | |
| 25 'string': 's', | |
| 26 'handle': 'h' } | |
| 27 if kind.endswith('[]'): | |
| 28 return 'a:' + MapKind(kind[0:len(kind)-2]) | |
| 29 if kind in map_to_kind: | |
| 30 return map_to_kind[kind] | |
| 31 return 'x:' + kind | |
| 32 | |
| 33 | |
| 34 def MapOrdinal(ordinal): | |
| 35 if ordinal == None: | |
| 36 return None; | |
| 37 return int(ordinal[1:]) # Strip leading '@' | |
| 38 | |
| 39 | |
| 40 def GetAttribute(attributes, name): | |
| 41 out = None | |
| 42 for attribute in attributes: | |
| 43 if attribute[0] == 'ATTRIBUTE' and attribute[1] == name: | |
| 44 out = attribute[2] | |
| 45 return out | |
| 46 | |
| 47 | |
| 48 def MapFields(fields): | |
| 49 out = [] | |
| 50 for field in fields: | |
| 51 if field[0] == 'FIELD': | |
| 52 out.append({'name': field[2], | |
| 53 'kind': MapKind(field[1]), | |
| 54 'ordinal': MapOrdinal(field[3])}) | |
| 55 return out | |
| 56 | |
| 57 | |
| 58 def MapParameters(parameters): | |
| 59 out = [] | |
| 60 for parameter in parameters: | |
| 61 if parameter[0] == 'PARAM': | |
| 62 out.append({'name': parameter[2], | |
| 63 'kind': MapKind(parameter[1]), | |
| 64 'ordinal': MapOrdinal(parameter[3])}) | |
| 65 return out | |
| 66 | |
| 67 | |
| 68 def MapMethods(methods): | |
| 69 out = [] | |
| 70 for method in methods: | |
| 71 if method[0] == 'METHOD': | |
| 72 out.append({'name': method[1], | |
| 73 'parameters': MapParameters(method[2]), | |
| 74 'ordinal': MapOrdinal(method[3])}) | |
| 75 return out | |
| 76 | |
| 77 | |
| 78 class MojomBuilder(): | |
| 79 | |
| 80 def __init__(self): | |
| 81 self.mojom = {} | |
| 82 | |
| 83 def AddStruct(self, name, attributes, fields): | |
| 84 struct = {} | |
| 85 struct['name'] = name | |
| 86 # TODO(darin): Add support for |attributes| | |
| 87 #struct['attributes'] = MapAttributes(attributes) | |
| 88 struct['fields'] = MapFields(fields) | |
| 89 self.mojom['structs'].append(struct) | |
| 90 | |
| 91 def AddInterface(self, name, attributes, methods): | |
| 92 interface = {} | |
| 93 interface['name'] = name | |
| 94 interface['peer'] = GetAttribute(attributes, 'Peer') | |
| 95 interface['methods'] = MapMethods(methods) | |
| 96 self.mojom['interfaces'].append(interface) | |
| 97 | |
| 98 def AddModule(self, name, namespace, contents): | |
| 99 self.mojom['name'] = name | |
| 100 self.mojom['namespace'] = namespace | |
| 101 self.mojom['structs'] = [] | |
| 102 self.mojom['interfaces'] = [] | |
| 103 for item in contents: | |
| 104 if item[0] == 'STRUCT': | |
| 105 self.AddStruct(name=item[1], attributes=item[2], fields=item[3]) | |
| 106 elif item[0] == 'INTERFACE': | |
| 107 self.AddInterface(name=item[1], attributes=item[2], methods=item[3]) | |
| 108 | |
| 109 def Build(self, tree, name): | |
| 110 if tree[0] == 'MODULE': | |
| 111 self.AddModule(name=name, namespace=tree[1], contents=tree[2]) | |
| 112 return self.mojom | |
| 113 | |
| 114 | |
| 115 def Translate(tree, name): | |
| 116 return MojomBuilder().Build(tree, name) | |
| 117 | |
| 118 | |
| 119 def Main(): | |
| 120 if len(sys.argv) < 2: | |
| 121 print("usage: %s filename" % (sys.argv[0])) | |
| 122 sys.exit(1) | |
| 123 tree = eval(open(sys.argv[1]).read()) | |
| 124 name = os.path.splitext(os.path.basename(sys.argv[1]))[0] | |
| 125 result = Translate(tree, name) | |
| 126 print(result) | |
| 127 | |
| 128 | |
| 129 if __name__ == '__main__': | |
| 130 Main() | |
| OLD | NEW |