| OLD | NEW |
| 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, 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 import 'dart:io' as io; | 6 import 'dart:io' as io; |
| 7 | 7 |
| 8 import '../lib/shared_messages.dart'; | 8 import '../lib/shared_messages.dart'; |
| 9 | 9 |
| 10 const String jsonPath = '../lib/generated/shared_messages.json'; | 10 const String jsonPath = '../lib/generated/shared_messages.json'; |
| (...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 165 if (index == null) { | 165 if (index == null) { |
| 166 throw "Couldn't find hole-position for $holeName $holeMap"; | 166 throw "Couldn't find hole-position for $holeName $holeMap"; |
| 167 } | 167 } |
| 168 return "{$index}"; | 168 return "{$index}"; |
| 169 } else { | 169 } else { |
| 170 return "{${seenHoles++}}"; | 170 return "{${seenHoles++}}"; |
| 171 } | 171 } |
| 172 }); | 172 }); |
| 173 } | 173 } |
| 174 | 174 |
| 175 String camlToAllCaps(String input) { |
| 176 StringBuffer out = new StringBuffer(); |
| 177 for (int i = 0; i < input.length; i++) { |
| 178 String c = input[i]; |
| 179 if (c.toUpperCase() == c) { |
| 180 out.write("_$c"); |
| 181 } else { |
| 182 out.write(c.toUpperCase()); |
| 183 } |
| 184 } |
| 185 return out.toString(); |
| 186 } |
| 187 |
| 175 /// Emits the messages in analyzer format. | 188 /// Emits the messages in analyzer format. |
| 176 /// | 189 /// |
| 177 /// Messages are encoded as instances of `ErrorCode` classes where the | 190 /// Messages are encoded as instances of `ErrorCode` classes where the |
| 178 /// corresponding class is given by the `category` field of the Message. | 191 /// corresponding class is given by the `category` field of the Message. |
| 179 /// | 192 /// |
| 180 /// All instances are stored as top-level const variables. | 193 /// All instances are stored as top-level const variables. |
| 181 /// | 194 /// |
| 182 /// A sample output looks as follows: | 195 /// A sample output looks as follows: |
| 183 /// | 196 /// |
| 184 /// const FooCategoryErrorCode EXAMPLE_MESSAGE = const FooCategoryErrorCode( | 197 /// const FooCategoryErrorCode EXAMPLE_MESSAGE = const FooCategoryErrorCode( |
| 185 /// "EXAMPLE_MESSAGE", | 198 /// "EXAMPLE_MESSAGE", |
| 186 /// "Don't use {0} with {1}", | 199 /// "Don't use {0} with {1}", |
| 187 /// "Just don't do it"); | 200 /// "Just don't do it"); |
| 188 void emitAnalyzer() { | 201 void emitAnalyzer() { |
| 189 var input = MESSAGES; | 202 var input = MESSAGES; |
| 190 var outPath = io.Platform.script.resolve(analyzerPath).toFilePath(); | 203 var outPath = io.Platform.script.resolve(analyzerPath).toFilePath(); |
| 191 print("Emitting analyzer:"); | 204 print("Emitting analyzer:"); |
| 192 print(" Input: ${input.length} entries"); | 205 print(" Input: ${input.length} entries"); |
| 193 print(" Output: $outPath"); | 206 print(" Output: $outPath"); |
| 194 | 207 |
| 195 StringBuffer out = new StringBuffer(); | 208 StringBuffer out = new StringBuffer(); |
| 196 out.writeln(copyrightHeader); | 209 out.writeln(copyrightHeader); |
| 197 out.writeln(dontEditWarning); | 210 out.writeln(dontEditWarning); |
| 198 out.writeln("import 'package:analyzer/src/generated/error.dart';"); | 211 out.writeln("import 'package:analyzer/src/generated/error.dart';"); |
| 199 out.writeln("import 'package:analyzer/src/generated/parser.dart' " | 212 out.writeln("import 'package:analyzer/src/generated/parser.dart' " |
| 200 "show ParserErrorCode;"); | 213 "show ParserErrorCode;"); |
| 201 input.forEach((name, message) { | 214 input.forEach((name, message) { |
| 202 if (!message.usedBy.contains(Platform.analyzer)) return; | 215 if (!message.usedBy.contains(Platform.analyzer)) return; |
| 216 List<Category> categories = message.categories; |
| 217 bool hasMultipleCategories = categories.length != 1; |
| 218 for (Category category in categories) { |
| 219 String className = category.name + "Code"; |
| 220 String analyzerName = |
| 221 hasMultipleCategories ? "$name${camlToAllCaps(category.name)}" : name; |
| 222 out.writeln(); |
| 223 out.writeln("const $className $analyzerName = const $className("); |
| 224 out.writeln(" '$name',"); |
| 203 | 225 |
| 204 Category category = message.category; | 226 String template = message.template; |
| 205 String className = category.name + "Code"; | 227 List holeOrder = message.templateHoleOrder; |
| 206 out.writeln(); | 228 String analyzerTemplate = convertToAnalyzerTemplate(template, holeOrder); |
| 207 out.writeln("const $className $name = const $className("); | 229 out.write(" "); |
| 208 out.writeln(" '$name',"); | 230 out.write(escapeString(analyzerTemplate)); |
| 209 | 231 out.write(",\n "); |
| 210 String template = message.template; | 232 out.write(escapeString(message.howToFix)); |
| 211 List holeOrder = message.templateHoleOrder; | 233 out.writeln("); // Generated. Don't edit."); |
| 212 String analyzerTemplate = convertToAnalyzerTemplate(template, holeOrder); | 234 } |
| 213 out.write(" "); | |
| 214 out.write(escapeString(analyzerTemplate)); | |
| 215 out.write(",\n "); | |
| 216 out.write(escapeString(message.howToFix)); | |
| 217 out.writeln("); // Generated. Don't edit."); | |
| 218 }); | 235 }); |
| 219 | 236 |
| 220 new io.File(outPath).writeAsStringSync(out.toString()); | 237 new io.File(outPath).writeAsStringSync(out.toString()); |
| 221 print("Emitting analyzer done."); | 238 print("Emitting analyzer done."); |
| 222 } | 239 } |
| 223 | 240 |
| 224 /// Translates the shared messages in `../lib/shared_messages.dart` to JSON, | 241 /// Translates the shared messages in `../lib/shared_messages.dart` to JSON, |
| 225 /// dart2js, and analyzer formats. | 242 /// dart2js, and analyzer formats. |
| 226 /// | 243 /// |
| 227 /// Emits the json-output to [jsonPath], the dart2js-output to [dart2jsPath], | 244 /// Emits the json-output to [jsonPath], the dart2js-output to [dart2jsPath], |
| 228 /// and the analyzer-output to [analyzerPath]. | 245 /// and the analyzer-output to [analyzerPath]. |
| 229 void main() { | 246 void main() { |
| 230 emitJson(); | 247 emitJson(); |
| 231 emitDart2js(); | 248 emitDart2js(); |
| 232 emitAnalyzer(); | 249 emitAnalyzer(); |
| 233 } | 250 } |
| OLD | NEW |