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

Side by Side Diff: tools/dom/scripts/htmldartgenerator.py

Issue 12087112: Can now correctly add documentation to constructors. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 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/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 the system to generate 6 """This module provides shared functionality for the system to generate
7 dart:html APIs from the IDL database.""" 7 dart:html APIs from the IDL database."""
8 8
9 import emitter 9 import emitter
10 from generator import AnalyzeOperation, ConstantOutputOrder, \ 10 from generator import AnalyzeOperation, ConstantOutputOrder, \
11 DartDomNameOfAttribute, FindMatchingAttribute, IsDartCollectionType, \ 11 DartDomNameOfAttribute, FindMatchingAttribute, IsDartCollectionType, \
12 IsPureInterface, TypeOrNothing 12 IsPureInterface, TypeOrNothing, GetAnnotationsAndComments, \
13 FormatAnnotationsAndComments
13 14
14 # Types that are accessible cross-frame in a limited fashion. 15 # Types that are accessible cross-frame in a limited fashion.
15 # In these cases, the base type (e.g., WindowBase) provides restricted access 16 # In these cases, the base type (e.g., WindowBase) provides restricted access
16 # while the subtype (e.g., Window) provides full access to the 17 # while the subtype (e.g., Window) provides full access to the
17 # corresponding objects if there are from the same frame. 18 # corresponding objects if there are from the same frame.
18 _secure_base_types = { 19 _secure_base_types = {
19 'Window': 'WindowBase', 20 'Window': 'WindowBase',
20 'Location': 'LocationBase', 21 'Location': 'LocationBase',
21 'History': 'HistoryBase', 22 'History': 'HistoryBase',
22 } 23 }
23 24
24 25
25 class HtmlDartGenerator(object): 26 class HtmlDartGenerator(object):
26 def __init__(self, interface, options): 27 def __init__(self, interface, options):
27 self._database = options.database 28 self._database = options.database
28 self._interface = interface 29 self._interface = interface
29 self._type_registry = options.type_registry 30 self._type_registry = options.type_registry
30 self._interface_type_info = self._type_registry.TypeInfo(self._interface.id) 31 self._interface_type_info = self._type_registry.TypeInfo(self._interface.id)
31 self._renamer = options.renamer 32 self._renamer = options.renamer
33 self._library_name = self._renamer.GetLibraryName(self._interface)
32 34
33 def EmitSupportCheck(self): 35 def EmitSupportCheck(self):
34 if self.HasSupportCheck(): 36 if self.HasSupportCheck():
35 support_check = self.GetSupportCheck() 37 support_check = self.GetSupportCheck()
36 self._members_emitter.Emit('\n' 38 self._members_emitter.Emit('\n'
37 ' /// Checks if this type is supported on the current platform.\n' 39 ' /// Checks if this type is supported on the current platform.\n'
38 ' static bool get supported => $SUPPORT_CHECK;\n', 40 ' static bool get supported => $SUPPORT_CHECK;\n',
39 SUPPORT_CHECK=support_check) 41 SUPPORT_CHECK=support_check)
40 42
41 def EmitEventGetter(self, events_class_name): 43 def EmitEventGetter(self, events_class_name):
(...skipping 267 matching lines...) Expand 10 before | Expand all | Expand 10 after
309 for constructor_info in constructors: 311 for constructor_info in constructors:
310 self._AddConstructor( 312 self._AddConstructor(
311 constructor_info, factory_name, factory_constructor_name) 313 constructor_info, factory_name, factory_constructor_name)
312 314
313 typed_array_type = None 315 typed_array_type = None
314 for interface in self._database.Hierarchy(self._interface): 316 for interface in self._database.Hierarchy(self._interface):
315 type_info = self._type_registry.TypeInfo(interface.id) 317 type_info = self._type_registry.TypeInfo(interface.id)
316 if type_info.is_typed_array(): 318 if type_info.is_typed_array():
317 typed_array_type = type_info.list_item_type() 319 typed_array_type = type_info.list_item_type()
318 break 320 break
321
322 annotations = FormatAnnotationsAndComments(
323 GetAnnotationsAndComments(self._library_name, self._interface.id,
324 self._interface.id), ' ')
325
326 fromListAnnotations = FormatAnnotationsAndComments(
327 GetAnnotationsAndComments(self._library_name, self._interface.id,
328 'fromList'), ' ')
329
330 fromBufferAnnotations = FormatAnnotationsAndComments(
331 GetAnnotationsAndComments(self._library_name, self._interface.id,
332 'fromBuffer'), ' ')
333
319 if typed_array_type: 334 if typed_array_type:
320 self._members_emitter.Emit( 335 self._members_emitter.Emit(
321 '\n' 336 '\n $(ANNOTATIONS)factory $CTOR(int length) =>\n'
322 ' factory $CTOR(int length) =>\n'
323 ' $FACTORY.create$(CTOR)(length);\n' 337 ' $FACTORY.create$(CTOR)(length);\n'
324 '\n' 338 '\n $(LIST_ANNOTATIONS)factory $CTOR.fromList(List<$TYPE> list) =>\n'
325 ' factory $CTOR.fromList(List<$TYPE> list) =>\n'
326 ' $FACTORY.create$(CTOR)_fromList(list);\n' 339 ' $FACTORY.create$(CTOR)_fromList(list);\n'
327 '\n' 340 '\n $(BUFFER_ANNOTATIONS)factory $CTOR.fromBuffer(ArrayBuffer buffer, '
328 ' factory $CTOR.fromBuffer(ArrayBuffer buffer, '
329 '[int byteOffset, int length]) => \n' 341 '[int byteOffset, int length]) => \n'
330 ' $FACTORY.create$(CTOR)_fromBuffer(buffer, byteOffset, length);\n' , 342 ' $FACTORY.create$(CTOR)_fromBuffer(buffer, byteOffset, length);\n' ,
331 CTOR=self._interface.id, 343 CTOR=self._interface.id,
344 ANNOTATIONS=annotations,
345 LIST_ANNOTATIONS=fromListAnnotations,
346 BUFFER_ANNOTATIONS=fromBufferAnnotations,
332 TYPE=self._DartType(typed_array_type), 347 TYPE=self._DartType(typed_array_type),
333 FACTORY=factory_name) 348 FACTORY=factory_name)
334 349
335 def _AddConstructor(self, 350 def _AddConstructor(self,
336 constructor_info, factory_name, factory_constructor_name): 351 constructor_info, factory_name, factory_constructor_name):
337 if self.GenerateCustomFactory(constructor_info): 352 if self.GenerateCustomFactory(constructor_info):
338 return 353 return
339 354
340 self._members_emitter.Emit('\n @DocsEditable'); 355 annotations = FormatAnnotationsAndComments(
356 GetAnnotationsAndComments(self._library_name, self._interface.id,
357 self._interface.id), ' ')
341 358
342 if not factory_constructor_name: 359 if not factory_constructor_name:
343 factory_constructor_name = '_create' 360 factory_constructor_name = '_create'
344 factory_parameters = constructor_info.ParametersAsArgumentList() 361 factory_parameters = constructor_info.ParametersAsArgumentList()
345 has_factory_provider = True 362 has_factory_provider = True
346 else: 363 else:
347 factory_parameters = ', '.join(constructor_info.factory_parameters) 364 factory_parameters = ', '.join(constructor_info.factory_parameters)
348 has_factory_provider = False 365 has_factory_provider = False
349 366
350 if constructor_info.pure_dart_constructor: 367 if constructor_info.pure_dart_constructor:
351 # TODO(antonm): use common dispatcher generation for this case as well. 368 # TODO(antonm): use common dispatcher generation for this case as well.
352 has_optional = any(param_info.is_optional 369 has_optional = any(param_info.is_optional
353 for param_info in constructor_info.param_infos) 370 for param_info in constructor_info.param_infos)
354 371
355 if not has_optional: 372 if not has_optional:
356 self._members_emitter.Emit( 373 self._members_emitter.Emit(
357 '\n' 374 '\n $(ANNOTATIONS)'
358 ' factory $CTOR($PARAMS) => ' 375 'factory $CTOR($PARAMS) => '
359 '$FACTORY.$CTOR_FACTORY_NAME($FACTORY_PARAMS);\n', 376 '$FACTORY.$CTOR_FACTORY_NAME($FACTORY_PARAMS);\n',
360 CTOR=constructor_info._ConstructorFullName(self._DartType), 377 CTOR=constructor_info._ConstructorFullName(self._DartType),
361 PARAMS=constructor_info.ParametersDeclaration(self._DartType), 378 PARAMS=constructor_info.ParametersDeclaration(self._DartType),
362 FACTORY=factory_name, 379 FACTORY=factory_name,
380 ANNOTATIONS=annotations,
363 CTOR_FACTORY_NAME=factory_constructor_name, 381 CTOR_FACTORY_NAME=factory_constructor_name,
364 FACTORY_PARAMS=factory_parameters) 382 FACTORY_PARAMS=factory_parameters)
365 else: 383 else:
366 if has_factory_provider: 384 if has_factory_provider:
367 dispatcher_emitter = self._members_emitter.Emit( 385 dispatcher_emitter = self._members_emitter.Emit(
368 '\n' 386 '\n $(ANNOTATIONS)'
369 ' factory $CTOR($PARAMS) {\n' 387 'factory $CTOR($PARAMS) {\n'
370 '$!DISPATCHER' 388 '$!DISPATCHER'
371 ' return $FACTORY._create($FACTORY_PARAMS);\n' 389 ' return $FACTORY._create($FACTORY_PARAMS);\n'
372 ' }\n', 390 ' }\n',
373 CTOR=constructor_info._ConstructorFullName(self._DartType), 391 CTOR=constructor_info._ConstructorFullName(self._DartType),
374 PARAMS=constructor_info.ParametersDeclaration(self._DartType), 392 PARAMS=constructor_info.ParametersDeclaration(self._DartType),
375 FACTORY=factory_name, 393 FACTORY=factory_name,
394 ANNOTATIONS=annotations,
376 FACTORY_PARAMS=constructor_info.ParametersAsArgumentList()) 395 FACTORY_PARAMS=constructor_info.ParametersAsArgumentList())
377 396
378 for index, param_info in enumerate(constructor_info.param_infos): 397 for index, param_info in enumerate(constructor_info.param_infos):
379 if param_info.is_optional: 398 if param_info.is_optional:
380 dispatcher_emitter.Emit( 399 dispatcher_emitter.Emit(
381 ' if (!?$OPT_PARAM_NAME) {\n' 400 ' if (!?$OPT_PARAM_NAME) {\n'
382 ' return $FACTORY._create($FACTORY_PARAMS);\n' 401 ' return $FACTORY._create($FACTORY_PARAMS);\n'
383 ' }\n', 402 ' }\n',
384 OPT_PARAM_NAME=param_info.name, 403 OPT_PARAM_NAME=param_info.name,
385 FACTORY=factory_name, 404 FACTORY=factory_name,
386 FACTORY_PARAMS=constructor_info.ParametersAsArgumentList(index)) 405 FACTORY_PARAMS=constructor_info.ParametersAsArgumentList(index))
387 else: 406 else:
388 inits = self._members_emitter.Emit( 407 inits = self._members_emitter.Emit(
389 '\n' 408 '\n $(ANNOTATIONS)'
390 ' factory $CONSTRUCTOR($PARAMS) {\n' 409 'factory $CONSTRUCTOR($PARAMS) {\n'
391 ' var e = $FACTORY.$CTOR_FACTORY_NAME($FACTORY_PARAMS);\n' 410 ' var e = $FACTORY.$CTOR_FACTORY_NAME($FACTORY_PARAMS);\n'
392 '$!INITS' 411 '$!INITS'
393 ' return e;\n' 412 ' return e;\n'
394 ' }\n', 413 ' }\n',
395 CONSTRUCTOR=constructor_info._ConstructorFullName(self._DartType), 414 CONSTRUCTOR=constructor_info._ConstructorFullName(self._DartType),
415 ANNOTATIONS=annotations,
396 FACTORY=factory_name, 416 FACTORY=factory_name,
397 CTOR_FACTORY_NAME=factory_constructor_name, 417 CTOR_FACTORY_NAME=factory_constructor_name,
398 PARAMS=constructor_info.ParametersDeclaration(self._DartType), 418 PARAMS=constructor_info.ParametersDeclaration(self._DartType),
399 FACTORY_PARAMS=factory_parameters) 419 FACTORY_PARAMS=factory_parameters)
400 420
401 for index, param_info in enumerate(constructor_info.param_infos): 421 for index, param_info in enumerate(constructor_info.param_infos):
402 if param_info.is_optional: 422 if param_info.is_optional:
403 inits.Emit(' if ($E != null) e.$E = $E;\n', E=param_info.name) 423 inits.Emit(' if ($E != null) e.$E = $E;\n', E=param_info.name)
404 else: 424 else:
405 def GenerateCall( 425 def GenerateCall(
406 stmts_emitter, call_emitter, 426 stmts_emitter, call_emitter,
407 version, signature_index, argument_count): 427 version, signature_index, argument_count):
408 name = emitter.Format('_create_$VERSION', VERSION=version) 428 name = emitter.Format('_create_$VERSION', VERSION=version)
409 call_emitter.Emit('$FACTORY.$NAME($FACTORY_PARAMS)', 429 call_emitter.Emit('$FACTORY.$NAME($FACTORY_PARAMS)',
410 FACTORY=factory_name, 430 FACTORY=factory_name,
411 NAME=name, 431 NAME=name,
412 FACTORY_PARAMS= \ 432 FACTORY_PARAMS= \
413 constructor_info.ParametersAsArgumentList(argument_count)) 433 constructor_info.ParametersAsArgumentList(argument_count))
414 self.EmitStaticFactoryOverload( 434 self.EmitStaticFactoryOverload(
415 constructor_info, name, 435 constructor_info, name,
416 constructor_info.idl_args[signature_index][:argument_count]) 436 constructor_info.idl_args[signature_index][:argument_count])
417 437
418 def IsOptional(signature_index, argument): 438 def IsOptional(signature_index, argument):
419 return self.IsConstructorArgumentOptional(argument) 439 return self.IsConstructorArgumentOptional(argument)
420 440
421 self._GenerateOverloadDispatcher( 441 self._GenerateOverloadDispatcher(
422 constructor_info.idl_args, 442 constructor_info.idl_args,
423 False, 443 False,
424 [info.name for info in constructor_info.param_infos], 444 [info.name for info in constructor_info.param_infos],
425 emitter.Format('factory $CTOR($PARAMS)', 445 emitter.Format('$(ANNOTATIONS)factory $CTOR($PARAMS)',
426 CTOR=constructor_info._ConstructorFullName(self._DartType), 446 CTOR=constructor_info._ConstructorFullName(self._DartType),
447 ANNOTATIONS=annotations,
427 PARAMS=constructor_info.ParametersDeclaration(self._DartType)), 448 PARAMS=constructor_info.ParametersDeclaration(self._DartType)),
428 GenerateCall, 449 GenerateCall,
429 IsOptional) 450 IsOptional)
430 451
431 def EmitHelpers(self, base_class): 452 def EmitHelpers(self, base_class):
432 pass 453 pass
433 454
434 def DeclareAttribute(self, attribute, type_name, attr_name, read_only): 455 def DeclareAttribute(self, attribute, type_name, attr_name, read_only):
435 """ Declares an attribute but does not include the code to invoke it. 456 """ Declares an attribute but does not include the code to invoke it.
436 """ 457 """
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after
529 walk(interface.parents) 550 walk(interface.parents)
530 else: 551 else:
531 walk(interface.parents[1:]) 552 walk(interface.parents[1:])
532 return result 553 return result
533 554
534 def _DartType(self, type_name): 555 def _DartType(self, type_name):
535 return self._type_registry.DartType(type_name) 556 return self._type_registry.DartType(type_name)
536 557
537 def _IsPrivate(self, name): 558 def _IsPrivate(self, name):
538 return name.startswith('_') 559 return name.startswith('_')
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698