Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(193)

Side by Side Diff: tools/json_schema_compiler/idl_schema.py

Issue 143473003: Generate ax enums from idl. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Add allow custom filename property to top level idl. Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 289 matching lines...) Expand 10 before | Expand all | Expand 10 after
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(),
307 'description': self.description, 307 'description': self.description,
308 'type': 'string', 308 'type': 'string',
309 'enum': enum} 309 'enum': enum}
310 for property_name in ('inline_doc', 'noinline_doc', 'nodoc'): 310 for property_name in (
311 'inline_doc', 'noinline_doc', 'nodoc', 'cpp_omit_enum_type',):
311 if self.node.GetProperty(property_name): 312 if self.node.GetProperty(property_name):
312 result[property_name] = True 313 result[property_name] = True
313 return result 314 return result
314 315
315 316
316 class Namespace(object): 317 class Namespace(object):
317 ''' 318 '''
318 Given an IDLNode representing an IDL namespace, converts into a Python 319 Given an IDLNode representing an IDL namespace, converts into a Python
319 dictionary that the JSON schema compiler expects to see. 320 dictionary that the JSON schema compiler expects to see.
320 ''' 321 '''
321 322
322 def __init__(self, 323 def __init__(self,
323 namespace_node, 324 namespace_node,
324 description, 325 description,
325 nodoc=False, 326 nodoc=False,
326 internal=False, 327 internal=False,
327 platforms=None, 328 platforms=None,
328 compiler_options=None): 329 compiler_options=None,
330 allow_custom_filename=False):
329 self.namespace = namespace_node 331 self.namespace = namespace_node
330 self.nodoc = nodoc 332 self.nodoc = nodoc
331 self.internal = internal 333 self.internal = internal
332 self.platforms = platforms 334 self.platforms = platforms
333 self.compiler_options = compiler_options 335 self.compiler_options = compiler_options
334 self.events = [] 336 self.events = []
335 self.functions = [] 337 self.functions = []
336 self.types = [] 338 self.types = []
337 self.callbacks = OrderedDict() 339 self.callbacks = OrderedDict()
338 self.description = description 340 self.description = description
341 self.allow_custom_filename = allow_custom_filename
339 342
340 def process(self): 343 def process(self):
341 for node in self.namespace.GetChildren(): 344 for node in self.namespace.GetChildren():
342 if node.cls == 'Dictionary': 345 if node.cls == 'Dictionary':
343 self.types.append(Dictionary(node).process(self.callbacks)) 346 self.types.append(Dictionary(node).process(self.callbacks))
344 elif node.cls == 'Callback': 347 elif node.cls == 'Callback':
345 k, v = Member(node).process(self.callbacks) 348 k, v = Member(node).process(self.callbacks)
346 self.callbacks[k] = v 349 self.callbacks[k] = v
347 elif node.cls == 'Interface' and node.GetName() == 'Functions': 350 elif node.cls == 'Interface' and node.GetName() == 'Functions':
348 self.functions = self.process_interface(node) 351 self.functions = self.process_interface(node)
349 elif node.cls == 'Interface' and node.GetName() == 'Events': 352 elif node.cls == 'Interface' and node.GetName() == 'Events':
350 self.events = self.process_interface(node) 353 self.events = self.process_interface(node)
351 elif node.cls == 'Enum': 354 elif node.cls == 'Enum':
352 self.types.append(Enum(node).process(self.callbacks)) 355 self.types.append(Enum(node).process(self.callbacks))
353 else: 356 else:
354 sys.exit('Did not process %s %s' % (node.cls, node)) 357 sys.exit('Did not process %s %s' % (node.cls, node))
355 if self.compiler_options is not None: 358 if self.compiler_options is not None:
356 compiler_options = self.compiler_options 359 compiler_options = self.compiler_options
357 else: 360 else:
358 compiler_options = {} 361 compiler_options = {}
359 return {'namespace': self.namespace.GetName(), 362 return {'namespace': self.namespace.GetName(),
360 'description': self.description, 363 'description': self.description,
361 'nodoc': self.nodoc, 364 'nodoc': self.nodoc,
362 'types': self.types, 365 'types': self.types,
363 'functions': self.functions, 366 'functions': self.functions,
364 'internal': self.internal, 367 'internal': self.internal,
365 'events': self.events, 368 'events': self.events,
366 'platforms': self.platforms, 369 'platforms': self.platforms,
367 'compiler_options': compiler_options} 370 'compiler_options': compiler_options,
371 'allow_custom_filename': self.allow_custom_filename}
368 372
369 def process_interface(self, node): 373 def process_interface(self, node):
370 members = [] 374 members = []
371 for member in node.GetChildren(): 375 for member in node.GetChildren():
372 if member.cls == 'Member': 376 if member.cls == 'Member':
373 name, properties = Member(member).process(self.callbacks) 377 name, properties = Member(member).process(self.callbacks)
374 members.append(properties) 378 members.append(properties)
375 return members 379 return members
376 380
377 381
378 class IDLSchema(object): 382 class IDLSchema(object):
379 ''' 383 '''
380 Given a list of IDLNodes and IDLAttributes, converts into a Python list 384 Given a list of IDLNodes and IDLAttributes, converts into a Python list
381 of api_defs that the JSON schema compiler expects to see. 385 of api_defs that the JSON schema compiler expects to see.
382 ''' 386 '''
383 387
384 def __init__(self, idl): 388 def __init__(self, idl):
385 self.idl = idl 389 self.idl = idl
386 390
387 def process(self): 391 def process(self):
388 namespaces = [] 392 namespaces = []
389 nodoc = False 393 nodoc = False
390 internal = False 394 internal = False
391 description = None 395 description = None
392 platforms = None 396 platforms = None
393 compiler_options = None 397 compiler_options = None
398 allow_custom_filename = False
394 for node in self.idl: 399 for node in self.idl:
395 if node.cls == 'Namespace': 400 if node.cls == 'Namespace':
396 if not description: 401 if not description:
397 # TODO(kalman): Go back to throwing an error here. 402 # TODO(kalman): Go back to throwing an error here.
398 print('%s must have a namespace-level comment. This will ' 403 print('%s must have a namespace-level comment. This will '
399 'appear on the API summary page.' % node.GetName()) 404 'appear on the API summary page.' % node.GetName())
400 description = '' 405 description = ''
401 namespace = Namespace(node, description, nodoc, internal, 406 namespace = Namespace(node, description, nodoc, internal,
402 platforms=platforms, 407 platforms=platforms,
403 compiler_options=compiler_options) 408 compiler_options=compiler_options,
409 allow_custom_filename=allow_custom_filename)
404 namespaces.append(namespace.process()) 410 namespaces.append(namespace.process())
405 nodoc = False 411 nodoc = False
406 internal = False 412 internal = False
407 platforms = None 413 platforms = None
408 compiler_options = None 414 compiler_options = None
415 allow_custom_filename = False
409 elif node.cls == 'Copyright': 416 elif node.cls == 'Copyright':
410 continue 417 continue
411 elif node.cls == 'Comment': 418 elif node.cls == 'Comment':
412 description = node.GetName() 419 description = node.GetName()
413 elif node.cls == 'ExtAttribute': 420 elif node.cls == 'ExtAttribute':
414 if node.name == 'nodoc': 421 if node.name == 'nodoc':
415 nodoc = bool(node.value) 422 nodoc = bool(node.value)
416 elif node.name == 'internal': 423 elif node.name == 'internal':
417 internal = bool(node.value) 424 internal = bool(node.value)
418 elif node.name == 'platforms': 425 elif node.name == 'platforms':
419 platforms = list(node.value) 426 platforms = list(node.value)
420 elif node.name == 'implemented_in': 427 elif node.name == 'implemented_in':
421 compiler_options = {'implemented_in': node.value} 428 compiler_options = {'implemented_in': node.value}
429 elif node.name == 'allow_custom_filename':
430 allow_custom_filename = True
422 else: 431 else:
423 continue 432 continue
424 else: 433 else:
425 sys.exit('Did not process %s %s' % (node.cls, node)) 434 sys.exit('Did not process %s %s' % (node.cls, node))
426 return namespaces 435 return namespaces
427 436
428 437
429 def Load(filename): 438 def Load(filename):
430 ''' 439 '''
431 Given the filename of an IDL file, parses it and returns an equivalent 440 Given the filename of an IDL file, parses it and returns an equivalent
(...skipping 14 matching lines...) Expand all
446 Dump a json serialization of parse result for the IDL files whose names 455 Dump a json serialization of parse result for the IDL files whose names
447 were passed in on the command line. 456 were passed in on the command line.
448 ''' 457 '''
449 for filename in sys.argv[1:]: 458 for filename in sys.argv[1:]:
450 schema = Load(filename) 459 schema = Load(filename)
451 print json.dumps(schema, indent=2) 460 print json.dumps(schema, indent=2)
452 461
453 462
454 if __name__ == '__main__': 463 if __name__ == '__main__':
455 Main() 464 Main()
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698