From 961a6ed3b9db5ffa2edce47290c239a9eb6a2b37 Mon Sep 17 00:00:00 2001 From: Benoit Giannangeli Date: Mon, 30 Oct 2023 10:31:48 +0100 Subject: [PATCH] fix(parser): Properly recover when import path is wrong token --- src/parser.zig | 32 +++++++++++++++++----- src/reporter.zig | 1 + tests/compile_errors/023-empty-import.buzz | 4 +++ 3 files changed, 30 insertions(+), 7 deletions(-) create mode 100644 tests/compile_errors/023-empty-import.buzz diff --git a/src/parser.zig b/src/parser.zig index dec3ae98..3e6a79f9 100644 --- a/src/parser.zig +++ b/src/parser.zig @@ -2802,8 +2802,23 @@ pub const Parser = struct { try self.consume(.String, "Expected import path."); - var path = self.parser.previous_token.?; - var file_name: []const u8 = path.lexeme[1..(path.lexeme.len - 1)]; + const path = self.parser.previous_token.?; + if (path.lexeme.len <= 1 or path.literal_string.?.len <= 0) { + self.reporter.reportErrorAt( + .empty_import, + path, + "Import path can't be empty", + ); + } + const file_name: []const u8 = if (path.lexeme.len <= 1 or path.literal_string.?.len <= 0) invalid: { + self.reporter.reportErrorAt( + .empty_import, + path, + "Import path can't be empty", + ); + + break :invalid "invalid"; + } else path.lexeme[1..(path.lexeme.len - 1)]; if (imported_symbols.count() == 0 and try self.match(.As)) { try self.consume(.Identifier, "Expected identifier after `as`."); @@ -2812,11 +2827,14 @@ pub const Parser = struct { try self.consume(.Semicolon, "Expected `;` after import."); - var import = try self.importScript( - file_name, - if (prefix) |pr| pr.lexeme else null, - &imported_symbols, - ); + var import = if (!self.reporter.had_error) + try self.importScript( + file_name, + if (prefix) |pr| pr.lexeme else null, + &imported_symbols, + ) + else + null; if (imported_symbols.count() > 0) { var it = imported_symbols.iterator(); diff --git a/src/reporter.zig b/src/reporter.zig index 80a99ba2..04c81ea6 100644 --- a/src/reporter.zig +++ b/src/reporter.zig @@ -101,6 +101,7 @@ pub const Error = enum(u8) { discarded_value = 88, unused_argument = 89, inferred_type = 90, + empty_import = 91, }; // Inspired by https://github.com/zesterer/ariadne diff --git a/tests/compile_errors/023-empty-import.buzz b/tests/compile_errors/023-empty-import.buzz new file mode 100644 index 00000000..ffc430ab --- /dev/null +++ b/tests/compile_errors/023-empty-import.buzz @@ -0,0 +1,4 @@ +| Import path can't be empty +import ""; + +test "empty import" {} \ No newline at end of file