OLD | NEW |
1 #! /usr/bin/env python | 1 #! /usr/bin/env python |
2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2012 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 import itertools | 6 import itertools |
7 import json | 7 import json |
8 import os.path | 8 import os.path |
9 import re | 9 import re |
10 import sys | 10 import sys |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
100 return_type = None | 100 return_type = None |
101 if self.node.GetProperty('TYPEREF') not in ('void', None): | 101 if self.node.GetProperty('TYPEREF') not in ('void', None): |
102 return_type = Typeref(self.node.GetProperty('TYPEREF'), | 102 return_type = Typeref(self.node.GetProperty('TYPEREF'), |
103 self.node.parent, | 103 self.node.parent, |
104 {'name': self.node.GetName()}).process(callbacks) | 104 {'name': self.node.GetName()}).process(callbacks) |
105 # The IDL parser doesn't allow specifying return types as optional. | 105 # The IDL parser doesn't allow specifying return types as optional. |
106 # Instead we infer any object return values to be optional. | 106 # Instead we infer any object return values to be optional. |
107 # TODO(asargent): fix the IDL parser to support optional return types. | 107 # TODO(asargent): fix the IDL parser to support optional return types. |
108 if return_type.get('type') == 'object' or '$ref' in return_type: | 108 if return_type.get('type') == 'object' or '$ref' in return_type: |
109 return_type['optional'] = True | 109 return_type['optional'] = True |
110 for node in self.node.children: | 110 for node in self.node.GetChildren(): |
111 parameter = Param(node).process(callbacks) | 111 parameter = Param(node).process(callbacks) |
112 if parameter['name'] in self.comment: | 112 if parameter['name'] in self.comment: |
113 parameter['description'] = self.comment[parameter['name']] | 113 parameter['description'] = self.comment[parameter['name']] |
114 parameters.append(parameter) | 114 parameters.append(parameter) |
115 return (self.node.GetName(), parameters, return_type) | 115 return (self.node.GetName(), parameters, return_type) |
116 | 116 |
117 | 117 |
118 class Param(object): | 118 class Param(object): |
119 ''' | 119 ''' |
120 Given a Param node representing a function parameter, converts into a Python | 120 Given a Param node representing a function parameter, converts into a Python |
(...skipping 11 matching lines...) Expand all Loading... |
132 class Dictionary(object): | 132 class Dictionary(object): |
133 ''' | 133 ''' |
134 Given an IDL Dictionary node, converts into a Python dictionary that the JSON | 134 Given an IDL Dictionary node, converts into a Python dictionary that the JSON |
135 schema compiler expects to see. | 135 schema compiler expects to see. |
136 ''' | 136 ''' |
137 def __init__(self, dictionary_node): | 137 def __init__(self, dictionary_node): |
138 self.node = dictionary_node | 138 self.node = dictionary_node |
139 | 139 |
140 def process(self, callbacks): | 140 def process(self, callbacks): |
141 properties = OrderedDict() | 141 properties = OrderedDict() |
142 for node in self.node.children: | 142 for node in self.node.GetChildren(): |
143 if node.cls == 'Member': | 143 if node.cls == 'Member': |
144 k, v = Member(node).process(callbacks) | 144 k, v = Member(node).process(callbacks) |
145 properties[k] = v | 145 properties[k] = v |
146 result = {'id': self.node.GetName(), | 146 result = {'id': self.node.GetName(), |
147 'properties': properties, | 147 'properties': properties, |
148 'type': 'object'} | 148 'type': 'object'} |
149 if self.node.GetProperty('inline_doc'): | 149 if self.node.GetProperty('inline_doc'): |
150 result['inline_doc'] = True | 150 result['inline_doc'] = True |
151 elif self.node.GetProperty('noinline_doc'): | 151 elif self.node.GetProperty('noinline_doc'): |
152 result['noinline_doc'] = True | 152 result['noinline_doc'] = True |
(...skipping 21 matching lines...) Expand all Loading... |
174 ('supportsFilters', lambda s: s == 'true'), | 174 ('supportsFilters', lambda s: s == 'true'), |
175 ('supportsListeners', lambda s: s == 'true'), | 175 ('supportsListeners', lambda s: s == 'true'), |
176 ('supportsRules', lambda s: s == 'true')]: | 176 ('supportsRules', lambda s: s == 'true')]: |
177 if self.node.GetProperty(option_name): | 177 if self.node.GetProperty(option_name): |
178 if 'options' not in properties: | 178 if 'options' not in properties: |
179 properties['options'] = {} | 179 properties['options'] = {} |
180 properties['options'][option_name] = sanitizer(self.node.GetProperty( | 180 properties['options'][option_name] = sanitizer(self.node.GetProperty( |
181 option_name)) | 181 option_name)) |
182 is_function = False | 182 is_function = False |
183 parameter_comments = OrderedDict() | 183 parameter_comments = OrderedDict() |
184 for node in self.node.children: | 184 for node in self.node.GetChildren(): |
185 if node.cls == 'Comment': | 185 if node.cls == 'Comment': |
186 (parent_comment, parameter_comments) = ProcessComment(node.GetName()) | 186 (parent_comment, parameter_comments) = ProcessComment(node.GetName()) |
187 properties['description'] = parent_comment | 187 properties['description'] = parent_comment |
188 elif node.cls == 'Callspec': | 188 elif node.cls == 'Callspec': |
189 is_function = True | 189 is_function = True |
190 name, parameters, return_type = (Callspec(node, parameter_comments) | 190 name, parameters, return_type = (Callspec(node, parameter_comments) |
191 .process(callbacks)) | 191 .process(callbacks)) |
192 properties['parameters'] = parameters | 192 properties['parameters'] = parameters |
193 if return_type is not None: | 193 if return_type is not None: |
194 properties['returns'] = return_type | 194 properties['returns'] = return_type |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
283 ''' | 283 ''' |
284 Given an IDL Enum node, converts into a Python dictionary that the JSON | 284 Given an IDL Enum node, converts into a Python dictionary that the JSON |
285 schema compiler expects to see. | 285 schema compiler expects to see. |
286 ''' | 286 ''' |
287 def __init__(self, enum_node): | 287 def __init__(self, enum_node): |
288 self.node = enum_node | 288 self.node = enum_node |
289 self.description = '' | 289 self.description = '' |
290 | 290 |
291 def process(self, callbacks): | 291 def process(self, callbacks): |
292 enum = [] | 292 enum = [] |
293 for node in self.node.children: | 293 for node in self.node.GetChildren(): |
294 if node.cls == 'EnumItem': | 294 if node.cls == 'EnumItem': |
295 enum_value = {'name': node.GetName()} | 295 enum_value = {'name': node.GetName()} |
296 for child in node.children: | 296 for child in node.GetChildren(): |
297 if child.cls == 'Comment': | 297 if child.cls == 'Comment': |
298 enum_value['description'] = ProcessComment(child.GetName())[0] | 298 enum_value['description'] = ProcessComment(child.GetName())[0] |
299 else: | 299 else: |
300 raise ValueError('Did not process %s %s' % (child.cls, child)) | 300 raise ValueError('Did not process %s %s' % (child.cls, child)) |
301 enum.append(enum_value) | 301 enum.append(enum_value) |
302 elif node.cls == 'Comment': | 302 elif node.cls == 'Comment': |
303 self.description = ProcessComment(node.GetName())[0] | 303 self.description = ProcessComment(node.GetName())[0] |
304 else: | 304 else: |
305 sys.exit('Did not process %s %s' % (node.cls, node)) | 305 sys.exit('Did not process %s %s' % (node.cls, node)) |
306 result = {'id' : self.node.GetName(), | 306 result = {'id' : self.node.GetName(), |
(...skipping 24 matching lines...) Expand all Loading... |
331 self.internal = internal | 331 self.internal = internal |
332 self.platforms = platforms | 332 self.platforms = platforms |
333 self.compiler_options = compiler_options | 333 self.compiler_options = compiler_options |
334 self.events = [] | 334 self.events = [] |
335 self.functions = [] | 335 self.functions = [] |
336 self.types = [] | 336 self.types = [] |
337 self.callbacks = OrderedDict() | 337 self.callbacks = OrderedDict() |
338 self.description = description | 338 self.description = description |
339 | 339 |
340 def process(self): | 340 def process(self): |
341 for node in self.namespace.children: | 341 for node in self.namespace.GetChildren(): |
342 if node.cls == 'Dictionary': | 342 if node.cls == 'Dictionary': |
343 self.types.append(Dictionary(node).process(self.callbacks)) | 343 self.types.append(Dictionary(node).process(self.callbacks)) |
344 elif node.cls == 'Callback': | 344 elif node.cls == 'Callback': |
345 k, v = Member(node).process(self.callbacks) | 345 k, v = Member(node).process(self.callbacks) |
346 self.callbacks[k] = v | 346 self.callbacks[k] = v |
347 elif node.cls == 'Interface' and node.GetName() == 'Functions': | 347 elif node.cls == 'Interface' and node.GetName() == 'Functions': |
348 self.functions = self.process_interface(node) | 348 self.functions = self.process_interface(node) |
349 elif node.cls == 'Interface' and node.GetName() == 'Events': | 349 elif node.cls == 'Interface' and node.GetName() == 'Events': |
350 self.events = self.process_interface(node) | 350 self.events = self.process_interface(node) |
351 elif node.cls == 'Enum': | 351 elif node.cls == 'Enum': |
352 self.types.append(Enum(node).process(self.callbacks)) | 352 self.types.append(Enum(node).process(self.callbacks)) |
353 else: | 353 else: |
354 sys.exit('Did not process %s %s' % (node.cls, node)) | 354 sys.exit('Did not process %s %s' % (node.cls, node)) |
355 if self.compiler_options is not None: | 355 if self.compiler_options is not None: |
356 compiler_options = self.compiler_options | 356 compiler_options = self.compiler_options |
357 else: | 357 else: |
358 compiler_options = {} | 358 compiler_options = {} |
359 return {'namespace': self.namespace.GetName(), | 359 return {'namespace': self.namespace.GetName(), |
360 'description': self.description, | 360 'description': self.description, |
361 'nodoc': self.nodoc, | 361 'nodoc': self.nodoc, |
362 'types': self.types, | 362 'types': self.types, |
363 'functions': self.functions, | 363 'functions': self.functions, |
364 'internal': self.internal, | 364 'internal': self.internal, |
365 'events': self.events, | 365 'events': self.events, |
366 'platforms': self.platforms, | 366 'platforms': self.platforms, |
367 'compiler_options': compiler_options} | 367 'compiler_options': compiler_options} |
368 | 368 |
369 def process_interface(self, node): | 369 def process_interface(self, node): |
370 members = [] | 370 members = [] |
371 for member in node.children: | 371 for member in node.GetChildren(): |
372 if member.cls == 'Member': | 372 if member.cls == 'Member': |
373 name, properties = Member(member).process(self.callbacks) | 373 name, properties = Member(member).process(self.callbacks) |
374 members.append(properties) | 374 members.append(properties) |
375 return members | 375 return members |
376 | 376 |
377 | 377 |
378 class IDLSchema(object): | 378 class IDLSchema(object): |
379 ''' | 379 ''' |
380 Given a list of IDLNodes and IDLAttributes, converts into a Python list | 380 Given a list of IDLNodes and IDLAttributes, converts into a Python list |
381 of api_defs that the JSON schema compiler expects to see. | 381 of api_defs that the JSON schema compiler expects to see. |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
446 Dump a json serialization of parse result for the IDL files whose names | 446 Dump a json serialization of parse result for the IDL files whose names |
447 were passed in on the command line. | 447 were passed in on the command line. |
448 ''' | 448 ''' |
449 for filename in sys.argv[1:]: | 449 for filename in sys.argv[1:]: |
450 schema = Load(filename) | 450 schema = Load(filename) |
451 print json.dumps(schema, indent=2) | 451 print json.dumps(schema, indent=2) |
452 | 452 |
453 | 453 |
454 if __name__ == '__main__': | 454 if __name__ == '__main__': |
455 Main() | 455 Main() |
OLD | NEW |