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 sys |
| 10 |
| 11 |
| 12 def MapKind(kind): |
| 13 # todo: add more types |
| 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 'string': 's', |
| 24 'handle': 'h' } |
| 25 if kind.endswith('[]'): |
| 26 return 'a:' + MapKind(kind[0:len(kind)-2]) |
| 27 if kind in map_to_kind: |
| 28 return map_to_kind[kind] |
| 29 return 'x:' + kind |
| 30 |
| 31 |
| 32 def MapOrdinal(ordinal): |
| 33 return int(ordinal[1:]) # Strip leading '@' |
| 34 |
| 35 |
| 36 def MapFields(fields): |
| 37 out = [] |
| 38 for field in fields: |
| 39 if field[0] == 'FIELD': |
| 40 out.append({'name': field[2], |
| 41 'kind': MapKind(field[1]), |
| 42 'ordinal': MapOrdinal(field[3])}) |
| 43 return out |
| 44 |
| 45 |
| 46 def MapParameters(parameters): |
| 47 out = [] |
| 48 for parameter in parameters: |
| 49 if parameter[0] == 'PARAM': |
| 50 out.append({'name': parameter[2], |
| 51 'kind': MapKind(parameter[1]), |
| 52 'ordinal': MapOrdinal(parameter[3])}) |
| 53 return out |
| 54 |
| 55 |
| 56 def MapMethods(methods): |
| 57 out = [] |
| 58 for method in methods: |
| 59 if method[0] == 'METHOD': |
| 60 out.append({'name': method[1], |
| 61 'parameters': MapParameters(method[2]), |
| 62 'ordinal': MapOrdinal(method[3])}) |
| 63 return out |
| 64 |
| 65 |
| 66 class MojomBuilder(): |
| 67 |
| 68 def __init__(self): |
| 69 self.mojom = {} |
| 70 |
| 71 def AddStruct(self, name, attributes, fields): |
| 72 struct = {} |
| 73 struct['name'] = name |
| 74 struct['fields'] = MapFields(fields) |
| 75 self.mojom['structs'].append(struct) |
| 76 # TODO(darin): Add support for |attributes| |
| 77 |
| 78 def AddInterface(self, name, methods): |
| 79 interface = {} |
| 80 interface['name'] = name |
| 81 interface['methods'] = MapMethods(methods) |
| 82 self.mojom['interfaces'].append(interface) |
| 83 |
| 84 def AddModule(self, name, contents): |
| 85 self.mojom['name'] = name |
| 86 self.mojom['namespace'] = name |
| 87 self.mojom['structs'] = [] |
| 88 self.mojom['interfaces'] = [] |
| 89 for item in contents: |
| 90 if item[0] == 'STRUCT': |
| 91 self.AddStruct(name=item[1], attributes=item[2], fields=item[3]) |
| 92 elif item[0] == 'INTERFACE': |
| 93 self.AddInterface(name=item[1], methods=item[2]) |
| 94 |
| 95 def Build(self, tree): |
| 96 if tree[0] == 'MODULE': |
| 97 self.AddModule(name=tree[1], contents=tree[2]) |
| 98 return self.mojom |
| 99 |
| 100 |
| 101 def Translate(tree): |
| 102 return MojomBuilder().Build(tree) |
| 103 |
| 104 |
| 105 def Main(): |
| 106 if len(sys.argv) < 2: |
| 107 print("usage: %s filename" % (sys.argv[0])) |
| 108 sys.exit(1) |
| 109 tree = eval(open(sys.argv[1]).read()) |
| 110 result = Translate(tree) |
| 111 print(result) |
| 112 |
| 113 |
| 114 if __name__ == '__main__': |
| 115 Main() |
OLD | NEW |