| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2014, 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 library analyzer.context.declared_variables; |
| 6 |
| 7 import 'dart:collection'; |
| 8 |
| 9 import 'package:analyzer/dart/constant/value.dart'; |
| 10 import 'package:analyzer/src/dart/constant/value.dart'; |
| 11 import 'package:analyzer/src/generated/resolver.dart' show TypeProvider; |
| 12 |
| 13 /** |
| 14 * An object used to provide access to the values of variables that have been |
| 15 * defined on the command line using the `-D` option. |
| 16 * |
| 17 * Clients may not extend, implement or mix-in this class. |
| 18 */ |
| 19 class DeclaredVariables { |
| 20 /** |
| 21 * A table mapping the names of declared variables to their values. |
| 22 */ |
| 23 HashMap<String, String> _declaredVariables = new HashMap<String, String>(); |
| 24 |
| 25 /** |
| 26 * Return the names of the variables for which a value has been defined. |
| 27 */ |
| 28 Iterable<String> get variableNames => _declaredVariables.keys; |
| 29 |
| 30 /** |
| 31 * Define a variable with the given [name] to have the given [value]. |
| 32 */ |
| 33 void define(String name, String value) { |
| 34 _declaredVariables[name] = value; |
| 35 } |
| 36 |
| 37 /** |
| 38 * Return the raw string value of the variable with the given [name], |
| 39 * or `null` of the variable is not defined. |
| 40 */ |
| 41 String get(String name) => _declaredVariables[name]; |
| 42 |
| 43 /** |
| 44 * Return the value of the variable with the given [name] interpreted as a |
| 45 * 'boolean' value. If the variable is not defined (or [name] is `null`), a |
| 46 * DartObject representing "unknown" is returned. If the value cannot be |
| 47 * parsed as a boolean, a DartObject representing 'null' is returned. The |
| 48 * [typeProvider] is the type provider used to find the type 'bool'. |
| 49 */ |
| 50 DartObject getBool(TypeProvider typeProvider, String name) { |
| 51 String value = _declaredVariables[name]; |
| 52 if (value == null) { |
| 53 return new DartObjectImpl(typeProvider.boolType, BoolState.UNKNOWN_VALUE); |
| 54 } |
| 55 if (value == "true") { |
| 56 return new DartObjectImpl(typeProvider.boolType, BoolState.TRUE_STATE); |
| 57 } else if (value == "false") { |
| 58 return new DartObjectImpl(typeProvider.boolType, BoolState.FALSE_STATE); |
| 59 } |
| 60 return new DartObjectImpl(typeProvider.nullType, NullState.NULL_STATE); |
| 61 } |
| 62 |
| 63 /** |
| 64 * Return the value of the variable with the given [name] interpreted as an |
| 65 * integer value. If the variable is not defined (or [name] is `null`), a |
| 66 * DartObject representing "unknown" is returned. If the value cannot be |
| 67 * parsed as an integer, a DartObject representing 'null' is returned. |
| 68 */ |
| 69 DartObject getInt(TypeProvider typeProvider, String name) { |
| 70 String value = _declaredVariables[name]; |
| 71 if (value == null) { |
| 72 return new DartObjectImpl(typeProvider.intType, IntState.UNKNOWN_VALUE); |
| 73 } |
| 74 int bigInteger; |
| 75 try { |
| 76 bigInteger = int.parse(value); |
| 77 } on FormatException { |
| 78 return new DartObjectImpl(typeProvider.nullType, NullState.NULL_STATE); |
| 79 } |
| 80 return new DartObjectImpl(typeProvider.intType, new IntState(bigInteger)); |
| 81 } |
| 82 |
| 83 /** |
| 84 * Return the value of the variable with the given [name] interpreted as a |
| 85 * String value, or `null` if the variable is not defined. Return the value of |
| 86 * the variable with the given name interpreted as a String value. If the |
| 87 * variable is not defined (or [name] is `null`), a DartObject representing |
| 88 * "unknown" is returned. The [typeProvider] is the type provider used to find |
| 89 * the type 'String'. |
| 90 */ |
| 91 DartObject getString(TypeProvider typeProvider, String name) { |
| 92 String value = _declaredVariables[name]; |
| 93 if (value == null) { |
| 94 return new DartObjectImpl( |
| 95 typeProvider.stringType, StringState.UNKNOWN_VALUE); |
| 96 } |
| 97 return new DartObjectImpl(typeProvider.stringType, new StringState(value)); |
| 98 } |
| 99 } |
| OLD | NEW |