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

Side by Side Diff: Source/bindings/scripts/idl_definitions.py

Issue 312683005: IDL: Support optional argument default value syntax (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: rebased Created 6 years, 6 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
OLDNEW
1 # Copyright (C) 2013 Google Inc. All rights reserved. 1 # Copyright (C) 2013 Google Inc. All rights reserved.
2 # 2 #
3 # Redistribution and use in source and binary forms, with or without 3 # Redistribution and use in source and binary forms, with or without
4 # modification, are permitted provided that the following conditions are 4 # modification, are permitted provided that the following conditions are
5 # met: 5 # met:
6 # 6 #
7 # * Redistributions of source code must retain the above copyright 7 # * Redistributions of source code must retain the above copyright
8 # notice, this list of conditions and the following disclaimer. 8 # notice, this list of conditions and the following disclaimer.
9 # * Redistributions in binary form must reproduce the above 9 # * Redistributions in binary form must reproduce the above
10 # copyright notice, this list of conditions and the following disclaimer 10 # copyright notice, this list of conditions and the following disclaimer
(...skipping 376 matching lines...) Expand 10 before | Expand all | Expand 10 after
387 self.value = value_node.GetName() 387 self.value = value_node.GetName()
388 388
389 if num_children == 3: 389 if num_children == 3:
390 ext_attributes_node = children[2] 390 ext_attributes_node = children[2]
391 self.extended_attributes = ext_attributes_node_to_extended_attribute s(ext_attributes_node) 391 self.extended_attributes = ext_attributes_node_to_extended_attribute s(ext_attributes_node)
392 else: 392 else:
393 self.extended_attributes = {} 393 self.extended_attributes = {}
394 394
395 395
396 ################################################################################ 396 ################################################################################
397 # Literals
398 ################################################################################
399
400 class IdlLiteral(object):
401 def __init__(self, idl_type, value):
402 self.idl_type = idl_type
403 self.value = value
404 self.is_null = False
405
406 def __str__(self):
407 if self.idl_type == 'DOMString':
408 return 'String("%s")' % self.value
409 if self.idl_type == 'integer':
410 return '%d' % self.value
411 if self.idl_type == 'float':
412 return '%g' % self.value
413 if self.idl_type == 'boolean':
414 return 'true' if self.value else 'false'
415 raise ValueError('Unsupported literal type: %s' % self.idl_type)
416
417
418 class IdlLiteralNull(IdlLiteral):
419 def __init__(self):
420 self.idl_type = 'NULL'
421 self.value = None
422 self.is_null = True
423
424 def __str__(self):
425 return 'nullptr'
426
427
428 def default_node_to_idl_literal(node):
429 # FIXME: This code is unnecessarily complicated due to the rather
430 # inconsistent way the upstream IDL parser outputs default values.
431 # http://crbug.com/374178
432 idl_type = node.GetProperty('TYPE')
433 if idl_type == 'DOMString':
434 value = node.GetProperty('NAME')
haraken 2014/06/13 13:46:52 Shouldn't this be node.GetProperty('VALUE') ?
Jens Widell 2014/06/13 14:14:52 The use of NAME/VALUE here is entirely dictated by
435 if '"' in value or '\\' in value:
436 raise ValueError('Unsupported string value: %r' % value)
437 return IdlLiteral(idl_type, value)
438 if idl_type == 'integer':
439 return IdlLiteral(idl_type, int(node.GetProperty('NAME')))
haraken 2014/06/13 13:46:52 Shouldn't this be int(node.GetProperty('VALUE') ?
440 elif idl_type == 'float':
Nils Barth (inactive) 2014/06/16 09:18:17 No else after return: these can all be |if|
441 return IdlLiteral(idl_type, float(node.GetProperty('VALUE')))
442 elif idl_type == 'boolean':
443 return IdlLiteral(idl_type, node.GetProperty('VALUE'))
444 elif idl_type == 'NULL':
445 return IdlLiteralNull()
446 raise ValueError('Unrecognized default value type: %s' % idl_type)
447
448
449 ################################################################################
397 # Operations 450 # Operations
398 ################################################################################ 451 ################################################################################
399 452
400 class IdlOperation(TypedObject): 453 class IdlOperation(TypedObject):
401 def __init__(self, node=None): 454 def __init__(self, node=None):
402 self.arguments = [] 455 self.arguments = []
403 self.extended_attributes = {} 456 self.extended_attributes = {}
404 self.specials = [] 457 self.specials = []
405 458
406 if not node: 459 if not node:
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
468 # Arguments 521 # Arguments
469 ################################################################################ 522 ################################################################################
470 523
471 class IdlArgument(TypedObject): 524 class IdlArgument(TypedObject):
472 def __init__(self, node): 525 def __init__(self, node):
473 self.extended_attributes = {} 526 self.extended_attributes = {}
474 self.idl_type = None 527 self.idl_type = None
475 self.is_optional = node.GetProperty('OPTIONAL') # syntax: (optional T) 528 self.is_optional = node.GetProperty('OPTIONAL') # syntax: (optional T)
476 self.is_variadic = False # syntax: (T...) 529 self.is_variadic = False # syntax: (T...)
477 self.name = node.GetName() 530 self.name = node.GetName()
531 self.default_value = None
478 532
479 children = node.GetChildren() 533 children = node.GetChildren()
480 for child in children: 534 for child in children:
481 child_class = child.GetClass() 535 child_class = child.GetClass()
482 if child_class == 'Type': 536 if child_class == 'Type':
483 self.idl_type = type_node_to_type(child) 537 self.idl_type = type_node_to_type(child)
484 elif child_class == 'ExtAttributes': 538 elif child_class == 'ExtAttributes':
485 self.extended_attributes = ext_attributes_node_to_extended_attri butes(child) 539 self.extended_attributes = ext_attributes_node_to_extended_attri butes(child)
486 elif child_class == 'Argument': 540 elif child_class == 'Argument':
487 child_name = child.GetName() 541 child_name = child.GetName()
488 if child_name != '...': 542 if child_name != '...':
489 raise ValueError('Unrecognized Argument node; expected "..." , got "%s"' % child_name) 543 raise ValueError('Unrecognized Argument node; expected "..." , got "%s"' % child_name)
490 self.is_variadic = child.GetProperty('ELLIPSIS') or False 544 self.is_variadic = child.GetProperty('ELLIPSIS') or False
545 elif child_class == 'Default':
546 self.default_value = default_node_to_idl_literal(child)
491 else: 547 else:
492 raise ValueError('Unrecognized node class: %s' % child_class) 548 raise ValueError('Unrecognized node class: %s' % child_class)
493 549
494 550
495 def arguments_node_to_arguments(node): 551 def arguments_node_to_arguments(node):
496 # [Constructor] and [CustomConstructor] without arguments (the bare form) 552 # [Constructor] and [CustomConstructor] without arguments (the bare form)
497 # have None instead of an arguments node, but have the same meaning as using 553 # have None instead of an arguments node, but have the same meaning as using
498 # an empty argument list, [Constructor()], so special-case this. 554 # an empty argument list, [Constructor()], so special-case this.
499 # http://www.w3.org/TR/WebIDL/#Constructor 555 # http://www.w3.org/TR/WebIDL/#Constructor
500 if node is None: 556 if node is None:
(...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after
689 child_class = child.GetClass() 745 child_class = child.GetClass()
690 if child_class != 'Type': 746 if child_class != 'Type':
691 raise ValueError('Unrecognized node class: %s' % child_class) 747 raise ValueError('Unrecognized node class: %s' % child_class)
692 return type_node_to_type(child) 748 return type_node_to_type(child)
693 749
694 750
695 def union_type_node_to_idl_union_type(node, is_nullable=False): 751 def union_type_node_to_idl_union_type(node, is_nullable=False):
696 member_types = [type_node_to_type(member_type_node) 752 member_types = [type_node_to_type(member_type_node)
697 for member_type_node in node.GetChildren()] 753 for member_type_node in node.GetChildren()]
698 return IdlUnionType(member_types, is_nullable=is_nullable) 754 return IdlUnionType(member_types, is_nullable=is_nullable)
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698