Skip to content

Commit

Permalink
fix(parser): Properly recover when import path is wrong token
Browse files Browse the repository at this point in the history
  • Loading branch information
giann committed Oct 30, 2023
1 parent fab58f3 commit 961a6ed
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 7 deletions.
32 changes: 25 additions & 7 deletions src/parser.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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`.");
Expand All @@ -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();
Expand Down
1 change: 1 addition & 0 deletions src/reporter.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions tests/compile_errors/023-empty-import.buzz
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
| Import path can't be empty
import "";

test "empty import" {}

0 comments on commit 961a6ed

Please sign in to comment.