Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file | |
| 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. | |
| 4 | |
| 5 import 'dart:io'; | |
| 6 | |
| 7 import 'dart:isolate'; | |
| 8 | |
| 9 import 'dart:convert'; | |
| 10 | |
| 11 import 'package:yaml/yaml.dart' show loadYaml; | |
| 12 | |
| 13 import 'package:front_end/src/fasta/parser/error_kind.dart' show ErrorKind; | |
| 14 | |
| 15 main(List<String> arguments) async { | |
| 16 var port = new ReceivePort(); | |
| 17 Uri messagesFile = Platform.script.resolve("../../messages.yaml"); | |
| 18 Map yaml = loadYaml(await new File.fromUri(messagesFile).readAsStringSync()); | |
| 19 Set<String> names = | |
| 20 new Set<String>.from(yaml.keys.map((String s) => "ErrorKind.$s")); | |
|
Siggi Cherem (dart-lang)
2017/03/17 21:57:52
Oh, I see that you are trying to match error kinds
ahe
2017/03/22 11:06:56
Actually, I was hoping to replace ErrorKind with i
| |
| 21 Set<String> kinds = | |
| 22 new Set<String>.from(ErrorKind.values.map((kind) => "$kind")); | |
| 23 Set<String> difference = kinds.difference(names); | |
| 24 if (difference.isNotEmpty) { | |
| 25 Uri errorKindFile = await Isolate.resolvePackageUri( | |
| 26 Uri.parse('package:front_end/src/fasta/parser/error_kind.dart')); | |
| 27 throw "Mismatch between '${errorKindFile.toFilePath()}' and" | |
| 28 " '${messagesFile.toFilePath()}': ${difference.join(' ')}."; | |
| 29 } | |
| 30 StringBuffer sb = new StringBuffer(); | |
| 31 | |
| 32 sb.writeln(""" | |
| 33 // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file | |
| 34 // for details. All rights reserved. Use of this source code is governed by a | |
| 35 // BSD-style license that can be found in the LICENSE file. | |
| 36 | |
| 37 // NOTE: THIS FILE IS GENERATED. DO NOT EDIT. | |
| 38 // | |
| 39 // Instead modify 'pkg/front_end/messages.yaml' and run | |
| 40 // 'pkg/front_end/tool/_fasta/generate_messages.dart' to update. | |
| 41 | |
| 42 library fasta.problems; | |
| 43 | |
| 44 import 'package:front_end/src/fasta/scanner/token.dart' show Token; | |
| 45 | |
| 46 import 'package:front_end/src/fasta/parser/error_kind.dart' show ErrorKind; | |
| 47 """); | |
| 48 | |
| 49 yaml.forEach((String name, description) { | |
| 50 while (description is String) { | |
| 51 description = yaml[description]; | |
| 52 } | |
| 53 Map map = description; | |
| 54 sb.writeln(compileTemplate(name, map['template'], map['tip'])); | |
| 55 }); | |
| 56 | |
| 57 Uri problemsFile = await Isolate.resolvePackageUri( | |
| 58 Uri.parse('package:front_end/src/fasta/problems.dart')); | |
| 59 await new File.fromUri(problemsFile).writeAsString("$sb", flush: true); | |
| 60 port.close(); | |
| 61 } | |
| 62 | |
| 63 final RegExp placeholderPattern = new RegExp("#[a-zA-Z0-9_]+"); | |
| 64 | |
| 65 String compileTemplate(String name, String template, String tip) { | |
| 66 var parameters = new Set<String>(); | |
| 67 var conversions = new Set<String>(); | |
| 68 var arguments = new Set<String>(); | |
| 69 for (Match match in placeholderPattern.allMatches("$template$tip")) { | |
| 70 switch (match[0]) { | |
| 71 case "#character": | |
| 72 parameters.add("String character"); | |
| 73 arguments.add("'character': character,"); | |
| 74 break; | |
| 75 | |
| 76 case "#unicode": | |
| 77 parameters.add("int codePoint"); | |
| 78 conversions.add("String unicode = " | |
| 79 "\"(U+\${codePoint.toRadixString(16).padLeft(4, '0')})\";"); | |
| 80 arguments.add("'codePoint': codePoint,"); | |
| 81 break; | |
| 82 | |
| 83 case "#name": | |
| 84 parameters.add("String name"); | |
| 85 arguments.add("'name': name,"); | |
| 86 break; | |
| 87 | |
| 88 case "#lexeme": | |
| 89 parameters.add("Token token"); | |
| 90 conversions.add("String lexeme = token.lexeme;"); | |
| 91 arguments.add("'token': token,"); | |
| 92 break; | |
| 93 | |
| 94 case "#string": | |
| 95 parameters.add("String string"); | |
| 96 arguments.add("'string': string,"); | |
| 97 break; | |
| 98 } | |
| 99 } | |
| 100 | |
| 101 String interpolate(String name, String text) { | |
| 102 if (text == null) return ""; | |
|
Siggi Cherem (dart-lang)
2017/03/17 21:57:52
maybe generate a comment instead? e.g.:
/* no $n
ahe
2017/03/22 11:06:56
I'll use a StringBuffer in a follow-up CL.
| |
| 103 return " '$name': " | |
| 104 "\"${text.replaceAll(r'$', r'\$').replaceAll('#', '\$')}\","; | |
| 105 } | |
| 106 | |
| 107 return """ | |
| 108 problem$name(${parameters.join(', ')}) { | |
| 109 // DO NOT EDIT. THIS FILE IS GENERATED. SEE TOP OF FILE. | |
| 110 ${conversions.join('\n ')} | |
| 111 return { | |
| 112 ${interpolate('message', template)} | |
| 113 ${interpolate('tip', tip)} | |
| 114 'code': ErrorKind.$name, | |
| 115 'arguments': { | |
| 116 ${arguments.join('\n ')} | |
| 117 }, | |
| 118 }; | |
| 119 } | |
| 120 """; | |
| 121 } | |
| OLD | NEW |