aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAndrew Kelley <andrew@ziglang.org>2021-06-01 14:00:54 -0400
committerGitHub <noreply@github.com>2021-06-01 14:00:54 -0400
commitf6932472950e7dbb451d4cfef8e5f4a1cc506ac0 (patch)
tree986353a4f281ca362f48e9aec31af691d85c8019 /src
parentd496400cff8b025dea262a9544e1b20482233089 (diff)
parentabd1c75c4aa70a83884e0509820dff8a6e51430c (diff)
downloadzig-f6932472950e7dbb451d4cfef8e5f4a1cc506ac0.tar.gz
zig-f6932472950e7dbb451d4cfef8e5f4a1cc506ac0.zip
Merge pull request #8917 from ifreund/fix-float-tokenize2
stage1, stage2: disallow 1.e9 and 0x1.p9 as float literals
Diffstat (limited to 'src')
-rw-r--r--src/stage1/tokenizer.cpp8
-rw-r--r--src/translate_c.zig11
2 files changed, 10 insertions, 9 deletions
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;
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);
}