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

Side by Side Diff: lib/html/scripts/generator.py

Issue 11026039: Refactor idl to dart renaming logic for interfaces. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 8 years, 2 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
« no previous file with comments | « lib/html/scripts/dartgenerator.py ('k') | lib/html/scripts/systemhtml.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 #!/usr/bin/python 1 #!/usr/bin/python
2 # Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 2 # Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
3 # for details. All rights reserved. Use of this source code is governed by a 3 # for details. All rights reserved. Use of this source code is governed by a
4 # BSD-style license that can be found in the LICENSE file. 4 # BSD-style license that can be found in the LICENSE file.
5 5
6 """This module provides shared functionality for systems to generate 6 """This module provides shared functionality for systems to generate
7 Dart APIs from the IDL database.""" 7 Dart APIs from the IDL database."""
8 8
9 import copy 9 import copy
10 import re 10 import re
(...skipping 327 matching lines...) Expand 10 before | Expand all | Expand 10 after
338 overloads: A list of IDL operation overloads with the same name. 338 overloads: A list of IDL operation overloads with the same name.
339 name: A string, the simple name of the operation. 339 name: A string, the simple name of the operation.
340 constructor_name: A string, the name of the constructor iff the constructor 340 constructor_name: A string, the name of the constructor iff the constructor
341 is named, e.g. 'fromList' in Int8Array.fromList(list). 341 is named, e.g. 'fromList' in Int8Array.fromList(list).
342 type_name: A string, the name of the return type of the operation. 342 type_name: A string, the name of the return type of the operation.
343 param_infos: A list of ParamInfo. 343 param_infos: A list of ParamInfo.
344 """ 344 """
345 345
346 def ParametersInterfaceDeclaration(self, rename_type): 346 def ParametersInterfaceDeclaration(self, rename_type):
347 """Returns a formatted string declaring the parameters for the interface.""" 347 """Returns a formatted string declaring the parameters for the interface."""
348 return self._FormatParams( 348 def type_function(param):
349 self.param_infos, None, 349 if param.dart_type == 'Dynamic':
Anton Muhin 2012/10/04 16:41:04 nit cannot rename_type handle the case of Dynamic
podivilov 2012/10/04 17:44:10 Using 'Dynamic' as dart_type here is a hack, so I
350 lambda param: TypeOrNothing(rename_type(param.dart_type), param.type_id) ) 350 return TypeOrNothing(param.dart_type, param.type_id)
351 return TypeOrNothing(rename_type(param.dart_type), param.type_id)
352 return self._FormatParams(self.param_infos, None, type_function)
351 353
352 def ParametersImplementationDeclaration(self, rename_type): 354 def ParametersImplementationDeclaration(self, rename_type):
353 """Returns a formatted string declaring the parameters for the 355 """Returns a formatted string declaring the parameters for the
354 implementation. 356 implementation.
355 357
356 Args: 358 Args:
357 rename_type: A function that allows the types to be renamed. 359 rename_type: A function that allows the types to be renamed.
358 The function is applied to the parameter's dart_type. 360 The function is applied to the parameter's dart_type.
359 """ 361 """
360 return self._FormatParams( 362 def type_function(param):
361 self.param_infos, 'null', 363 if param.dart_type == 'Dynamic':
Anton Muhin 2012/10/04 16:41:04 ditto, and maybe introduce a helper function
podivilov 2012/10/04 17:44:10 ditto
362 lambda param: TypeOrNothing(rename_type(param.dart_type))) 364 return TypeOrNothing(param.dart_type)
365 return TypeOrNothing(rename_type(param.dart_type))
366 return self._FormatParams(self.param_infos, 'null', type_function)
363 367
364 def ParametersAsArgumentList(self, parameter_count = None): 368 def ParametersAsArgumentList(self, parameter_count = None):
365 """Returns a string of the parameter names suitable for passing the 369 """Returns a string of the parameter names suitable for passing the
366 parameters as arguments. 370 parameters as arguments.
367 """ 371 """
368 if parameter_count is None: 372 if parameter_count is None:
369 parameter_count = len(self.param_infos) 373 parameter_count = len(self.param_infos)
370 return ', '.join(map( 374 return ', '.join(map(
371 lambda param_info: param_info.name, 375 lambda param_info: param_info.name,
372 self.param_infos[:parameter_count])) 376 self.param_infos[:parameter_count]))
(...skipping 19 matching lines...) Expand all
392 argtexts = map(FormatParam, required) 396 argtexts = map(FormatParam, required)
393 if optional: 397 if optional:
394 argtexts.append('[' + ', '.join(map(FormatParam, optional)) + ']') 398 argtexts.append('[' + ', '.join(map(FormatParam, optional)) + ']')
395 return ', '.join(argtexts) 399 return ', '.join(argtexts)
396 400
397 def IsStatic(self): 401 def IsStatic(self):
398 is_static = self.overloads[0].is_static 402 is_static = self.overloads[0].is_static
399 assert any([is_static == o.is_static for o in self.overloads]) 403 assert any([is_static == o.is_static for o in self.overloads])
400 return is_static 404 return is_static
401 405
402 def _ConstructorFullName(self): 406 def _ConstructorFullName(self, rename_type):
403 if self.constructor_name: 407 if self.constructor_name:
404 return self.type_name + '.' + self.constructor_name 408 return rename_type(self.type_name) + '.' + self.constructor_name
405 else: 409 else:
406 return self.type_name 410 return rename_type(self.type_name)
407 411
408 def ConstructorFactoryName(self, rename_type): 412 def ConstructorFactoryName(self, rename_type):
409 return 'create' + rename_type(self._ConstructorFullName()).replace('.', '_') 413 return 'create' + self._ConstructorFullName(rename_type).replace('.', '_')
410 414
411 def GenerateFactoryInvocation(self, rename_type, emitter, factory_provider): 415 def GenerateFactoryInvocation(self, rename_type, emitter, factory_provider):
412 has_optional = any(param_info.is_optional 416 has_optional = any(param_info.is_optional
413 for param_info in self.param_infos) 417 for param_info in self.param_infos)
414 418
415 factory_name = self.ConstructorFactoryName(rename_type) 419 factory_name = self.ConstructorFactoryName(rename_type)
416 if not has_optional: 420 if not has_optional:
417 emitter.Emit( 421 emitter.Emit(
418 '\n' 422 '\n'
419 ' factory $CTOR($PARAMS) => ' 423 ' factory $CTOR($PARAMS) => '
420 '$FACTORY.$CTOR_FACTORY_NAME($FACTORY_PARAMS);\n', 424 '$FACTORY.$CTOR_FACTORY_NAME($FACTORY_PARAMS);\n',
421 CTOR=rename_type(self._ConstructorFullName()), 425 CTOR=self._ConstructorFullName(rename_type),
422 PARAMS=self.ParametersInterfaceDeclaration(rename_type), 426 PARAMS=self.ParametersInterfaceDeclaration(rename_type),
423 FACTORY=factory_provider, 427 FACTORY=factory_provider,
424 CTOR_FACTORY_NAME=factory_name, 428 CTOR_FACTORY_NAME=factory_name,
425 FACTORY_PARAMS=self.ParametersAsArgumentList()) 429 FACTORY_PARAMS=self.ParametersAsArgumentList())
426 return 430 return
427 431
428 dispatcher_emitter = emitter.Emit( 432 dispatcher_emitter = emitter.Emit(
429 '\n' 433 '\n'
430 ' factory $CTOR($PARAMS) {\n' 434 ' factory $CTOR($PARAMS) {\n'
431 '$!DISPATCHER' 435 '$!DISPATCHER'
432 ' return $FACTORY.$CTOR_FACTORY_NAME($FACTORY_PARAMS);\n' 436 ' return $FACTORY.$CTOR_FACTORY_NAME($FACTORY_PARAMS);\n'
433 ' }\n', 437 ' }\n',
434 CTOR=rename_type(self._ConstructorFullName()), 438 CTOR=self._ConstructorFullName(rename_type),
435 PARAMS=self.ParametersInterfaceDeclaration(rename_type), 439 PARAMS=self.ParametersInterfaceDeclaration(rename_type),
436 FACTORY=factory_provider, 440 FACTORY=factory_provider,
437 CTOR_FACTORY_NAME=factory_name, 441 CTOR_FACTORY_NAME=factory_name,
438 FACTORY_PARAMS=self.ParametersAsArgumentList()) 442 FACTORY_PARAMS=self.ParametersAsArgumentList())
439 443
440 # If we have optional parameters, check to see if they are set 444 # If we have optional parameters, check to see if they are set
441 # and call the appropriate factory method. 445 # and call the appropriate factory method.
442 def EmitOptionalParameterInvocation(index): 446 def EmitOptionalParameterInvocation(index):
443 dispatcher_emitter.Emit( 447 dispatcher_emitter.Emit(
444 ' if (!?$OPT_PARAM_NAME) {\n' 448 ' if (!?$OPT_PARAM_NAME) {\n'
(...skipping 229 matching lines...) Expand 10 before | Expand all | Expand 10 after
674 return ['"Dart%s.h"' % include for include in includes] 678 return ['"Dart%s.h"' % include for include in includes]
675 679
676 def to_dart_conversion(self, value, interface_name=None, attributes=None): 680 def to_dart_conversion(self, value, interface_name=None, attributes=None):
677 return 'Dart%s::toDart(%s)' % (self._idl_type, value) 681 return 'Dart%s::toDart(%s)' % (self._idl_type, value)
678 682
679 def custom_to_dart(self): 683 def custom_to_dart(self):
680 return self._data.custom_to_dart 684 return self._data.custom_to_dart
681 685
682 686
683 class InterfaceIDLTypeInfo(IDLTypeInfo): 687 class InterfaceIDLTypeInfo(IDLTypeInfo):
684 def __init__(self, idl_type, data): 688 def __init__(self, idl_type, data, dart_interface_name):
685 super(InterfaceIDLTypeInfo, self).__init__(idl_type, data) 689 super(InterfaceIDLTypeInfo, self).__init__(idl_type, data)
690 self._dart_interface_name = dart_interface_name
691
692 def dart_type(self):
693 return self._data.dart_type or self._dart_interface_name
686 694
687 695
688 class SequenceIDLTypeInfo(IDLTypeInfo): 696 class SequenceIDLTypeInfo(IDLTypeInfo):
689 def __init__(self, idl_type, data, item_info): 697 def __init__(self, idl_type, data, item_info):
690 super(SequenceIDLTypeInfo, self).__init__(idl_type, data) 698 super(SequenceIDLTypeInfo, self).__init__(idl_type, data)
691 self._item_info = item_info 699 self._item_info = item_info
692 700
693 def dart_type(self): 701 def dart_type(self):
694 return 'List<%s>' % self._item_info.dart_type() 702 return 'List<%s>' % self._item_info.dart_type()
695 703
(...skipping 265 matching lines...) Expand 10 before | Expand all | Expand 10 after
961 self._database = database 969 self._database = database
962 self._renamer = renamer 970 self._renamer = renamer
963 self._cache = {} 971 self._cache = {}
964 972
965 def TypeInfo(self, type_name): 973 def TypeInfo(self, type_name):
966 if not type_name in self._cache: 974 if not type_name in self._cache:
967 self._cache[type_name] = self._TypeInfo(type_name) 975 self._cache[type_name] = self._TypeInfo(type_name)
968 return self._cache[type_name] 976 return self._cache[type_name]
969 977
970 def DartType(self, type_name): 978 def DartType(self, type_name):
971 dart_type = self.TypeInfo(type_name).dart_type() 979 return self.TypeInfo(type_name).dart_type()
972 if self._database.HasInterface(dart_type):
973 interface = self._database.GetInterface(dart_type)
974 if self._renamer:
975 return self._renamer.RenameInterface(interface)
976 else:
977 return interface.ext_attrs.get('InterfaceName', interface.id)
978 return dart_type
979 980
980 def _TypeInfo(self, type_name): 981 def _TypeInfo(self, type_name):
981 match = re.match(r'(?:sequence<(\w+)>|(\w+)\[\])$', type_name) 982 match = re.match(r'(?:sequence<(\w+)>|(\w+)\[\])$', type_name)
982 if match: 983 if match:
983 if type_name == 'DOMString[]': 984 if type_name == 'DOMString[]':
984 return DOMStringArrayTypeInfo(TypeData('Sequence'), self.TypeInfo('DOMSt ring')) 985 return DOMStringArrayTypeInfo(TypeData('Sequence'), self.TypeInfo('DOMSt ring'))
985 item_info = self.TypeInfo(match.group(1) or match.group(2)) 986 item_info = self.TypeInfo(match.group(1) or match.group(2))
986 return SequenceIDLTypeInfo(type_name, TypeData('Sequence'), item_info) 987 return SequenceIDLTypeInfo(type_name, TypeData('Sequence'), item_info)
988
987 if not type_name in _idl_type_registry: 989 if not type_name in _idl_type_registry:
988 return InterfaceIDLTypeInfo(type_name, TypeData('Interface')) 990 interface = self._database.GetInterface(type_name)
991 return InterfaceIDLTypeInfo(
992 type_name,
993 TypeData('Interface'),
994 self._renamer.RenameInterface(interface))
995
989 type_data = _idl_type_registry.get(type_name) 996 type_data = _idl_type_registry.get(type_name)
997 if type_data.clazz == 'Interface':
998 if self._database.HasInterface(type_name):
999 dart_interface_name = self._renamer.RenameInterface(
1000 self._database.GetInterface(type_name))
1001 else:
1002 dart_interface_name = type_name
1003 return InterfaceIDLTypeInfo(type_name, type_data, dart_interface_name)
1004
990 class_name = '%sIDLTypeInfo' % type_data.clazz 1005 class_name = '%sIDLTypeInfo' % type_data.clazz
991 return globals()[class_name](type_name, type_data) 1006 return globals()[class_name](type_name, type_data)
OLDNEW
« no previous file with comments | « lib/html/scripts/dartgenerator.py ('k') | lib/html/scripts/systemhtml.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698