OLD | NEW |
1 // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file | 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 | 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:io'; | 5 import 'dart:io'; |
6 | 6 |
7 import 'dart:isolate'; | 7 import 'dart:isolate'; |
8 | 8 |
9 import 'package:yaml/yaml.dart' show loadYaml; | 9 import 'package:yaml/yaml.dart' show loadYaml; |
10 | 10 |
11 import 'package:front_end/src/fasta/parser/error_kind.dart' show ErrorKind; | |
12 | |
13 import 'package:dart_style/dart_style.dart' show DartFormatter; | 11 import 'package:dart_style/dart_style.dart' show DartFormatter; |
14 | 12 |
15 main(List<String> arguments) async { | 13 main(List<String> arguments) async { |
16 var port = new ReceivePort(); | 14 var port = new ReceivePort(); |
17 Uri messagesFile = Platform.script.resolve("../../messages.yaml"); | 15 Uri messagesFile = Platform.script.resolve("../../messages.yaml"); |
18 Map yaml = loadYaml(await new File.fromUri(messagesFile).readAsStringSync()); | 16 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")); | |
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(); | 17 StringBuffer sb = new StringBuffer(); |
31 | 18 |
32 sb.writeln(""" | 19 sb.writeln(""" |
33 // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file | 20 // 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 | 21 // 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. | 22 // BSD-style license that can be found in the LICENSE file. |
36 | 23 |
37 // NOTE: THIS FILE IS GENERATED. DO NOT EDIT. | 24 // NOTE: THIS FILE IS GENERATED. DO NOT EDIT. |
38 // | 25 // |
39 // Instead modify 'pkg/front_end/messages.yaml' and run | 26 // Instead modify 'pkg/front_end/messages.yaml' and run |
40 // 'pkg/front_end/tool/_fasta/generate_messages.dart' to update. | 27 // 'pkg/front_end/tool/_fasta/generate_messages.dart' to update. |
41 | 28 |
42 library fasta.problems; | 29 part of fasta.codes; |
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 """); | 30 """); |
48 | 31 |
49 yaml.forEach((String name, description) { | 32 yaml.forEach((String name, description) { |
50 while (description is String) { | 33 while (description is String) { |
51 description = yaml[description]; | 34 description = yaml[description]; |
52 } | 35 } |
53 Map map = description; | 36 Map map = description; |
54 sb.writeln(compileTemplate(name, map['template'], map['tip'])); | 37 sb.writeln(compileTemplate(name, map['template'], map['tip'], |
| 38 map['analyzerCode'], map['dart2jsCode'])); |
55 }); | 39 }); |
56 | 40 |
57 String dartfmtedText = new DartFormatter().format("$sb"); | 41 String dartfmtedText = new DartFormatter().format("$sb"); |
58 | 42 |
59 Uri problemsFile = await Isolate.resolvePackageUri( | 43 Uri problemsFile = await Isolate.resolvePackageUri( |
60 Uri.parse('package:front_end/src/fasta/problems.dart')); | 44 Uri.parse('package:front_end/src/fasta/fasta_codes_generated.dart')); |
61 await new File.fromUri(problemsFile) | 45 await new File.fromUri(problemsFile) |
62 .writeAsString(dartfmtedText, flush: true); | 46 .writeAsString(dartfmtedText, flush: true); |
63 port.close(); | 47 port.close(); |
64 } | 48 } |
65 | 49 |
66 final RegExp placeholderPattern = new RegExp("#[a-zA-Z0-9_]+"); | 50 final RegExp placeholderPattern = new RegExp("#[a-zA-Z0-9_]+"); |
67 | 51 |
68 String compileTemplate(String name, String template, String tip) { | 52 String compileTemplate(String name, String template, String tip, |
| 53 String analyzerCode, String dart2jsCode) { |
69 var parameters = new Set<String>(); | 54 var parameters = new Set<String>(); |
70 var conversions = new Set<String>(); | 55 var conversions = new Set<String>(); |
71 var arguments = new Set<String>(); | 56 var arguments = new Set<String>(); |
| 57 parameters.add("Uri uri"); |
| 58 parameters.add("int charOffset"); |
72 for (Match match in placeholderPattern.allMatches("$template${tip ?? ''}")) { | 59 for (Match match in placeholderPattern.allMatches("$template${tip ?? ''}")) { |
73 switch (match[0]) { | 60 switch (match[0]) { |
74 case "#character": | 61 case "#character": |
75 parameters.add("String character"); | 62 parameters.add("String character"); |
76 arguments.add("'character': character,"); | 63 arguments.add("'character': character"); |
77 break; | 64 break; |
78 | 65 |
79 case "#unicode": | 66 case "#unicode": |
80 parameters.add("int codePoint"); | 67 parameters.add("int codePoint"); |
81 conversions.add("String unicode = " | 68 conversions.add("String unicode = " |
82 "\"(U+\${codePoint.toRadixString(16).padLeft(4, '0')})\";"); | 69 "\"(U+\${codePoint.toRadixString(16).padLeft(4, '0')})\";"); |
83 arguments.add("'codePoint': codePoint,"); | 70 arguments.add("'codePoint': codePoint"); |
84 break; | 71 break; |
85 | 72 |
86 case "#name": | 73 case "#name": |
87 parameters.add("String name"); | 74 parameters.add("String name"); |
88 arguments.add("'name': name,"); | 75 arguments.add("'name': name"); |
89 break; | 76 break; |
90 | 77 |
91 case "#lexeme": | 78 case "#lexeme": |
92 parameters.add("Token token"); | 79 parameters.add("Token token"); |
93 conversions.add("String lexeme = token.lexeme;"); | 80 conversions.add("String lexeme = token.lexeme;"); |
94 arguments.add("'token': token,"); | 81 arguments.add("'token': token"); |
95 break; | 82 break; |
96 | 83 |
97 case "#string": | 84 case "#string": |
98 parameters.add("String string"); | 85 parameters.add("String string"); |
99 arguments.add("'string': string,"); | 86 arguments.add("'string': string"); |
100 break; | 87 break; |
101 | 88 |
102 default: | 89 default: |
103 throw "Unhandled placeholder in template: ${match[0]}"; | 90 throw "Unhandled placeholder in template: ${match[0]}"; |
104 } | 91 } |
105 } | 92 } |
106 | 93 |
107 String interpolate(String name, String text) { | 94 String interpolate(String name, String text) { |
108 if (text == null) return ""; | 95 return "$name: " |
109 return " '$name': " | 96 "\"${text.replaceAll(r'$', r'\$').replaceAll('#', '\$')}\""; |
110 "\"${text.replaceAll(r'$', r'\$').replaceAll('#', '\$')}\","; | |
111 } | 97 } |
112 | 98 |
| 99 List<String> codeArguments = <String>[]; |
| 100 if (template != null) { |
| 101 codeArguments.add('template: r"$template"'); |
| 102 } |
| 103 if (tip != null) { |
| 104 codeArguments.add('tip: r"$tip"'); |
| 105 } |
| 106 if (analyzerCode != null) { |
| 107 codeArguments.add('analyzerCode: "$analyzerCode"'); |
| 108 } |
| 109 if (dart2jsCode != null) { |
| 110 codeArguments.add('dart2jsCode: "$dart2jsCode"'); |
| 111 } |
| 112 |
| 113 codeArguments.add("format: _format$name"); |
| 114 |
| 115 List<String> messageArguments = <String>[]; |
| 116 messageArguments.add(interpolate("message", template)); |
| 117 if (tip != null) { |
| 118 messageArguments.add(interpolate("tip", tip)); |
| 119 } |
| 120 messageArguments.add("arguments: { ${arguments.join(', ')} }"); |
| 121 |
113 return """ | 122 return """ |
114 problem$name(${parameters.join(', ')}) { | 123 // DO NOT EDIT. THIS FILE IS GENERATED. SEE TOP OF FILE. |
115 // DO NOT EDIT. THIS FILE IS GENERATED. SEE TOP OF FILE. | 124 const FastaCode<_$name> code$name = |
| 125 const FastaCode<_$name>(${codeArguments.join(', ')}); |
| 126 |
| 127 typedef FastaMessage _$name(${parameters.join(', ')}); |
| 128 |
| 129 // DO NOT EDIT. THIS FILE IS GENERATED. SEE TOP OF FILE. |
| 130 FastaMessage _format$name(${parameters.join(', ')}) { |
116 ${conversions.join('\n ')} | 131 ${conversions.join('\n ')} |
117 return { | 132 return new FastaMessage( |
118 ${interpolate('message', template)} | 133 uri, |
119 ${interpolate('tip', tip)} | 134 charOffset, |
120 'code': ErrorKind.$name, | 135 code$name, |
121 'arguments': { | 136 ${messageArguments.join(', ')}); |
122 ${arguments.join('\n ')} | |
123 }, | |
124 }; | |
125 } | 137 } |
126 """; | 138 """; |
127 } | 139 } |
OLD | NEW |