| OLD | NEW |
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 library codegen.protocol; | 5 library codegen.protocol; |
| 6 | 6 |
| 7 import 'dart:convert'; | 7 import 'dart:convert'; |
| 8 | 8 |
| 9 import 'package:html5lib/dom.dart' as dom; | 9 import 'package:html5lib/dom.dart' as dom; |
| 10 | 10 |
| 11 import 'api.dart'; | 11 import 'api.dart'; |
| 12 import 'codegen_dart.dart'; | 12 import 'codegen_dart.dart'; |
| 13 import 'codegen_tools.dart'; | 13 import 'codegen_tools.dart'; |
| 14 import 'from_html.dart'; | 14 import 'from_html.dart'; |
| 15 import 'implied_types.dart'; | 15 import 'implied_types.dart'; |
| 16 import 'to_html.dart'; | 16 import 'to_html.dart'; |
| 17 | 17 |
| 18 /** | 18 /** |
| 19 * Translate spec_input.html into protocol_matchers.dart. |
| 20 */ |
| 21 main() { |
| 22 target.generate(); |
| 23 } |
| 24 |
| 25 /** |
| 19 * Special flags that need to be inserted into the declaration of the Element | 26 * Special flags that need to be inserted into the declaration of the Element |
| 20 * class. | 27 * class. |
| 21 */ | 28 */ |
| 22 const Map<String, String> specialElementFlags = const { | 29 const Map<String, String> specialElementFlags = const { |
| 23 'abstract': '0x01', | 30 'abstract': '0x01', |
| 24 'const': '0x02', | 31 'const': '0x02', |
| 25 'final': '0x04', | 32 'final': '0x04', |
| 26 'static': '0x08', | 33 'static': '0x08', |
| 27 'private': '0x10', | 34 'private': '0x10', |
| 28 'deprecated': '0x20' | 35 'deprecated': '0x20' |
| 29 }; | 36 }; |
| 30 | 37 |
| 31 final GeneratedFile target = new GeneratedFile( | 38 final GeneratedFile target = new GeneratedFile( |
| 32 '../../lib/src/generated_protocol.dart', () { | 39 '../../lib/src/generated_protocol.dart', () { |
| 33 CodegenProtocolVisitor visitor = new CodegenProtocolVisitor(readApi()); | 40 CodegenProtocolVisitor visitor = new CodegenProtocolVisitor(readApi()); |
| 34 return visitor.collectCode(visitor.visitApi); | 41 return visitor.collectCode(visitor.visitApi); |
| 35 }); | 42 }); |
| 36 | 43 |
| 37 /** | 44 /** |
| 38 * Translate spec_input.html into protocol_matchers.dart. | |
| 39 */ | |
| 40 main() { | |
| 41 target.generate(); | |
| 42 } | |
| 43 | |
| 44 /** | |
| 45 * Callback type used to represent arbitrary code generation. | 45 * Callback type used to represent arbitrary code generation. |
| 46 */ | 46 */ |
| 47 typedef void CodegenCallback(); | 47 typedef void CodegenCallback(); |
| 48 | 48 |
| 49 typedef String FromJsonSnippetCallback(String jsonPath, String json); | 49 typedef String FromJsonSnippetCallback(String jsonPath, String json); |
| 50 | 50 |
| 51 typedef String ToJsonSnippetCallback(String value); | 51 typedef String ToJsonSnippetCallback(String value); |
| 52 | 52 |
| 53 /** | 53 /** |
| 54 * Visitor which produces Dart code representing the API. | 54 * Visitor which produces Dart code representing the API. |
| (...skipping 321 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 376 } | 376 } |
| 377 writeln(' implements HasToJson {'); | 377 writeln(' implements HasToJson {'); |
| 378 indent(() { | 378 indent(() { |
| 379 if (emitSpecialStaticMembers(className)) { | 379 if (emitSpecialStaticMembers(className)) { |
| 380 writeln(); | 380 writeln(); |
| 381 } | 381 } |
| 382 for (TypeObjectField field in type.fields) { | 382 for (TypeObjectField field in type.fields) { |
| 383 if (field.value != null) { | 383 if (field.value != null) { |
| 384 continue; | 384 continue; |
| 385 } | 385 } |
| 386 writeln('${dartType(field.type)} _${field.name};'); |
| 387 writeln(); |
| 388 } |
| 389 for (TypeObjectField field in type.fields) { |
| 390 if (field.value != null) { |
| 391 continue; |
| 392 } |
| 386 docComment(toHtmlVisitor.collectHtml(() { | 393 docComment(toHtmlVisitor.collectHtml(() { |
| 387 toHtmlVisitor.translateHtml(field.html); | 394 toHtmlVisitor.translateHtml(field.html); |
| 388 })); | 395 })); |
| 389 writeln('${dartType(field.type)} ${field.name};'); | 396 writeln('${dartType(field.type)} get ${field.name} => _${field.name};'); |
| 397 writeln(); |
| 398 docComment(toHtmlVisitor.collectHtml(() { |
| 399 toHtmlVisitor.translateHtml(field.html); |
| 400 })); |
| 401 writeln('void set ${field.name}(${dartType(field.type)} value) {'); |
| 402 indent(() { |
| 403 if (!field.optional) { |
| 404 writeln('assert(value != null);'); |
| 405 } |
| 406 writeln('this._${field.name} = value;'); |
| 407 }); |
| 408 writeln('}'); |
| 390 writeln(); | 409 writeln(); |
| 391 } | 410 } |
| 392 emitObjectConstructor(type, className); | 411 emitObjectConstructor(type, className); |
| 393 writeln(); | 412 writeln(); |
| 394 emitObjectFromJsonConstructor(className, type, impliedType); | 413 emitObjectFromJsonConstructor(className, type, impliedType); |
| 395 writeln(); | 414 writeln(); |
| 396 if (emitConvenienceConstructor(className, impliedType)) { | 415 if (emitConvenienceConstructor(className, impliedType)) { |
| 397 writeln(); | 416 writeln(); |
| 398 } | 417 } |
| 399 if (emitSpecialConstructors(className)) { | 418 if (emitSpecialConstructors(className)) { |
| (...skipping 30 matching lines...) Expand all Loading... |
| 430 * Emit the constructor for an object class. | 449 * Emit the constructor for an object class. |
| 431 */ | 450 */ |
| 432 void emitObjectConstructor(TypeObject type, String className) { | 451 void emitObjectConstructor(TypeObject type, String className) { |
| 433 List<String> args = <String>[]; | 452 List<String> args = <String>[]; |
| 434 List<String> optionalArgs = <String>[]; | 453 List<String> optionalArgs = <String>[]; |
| 435 List<CodegenCallback> extraInitCode = <CodegenCallback>[]; | 454 List<CodegenCallback> extraInitCode = <CodegenCallback>[]; |
| 436 for (TypeObjectField field in type.fields) { | 455 for (TypeObjectField field in type.fields) { |
| 437 if (field.value != null) { | 456 if (field.value != null) { |
| 438 continue; | 457 continue; |
| 439 } | 458 } |
| 440 String arg = 'this.${field.name}'; | 459 String arg = '${dartType(field.type)} ${field.name}'; |
| 460 String setValueFromArg = 'this.${field.name} = ${field.name};'; |
| 441 if (isOptionalConstructorArg(className, field)) { | 461 if (isOptionalConstructorArg(className, field)) { |
| 442 optionalArgs.add(arg); | 462 optionalArgs.add(arg); |
| 443 if (!field.optional) { | 463 if (!field.optional) { |
| 444 // Optional constructor arg, but non-optional field. If no arg is | 464 // Optional constructor arg, but non-optional field. If no arg is |
| 445 // given, the constructor should populate with the empty list. | 465 // given, the constructor should populate with the empty list. |
| 446 TypeDecl fieldType = field.type; | 466 TypeDecl fieldType = field.type; |
| 447 if (fieldType is TypeList) { | 467 if (fieldType is TypeList) { |
| 448 extraInitCode.add(() { | 468 extraInitCode.add(() { |
| 449 writeln('if (${field.name} == null) {'); | 469 writeln('if (${field.name} == null) {'); |
| 450 indent(() { | 470 indent(() { |
| 451 writeln('${field.name} = <${dartType(fieldType.itemType)}>[];'); | 471 writeln( |
| 472 'this.${field.name} = <${dartType(fieldType.itemType)}>[];')
; |
| 473 }); |
| 474 writeln('} else {'); |
| 475 indent(() { |
| 476 writeln(setValueFromArg); |
| 452 }); | 477 }); |
| 453 writeln('}'); | 478 writeln('}'); |
| 454 }); | 479 }); |
| 455 } else { | 480 } else { |
| 456 throw new Exception( | 481 throw new Exception( |
| 457 "Don't know how to create default field value."); | 482 "Don't know how to create default field value."); |
| 458 } | 483 } |
| 484 } else { |
| 485 extraInitCode.add(() { |
| 486 writeln(setValueFromArg); |
| 487 }); |
| 459 } | 488 } |
| 460 } else { | 489 } else { |
| 461 args.add(arg); | 490 args.add(arg); |
| 491 extraInitCode.add(() { |
| 492 writeln(setValueFromArg); |
| 493 }); |
| 462 } | 494 } |
| 463 } | 495 } |
| 464 if (optionalArgs.isNotEmpty) { | 496 if (optionalArgs.isNotEmpty) { |
| 465 args.add('{${optionalArgs.join(', ')}}'); | 497 args.add('{${optionalArgs.join(', ')}}'); |
| 466 } | 498 } |
| 467 write('$className(${args.join(', ')})'); | 499 write('$className(${args.join(', ')})'); |
| 468 if (extraInitCode.isEmpty) { | 500 if (extraInitCode.isEmpty) { |
| 469 writeln(';'); | 501 writeln(';'); |
| 470 } else { | 502 } else { |
| 471 writeln(' {'); | 503 writeln(' {'); |
| (...skipping 719 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1191 | 1223 |
| 1192 @override | 1224 @override |
| 1193 String get asClosure => '($type value) => ${callback('value')}'; | 1225 String get asClosure => '($type value) => ${callback('value')}'; |
| 1194 | 1226 |
| 1195 @override | 1227 @override |
| 1196 bool get isIdentity => false; | 1228 bool get isIdentity => false; |
| 1197 | 1229 |
| 1198 @override | 1230 @override |
| 1199 String asSnippet(String value) => callback(value); | 1231 String asSnippet(String value) => callback(value); |
| 1200 } | 1232 } |
| OLD | NEW |