| 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 import 'dart:convert'; | 5 import 'dart:convert'; |
| 6 | 6 |
| 7 import 'package:analyzer/src/codegen/tools.dart'; | 7 import 'package:analyzer/src/codegen/tools.dart'; |
| 8 import 'package:front_end/src/codegen/tools.dart'; | 8 import 'package:front_end/src/codegen/tools.dart'; |
| 9 import 'package:html/dom.dart' as dom; | 9 import 'package:html/dom.dart' as dom; |
| 10 import 'package:path/path.dart' as path; | 10 import 'package:path/path.dart' as path; |
| (...skipping 10 matching lines...) Expand all Loading... |
| 21 */ | 21 */ |
| 22 const Map<String, String> specialElementFlags = const { | 22 const Map<String, String> specialElementFlags = const { |
| 23 'abstract': '0x01', | 23 'abstract': '0x01', |
| 24 'const': '0x02', | 24 'const': '0x02', |
| 25 'final': '0x04', | 25 'final': '0x04', |
| 26 'static': '0x08', | 26 'static': '0x08', |
| 27 'private': '0x10', | 27 'private': '0x10', |
| 28 'deprecated': '0x20' | 28 'deprecated': '0x20' |
| 29 }; | 29 }; |
| 30 | 30 |
| 31 final GeneratedFile target = | 31 GeneratedFile target(bool responseRequiresRequestTime) { |
| 32 new GeneratedFile('lib/protocol/protocol_generated.dart', (String pkgPath) { | 32 return new GeneratedFile('lib/protocol/protocol_generated.dart', |
| 33 CodegenProtocolVisitor visitor = | 33 (String pkgPath) { |
| 34 new CodegenProtocolVisitor(path.basename(pkgPath), readApi(pkgPath)); | 34 CodegenProtocolVisitor visitor = new CodegenProtocolVisitor( |
| 35 return visitor.collectCode(visitor.visitApi); | 35 path.basename(pkgPath), responseRequiresRequestTime, readApi(pkgPath)); |
| 36 }); | 36 return visitor.collectCode(visitor.visitApi); |
| 37 }); |
| 38 } |
| 37 | 39 |
| 38 /** | 40 /** |
| 39 * Callback type used to represent arbitrary code generation. | 41 * Callback type used to represent arbitrary code generation. |
| 40 */ | 42 */ |
| 41 typedef void CodegenCallback(); | 43 typedef void CodegenCallback(); |
| 42 | 44 |
| 43 typedef String FromJsonSnippetCallback(String jsonPath, String json); | 45 typedef String FromJsonSnippetCallback(String jsonPath, String json); |
| 44 | 46 |
| 45 typedef String ToJsonSnippetCallback(String value); | 47 typedef String ToJsonSnippetCallback(String value); |
| 46 | 48 |
| (...skipping 19 matching lines...) Expand all Loading... |
| 66 */ | 68 */ |
| 67 static const String disclaimer = | 69 static const String disclaimer = |
| 68 'Clients may not extend, implement or mix-in this class.'; | 70 'Clients may not extend, implement or mix-in this class.'; |
| 69 | 71 |
| 70 /** | 72 /** |
| 71 * The name of the package into which code is being generated. | 73 * The name of the package into which code is being generated. |
| 72 */ | 74 */ |
| 73 final String packageName; | 75 final String packageName; |
| 74 | 76 |
| 75 /** | 77 /** |
| 78 * A flag indicating whether the class [Response] requires a `requestTime` |
| 79 * parameter. |
| 80 */ |
| 81 final bool responseRequiresRequestTime; |
| 82 |
| 83 /** |
| 76 * Visitor used to produce doc comments. | 84 * Visitor used to produce doc comments. |
| 77 */ | 85 */ |
| 78 final ToHtmlVisitor toHtmlVisitor; | 86 final ToHtmlVisitor toHtmlVisitor; |
| 79 | 87 |
| 80 /** | 88 /** |
| 81 * Types implied by the API. This includes types explicitly named in the | 89 * Types implied by the API. This includes types explicitly named in the |
| 82 * API as well as those implied by the definitions of requests, responses, | 90 * API as well as those implied by the definitions of requests, responses, |
| 83 * notifications, etc. | 91 * notifications, etc. |
| 84 */ | 92 */ |
| 85 final Map<String, ImpliedType> impliedTypes; | 93 final Map<String, ImpliedType> impliedTypes; |
| 86 | 94 |
| 87 CodegenProtocolVisitor(this.packageName, Api api) | 95 CodegenProtocolVisitor( |
| 96 this.packageName, this.responseRequiresRequestTime, Api api) |
| 88 : toHtmlVisitor = new ToHtmlVisitor(api), | 97 : toHtmlVisitor = new ToHtmlVisitor(api), |
| 89 impliedTypes = computeImpliedTypes(api), | 98 impliedTypes = computeImpliedTypes(api), |
| 90 super(api) { | 99 super(api) { |
| 91 codeGeneratorSettings.commentLineLength = 79; | 100 codeGeneratorSettings.commentLineLength = 79; |
| 92 codeGeneratorSettings.languageName = 'dart'; | 101 codeGeneratorSettings.languageName = 'dart'; |
| 93 } | 102 } |
| 94 | 103 |
| 95 /** | 104 /** |
| 96 * Compute the code necessary to compare two objects for equality. | 105 * Compute the code necessary to compare two objects for equality. |
| 97 */ | 106 */ |
| (...skipping 850 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 948 return false; | 957 return false; |
| 949 } | 958 } |
| 950 | 959 |
| 951 /** | 960 /** |
| 952 * Emit the toResponse() code for a class, if appropriate. Returns true if | 961 * Emit the toResponse() code for a class, if appropriate. Returns true if |
| 953 * code was emitted. | 962 * code was emitted. |
| 954 */ | 963 */ |
| 955 bool emitToResponseMember(ImpliedType impliedType) { | 964 bool emitToResponseMember(ImpliedType impliedType) { |
| 956 if (impliedType.kind == 'requestResult') { | 965 if (impliedType.kind == 'requestResult') { |
| 957 writeln('@override'); | 966 writeln('@override'); |
| 958 writeln('Response toResponse(String id) {'); | 967 if (responseRequiresRequestTime) { |
| 968 writeln('Response toResponse(String id, int requestTime) {'); |
| 969 } else { |
| 970 writeln('Response toResponse(String id) {'); |
| 971 } |
| 959 indent(() { | 972 indent(() { |
| 960 String jsonPart = impliedType.type != null ? 'toJson()' : 'null'; | 973 String jsonPart = impliedType.type != null ? 'toJson()' : 'null'; |
| 961 writeln('return new Response(id, result: $jsonPart);'); | 974 if (responseRequiresRequestTime) { |
| 975 writeln('return new Response(id, requestTime, result: $jsonPart);'); |
| 976 } else { |
| 977 writeln('return new Response(id, result: $jsonPart);'); |
| 978 } |
| 962 }); | 979 }); |
| 963 writeln('}'); | 980 writeln('}'); |
| 964 return true; | 981 return true; |
| 965 } | 982 } |
| 966 return false; | 983 return false; |
| 967 } | 984 } |
| 968 | 985 |
| 969 /** | 986 /** |
| 970 * Compute the code necessary to translate [type] from JSON. | 987 * Compute the code necessary to translate [type] from JSON. |
| 971 */ | 988 */ |
| (...skipping 328 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1300 | 1317 |
| 1301 @override | 1318 @override |
| 1302 String get asClosure => '($type value) => ${callback('value')}'; | 1319 String get asClosure => '($type value) => ${callback('value')}'; |
| 1303 | 1320 |
| 1304 @override | 1321 @override |
| 1305 bool get isIdentity => false; | 1322 bool get isIdentity => false; |
| 1306 | 1323 |
| 1307 @override | 1324 @override |
| 1308 String asSnippet(String value) => callback(value); | 1325 String asSnippet(String value) => callback(value); |
| 1309 } | 1326 } |
| OLD | NEW |