1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Copyright 2012 Jean Privat <jean@pryen.org>
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
17 # Loading of Nit source files
19 # The loader takes care of looking for module and projects in the file system, and the associated case of errors.
20 # The loading requires several steps:
22 # Identify: create an empty model entity associated to a name or a file path.
23 # Identification is used for instance when names are given in the command line.
24 # See `identify_module` and `identify_group`.
26 # Scan: visit directories and identify their contents.
27 # Scanning is done to enable the searching of modules in projects.
28 # See `scan_group` and `scan_full`.
30 # Parse: load the AST and associate it with the model entity.
31 # See `MModule::parse`.
33 # Import: means recursively load modules imported by a module.
34 # See `build_module_importation`.
36 # Load: means doing the full sequence: identify, parse and import.
37 # See `ModelBuilder::parse`, `ModelBuilder::parse_full`, `MModule::load` `ModelBuilder::load_module.
40 import modelbuilder_base
44 redef class ToolContext
46 var opt_path
= new OptionArray("Add an additional include path (may be used more than once)", "-I", "--path")
48 # Option --only-metamodel
49 var opt_only_metamodel
= new OptionBool("Stop after meta-model processing", "--only-metamodel")
52 var opt_only_parse
= new OptionBool("Only proceed to parse files", "--only-parse")
57 option_context
.add_option
(opt_path
, opt_only_parse
, opt_only_metamodel
)
61 redef class ModelBuilder
66 # Setup the paths value
67 paths
.append
(toolcontext
.opt_path
.value
)
69 # Packages managed by nitpm, only use when not testing with tests.sh
70 if "NIT_TESTING_TESTS_SH".environ
!= "true" then
71 paths
.add nitpm_lib_dir
74 var path_env
= "NIT_PATH".environ
75 if not path_env
.is_empty
then
76 paths
.append
(path_env
.split_with
(':'))
79 var nit_dir
= toolcontext
.nit_dir
80 if nit_dir
!= null then
81 var libname
= nit_dir
/"lib"
82 if libname
.file_exists
then paths
.add
(libname
)
83 libname
= nit_dir
/"contrib"
84 if libname
.file_exists
then paths
.add
(libname
)
88 # Load a bunch of modules.
89 # `modules` can contains filenames or module names.
90 # Imported modules are automatically loaded and modelized.
91 # The result is the corresponding model elements.
92 # Errors and warnings are printed with the toolcontext.
94 # Note: class and property model elements are not analysed.
95 fun parse
(modules
: Sequence[String]): Array[MModule]
98 # Parse and recursively load
99 self.toolcontext
.info
("*** PARSE ***", 1)
100 var mmodules
= new ArraySet[MModule]
102 var nmodule
= self.load_module
(a
)
103 if nmodule
== null then continue # Skip error
104 var mmodule
= nmodule
.mmodule
105 if mmodule
== null then continue # skip error
109 self.toolcontext
.info
("*** END PARSE: {time1-time0} ***", 2)
111 self.toolcontext
.check_errors
113 if toolcontext
.opt_only_parse
.value
then
114 self.toolcontext
.info
("*** ONLY PARSE...", 1)
115 self.toolcontext
.quit
121 # Identify a bunch of modules and groups.
123 # This does the same as `parse_full` but does only the identification (cf. `identify_module`)
124 fun scan_full
(names
: Sequence[String]): Array[MModule]
126 var mmodules
= new Array[MModule]
128 # Case of a group (root or sub-directory)
129 var mgroup
= self.identify_group
(a
)
130 if mgroup
!= null then
132 for mg
in mgroup
.in_nesting
.smallers
do mmodules
.add_all mg
.mmodules
136 # Case of a directory that is not a group
137 var stat
= a
.to_path
.stat
138 if stat
!= null and stat
.is_dir
then
139 self.toolcontext
.info
("look in directory {a}", 2)
141 alpha_comparator
.sort
(fs
)
142 # Try each entry as a group or a module
144 if f
.first
== '.' then continue
146 mgroup
= identify_group
(af
)
147 if mgroup
!= null then
149 for mg
in mgroup
.in_nesting
.smallers
do mmodules
.add_all mg
.mmodules
152 var mmodule
= identify_module
(af
)
153 if mmodule
!= null then
156 self.toolcontext
.info
("ignore file {af}", 2)
162 var mmodule
= identify_module
(a
)
163 if mmodule
== null then
164 var le
= last_loader_error
166 toolcontext
.error
(null, le
)
167 else if a
.file_exists
then
168 toolcontext
.error
(null, "Error: `{a}` is not a Nit source file.")
170 toolcontext
.error
(null, "Error: cannot find module `{a}`.")
180 # Load a bunch of modules and groups.
184 # * a path to a module, a group or a directory of packages.
185 # * a short name of a module or a group that are looked in the `paths` (-I)
187 # Then, for each entry, if it is:
189 # * a module, then is it parsed and returned.
190 # * a group then recursively all its modules are parsed.
191 # * a directory of packages then all the modules of all packages are parsed.
192 # * else an error is displayed.
194 # See `parse` for details.
195 fun parse_full
(names
: Sequence[String]): Array[MModule]
198 # Parse and recursively load
199 self.toolcontext
.info
("*** PARSE ***", 1)
200 var mmodules
= new ArraySet[MModule]
201 var scans
= scan_full
(names
)
202 for mmodule
in scans
do
203 var ast
= mmodule
.load
(self)
204 if ast
== null then continue # Skip error
208 self.toolcontext
.info
("*** END PARSE: {time1-time0} ***", 2)
210 self.toolcontext
.check_errors
212 if toolcontext
.opt_only_parse
.value
then
213 self.toolcontext
.info
("*** ONLY PARSE...", 1)
214 self.toolcontext
.quit
220 # The list of directories to search for top level modules
221 # The list is initially set with:
223 # * the toolcontext --path option
224 # * the NIT_PATH environment variable
225 # * `toolcontext.nit_dir`
226 # Path can be added (or removed) by the client
227 var paths
= new Array[String]
229 # Like (and used by) `get_mmodule_by_name` but does not force the parsing of the MModule (cf. `identify_module`)
230 fun search_mmodule_by_name
(anode
: nullable ANode, mgroup
: nullable MGroup, name
: String): nullable MModule
232 # First, look in groups
235 var r
= c
.mpackage
.root
238 var res
= r
.mmodules_by_name
(name
)
239 if res
.not_empty
then return res
.first
242 # Look at some known directories
243 var lookpaths
= self.paths
245 # Look in the directory of the group package also (even if not explicitly in the path)
246 if mgroup
!= null then
247 # path of the root group
248 var dirname
= mgroup
.mpackage
.root
.filepath
249 if dirname
!= null then
250 dirname
= dirname
.join_path
("..").simplify_path
251 if not lookpaths
.has
(dirname
) and dirname
.file_exists
then
252 lookpaths
= lookpaths
.to_a
253 lookpaths
.add
(dirname
)
258 if mgroup
!= null then
259 var alias
= mgroup
.mpackage
.import_alias
(name
)
260 if alias
!= null then name
= alias
264 if anode
!= null then loc
= anode
.hot_location
265 var candidate
= search_module_in_paths
(loc
, name
, lookpaths
)
267 if candidate
== null then
268 if mgroup
!= null then
269 error
(anode
, "Error: cannot find module `{name}` from `{mgroup.name}`. Tried: {lookpaths.join(", ")}.")
271 error
(anode
, "Error: cannot find module `{name}`. Tried: {lookpaths.join(", ")}.")
278 # Get a module by its short name; if required, the module is loaded, parsed and its hierarchies computed.
279 # If `mgroup` is set, then the module search starts from it up to the top level (see `paths`);
280 # if `mgroup` is null then the module is searched in the top level only.
281 # If no module exists or there is a name conflict, then an error on `anode` is displayed and null is returned.
282 fun get_mmodule_by_name
(anode
: nullable ANode, mgroup
: nullable MGroup, name
: String): nullable MModule
284 var mmodule
= search_mmodule_by_name
(anode
, mgroup
, name
)
285 if mmodule
== null then return null # Forward error
286 var ast
= mmodule
.load
(self)
287 if ast
== null then return null # Forward error
291 # Search a module `name` from path `lookpaths`.
292 # If found, the module is returned.
293 private fun search_module_in_paths
(location
: nullable Location, name
: String, lookpaths
: Collection[String]): nullable MModule
296 if name
.has
('=') then
297 name_no_version
= name
.split
('=').first
298 else name_no_version
= name
300 var res
= new ArraySet[MModule]
301 for dirname
in lookpaths
do
302 # Try a single module file
303 var mp
= identify_module
((dirname
/"{name}.nit").simplify_path
)
304 if mp
!= null then res
.add mp
305 # Try the default module of a group
306 var g
= identify_group
((dirname
/name
).simplify_path
)
309 res
.add_all g
.mmodules_by_name
(name_no_version
)
312 if res
.is_empty
then return null
313 if res
.length
> 1 then
314 toolcontext
.error
(location
, "Error: conflicting module files for `{name}`: `{[for x in res do x.filepath or else x.full_name].join("`, `")}`")
319 # Search groups named `name` from paths `lookpaths`.
320 private fun search_group_in_paths
(name
: String, lookpaths
: Collection[String]): ArraySet[MGroup]
322 var res
= new ArraySet[MGroup]
323 for dirname
in lookpaths
do
324 # try a single group directory
325 var mg
= identify_group
(dirname
/name
)
333 # Cache for `identify_module` by relative and real paths
334 private var identified_modules_by_path
= new HashMap[String, nullable MModule]
336 # All the currently identified modules.
337 # See `identify_module`.
339 # An identified module exists in the model but might be not yet parsed (no AST), or not yet analysed (no importation).
340 var identified_modules
= new Array[MModule]
342 # All the currently parsed modules.
344 # A parsed module exists in the model but might be not yet analysed (no importation).
345 var parsed_modules
= new Array[MModule]
347 # Some `loader` services are silent and return `null` on error.
349 # Those services can set `last_loader_error` to precise an specific error message.
350 # if `last_loader_error == null` then a generic error message can be used.
352 # See `identified_modules` and `identify_group` for details.
353 var last_loader_error
: nullable String = null
355 # Identify a source file and load the associated package and groups if required.
357 # This method does what the user expects when giving an argument to a Nit tool.
359 # * If `path` is an existing Nit source file (with the `.nit` extension),
360 # then the associated MModule is returned
361 # * If `path` is a directory (with a `/`),
362 # then the MModule of its default module is returned (if any)
363 # * If `path` is a simple identifier (eg. `digraph`),
364 # then the main module of the package `digraph` is searched in `paths` and returned.
366 # Silently return `null` if `path` does not exists or cannot be identified.
367 # If `null` is returned, `last_loader_error` can be set to a specific error message.
369 # On success, it returns a module that is possibly not yet parsed (no AST), or not yet analysed (no importation).
370 # If the module was already identified, or loaded, it is returned.
371 fun identify_module
(path
: String): nullable MModule
373 last_loader_error
= null
375 # special case for not a nit file
376 if not path
.has_suffix
(".nit") then do
377 # search dirless files in known -I paths
378 if not path
.chars
.has
('/') then
379 var res
= search_module_in_paths
(null, path
, self.paths
)
380 if res
!= null then return res
383 # Found nothing? maybe it is a group...
384 if path
.file_exists
then
385 var mgroup
= identify_group
(path
)
386 if mgroup
!= null then
387 var owner_path
= mgroup
.filepath
.join_path
(mgroup
.name
+ ".nit")
388 if owner_path
.file_exists
then
395 # Found nothing? maybe it is a qualified name
396 if path
.chars
.has
(':') then
397 var ids
= path
.split
("::")
398 var g
= identify_group
(ids
.first
)
401 var ms
= g
.mmodules_by_name
(ids
.last
)
405 if m
.full_name
== path
then
410 # Where there is only one or two names `foo::bar`
411 # then accept module that matches `foo::*::bar`
412 if ids
.length
<= 2 then
413 if ms
.length
== 1 then return ms
.first
414 if ms
.length
> 1 then
415 var l
= new Array[String]
418 if fp
!= null then fp
= " ({fp})" else fp
= ""
419 l
.add
"`{m.full_name}`{fp}"
421 last_loader_error
= "Error: conflicting module for `{path}`: {l.join(", ")} "
426 var bests
= new BestDistance[String](path
.length
/ 2)
427 # We found nothing. But propose something in the package?
428 for sg
in g
.mpackage
.mgroups
do
429 for m
in sg
.mmodules
do
430 var d
= path
.levenshtein_distance
(m
.full_name
)
431 bests
.update
(d
, m
.full_name
)
434 var last_loader_error
= "Error: cannot find module `{path}`."
435 if bests
.best_items
.not_empty
then
436 last_loader_error
+= " Did you mean " + bests
.best_items
.join
(", ", " or ") + "?"
438 self.last_loader_error
= last_loader_error
446 # Does the file exists?
447 if not path
.file_exists
then
451 # Fast track, the path is already known
452 if identified_modules_by_path
.has_key
(path
) then return identified_modules_by_path
[path
]
453 var rp
= module_absolute_path
(path
)
454 if identified_modules_by_path
.has_key
(rp
) then return identified_modules_by_path
[rp
]
456 var pn
= path
.basename
(".nit")
459 var mgrouppath
= path
.join_path
("..").simplify_path
460 var mgroup
= identify_group
(mgrouppath
)
462 if mgroup
!= null then
463 var mpackage
= mgroup
.mpackage
464 if not mpackage
.accept
(path
) then
466 toolcontext
.info
("module `{path}` excluded from package `{mpackage}`", 2)
469 if mgroup
== null then
471 var loc
= new Location.opaque_file
(path
)
472 var mpackage
= new MPackage(pn
, model
, loc
)
473 mgroup
= new MGroup(pn
, loc
, mpackage
, null) # same name for the root group
474 mpackage
.root
= mgroup
475 toolcontext
.info
("found singleton package `{pn}` at {path}", 2)
477 # Attach homonymous `ini` file to the package
478 var inipath
= path
.dirname
/ "{pn}.ini"
479 if inipath
.file_exists
then
480 var ini
= new ConfigTree(inipath
)
485 var loc
= new Location.opaque_file
(path
)
486 var res
= new MModule(model
, mgroup
, pn
, loc
)
488 identified_modules_by_path
[rp
] = res
489 identified_modules_by_path
[path
] = res
490 identified_modules
.add
(res
)
495 private var mgroups
= new HashMap[String, nullable MGroup]
497 # Return the mgroup associated to a directory path.
498 # If the directory is not a group null is returned.
500 # Silently return `null` if `dirpath` does not exists, is not a directory,
501 # cannot be identified or cannot be attached to a mpackage.
502 # If `null` is returned, `last_loader_error` can be set to a specific error message.
504 # Note: `paths` is also used to look for mgroups
505 fun identify_group
(dirpath
: String): nullable MGroup
508 last_loader_error
= null
510 var stat
= dirpath
.file_stat
512 if stat
== null or not stat
.is_dir
then do
513 # search dirless directories in known -I paths
514 if dirpath
.chars
.has
('/') then return null
516 var try
= p
/ dirpath
526 # Filter out non-directories
527 if not stat
.is_dir
then
528 last_loader_error
= "Error: `{dirpath}` is not a directory."
532 # Fast track, the path is already known
533 var rdp
= module_absolute_path
(dirpath
)
534 if mgroups
.has_key
(rdp
) then
538 # By default, the name of the package or group is the base_name of the directory
539 var pn
= rdp
.basename
541 # Check `package.ini` that indicate a package
544 var inipath
= dirpath
/ "package.ini"
545 if inipath
.file_exists
then
546 ini
= new ConfigTree(inipath
)
550 # No ini, multiple course of action
552 # The root of the directory hierarchy in the file system.
555 last_loader_error
= "Error: `{dirpath}` is not a Nit package."
559 # Special stopper `packages.ini`
560 if (dirpath
/"packages.ini").file_exists
then
561 # dirpath cannot be a package since it is a package directory
563 last_loader_error
= "Error: `{dirpath}` is not a Nit package."
567 # check the parent directory (if it does not contain the stopper file)
568 var parentpath
= dirpath
.join_path
("..").simplify_path
569 var stopper
= parentpath
/ "packages.ini"
570 if not stopper
.file_exists
then
571 # Recursively get the parent group
572 parent
= identify_group
(parentpath
)
573 if parent
!= null then do
574 var mpackage
= parent
.mpackage
575 if not mpackage
.accept
(dirpath
) then
576 toolcontext
.info
("directory `{dirpath}` excluded from package `{mpackage}`", 2)
580 if parent
== null then
581 # Parent is not a group, thus we are not a group either
583 last_loader_error
= "Error: `{dirpath}` is not a Nit package."
589 var loc
= new Location.opaque_file
(dirpath
)
591 if parent
== null then
592 # no parent, thus new package
593 if ini
!= null then pn
= ini
["package.name"] or else pn
594 var mpackage
= new MPackage(pn
, model
, loc
)
595 mgroup
= new MGroup(pn
, loc
, mpackage
, null) # same name for the root group
596 mpackage
.root
= mgroup
597 toolcontext
.info
("found package `{mpackage}` at {dirpath}", 2)
600 mgroup
= new MGroup(pn
, loc
, parent
.mpackage
, parent
)
601 toolcontext
.info
("found sub group `{mgroup.full_name}` at {dirpath}", 2)
604 # search documentation
605 # in src first so the documentation of the package code can be distinct for the documentation of the package usage
606 var readme
= dirpath
.join_path
("README.md")
607 if not readme
.file_exists
then readme
= dirpath
.join_path
("README")
608 if readme
.file_exists
then
609 var mdoc
= load_markdown
(readme
)
611 mdoc
.original_mentity
= mgroup
614 mgroups
[rdp
] = mgroup
618 # Load a markdown file as a documentation object
619 fun load_markdown
(filepath
: String): MDoc
621 var s
= new FileReader.open
(filepath
)
622 var lines
= new Array[String]
623 var line_starts
= new Array[Int]
626 var line
= s
.read_line
629 len
+= line
.length
+ 1
632 var source
= new SourceFile.from_string
(filepath
, lines
.join
("\n"))
633 source
.line_starts
.add_all line_starts
634 var mdoc
= new MDoc(new Location(source
, 1, lines
.length
, 0, 0))
635 mdoc
.content
.add_all
(lines
)
639 # Force the identification of all MModule of the group and sub-groups in the file system.
641 # When a group is scanned, its sub-groups hierarchy is filled (see `MGroup::in_nesting`)
642 # and the potential modules (and nested modules) are identified (see `MGroup::modules`).
644 # Basically, this recursively call `identify_group` and `identify_module` on each directory entry.
646 # No-op if the group was already scanned (see `MGroup::scanned`).
647 fun scan_group
(mgroup
: MGroup) do
648 if mgroup
.scanned
then return
649 mgroup
.scanned
= true
650 var p
= mgroup
.filepath
651 # a virtual group has nothing to scan
652 if p
== null then return
654 alpha_comparator
.sort
(files
)
656 if f
.first
== '.' then continue
658 var g
= identify_group
(fp
)
659 # Recursively scan for groups of the same package
662 else if g
.mpackage
== mgroup
.mpackage
then
668 # Transform relative paths (starting with '../') into absolute paths
669 private fun module_absolute_path
(path
: String): String do
673 # Try to load a module AST using a path.
674 # Display an error if there is a problem (IO / lexer / parser) and return null
676 # The AST is loaded as is total independence of the model and its entities.
678 # AST are not cached or reused thus a new AST is returned on success.
679 fun load_module_ast
(filename
: String): nullable AModule
681 if not filename
.has_suffix
(".nit") then
682 self.toolcontext
.error
(null, "Error: file `{filename}` is not a valid nit module.")
685 if not filename
.file_exists
then
686 self.toolcontext
.error
(null, "Error: file `{filename}` not found.")
690 self.toolcontext
.info
("load module {filename}", 2)
693 var file
= new FileReader.open
(filename
)
694 var lexer
= new Lexer(new SourceFile(filename
, file
))
695 var parser
= new Parser(lexer
)
696 var tree
= parser
.parse
699 # Handle lexer and parser error
700 var nmodule
= tree
.n_base
701 if nmodule
== null then
702 var neof
= tree
.n_eof
703 assert neof
isa AError
704 error
(neof
, neof
.message
)
711 # Remove Nit source files from a list of arguments.
713 # Items of `args` that can be loaded as a nit file will be removed from `args` and returned.
714 fun filter_nit_source
(args
: Array[String]): Array[String]
716 var keep
= new Array[String]
717 var res
= new Array[String]
719 var stat
= a
.to_path
.stat
720 if stat
!= null and stat
.is_dir
then
724 var l
= identify_module
(a
)
736 # Try to load a module using a path.
737 # Display an error if there is a problem (IO / lexer / parser) and return null.
738 # Note: usually, you do not need this method, use `get_mmodule_by_name` instead.
740 # The MModule is located, created, parsed and the importation is performed.
741 fun load_module
(filename
: String): nullable AModule
743 # Look for the module
744 var mmodule
= identify_module
(filename
)
745 if mmodule
== null then
746 var le
= last_loader_error
748 toolcontext
.error
(null, le
)
749 else if filename
.file_exists
then
750 toolcontext
.error
(null, "Error: `{filename}` is not a Nit source file.")
752 toolcontext
.error
(null, "Error: cannot find module `{filename}`.")
758 return mmodule
.load
(self)
761 # Injection of a new module without source.
762 # Used by the interpreter.
763 fun load_rt_module
(parent
: nullable MModule, nmodule
: AModule, mod_name
: String): nullable MModule
768 if parent
!= null then mgroup
= parent
.mgroup
769 var mmodule
= new MModule(model
, mgroup
, mod_name
, nmodule
.location
)
770 nmodule
.mmodule
= mmodule
771 nmodules
.add
(nmodule
)
772 parsed_modules
.add mmodule
773 self.mmodule2nmodule
[mmodule
] = nmodule
775 if parent
!= null then
776 var imported_modules
= new Array[MModule]
777 imported_modules
.add
(parent
)
778 mmodule
.set_visibility_for
(parent
, intrude_visibility
)
779 mmodule
.set_imported_mmodules
(imported_modules
)
781 build_module_importation
(nmodule
)
786 # Visit the AST and create the `MModule` object
787 private fun build_a_mmodule
(mgroup
: nullable MGroup, nmodule
: AModule)
789 var mmodule
= nmodule
.mmodule
790 assert mmodule
!= null
792 # Check the module name
793 var decl
= nmodule
.n_moduledecl
795 var decl_name
= decl
.n_name
.n_id
.text
796 if decl_name
!= mmodule
.name
then
797 warning
(decl
.n_name
, "module-name-mismatch", "Error: module name mismatch; declared {decl_name} file named {mmodule.name}.")
801 # Check for conflicting module names in the package
802 if mgroup
!= null then
803 var others
= model
.get_mmodules_by_name
(mmodule
.name
)
804 if others
!= null then for other
in others
do
805 if other
!= mmodule
and mmodule2nmodule
.has_key
(mmodule
) and other
.mgroup
!= null and other
.mgroup
.mpackage
== mgroup
.mpackage
then
807 if decl
== null then node
= nmodule
else node
= decl
.n_name
808 error
(node
, "Error: a module named `{other.full_name}` already exists at {other.location}.")
814 nmodules
.add
(nmodule
)
815 self.mmodule2nmodule
[mmodule
] = nmodule
817 var source
= nmodule
.location
.file
818 if source
!= null then
819 assert source
.mmodule
== null
820 source
.mmodule
= mmodule
824 # Extract documentation
825 var ndoc
= decl
.n_doc
827 var mdoc
= ndoc
.to_mdoc
829 mdoc
.original_mentity
= mmodule
831 # Is the module generated?
832 mmodule
.is_generated
= not decl
.get_annotations
("generated").is_empty
836 # Resolve the module identification for a given `AModuleName`.
838 # This method handles qualified names as used in `AModuleName`.
839 fun search_module_by_amodule_name
(n_name
: AModuleName, mgroup
: nullable MGroup): nullable MModule
841 var mod_name
= n_name
.n_id
.text
843 # If a quad is given, we ignore the starting group (go from path)
844 if n_name
.n_quad
!= null then mgroup
= null
846 # If name not qualified, just search the name
847 if n_name
.n_path
.is_empty
then
848 # Fast search if no n_path
849 return search_mmodule_by_name
(n_name
, mgroup
, mod_name
)
852 # If qualified and in a group
853 if mgroup
!= null then
854 # First search in the package
855 var r
= mgroup
.mpackage
.root
858 # Get all modules with the final name
859 var res
= r
.mmodules_by_name
(mod_name
)
860 # Filter out the name that does not match the qualifiers
861 res
= [for x
in res
do if match_amodulename
(n_name
, x
) then x
]
862 if res
.not_empty
then
863 if res
.length
> 1 then
864 error
(n_name
, "Error: conflicting module files for `{mod_name}`: `{[for x in res do x.filepath or else x.full_name].join("`, `")}`")
870 # If no module yet, then assume that the first element of the path
871 # Is to be searched in the path.
872 var root_name
= n_name
.n_path
.first
.text
874 # Search for an alias in required external packages
875 if mgroup
!= null then
876 var alias
= mgroup
.mpackage
.import_alias
(root_name
)
877 if alias
!= null then root_name
= alias
880 var roots
= search_group_in_paths
(root_name
, paths
)
881 if roots
.is_empty
then
882 error
(n_name
, "Error: cannot find `{root_name}`. Tried: {paths.join(", ")}.")
886 var res
= new ArraySet[MModule]
888 # Then, for each root, collect modules that matches the qualifiers
890 var root_res
= r
.mmodules_by_name
(mod_name
)
891 for x
in root_res
do if match_amodulename
(n_name
, x
) then res
.add x
893 if res
.not_empty
then
894 if res
.length
> 1 then
895 error
(n_name
, "Error: conflicting module files for `{mod_name}`: `{[for x in res do x.filepath or else x.full_name].join("`, `")}`")
899 # If still nothing, just call a basic search that will fail and will produce an error message
900 error
(n_name
, "Error: cannot find module `{mod_name}` from `{root_name}`. Tried: {paths.join(", ")}.")
904 # Is elements of `n_name` correspond to the group nesting of `m`?
906 # Basically it check that `bar::foo` matches `bar/foo.nit` and `bar/baz/foo.nit`
907 # but not `baz/foo.nit` nor `foo/bar.nit`
909 # Is used by `search_module_by_amodule_name` to validate qualified names.
910 private fun match_amodulename
(n_name
: AModuleName, m
: MModule): Bool
912 var g
: nullable MGroup = m
.mgroup
913 for grp
in n_name
.n_path
.reverse_iterator
do
914 while g
!= null and grp
.text
!= g
.name
do
921 # Analyze the module importation and fill the module_importation_hierarchy
923 # If the importation was already done (`nmodule.is_importation_done`), this method does a no-op.
925 # REQUIRE `nmodule.mmodule != null`
926 # ENSURE `nmodule.is_importation_done`
927 fun build_module_importation
(nmodule
: AModule)
929 if nmodule
.is_importation_done
then return
930 nmodule
.is_importation_done
= true
931 var mmodule
= nmodule
.mmodule
.as(not null)
933 var imported_modules
= new Array[MModule]
934 for aimport
in nmodule
.n_imports
do
935 # Do not imports conditional
936 var atconditionals
= aimport
.get_annotations
("conditional")
937 if atconditionals
.not_empty
then continue
940 if not aimport
isa AStdImport then
944 # Load the imported module
945 var sup
= search_module_by_amodule_name
(aimport
.n_name
, mmodule
.mgroup
)
947 mmodule
.is_broken
= true
948 nmodule
.mmodule
= null # invalidate the module
949 continue # Skip error
951 var ast
= sup
.load
(self)
953 mmodule
.is_broken
= true
954 nmodule
.mmodule
= null # invalidate the module
955 continue # Skip error
958 aimport
.mmodule
= sup
959 imported_modules
.add
(sup
)
960 var mvisibility
= aimport
.n_visibility
.mvisibility
961 if mvisibility
== protected_visibility
then
962 mmodule
.is_broken
= true
963 error
(aimport
.n_visibility
, "Error: only properties can be protected.")
964 mmodule
.is_broken
= true
965 nmodule
.mmodule
= null # invalidate the module
968 if sup
== mmodule
then
969 error
(aimport
.n_name
, "Error: dependency loop in module {mmodule}.")
970 mmodule
.is_broken
= true
971 nmodule
.mmodule
= null # invalidate the module
973 if sup
.in_importation
< mmodule
then
974 error
(aimport
.n_name
, "Error: dependency loop between modules {mmodule} and {sup}.")
975 mmodule
.is_broken
= true
976 nmodule
.mmodule
= null # invalidate the module
979 mmodule
.set_visibility_for
(sup
, mvisibility
)
982 var mod_name
= "core"
983 var sup
= self.get_mmodule_by_name
(nmodule
, null, mod_name
)
985 mmodule
.is_broken
= true
986 nmodule
.mmodule
= null # invalidate the module
988 imported_modules
.add
(sup
)
989 mmodule
.set_visibility_for
(sup
, public_visibility
)
993 # Declare conditional importation
994 for aimport
in nmodule
.n_imports
do
995 if not aimport
isa AStdImport then continue
996 var atconditionals
= aimport
.get_annotations
("conditional")
997 if atconditionals
.is_empty
then continue
999 var suppath
= search_module_by_amodule_name
(aimport
.n_name
, mmodule
.mgroup
)
1000 if suppath
== null then continue # skip error
1002 for atconditional
in atconditionals
do
1003 var nargs
= atconditional
.n_args
1004 if nargs
.is_empty
then
1005 error
(atconditional
, "Syntax Error: `conditional` expects module identifiers as arguments.")
1010 var rule
= new Array[MModule]
1012 # First element is the goal, thus
1015 # Second element is the first condition, that is to be a client of the current module
1018 # Other condition are to be also a client of each modules indicated as arguments of the annotation
1019 for narg
in nargs
do
1022 error
(narg
, "Syntax Error: `conditional` expects module identifier as arguments.")
1026 var mp
= search_mmodule_by_name
(narg
, mmodule
.mgroup
, id
)
1027 if mp
== null then continue
1032 conditional_importations
.add rule
1036 mmodule
.set_imported_mmodules
(imported_modules
)
1038 apply_conditional_importations
(mmodule
)
1040 self.toolcontext
.info
("{mmodule} imports {mmodule.in_importation.direct_greaters.join(", ")}", 3)
1042 # Force `core` to be public if imported
1043 for sup
in mmodule
.in_importation
.greaters
do
1044 if sup
.name
== "core" then
1045 mmodule
.set_visibility_for
(sup
, public_visibility
)
1049 # TODO: Correctly check for useless importation
1050 # It is even doable?
1051 var directs
= mmodule
.in_importation
.direct_greaters
1052 for nim
in nmodule
.n_imports
do
1053 if not nim
isa AStdImport then continue
1054 var im
= nim
.mmodule
1055 if im
== null then continue
1056 if directs
.has
(im
) then continue
1057 # This generates so much noise that it is simpler to just comment it
1058 #warning(nim, "Warning: possible useless importation of {im}")
1062 # Global list of conditional importation rules.
1064 # Each rule is a "Horn clause"-like sequence of modules.
1065 # It means that the first module is the module to automatically import.
1066 # The remaining modules are the conditions of the rule.
1068 # Rules are declared by `build_module_importation` and are applied by `apply_conditional_importations`
1069 # (and `build_module_importation` that calls it).
1071 # TODO (when the loader will be rewritten): use a better representation and move up rules in the model.
1072 var conditional_importations
= new Array[SequenceRead[MModule]]
1074 # Extends the current importations according to imported rules about conditional importation
1075 fun apply_conditional_importations
(mmodule
: MModule)
1077 # Because a conditional importation may cause additional conditional importation, use a fixed point
1078 # The rules are checked naively because we assume that it does not worth to be optimized
1079 var check_conditional_importations
= true
1080 while check_conditional_importations
do
1081 check_conditional_importations
= false
1083 for ci
in conditional_importations
do
1085 for i
in [1..ci
.length
[ do
1088 if mmodule
== m
or not mmodule
.in_importation
.greaters
.has
(m
) then continue label
1090 # Still here? It means that all conditions modules are loaded and imported
1092 # Identify the module to automatically import
1094 var ast
= sup
.load
(self)
1095 if ast
== null then continue
1097 # Do nothing if already imported
1098 if mmodule
.in_importation
.greaters
.has
(sup
) then continue label
1101 self.toolcontext
.info
("{mmodule} conditionally imports {sup}", 3)
1102 # TODO visibility rules (currently always public)
1103 mmodule
.set_visibility_for
(sup
, public_visibility
)
1104 # TODO linearization rules (currently added at the end in the order of the rules)
1105 mmodule
.set_imported_mmodules
([sup
])
1107 # Prepare to reapply the rules
1108 check_conditional_importations
= true
1113 # All the loaded modules
1114 var nmodules
= new Array[AModule]
1116 # Register the nmodule associated to each mmodule
1118 # Public clients need to use `mmodule2node` to access stuff.
1119 private var mmodule2nmodule
= new HashMap[MModule, AModule]
1121 # Retrieve the associated AST node of a mmodule.
1122 # This method is used to associate model entity with syntactic entities.
1124 # If the module is not associated with a node, returns null.
1125 fun mmodule2node
(mmodule
: MModule): nullable AModule
1127 return mmodule2nmodule
.get_or_null
(mmodule
)
1132 # Force the parsing of the module using `modelbuilder`.
1134 # If the module was already parsed, the existing ASI is returned.
1135 # Else the source file is loaded, and parsed and some
1137 # The importation is not done by this
1139 # REQUIRE: `filepath != null`
1140 # ENSURE: `modelbuilder.parsed_modules.has(self)`
1141 fun parse
(modelbuilder
: ModelBuilder): nullable AModule
1143 # Already known and loaded? then return it
1144 var nmodule
= modelbuilder
.mmodule2nmodule
.get_or_null
(self)
1145 if nmodule
!= null then return nmodule
1147 var filepath
= self.filepath
1148 assert filepath
!= null
1150 nmodule
= modelbuilder
.load_module_ast
(filepath
)
1151 if nmodule
== null then return null # forward error
1154 nmodule
.mmodule
= self
1155 self.location
= nmodule
.location
1156 modelbuilder
.build_a_mmodule
(mgroup
, nmodule
)
1158 modelbuilder
.parsed_modules
.add
self
1162 # Parse and process importation of a given MModule.
1164 # Basically chains `parse` and `build_module_importation`.
1165 fun load
(modelbuilder
: ModelBuilder): nullable AModule
1167 var nmodule
= parse
(modelbuilder
)
1168 if nmodule
== null then return null
1170 modelbuilder
.build_module_importation
(nmodule
)
1175 redef class MPackage
1176 # The associated `.ini` file, if any
1178 # The `ini` file is given as is and might contain invalid or missing information.
1180 # Some packages, like stand-alone packages or virtual packages have no `ini` file associated.
1181 var ini
: nullable ConfigTree = null
1183 # Array of relative source paths excluded according to the `source.exclude` key of the `ini`
1184 var excludes
: nullable Array[String] is lazy
do
1186 if ini
== null then return null
1187 var exclude
= ini
["source.exclude"]
1188 if exclude
== null then return null
1189 var excludes
= exclude
.split
(":")
1193 # Does the source inclusion/inclusion rules of the package `ini` accept such path?
1194 fun accept
(filepath
: String): Bool
1196 var excludes
= self.excludes
1197 if excludes
!= null then
1198 var relpath
= root
.filepath
.relpath
(filepath
)
1199 if excludes
.has
(relpath
) then return false
1204 # Get the name to search for, for a `root_name` declared as `import` in `ini`
1205 fun import_alias
(root_name
: String): nullable String
1207 var map
= import_aliases_parsed
1208 if map
== null then return null
1210 var val
= map
.get_or_null
(root_name
)
1211 if val
== null then return null
1216 private var import_aliases_parsed
: nullable Map[String, ExternalPackage] is lazy
do
1218 if ini
== null then return null
1220 var import_line
= ini
["package.import"]
1221 if import_line
== null then return null
1223 var map
= import_line
.parse_import
1224 if map
.is_empty
then return null
1231 # Is the group interesting for a final user?
1233 # Groups are mandatory in the model but for simple packages they are not
1234 # always interesting.
1236 # A interesting group has, at least, one of the following true:
1238 # * it has 2 modules or more
1239 # * it has a subgroup
1240 # * it has a documentation
1241 fun is_interesting
: Bool
1243 return mmodules
.length
> 1 or
1244 not in_nesting
.direct_smallers
.is_empty
or
1246 (mmodules
.length
== 1 and default_mmodule
== null)
1249 # Are files and directories in self scanned?
1251 # See `ModelBuilder::scan_group`.
1254 # Return the modules in self and subgroups named `name`.
1256 # If `self` is not scanned (see `ModelBuilder::scan_group`) the
1257 # results might be partial.
1258 fun mmodules_by_name
(name
: String): Array[MModule]
1260 var res
= new Array[MModule]
1261 for g
in in_nesting
.smallers
do
1262 for mp
in g
.mmodules
do
1263 if mp
.name
== name
then
1272 redef class SourceFile
1273 # Associated mmodule, once created
1274 var mmodule
: nullable MModule = null
1277 redef class AStdImport
1278 # The imported module once determined
1279 var mmodule
: nullable MModule = null
1283 # The associated MModule once build by a `ModelBuilder`
1284 var mmodule
: nullable MModule = null
1286 # Flag that indicate if the importation is already completed
1287 var is_importation_done
: Bool = false