authorgravatar for tssund93@gmail.comTravis <tssund93@gmail.com> 2020-10-29 11:04:50-05:00
committergravatar for tssund93@gmail.comTravis <tssund93@gmail.com> 2020-10-29 12:03:45-05:00
logd7f9128b5d3c8c8f6a0a617c45717cdc7e3543fd
tree8da94ed09600cfcebe599c531d7aa887531e5514
parent960b5b518fa7ff7c6760d3e5f3c1256e553e6ba8

add error message to zig side of tokenizing/parsing


4 files changed, 27 insertions(+), 3 deletions(-)

lib/std/zig/ast.zig+4
......@@ -171,6 +171,7 @@ pub const Error = union(enum) {
171171 ExpectedBlockOrField: ExpectedBlockOrField,
172172 DeclBetweenFields: DeclBetweenFields,
173173 InvalidAnd: InvalidAnd,
174 AsteriskAfterPointerDereference: AsteriskAfterPointerDereference,
174175
175176 pub fn render(self: *const Error, tokens: []const Token.Id, stream: anytype) !void {
176177 switch (self.*) {
......@@ -222,6 +223,7 @@ pub const Error = union(enum) {
222223 .ExpectedBlockOrField => |*x| return x.render(tokens, stream),
223224 .DeclBetweenFields => |*x| return x.render(tokens, stream),
224225 .InvalidAnd => |*x| return x.render(tokens, stream),
226 .AsteriskAfterPointerDereference => |*x| return x.render(tokens, stream),
225227 }
226228 }
227229
......@@ -275,6 +277,7 @@ pub const Error = union(enum) {
275277 .ExpectedBlockOrField => |x| return x.token,
276278 .DeclBetweenFields => |x| return x.token,
277279 .InvalidAnd => |x| return x.token,
280 .AsteriskAfterPointerDereference => |x| return x.token,
278281 }
279282 }
280283
......@@ -323,6 +326,7 @@ pub const Error = union(enum) {
323326 pub const ExtraAllowZeroQualifier = SimpleError("Extra allowzero qualifier");
324327 pub const DeclBetweenFields = SimpleError("Declarations are not allowed between container fields");
325328 pub const InvalidAnd = SimpleError("`&&` is invalid. Note that `and` is boolean AND.");
329 pub const AsteriskAfterPointerDereference = SimpleError("`.*` can't be followed by `*`. Are you missing a space?");
326330
327331 pub const ExpectedCall = struct {
328332 node: *Node,
lib/std/zig/parse.zig+7
......@@ -2701,6 +2701,13 @@ const Parser = struct {
27012701 return &node.base;
27022702 }
27032703
2704 if (p.token_ids[p.tok_i] == .Invalid_periodasterisks) {
2705 try p.errors.append(p.gpa, .{
2706 .AsteriskAfterPointerDereference = .{ .token = p.tok_i },
2707 });
2708 return null;
2709 }
2710
27042711 if (p.eatToken(.Period)) |period| {
27052712 if (try p.parseIdentifier()) |identifier| {
27062713 const node = try p.arena.allocator.create(Node.SimpleInfixOp);
lib/std/zig/parser_test.zig+11
......@@ -219,6 +219,17 @@ test "recovery: invalid global error set access" {
219219 });
220220}
221221
222test "recovery: invalid asterisk after pointer dereference" {
223 try testError(
224 \\test "" {
225 \\ var sequence = "repeat".*** 10;
226 \\}
227 , &[_]Error{
228 .AsteriskAfterPointerDereference,
229 .ExpectedToken,
230 });
231}
232
222233test "recovery: missing semicolon after if, for, while stmt" {
223234 try testError(
224235 \\test "" {
lib/std/zig/tokenizer.zig+5-3
......@@ -78,6 +78,7 @@ pub const Token = struct {
7878 pub const Id = enum {
7979 Invalid,
8080 Invalid_ampersands,
81 Invalid_periodasterisks,
8182 Identifier,
8283 StringLiteral,
8384 MultilineStringLiteralLine,
......@@ -201,6 +202,7 @@ pub const Token = struct {
201202 return switch (id) {
202203 .Invalid => "Invalid",
203204 .Invalid_ampersands => "&&",
205 .Invalid_periodasterisks => ".**",
204206 .Identifier => "Identifier",
205207 .StringLiteral => "StringLiteral",
206208 .MultilineStringLiteralLine => "MultilineStringLiteralLine",
......@@ -1002,13 +1004,13 @@ pub const Tokenizer = struct {
10021004
10031005 .period_asterisk => switch (c) {
10041006 '*' => {
1005 result.id = .Invalid;
1007 result.id = .Invalid_periodasterisks;
10061008 break;
10071009 },
10081010 else => {
10091011 result.id = .PeriodAsterisk;
10101012 break;
1011 }
1013 },
10121014 },
10131015
10141016 .slash => switch (c) {
......@@ -1794,7 +1796,7 @@ test "correctly parse pointer dereference followed by asterisk" {
17941796
17951797 testTokenize("\"b\".*** 10", &[_]Token.Id{
17961798 .StringLiteral,
1797 .Invalid,
1799 .Invalid_periodasterisks,
17981800 .AsteriskAsterisk,
17991801 .IntegerLiteral,
18001802 });