| 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 * Define a variable with the given [name] to have the given [value]. |
| 27 */ |
| 28 void define(String name, String value) { |
| 29 _declaredVariables[name] = value; |
| 30 } |
| 31 |
| 32 /** |
| 33 * Return the value of the variable with the given [name] interpreted as a |
| 34 * 'boolean' value. If the variable is not defined (or [name] is `null`), a |
| 35 * DartObject representing "unknown" is returned. If the value cannot be |
| 36 * parsed as a boolean, a DartObject representing 'null' is returned. The |
| 37 * [typeProvider] is the type provider used to find the type 'bool'. |
| 38 */ |
| 39 DartObject getBool(TypeProvider typeProvider, String name) { |
| 40 String value = _declaredVariables[name]; |
| 41 if (value == null) { |
| 42 return new DartObjectImpl(typeProvider.boolType, BoolState.UNKNOWN_VALUE); |
| 43 } |
| 44 if (value == "true") { |
| 45 return new DartObjectImpl(typeProvider.boolType, BoolState.TRUE_STATE); |
| 46 } else if (value == "false") { |
| 47 return new DartObjectImpl(typeProvider.boolType, BoolState.FALSE_STATE); |
| 48 } |
| 49 return new DartObjectImpl(typeProvider.nullType, NullState.NULL_STATE); |
| 50 } |
| 51 |
| 52 /** |
| 53 * Return the value of the variable with the given [name] interpreted as an |
| 54 * integer value. If the variable is not defined (or [name] is `null`), a |
| 55 * DartObject representing "unknown" is returned. If the value cannot be |
| 56 * parsed as an integer, a DartObject representing 'null' is returned. |
| 57 */ |
| 58 DartObject getInt(TypeProvider typeProvider, String name) { |
| 59 String value = _declaredVariables[name]; |
| 60 if (value == null) { |
| 61 return new DartObjectImpl(typeProvider.intType, IntState.UNKNOWN_VALUE); |
| 62 } |
| 63 int bigInteger; |
| 64 try { |
| 65 bigInteger = int.parse(value); |
| 66 } on FormatException { |
| 67 return new DartObjectImpl(typeProvider.nullType, NullState.NULL_STATE); |
| 68 } |
| 69 return new DartObjectImpl(typeProvider.intType, new IntState(bigInteger)); |
| 70 } |
| 71 |
| 72 /** |
| 73 * Return the value of the variable with the given [name] interpreted as a |
| 74 * String value, or `null` if the variable is not defined. Return the value of |
| 75 * the variable with the given name interpreted as a String value. If the |
| 76 * variable is not defined (or [name] is `null`), a DartObject representing |
| 77 * "unknown" is returned. The [typeProvider] is the type provider used to find |
| 78 * the type 'String'. |
| 79 */ |
| 80 DartObject getString(TypeProvider typeProvider, String name) { |
| 81 String value = _declaredVariables[name]; |
| 82 if (value == null) { |
| 83 return new DartObjectImpl( |
| 84 typeProvider.stringType, StringState.UNKNOWN_VALUE); |
| 85 } |
| 86 return new DartObjectImpl(typeProvider.stringType, new StringState(value)); |
| 87 } |
| 88 } |
| OLD | NEW |