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 |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
47 port.close(); | 47 port.close(); |
48 } | 48 } |
49 | 49 |
50 final RegExp placeholderPattern = new RegExp("#[a-zA-Z0-9_]+"); | 50 final RegExp placeholderPattern = new RegExp("#[a-zA-Z0-9_]+"); |
51 | 51 |
52 String compileTemplate(String name, String template, String tip, | 52 String compileTemplate(String name, String template, String tip, |
53 String analyzerCode, String dart2jsCode) { | 53 String analyzerCode, String dart2jsCode) { |
54 var parameters = new Set<String>(); | 54 var parameters = new Set<String>(); |
55 var conversions = new Set<String>(); | 55 var conversions = new Set<String>(); |
56 var arguments = new Set<String>(); | 56 var arguments = new Set<String>(); |
57 parameters.add("Uri uri"); | |
58 parameters.add("int charOffset"); | |
59 for (Match match in placeholderPattern.allMatches("$template${tip ?? ''}")) { | 57 for (Match match in placeholderPattern.allMatches("$template${tip ?? ''}")) { |
60 switch (match[0]) { | 58 switch (match[0]) { |
61 case "#character": | 59 case "#character": |
62 parameters.add("String character"); | 60 parameters.add("String character"); |
63 arguments.add("'character': character"); | 61 arguments.add("'character': character"); |
64 break; | 62 break; |
65 | 63 |
66 case "#unicode": | 64 case "#unicode": |
67 parameters.add("int codePoint"); | 65 parameters.add("int codePoint"); |
68 conversions.add("String unicode = " | 66 conversions.add("String unicode = " |
(...skipping 17 matching lines...) Expand all Loading... |
86 arguments.add("'string': string"); | 84 arguments.add("'string': string"); |
87 break; | 85 break; |
88 | 86 |
89 default: | 87 default: |
90 throw "Unhandled placeholder in template: ${match[0]}"; | 88 throw "Unhandled placeholder in template: ${match[0]}"; |
91 } | 89 } |
92 } | 90 } |
93 | 91 |
94 String interpolate(String name, String text) { | 92 String interpolate(String name, String text) { |
95 return "$name: " | 93 return "$name: " |
96 "\"${text.replaceAll(r'$', r'\$').replaceAll('#', '\$')}\""; | 94 "\"\"\"${text.replaceAll(r'$', r'\$').replaceAll('#', '\$')}\"\"\""; |
97 } | 95 } |
98 | 96 |
99 List<String> codeArguments = <String>[]; | 97 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) { | 98 if (analyzerCode != null) { |
107 codeArguments.add('analyzerCode: "$analyzerCode"'); | 99 codeArguments.add('analyzerCode: "$analyzerCode"'); |
108 } | 100 } |
109 if (dart2jsCode != null) { | 101 if (dart2jsCode != null) { |
110 codeArguments.add('dart2jsCode: "$dart2jsCode"'); | 102 codeArguments.add('dart2jsCode: "$dart2jsCode"'); |
111 } | 103 } |
112 | 104 |
113 codeArguments.add("format: _format$name"); | 105 if (parameters.isEmpty && conversions.isEmpty && arguments.isEmpty) { |
| 106 if (template != null) { |
| 107 codeArguments.add('message: r"""$template"""'); |
| 108 } |
| 109 if (tip != null) { |
| 110 codeArguments.add('tip: r"""$tip"""'); |
| 111 } |
| 112 |
| 113 return """ |
| 114 // DO NOT EDIT. THIS FILE IS GENERATED. SEE TOP OF FILE. |
| 115 const Code<Null> code$name = message$name; |
| 116 |
| 117 // DO NOT EDIT. THIS FILE IS GENERATED. SEE TOP OF FILE. |
| 118 const MessageCode message$name = |
| 119 const MessageCode(\"$name\", ${codeArguments.join(', ')}); |
| 120 """; |
| 121 } |
| 122 |
| 123 List<String> templateArguments = <String>[]; |
| 124 if (template != null) { |
| 125 templateArguments.add('messageTemplate: r"""$template"""'); |
| 126 } |
| 127 if (tip != null) { |
| 128 templateArguments.add('tipTemplate: r"""$tip"""'); |
| 129 } |
| 130 |
| 131 templateArguments.add("withArguments: _withArguments$name"); |
114 | 132 |
115 List<String> messageArguments = <String>[]; | 133 List<String> messageArguments = <String>[]; |
116 messageArguments.add(interpolate("message", template)); | 134 messageArguments.add(interpolate("message", template)); |
117 if (tip != null) { | 135 if (tip != null) { |
118 messageArguments.add(interpolate("tip", tip)); | 136 messageArguments.add(interpolate("tip", tip)); |
119 } | 137 } |
120 messageArguments.add("arguments: { ${arguments.join(', ')} }"); | 138 messageArguments.add("arguments: { ${arguments.join(', ')} }"); |
121 | 139 |
122 return """ | 140 return """ |
123 // DO NOT EDIT. THIS FILE IS GENERATED. SEE TOP OF FILE. | 141 // DO NOT EDIT. THIS FILE IS GENERATED. SEE TOP OF FILE. |
124 const FastaCode<_$name> code$name = | 142 const Template<Message Function(${parameters.join(', ')})> template$name = |
125 const FastaCode<_$name>(\"$name\", ${codeArguments.join(', ')}); | 143 const Template<Message Function(${parameters.join(', ')})>( |
126 | 144 ${templateArguments.join(', ')}); |
127 typedef FastaMessage _$name(${parameters.join(', ')}); | |
128 | 145 |
129 // DO NOT EDIT. THIS FILE IS GENERATED. SEE TOP OF FILE. | 146 // DO NOT EDIT. THIS FILE IS GENERATED. SEE TOP OF FILE. |
130 FastaMessage _format$name(${parameters.join(', ')}) { | 147 const Code<Message Function(${parameters.join(', ')})> code$name = |
| 148 const Code<Message Function(${parameters.join(', ')})>( |
| 149 \"$name\", template$name, ${codeArguments.join(', ')}); |
| 150 |
| 151 // DO NOT EDIT. THIS FILE IS GENERATED. SEE TOP OF FILE. |
| 152 Message _withArguments$name(${parameters.join(', ')}) { |
131 ${conversions.join('\n ')} | 153 ${conversions.join('\n ')} |
132 return new FastaMessage( | 154 return new Message( |
133 uri, | |
134 charOffset, | |
135 code$name, | 155 code$name, |
136 ${messageArguments.join(', ')}); | 156 ${messageArguments.join(', ')}); |
137 } | 157 } |
138 """; | 158 """; |
139 } | 159 } |
OLD | NEW |