Browse Source

Merge pull request #3544 from ntn9995/fix-parser-empty-or-no-pkg

Fix core:odin/parser crashing on empty and/or no package files
gingerBill 1 year ago
parent
commit
b4d0b1d17d
1 changed files with 11 additions and 1 deletions
  1. 11 1
      core/odin/parser/parse_files.odin

+ 11 - 1
core/odin/parser/parse_files.odin

@@ -6,6 +6,7 @@ import "core:path/filepath"
 import "core:fmt"
 import "core:fmt"
 import "core:os"
 import "core:os"
 import "core:slice"
 import "core:slice"
+import "core:strings"
 
 
 collect_package :: proc(path: string) -> (pkg: ^ast.Package, success: bool) {
 collect_package :: proc(path: string) -> (pkg: ^ast.Package, success: bool) {
 	NO_POS :: tokenizer.Pos{}
 	NO_POS :: tokenizer.Pos{}
@@ -32,11 +33,18 @@ collect_package :: proc(path: string) -> (pkg: ^ast.Package, success: bool) {
 		if !ok {
 		if !ok {
 			return
 			return
 		}
 		}
+
 		src, ok = os.read_entire_file(fullpath)
 		src, ok = os.read_entire_file(fullpath)
 		if !ok {
 		if !ok {
 			delete(fullpath)
 			delete(fullpath)
 			return
 			return
 		}
 		}
+		if strings.trim_space(string(src)) == "" {
+			delete(fullpath)
+			delete(src)
+			continue
+		}
+
 		file := ast.new(ast.File, NO_POS, NO_POS)
 		file := ast.new(ast.File, NO_POS, NO_POS)
 		file.pkg = pkg
 		file.pkg = pkg
 		file.src = string(src)
 		file.src = string(src)
@@ -69,7 +77,9 @@ parse_package :: proc(pkg: ^ast.Package, p: ^Parser = nil) -> bool {
 		if !parse_file(p, file) {
 		if !parse_file(p, file) {
 			ok = false
 			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
 			pkg.name = file.pkg_decl.name
 		} else if 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)
 			error(p, file.pkg_decl.pos, "different package name, expected '%s', got '%s'", pkg.name, file.pkg_decl.name)