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.
6 """Translate parse tree to Mojom IR"""
13 # todo: add more types
14 map_to_kind = { 'bool': 'b',
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]
32 def MapOrdinal(ordinal):
33 return int(ordinal[1:]) # Strip leading '@'
36 def MapFields(fields):
39 if field[0] == 'FIELD':
40 out.append({'name': field[2],
41 'kind': MapKind(field[1]),
42 'ordinal': MapOrdinal(field[3])})
46 def MapParameters(parameters):
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])})
56 def MapMethods(methods):
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])})
71 def AddStruct(self, name, attributes, fields):
74 struct['fields'] = MapFields(fields)
75 self.mojom['structs'].append(struct)
76 # TODO(darin): Add support for |attributes|
78 def AddInterface(self, name, methods):
80 interface['name'] = name
81 interface['methods'] = MapMethods(methods)
82 self.mojom['interfaces'].append(interface)
84 def AddModule(self, name, contents):
85 self.mojom['name'] = name
86 self.mojom['namespace'] = name
87 self.mojom['structs'] = []
88 self.mojom['interfaces'] = []
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])
95 def Build(self, tree):
96 if tree[0] == 'MODULE':
97 self.AddModule(name=tree[1], contents=tree[2])
102 return MojomBuilder().Build(tree)
106 if len(sys.argv) < 2:
107 print("usage: %s filename" % (sys.argv[0]))
109 tree = eval(open(sys.argv[1]).read())
110 result = Translate(tree)
114 if __name__ == '__main__':