From 960b5b518fa7ff7c6760d3e5f3c1256e553e6ba8 Mon Sep 17 00:00:00 2001 From: Travis Date: Wed, 28 Oct 2020 21:45:58 -0500 Subject: [PATCH] updated zig tokenizer to handle .*** and added tests --- lib/std/zig/tokenizer.zig | 44 ++++++++++++++++++++++++++++++++++++--- test/compile_errors.zig | 8 +++++++ 2 files changed, 49 insertions(+), 3 deletions(-) diff --git a/lib/std/zig/tokenizer.zig b/lib/std/zig/tokenizer.zig index c8f33dbfaa..280ae5c1f8 100644 --- a/lib/std/zig/tokenizer.zig +++ b/lib/std/zig/tokenizer.zig @@ -403,6 +403,7 @@ pub const Tokenizer = struct { angle_bracket_angle_bracket_right, period, period_2, + period_asterisk, saw_at_sign, }; @@ -979,9 +980,7 @@ pub const Tokenizer = struct { state = .period_2; }, '*' => { - result.id = .PeriodAsterisk; - self.index += 1; - break; + state = .period_asterisk; }, else => { result.id = .Period; @@ -1001,6 +1000,17 @@ pub const Tokenizer = struct { }, }, + .period_asterisk => switch (c) { + '*' => { + result.id = .Invalid; + break; + }, + else => { + result.id = .PeriodAsterisk; + break; + } + }, + .slash => switch (c) { '/' => { state = .line_comment_start; @@ -1376,6 +1386,9 @@ pub const Tokenizer = struct { .period_2 => { result.id = .Ellipsis2; }, + .period_asterisk => { + result.id = .PeriodAsterisk; + }, .pipe => { result.id = .Pipe; }, @@ -1762,6 +1775,31 @@ test "correctly parse pointer assignment" { }); } +test "correctly parse pointer dereference followed by asterisk" { + testTokenize("\"b\".* ** 10", &[_]Token.Id{ + .StringLiteral, + .PeriodAsterisk, + .AsteriskAsterisk, + .IntegerLiteral, + }); + + testTokenize("(\"b\".*)** 10", &[_]Token.Id{ + .LParen, + .StringLiteral, + .PeriodAsterisk, + .RParen, + .AsteriskAsterisk, + .IntegerLiteral, + }); + + testTokenize("\"b\".*** 10", &[_]Token.Id{ + .StringLiteral, + .Invalid, + .AsteriskAsterisk, + .IntegerLiteral, + }); +} + test "tokenizer - range literals" { testTokenize("0...9", &[_]Token.Id{ .IntegerLiteral, .Ellipsis3, .IntegerLiteral }); testTokenize("'0'...'9'", &[_]Token.Id{ .CharLiteral, .Ellipsis3, .CharLiteral }); diff --git a/test/compile_errors.zig b/test/compile_errors.zig index eb058bd0e7..8d5c4dc8a2 100644 --- a/test/compile_errors.zig +++ b/test/compile_errors.zig @@ -8195,4 +8195,12 @@ pub fn addCases(cases: *tests.CompileErrorContext) void { , &[_][]const u8{ "tmp.zig:4:9: error: expected type '*c_void', found '?*c_void'", }); + + cases.add("Issue #6823: don't allow .* to be followed by **", + \\fn foo() void { + \\ var sequence = "repeat".*** 10; + \\} + , &[_][]const u8{ + "tmp.zig:2:30: error: `.*` can't be followed by `*`. Are you missing a space?", + }); }