loader: rename `get_group` to `identify_group`
[nit.git] / src / loader.nit
index 00d957a..27ab2c3 100644 (file)
@@ -134,8 +134,8 @@ redef class ModelBuilder
                self.toolcontext.info("*** PARSE ***", 1)
                var mmodules = new ArraySet[MModule]
                for a in names do
-                       # Case of a group
-                       var mgroup = self.get_mgroup(a)
+                       # Case of a group (root or sub-directory)
+                       var mgroup = self.identify_group(a)
                        if mgroup != null then
                                mmodules.add_all parse_group(mgroup)
                                continue
@@ -149,7 +149,7 @@ redef class ModelBuilder
                                # Try each entry as a group or a module
                                for f in fs do
                                        var af = a/f
-                                       mgroup = get_mgroup(af)
+                                       mgroup = identify_group(af)
                                        if mgroup != null then
                                                mmodules.add_all parse_group(mgroup)
                                                continue
@@ -274,7 +274,7 @@ redef class ModelBuilder
                        var mp = identify_file((dirname/"{name}.nit").simplify_path)
                        if mp != null then res.add mp
                        # Try the default module of a group
-                       var g = get_mgroup((dirname/name).simplify_path)
+                       var g = identify_group((dirname/name).simplify_path)
                        if g != null then
                                scan_group(g)
                                res.add_all g.mmodule_paths_by_name(name)
@@ -293,7 +293,7 @@ redef class ModelBuilder
                var res = new ArraySet[MGroup]
                for dirname in lookpaths do
                        # try a single group directory
-                       var mg = get_mgroup(dirname/name)
+                       var mg = identify_group(dirname/name)
                        if mg != null then
                                res.add mg
                        end
@@ -333,7 +333,7 @@ redef class ModelBuilder
                        # Found nothing? maybe it is a group...
                        var candidate = null
                        if path.file_exists then
-                               var mgroup = get_mgroup(path)
+                               var mgroup = identify_group(path)
                                if mgroup != null then
                                        var owner_path = mgroup.filepath.join_path(mgroup.name + ".nit")
                                        if owner_path.file_exists then candidate = owner_path
@@ -352,13 +352,15 @@ redef class ModelBuilder
                end
 
                # Fast track, the path is already known
-               var pn = path.basename(".nit")
+               if identified_files_by_path.has_key(path) then return identified_files_by_path[path]
                var rp = module_absolute_path(path)
                if identified_files_by_path.has_key(rp) then return identified_files_by_path[rp]
 
+               var pn = path.basename(".nit")
+
                # Search for a group
                var mgrouppath = path.join_path("..").simplify_path
-               var mgroup = get_mgroup(mgrouppath)
+               var mgroup = identify_group(mgrouppath)
 
                if mgroup == null then
                        # singleton package
@@ -380,6 +382,7 @@ redef class ModelBuilder
                mgroup.module_paths.add(res)
 
                identified_files_by_path[rp] = res
+               identified_files_by_path[path] = res
                identified_files.add(res)
                return res
        end
@@ -391,12 +394,17 @@ redef class ModelBuilder
        # If the directory is not a group null is returned.
        #
        # Note: `paths` is also used to look for mgroups
-       fun get_mgroup(dirpath: String): nullable MGroup
+       fun identify_group(dirpath: String): nullable MGroup
        do
-               if not dirpath.file_exists then do
+               var stat = dirpath.file_stat
+
+               if stat == null then do
+                       # search dirless directories in known -I paths
+                       if dirpath.chars.has('/') then return null
                        for p in paths do
                                var try = p / dirpath
-                               if try.file_exists then
+                               stat = try.file_stat
+                               if stat != null then
                                        dirpath = try
                                        break label
                                end
@@ -404,20 +412,19 @@ redef class ModelBuilder
                        return null
                end label
 
+               # Filter out non-directories
+               if not stat.is_dir then
+                       return null
+               end
+
+               # Fast track, the path is already known
                var rdp = module_absolute_path(dirpath)
                if mgroups.has_key(rdp) then
                        return mgroups[rdp]
                end
 
-               # Filter out non-directories
-               var stat = dirpath.file_stat
-               if stat == null or not stat.is_dir then
-                       mgroups[rdp] = null
-                       return null
-               end
-
                # By default, the name of the package or group is the base_name of the directory
-               var pn = rdp.basename(".nit")
+               var pn = rdp.basename
 
                # Check `package.ini` that indicate a package
                var ini = null
@@ -448,7 +455,7 @@ redef class ModelBuilder
                        var stopper = parentpath / "packages.ini"
                        if not stopper.file_exists then
                                # Recursively get the parent group
-                               parent = get_mgroup(parentpath)
+                               parent = identify_group(parentpath)
                                if parent == null then
                                        # Parent is not a group, thus we are not a group either
                                        mgroups[rdp] = null
@@ -523,12 +530,13 @@ redef class ModelBuilder
                if p == null then return
                for f in p.files do
                        var fp = p/f
-                       var g = get_mgroup(fp)
+                       var g = identify_group(fp)
                        # Recursively scan for groups of the same package
-                       if g != null and g.mpackage == mgroup.mpackage then
+                       if g == null then
+                               identify_file(fp)
+                       else if g.mpackage == mgroup.mpackage then
                                scan_group(g)
                        end
-                       identify_file(fp)
                end
        end
 
@@ -811,11 +819,13 @@ redef class ModelBuilder
                        # Load the imported module
                        var suppath = seach_module_by_amodule_name(aimport.n_name, mmodule.mgroup)
                        if suppath == null then
+                               mmodule.is_broken = true
                                nmodule.mmodule = null # invalidate the module
                                continue # Skip error
                        end
                        var sup = load_module_path(suppath)
                        if sup == null then
+                               mmodule.is_broken = true
                                nmodule.mmodule = null # invalidate the module
                                continue # Skip error
                        end
@@ -824,16 +834,20 @@ redef class ModelBuilder
                        imported_modules.add(sup)
                        var mvisibility = aimport.n_visibility.mvisibility
                        if mvisibility == protected_visibility then
+                               mmodule.is_broken = true
                                error(aimport.n_visibility, "Error: only properties can be protected.")
+                               mmodule.is_broken = true
                                nmodule.mmodule = null # invalidate the module
                                return
                        end
                        if sup == mmodule then
                                error(aimport.n_name, "Error: dependency loop in module {mmodule}.")
+                               mmodule.is_broken = true
                                nmodule.mmodule = null # invalidate the module
                        end
                        if sup.in_importation < mmodule then
                                error(aimport.n_name, "Error: dependency loop between modules {mmodule} and {sup}.")
+                               mmodule.is_broken = true
                                nmodule.mmodule = null # invalidate the module
                                return
                        end
@@ -843,6 +857,7 @@ redef class ModelBuilder
                        var mod_name = "core"
                        var sup = self.get_mmodule_by_name(nmodule, null, mod_name)
                        if sup == null then
+                               mmodule.is_broken = true
                                nmodule.mmodule = null # invalidate the module
                        else # Skip error
                                imported_modules.add(sup)