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: pkg/analyzer_plugin/tool/spec/codegen_dart_protocol.dart

Issue 2880443006: Generate common types into a separate library (Closed)
Patch Set: add missed files Created 3 years, 7 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
OLDNEW
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 101 matching lines...) Expand 10 before | Expand all | Expand 10 after
112 String subComparison = 112 String subComparison =
113 compareEqualsCode(resolvedType.valueType, 'a', 'b'); 113 compareEqualsCode(resolvedType.valueType, 'a', 'b');
114 String closure = '($valueTypeName a, $valueTypeName b) => $subComparison'; 114 String closure = '($valueTypeName a, $valueTypeName b) => $subComparison';
115 return 'mapEqual($thisVar, $otherVar, $closure)'; 115 return 'mapEqual($thisVar, $otherVar, $closure)';
116 } 116 }
117 throw new Exception( 117 throw new Exception(
118 "Don't know how to compare for equality: $resolvedType"); 118 "Don't know how to compare for equality: $resolvedType");
119 } 119 }
120 120
121 /** 121 /**
122 * Translate each type implied by the API to a class. 122 * Translate each of the given [types] implied by the API to a class.
123 */ 123 */
124 void emitClasses() { 124 void emitClasses(List<ImpliedType> types) {
125 List<ImpliedType> types = impliedTypes.values.toList();
126 types.sort((first, second) =>
127 capitalize(first.camelName).compareTo(capitalize(second.camelName)));
128 for (ImpliedType impliedType in types) { 125 for (ImpliedType impliedType in types) {
129 TypeDecl type = impliedType.type; 126 TypeDecl type = impliedType.type;
130 String dartTypeName = capitalize(impliedType.camelName); 127 String dartTypeName = capitalize(impliedType.camelName);
131 if (type == null) { 128 if (type == null) {
132 writeln(); 129 writeln();
133 emitEmptyObjectClass(dartTypeName, impliedType); 130 emitEmptyObjectClass(dartTypeName, impliedType);
134 } else if (type is TypeObject) { 131 } else if (type is TypeObject) {
135 writeln(); 132 writeln();
136 emitObjectClass(dartTypeName, type, impliedType); 133 emitObjectClass(dartTypeName, type, impliedType);
137 } else if (type is TypeEnum) { 134 } else if (type is TypeEnum) {
(...skipping 250 matching lines...) Expand 10 before | Expand all | Expand 10 after
388 }); 385 });
389 writeln('}'); 386 writeln('}');
390 String humanReadableNameString = 387 String humanReadableNameString =
391 literalString(impliedType.humanReadableName); 388 literalString(impliedType.humanReadableName);
392 writeln( 389 writeln(
393 'throw jsonDecoder.mismatch(jsonPath, $humanReadableNameString, json); '); 390 'throw jsonDecoder.mismatch(jsonPath, $humanReadableNameString, json); ');
394 }); 391 });
395 writeln('}'); 392 writeln('}');
396 } 393 }
397 394
395 void emitImports() {
396 writeln("import 'dart:convert' hide JsonDecoder;");
397 writeln();
398 writeln("import 'package:analyzer/src/generated/utilities_general.dart';");
399 writeln("import 'package:$packageName/protocol/protocol.dart';");
400 writeln(
401 "import 'package:$packageName/src/protocol/protocol_internal.dart';");
402 for (String uri in api.types.importUris) {
403 write("import '");
404 write(uri);
405 writeln("';");
406 }
407 }
408
398 /** 409 /**
399 * Emit the class to encapsulate an object type. 410 * Emit the class to encapsulate an object type.
400 */ 411 */
401 void emitObjectClass( 412 void emitObjectClass(
402 String className, TypeObject type, ImpliedType impliedType) { 413 String className, TypeObject type, ImpliedType impliedType) {
403 docComment(toHtmlVisitor.collectHtml(() { 414 docComment(toHtmlVisitor.collectHtml(() {
404 toHtmlVisitor.p(() { 415 toHtmlVisitor.p(() {
405 toHtmlVisitor.write(impliedType.humanReadableName); 416 toHtmlVisitor.write(impliedType.humanReadableName);
406 }); 417 });
407 if (impliedType.type != null) { 418 if (impliedType.type != null) {
(...skipping 637 matching lines...) Expand 10 before | Expand all | Expand 10 after
1045 } 1056 }
1046 } 1057 }
1047 return new FromJsonSnippet((String jsonPath, String json) => 1058 return new FromJsonSnippet((String jsonPath, String json) =>
1048 'jsonDecoder.decodeUnion($jsonPath, $json, ${literalString(type.field) }, {${decoders.join(', ')}})'); 1059 'jsonDecoder.decodeUnion($jsonPath, $json, ${literalString(type.field) }, {${decoders.join(', ')}})');
1049 } else { 1060 } else {
1050 throw new Exception("Can't convert $type from JSON"); 1061 throw new Exception("Can't convert $type from JSON");
1051 } 1062 }
1052 } 1063 }
1053 1064
1054 /** 1065 /**
1066 * Return a list of the classes to be emitted.
1067 */
1068 List<ImpliedType> getClassesToEmit() {
1069 List<ImpliedType> types = impliedTypes.values.where((ImpliedType type) {
1070 ApiNode node = type.apiNode;
1071 return !(node is TypeDefinition && node.isExternal);
1072 }).toList();
1073 types.sort((first, second) =>
1074 capitalize(first.camelName).compareTo(capitalize(second.camelName)));
1075 return types;
1076 }
1077
1078 /**
1055 * True if the constructor argument for the given field should be optional. 1079 * True if the constructor argument for the given field should be optional.
1056 */ 1080 */
1057 bool isOptionalConstructorArg(String className, TypeObjectField field) { 1081 bool isOptionalConstructorArg(String className, TypeObjectField field) {
1058 if (field.optional) { 1082 if (field.optional) {
1059 return true; 1083 return true;
1060 } 1084 }
1061 List<String> forceOptional = _optionalConstructorArguments[className]; 1085 List<String> forceOptional = _optionalConstructorArguments[className];
1062 if (forceOptional != null && forceOptional.contains(field.name)) { 1086 if (forceOptional != null && forceOptional.contains(field.name)) {
1063 return true; 1087 return true;
1064 } 1088 }
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
1124 dartType(type), (String value) => '$value.toJson()'); 1148 dartType(type), (String value) => '$value.toJson()');
1125 } else { 1149 } else {
1126 throw new Exception("Can't convert $resolvedType from JSON"); 1150 throw new Exception("Can't convert $resolvedType from JSON");
1127 } 1151 }
1128 } 1152 }
1129 1153
1130 @override 1154 @override
1131 visitApi() { 1155 visitApi() {
1132 outputHeader(year: '2017'); 1156 outputHeader(year: '2017');
1133 writeln(); 1157 writeln();
1134 writeln("import 'dart:convert' hide JsonDecoder;"); 1158 emitImports();
1135 writeln(); 1159 emitClasses(getClassesToEmit());
1136 writeln("import 'package:analyzer/src/generated/utilities_general.dart';");
1137 writeln("import 'package:$packageName/protocol/protocol.dart';");
1138 writeln(
1139 "import 'package:$packageName/src/protocol/protocol_internal.dart';");
1140 emitClasses();
1141 } 1160 }
1142 } 1161 }
1143 1162
1144 /** 1163 /**
1145 * Container for code that can be used to translate a data type from JSON. 1164 * Container for code that can be used to translate a data type from JSON.
1146 */ 1165 */
1147 abstract class FromJsonCode { 1166 abstract class FromJsonCode {
1148 /** 1167 /**
1149 * Get the translation code in the form of a closure. 1168 * Get the translation code in the form of a closure.
1150 */ 1169 */
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after
1281 1300
1282 @override 1301 @override
1283 String get asClosure => '($type value) => ${callback('value')}'; 1302 String get asClosure => '($type value) => ${callback('value')}';
1284 1303
1285 @override 1304 @override
1286 bool get isIdentity => false; 1305 bool get isIdentity => false;
1287 1306
1288 @override 1307 @override
1289 String asSnippet(String value) => callback(value); 1308 String asSnippet(String value) => callback(value);
1290 } 1309 }
OLDNEW
« no previous file with comments | « pkg/analyzer_plugin/tool/spec/api.dart ('k') | pkg/analyzer_plugin/tool/spec/codegen_inttest_methods.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698