platform/graphql_parser/lib/src/language/ast/number_value.dart

36 lines
911 B
Dart
Raw Normal View History

2017-07-03 15:37:35 +00:00
import 'dart:math' as math;
import 'package:source_span/source_span.dart';
2017-01-22 23:15:53 +00:00
import '../token.dart';
2019-08-08 01:48:41 +00:00
import 'input_value.dart';
2017-01-22 23:15:53 +00:00
2019-08-08 03:07:32 +00:00
/// A GraphQL number literal.
2019-08-08 01:48:41 +00:00
class NumberValueContext extends InputValueContext<num> {
2019-08-08 03:07:32 +00:00
/// The source token.
final Token numberToken;
2017-01-22 23:15:53 +00:00
2019-08-08 03:07:32 +00:00
NumberValueContext(this.numberToken);
2017-01-22 23:15:53 +00:00
2019-08-08 03:07:32 +00:00
/// The [num] value of the [numberToken].
2017-07-03 15:37:35 +00:00
num get numberValue {
2019-08-08 03:07:32 +00:00
var text = numberToken.text;
2019-08-08 02:28:20 +00:00
if (!text.contains('E') && !text.contains('e')) {
2017-07-03 15:37:35 +00:00
return num.parse(text);
2019-08-08 02:28:20 +00:00
} else {
2017-07-03 15:37:35 +00:00
var split = text.split(text.contains('E') ? 'E' : 'e');
var base = num.parse(split[0]);
var exp = num.parse(split[1]);
return base * math.pow(10, exp);
}
}
2019-08-08 03:07:32 +00:00
/// Use [numberToken] instead.
@deprecated
Token get NUMBER => numberToken;
2017-07-03 15:37:35 +00:00
@override
2019-08-08 03:07:32 +00:00
FileSpan get span => numberToken.span;
2017-07-03 15:37:35 +00:00
2017-01-22 23:15:53 +00:00
@override
2019-08-08 01:48:41 +00:00
num computeValue(Map<String, dynamic> variables) => numberValue;
2017-01-22 23:15:53 +00:00
}