allow parsing of exponential numbers if they resolve to int

This commit is contained in:
Emil Lerch 2021-06-23 18:18:42 -07:00
parent c2e2778d77
commit 77caa626f0
Signed by: lobo
GPG Key ID: A7B62D657EF764F8

View File

@ -1543,8 +1543,13 @@ fn parseInternal(comptime T: type, token: Token, tokens: *TokenStream, options:
.Number => |n| n,
else => return error.UnexpectedToken,
};
if (!numberToken.is_integer) return error.UnexpectedToken;
// This is a bug. you can still potentially have an integer that has exponents
// if (!numberToken.is_integer) return error.UnexpectedToken;
if (numberToken.is_integer)
return try std.fmt.parseInt(T, numberToken.slice(tokens.slice, tokens.i - 1), 10);
const float = try std.fmt.parseFloat(f128, numberToken.slice(tokens.slice, tokens.i - 1));
if (std.math.round(float) != float) return error.InvalidNumber;
return @floatToInt(T, float);
},
.Optional => |optionalInfo| {
if (token == .Null) {
@ -2002,6 +2007,11 @@ test "parse into struct with no fields" {
const T = struct {};
try testing.expectEqual(T{}, try parse(T, &TokenStream.init("{}"), ParseOptions{}));
}
test "parse exponential into int" {
const T = struct { int: i64 };
const r = try parse(T, &TokenStream.init("{ \"int\": 4.2e2 }"), ParseOptions{});
try testing.expectEqual(@as(i64, 420), r.int);
}
test "parse into struct with misc fields" {
@setEvalBranchQuota(10000);