diff options
| author | gingerBill <gingerBill@users.noreply.github.com> | 2024-05-10 16:01:16 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-05-10 16:01:16 +0100 |
| commit | b4d0b1d17daa28084375b4bc079dbf28b640a00b (patch) | |
| tree | 6a3262f90550091e04b5d45bb0bb5a9d7d44e23a | |
| parent | 20d35acce19a5d3d3ada15efe26a8108b4fcb211 (diff) | |
| parent | 3add85e7a725a3b6da0f7ac10df24cfb51591473 (diff) | |
Merge pull request #3544 from ntn9995/fix-parser-empty-or-no-pkg
Fix core:odin/parser crashing on empty and/or no package files
| -rw-r--r-- | core/odin/parser/parse_files.odin | 12 |
1 files changed, 11 insertions, 1 deletions
diff --git a/core/odin/parser/parse_files.odin b/core/odin/parser/parse_files.odin index 6452faf4c..5f455c749 100644 --- a/core/odin/parser/parse_files.odin +++ b/core/odin/parser/parse_files.odin @@ -6,6 +6,7 @@ import "core:path/filepath" import "core:fmt" import "core:os" import "core:slice" +import "core:strings" collect_package :: proc(path: string) -> (pkg: ^ast.Package, success: bool) { NO_POS :: tokenizer.Pos{} @@ -32,11 +33,18 @@ collect_package :: proc(path: string) -> (pkg: ^ast.Package, success: bool) { if !ok { return } + src, ok = os.read_entire_file(fullpath) if !ok { delete(fullpath) return } + if strings.trim_space(string(src)) == "" { + delete(fullpath) + delete(src) + continue + } + file := ast.new(ast.File, NO_POS, NO_POS) file.pkg = pkg file.src = string(src) @@ -69,7 +77,9 @@ parse_package :: proc(pkg: ^ast.Package, p: ^Parser = nil) -> bool { if !parse_file(p, file) { ok = false } - if pkg.name == "" { + if file.pkg_decl == nil { + error(p, p.curr_tok.pos, "Expected a package declaration at the start of the file") + } else if pkg.name == "" { pkg.name = file.pkg_decl.name } else if pkg.name != file.pkg_decl.name { error(p, file.pkg_decl.pos, "different package name, expected '%s', got '%s'", pkg.name, file.pkg_decl.name) |