From 608bc1cbd5910ee796cc292b43db77547858bf98 Mon Sep 17 00:00:00 2001 From: Isaac Freund Date: Thu, 27 May 2021 21:08:54 +0000 Subject: stage2: disallow `1.e9` and `0x1.p9` as float literals Instead require `1e9` and `0x1p9`, disallowing the trailing dot. This change to the grammar is consistent with forbidding `1.` and `0x1.` as float literals and ensures there is only one way to do things here. --- src/translate_c.zig | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/translate_c.zig b/src/translate_c.zig index 715ce73d45..b462743753 100644 --- a/src/translate_c.zig +++ b/src/translate_c.zig @@ -4932,8 +4932,17 @@ fn parseCNumLit(c: *Context, m: *MacroCtx) ParseError!Node { } }, .FloatLiteral => |suffix| { - if (lit_bytes[0] == '.') + const dot_index = mem.indexOfScalar(u8, lit_bytes, '.').?; + if (dot_index == 0) { lit_bytes = try std.fmt.allocPrint(c.arena, "0{s}", .{lit_bytes}); + } else if (dot_index + 1 == lit_bytes.len or !std.ascii.isDigit(lit_bytes[dot_index + 1])) { + // If the literal lacks a digit after the `.`, we need to + // add one since `1.` or `1.e10` would be invalid syntax in Zig. + lit_bytes = try std.fmt.allocPrint(c.arena, "{s}0{s}", .{ + lit_bytes[0 .. dot_index + 1], + lit_bytes[dot_index + 1 ..], + }); + } if (suffix == .none) { return transCreateNodeNumber(c, lit_bytes, .float); } -- cgit v1.2.3 From abd1c75c4aa70a83884e0509820dff8a6e51430c Mon Sep 17 00:00:00 2001 From: Isaac Freund Date: Mon, 31 May 2021 19:14:33 +0000 Subject: stage1: disallow 1.e9 and 0x1.p9 as float literals --- src/stage1/tokenizer.cpp | 8 -------- 1 file changed, 8 deletions(-) (limited to 'src') diff --git a/src/stage1/tokenizer.cpp b/src/stage1/tokenizer.cpp index 4550f32e8c..f10579c966 100644 --- a/src/stage1/tokenizer.cpp +++ b/src/stage1/tokenizer.cpp @@ -1286,10 +1286,6 @@ void tokenize(const char *source, Tokenization *out) { t.column -= 1; t.state = TokenizeState_start; continue; - case 'e': - case 'E': - t.state = TokenizeState_float_exponent_unsigned; - break; case DIGIT: t.state = TokenizeState_float_fraction_dec; break; @@ -1308,10 +1304,6 @@ void tokenize(const char *source, Tokenization *out) { t.column -= 1; t.state = TokenizeState_start; continue; - case 'p': - case 'P': - t.state = TokenizeState_float_exponent_unsigned; - break; case HEXDIGIT: t.out->ids.last() = TokenIdFloatLiteral; t.state = TokenizeState_float_fraction_hex; -- cgit v1.2.3