OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 /** | 5 /** |
6 * This provides classes to represent the internal structure of the | 6 * This provides classes to represent the internal structure of the |
7 * arguments to `Intl.message`. It is used when parsing sources to extract | 7 * arguments to `Intl.message`. It is used when parsing sources to extract |
8 * messages or to generate code for message substitution. Normal programs | 8 * messages or to generate code for message substitution. Normal programs |
9 * using Intl would not import this library. | 9 * using Intl would not import this library. |
10 * | 10 * |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
62 */ | 62 */ |
63 get arguments => parent == null ? const [] : parent.arguments; | 63 get arguments => parent == null ? const [] : parent.arguments; |
64 | 64 |
65 /** | 65 /** |
66 * We find the examples from the top-level [MainMessage] and use those | 66 * We find the examples from the top-level [MainMessage] and use those |
67 * when writing out variables. [MainMessage] overrides this to return | 67 * when writing out variables. [MainMessage] overrides this to return |
68 * the actual examples. | 68 * the actual examples. |
69 */ | 69 */ |
70 get examples => parent == null ? const [] : parent.examples; | 70 get examples => parent == null ? const [] : parent.examples; |
71 | 71 |
| 72 /** |
| 73 * The name of the top-level [MainMessage]. |
| 74 */ |
| 75 String get name => parent == null ? '<unnamed>' : parent.name; |
| 76 |
72 String checkValidity(MethodInvocation node, List arguments, | 77 String checkValidity(MethodInvocation node, List arguments, |
73 String outerName, FormalParameterList outerArgs) { | 78 String outerName, FormalParameterList outerArgs) { |
74 var hasArgs = arguments.any( | 79 var hasArgs = arguments.any( |
75 (each) => each is NamedExpression && each.name.label.name == 'args'); | 80 (each) => each is NamedExpression && each.name.label.name == 'args'); |
76 var hasParameters = !outerArgs.parameters.isEmpty; | 81 var hasParameters = !outerArgs.parameters.isEmpty; |
77 if (!hasArgs && hasParameters) { | 82 if (!hasArgs && hasParameters) { |
78 return "The 'args' argument for Intl.message must be specified"; | 83 return "The 'args' argument for Intl.message must be specified"; |
79 } | 84 } |
80 | 85 |
81 var messageName = arguments.firstWhere( | 86 var messageName = arguments.firstWhere( |
(...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
267 | 272 |
268 /** The index in the list of parameters of the containing function. */ | 273 /** The index in the list of parameters of the containing function. */ |
269 int _index; | 274 int _index; |
270 int get index { | 275 int get index { |
271 if (_index != null) return _index; | 276 if (_index != null) return _index; |
272 if (arguments.isEmpty) return null; | 277 if (arguments.isEmpty) return null; |
273 // We may have been given an all-uppercase version of the name, so compare | 278 // We may have been given an all-uppercase version of the name, so compare |
274 // case-insensitive. | 279 // case-insensitive. |
275 _index = arguments.map((x) => x.toUpperCase()).toList() | 280 _index = arguments.map((x) => x.toUpperCase()).toList() |
276 .indexOf(_variableNameUpper); | 281 .indexOf(_variableNameUpper); |
| 282 if (_index == -1) { |
| 283 throw new ArgumentError( |
| 284 "Cannot find parameter named '$_variableNameUpper' in " |
| 285 "message named '$name'. Available " |
| 286 "parameters are $arguments"); |
| 287 } |
277 return _index; | 288 return _index; |
278 } | 289 } |
279 | 290 |
280 /** | 291 /** |
281 * The variable name we get from parsing. This may be an all uppercase version | 292 * The variable name we get from parsing. This may be an all uppercase version |
282 * of the Dart argument name. | 293 * of the Dart argument name. |
283 */ | 294 */ |
284 String _variableNameUpper; | 295 String _variableNameUpper; |
285 | 296 |
286 /** | 297 /** |
(...skipping 397 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
684 out.write('('); | 695 out.write('('); |
685 out.write(mainArgument); | 696 out.write(mainArgument); |
686 var args = codeAttributeNames; | 697 var args = codeAttributeNames; |
687 out.write(", {"); | 698 out.write(", {"); |
688 args.fold(out, (buffer, arg) => buffer..write( | 699 args.fold(out, (buffer, arg) => buffer..write( |
689 "'$arg': '${this[arg].toCode()}', ")); | 700 "'$arg': '${this[arg].toCode()}', ")); |
690 out.write("})}"); | 701 out.write("})}"); |
691 return out.toString(); | 702 return out.toString(); |
692 } | 703 } |
693 } | 704 } |
OLD | NEW |