Chromium Code Reviews| 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 221 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 232 } | 232 } |
| 233 | 233 |
| 234 /** | 234 /** |
| 235 * Represents an interpolation of a variable value in a message. We expect | 235 * Represents an interpolation of a variable value in a message. We expect |
| 236 * this to be specified as an [index] into the list of variables, or else | 236 * this to be specified as an [index] into the list of variables, or else |
| 237 * as the name of a variable that exists in [arguments] and we will | 237 * as the name of a variable that exists in [arguments] and we will |
| 238 * compute the variable name or the index based on the value of the other. | 238 * compute the variable name or the index based on the value of the other. |
| 239 */ | 239 */ |
| 240 class VariableSubstitution extends Message { | 240 class VariableSubstitution extends Message { |
| 241 VariableSubstitution(this._index, Message parent) : super(parent); | 241 VariableSubstitution(this._index, Message parent) : super(parent); |
| 242 VariableSubstitution.named(this._variableName, Message parent) | 242 |
| 243 : super(parent); | 243 /** |
| 244 * Create a substitution based on the name rather than the index. The name | |
|
Emily Fortuna
2013/09/12 00:52:19
yay, comments!
| |
| 245 * may have been used as all upper-case in the translation tool, so we | |
| 246 * save it separately and look it up case-insensitively once the parent | |
| 247 * (and its arguments) are definitely available. | |
| 248 */ | |
| 249 VariableSubstitution.named(String name, Message parent) | |
| 250 : super(parent) { | |
| 251 _variableNameUpper = name.toUpperCase(); | |
| 252 } | |
| 244 | 253 |
| 245 /** The index in the list of parameters of the containing function. */ | 254 /** The index in the list of parameters of the containing function. */ |
| 246 int _index; | 255 int _index; |
| 247 int get index { | 256 int get index { |
| 248 if (_index != null) return _index; | 257 if (_index != null) return _index; |
| 249 if (arguments.isEmpty) return null; | 258 if (arguments.isEmpty) return null; |
| 250 return _index = arguments.indexOf(_variableName); | 259 // We may have been given an all-uppercase version of the name, so compare |
| 260 // case-insensitive. | |
| 261 _index = arguments.map((x) => x.toUpperCase()).toList() | |
| 262 .indexOf(_variableNameUpper); | |
| 263 return _index; | |
| 251 } | 264 } |
| 252 | 265 |
| 253 /** | 266 /** |
| 267 * The variable name we get from parsing. This may be an all uppercase version | |
| 268 * of the Dart argument name. | |
| 269 */ | |
| 270 String _variableNameUpper; | |
| 271 | |
| 272 /** | |
| 254 * The name of the variable in the parameter list of the containing function. | 273 * The name of the variable in the parameter list of the containing function. |
| 255 * Used when generating code for the interpolation. | 274 * Used when generating code for the interpolation. |
| 256 */ | 275 */ |
| 257 String get variableName => | 276 String get variableName => |
| 258 _variableName == null ? _variableName = arguments[index] : _variableName; | 277 _variableName == null ? _variableName = arguments[index] : _variableName; |
| 259 String _variableName; | 278 String _variableName; |
| 260 // Although we only allow simple variable references, we always enclose them | 279 // Although we only allow simple variable references, we always enclose them |
| 261 // in curly braces so that there's no possibility of ambiguity with | 280 // in curly braces so that there's no possibility of ambiguity with |
| 262 // surrounding text. | 281 // surrounding text. |
| 263 toCode() => "\${${variableName}}"; | 282 toCode() => "\${${variableName}}"; |
| (...skipping 378 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 642 out.write('('); | 661 out.write('('); |
| 643 out.write(mainArgument); | 662 out.write(mainArgument); |
| 644 var args = codeAttributeNames; | 663 var args = codeAttributeNames; |
| 645 out.write(", {"); | 664 out.write(", {"); |
| 646 args.fold(out, (buffer, arg) => buffer..write( | 665 args.fold(out, (buffer, arg) => buffer..write( |
| 647 "'$arg': '${this[arg].toCode()}', ")); | 666 "'$arg': '${this[arg].toCode()}', ")); |
| 648 out.write("})}"); | 667 out.write("})}"); |
| 649 return out.toString(); | 668 return out.toString(); |
| 650 } | 669 } |
| 651 } | 670 } |
| OLD | NEW |