| OLD | NEW |
| 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 Loading... |
| 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') | |
| 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'))) | |
| 440 if idl_type == 'float': | |
| 441 return IdlLiteral(idl_type, float(node.GetProperty('VALUE'))) | |
| 442 if idl_type == 'boolean': | |
| 443 return IdlLiteral(idl_type, node.GetProperty('VALUE')) | |
| 444 if idl_type == 'NULL': | |
| 445 return IdlLiteralNull() | |
| 446 raise ValueError('Unrecognized default value type: %s' % idl_type) | |
| 447 | |
| 448 | |
| 449 ################################################################################ | |
| 450 # Operations | 397 # Operations |
| 451 ################################################################################ | 398 ################################################################################ |
| 452 | 399 |
| 453 class IdlOperation(TypedObject): | 400 class IdlOperation(TypedObject): |
| 454 def __init__(self, node=None): | 401 def __init__(self, node=None): |
| 455 self.arguments = [] | 402 self.arguments = [] |
| 456 self.extended_attributes = {} | 403 self.extended_attributes = {} |
| 457 self.specials = [] | 404 self.specials = [] |
| 458 | 405 |
| 459 if not node: | 406 if not node: |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 521 # Arguments | 468 # Arguments |
| 522 ################################################################################ | 469 ################################################################################ |
| 523 | 470 |
| 524 class IdlArgument(TypedObject): | 471 class IdlArgument(TypedObject): |
| 525 def __init__(self, node): | 472 def __init__(self, node): |
| 526 self.extended_attributes = {} | 473 self.extended_attributes = {} |
| 527 self.idl_type = None | 474 self.idl_type = None |
| 528 self.is_optional = node.GetProperty('OPTIONAL') # syntax: (optional T) | 475 self.is_optional = node.GetProperty('OPTIONAL') # syntax: (optional T) |
| 529 self.is_variadic = False # syntax: (T...) | 476 self.is_variadic = False # syntax: (T...) |
| 530 self.name = node.GetName() | 477 self.name = node.GetName() |
| 531 self.default_value = None | |
| 532 | 478 |
| 533 children = node.GetChildren() | 479 children = node.GetChildren() |
| 534 for child in children: | 480 for child in children: |
| 535 child_class = child.GetClass() | 481 child_class = child.GetClass() |
| 536 if child_class == 'Type': | 482 if child_class == 'Type': |
| 537 self.idl_type = type_node_to_type(child) | 483 self.idl_type = type_node_to_type(child) |
| 538 elif child_class == 'ExtAttributes': | 484 elif child_class == 'ExtAttributes': |
| 539 self.extended_attributes = ext_attributes_node_to_extended_attri
butes(child) | 485 self.extended_attributes = ext_attributes_node_to_extended_attri
butes(child) |
| 540 elif child_class == 'Argument': | 486 elif child_class == 'Argument': |
| 541 child_name = child.GetName() | 487 child_name = child.GetName() |
| 542 if child_name != '...': | 488 if child_name != '...': |
| 543 raise ValueError('Unrecognized Argument node; expected "..."
, got "%s"' % child_name) | 489 raise ValueError('Unrecognized Argument node; expected "..."
, got "%s"' % child_name) |
| 544 self.is_variadic = child.GetProperty('ELLIPSIS') or False | 490 self.is_variadic = child.GetProperty('ELLIPSIS') or False |
| 545 elif child_class == 'Default': | |
| 546 self.default_value = default_node_to_idl_literal(child) | |
| 547 else: | 491 else: |
| 548 raise ValueError('Unrecognized node class: %s' % child_class) | 492 raise ValueError('Unrecognized node class: %s' % child_class) |
| 549 | 493 |
| 550 | 494 |
| 551 def arguments_node_to_arguments(node): | 495 def arguments_node_to_arguments(node): |
| 552 # [Constructor] and [CustomConstructor] without arguments (the bare form) | 496 # [Constructor] and [CustomConstructor] without arguments (the bare form) |
| 553 # have None instead of an arguments node, but have the same meaning as using | 497 # have None instead of an arguments node, but have the same meaning as using |
| 554 # an empty argument list, [Constructor()], so special-case this. | 498 # an empty argument list, [Constructor()], so special-case this. |
| 555 # http://www.w3.org/TR/WebIDL/#Constructor | 499 # http://www.w3.org/TR/WebIDL/#Constructor |
| 556 if node is None: | 500 if node is None: |
| (...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 745 child_class = child.GetClass() | 689 child_class = child.GetClass() |
| 746 if child_class != 'Type': | 690 if child_class != 'Type': |
| 747 raise ValueError('Unrecognized node class: %s' % child_class) | 691 raise ValueError('Unrecognized node class: %s' % child_class) |
| 748 return type_node_to_type(child) | 692 return type_node_to_type(child) |
| 749 | 693 |
| 750 | 694 |
| 751 def union_type_node_to_idl_union_type(node, is_nullable=False): | 695 def union_type_node_to_idl_union_type(node, is_nullable=False): |
| 752 member_types = [type_node_to_type(member_type_node) | 696 member_types = [type_node_to_type(member_type_node) |
| 753 for member_type_node in node.GetChildren()] | 697 for member_type_node in node.GetChildren()] |
| 754 return IdlUnionType(member_types, is_nullable=is_nullable) | 698 return IdlUnionType(member_types, is_nullable=is_nullable) |
| OLD | NEW |