loader: add contrib to the default path of projects
[nit.git] / src / loader.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2012 Jean Privat <jean@pryen.org>
4 #
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
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
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.
16
17 # Loading of Nit source files
18 module loader
19
20 import modelbuilder_base
21
22 redef class ToolContext
23 # Option --path
24 var opt_path = new OptionArray("Set include path for loaders (may be used more than once)", "-I", "--path")
25
26 # Option --only-metamodel
27 var opt_only_metamodel = new OptionBool("Stop after meta-model processing", "--only-metamodel")
28
29 # Option --only-parse
30 var opt_only_parse = new OptionBool("Only proceed to parse step of loaders", "--only-parse")
31
32 redef init
33 do
34 super
35 option_context.add_option(opt_path, opt_only_parse, opt_only_metamodel)
36 end
37 end
38
39 redef class ModelBuilder
40 redef init
41 do
42 super
43
44 # Setup the paths value
45 paths.append(toolcontext.opt_path.value)
46
47 var path_env = "NIT_PATH".environ
48 if not path_env.is_empty then
49 paths.append(path_env.split_with(':'))
50 end
51
52 var nit_dir = toolcontext.nit_dir
53 var libname = nit_dir/"lib"
54 if libname.file_exists then paths.add(libname)
55 libname = nit_dir/"contrib"
56 if libname.file_exists then paths.add(libname)
57 end
58
59 # Load a bunch of modules.
60 # `modules` can contains filenames or module names.
61 # Imported modules are automatically loaded and modelized.
62 # The result is the corresponding model elements.
63 # Errors and warnings are printed with the toolcontext.
64 #
65 # Note: class and property model elements are not analysed.
66 fun parse(modules: Sequence[String]): Array[MModule]
67 do
68 var time0 = get_time
69 # Parse and recursively load
70 self.toolcontext.info("*** PARSE ***", 1)
71 var mmodules = new ArraySet[MModule]
72 for a in modules do
73 var nmodule = self.load_module(a)
74 if nmodule == null then continue # Skip error
75 # Load imported module
76 build_module_importation(nmodule)
77 var mmodule = nmodule.mmodule
78 if mmodule == null then continue # skip error
79 mmodules.add mmodule
80 end
81 var time1 = get_time
82 self.toolcontext.info("*** END PARSE: {time1-time0} ***", 2)
83
84 self.toolcontext.check_errors
85
86 if toolcontext.opt_only_parse.value then
87 self.toolcontext.info("*** ONLY PARSE...", 1)
88 exit(0)
89 end
90
91 return mmodules.to_a
92 end
93
94 # Load recursively all modules of the group `mgroup`.
95 # See `parse` for details.
96 fun parse_group(mgroup: MGroup): Array[MModule]
97 do
98 var res = new Array[MModule]
99 visit_group(mgroup)
100 for mg in mgroup.in_nesting.smallers do
101 for mp in mg.module_paths do
102 var nmodule = self.load_module(mp.filepath)
103 if nmodule == null then continue # Skip error
104 # Load imported module
105 build_module_importation(nmodule)
106 var mmodule = nmodule.mmodule
107 if mmodule == null then continue # Skip error
108 res.add mmodule
109 end
110 end
111 return res
112 end
113
114 # Load a bunch of modules and groups.
115 #
116 # Each name can be:
117 #
118 # * a path to a module, a group or a directory of projects.
119 # * a short name of a module or a group that are looked in the `paths` (-I)
120 #
121 # Then, for each entry, if it is:
122 #
123 # * a module, then is it parser and returned.
124 # * a group then recursively all its modules are parsed.
125 # * a directory of projects then all the modules of all projects are parsed.
126 # * else an error is displayed.
127 #
128 # See `parse` for details.
129 fun parse_full(names: Sequence[String]): Array[MModule]
130 do
131 var time0 = get_time
132 # Parse and recursively load
133 self.toolcontext.info("*** PARSE ***", 1)
134 var mmodules = new ArraySet[MModule]
135 for a in names do
136 # Case of a group
137 var mgroup = self.get_mgroup(a)
138 if mgroup != null then
139 mmodules.add_all parse_group(mgroup)
140 continue
141 end
142
143 # Case of a directory that is not a group
144 var stat = a.to_path.stat
145 if stat != null and stat.is_dir then
146 self.toolcontext.info("look in directory {a}", 2)
147 var fs = a.files
148 # Try each entry as a group or a module
149 for f in fs do
150 var af = a/f
151 mgroup = get_mgroup(af)
152 if mgroup != null then
153 mmodules.add_all parse_group(mgroup)
154 continue
155 end
156 var mp = identify_file(af)
157 if mp != null then
158 var nmodule = self.load_module(af)
159 if nmodule == null then continue # Skip error
160 build_module_importation(nmodule)
161 var mmodule = nmodule.mmodule
162 if mmodule == null then continue # Skip error
163 mmodules.add mmodule
164 else
165 self.toolcontext.info("ignore file {af}", 2)
166 end
167 end
168 continue
169 end
170
171 var nmodule = self.load_module(a)
172 if nmodule == null then continue # Skip error
173 # Load imported module
174 build_module_importation(nmodule)
175 var mmodule = nmodule.mmodule
176 if mmodule == null then continue # Skip error
177 mmodules.add mmodule
178 end
179 var time1 = get_time
180 self.toolcontext.info("*** END PARSE: {time1-time0} ***", 2)
181
182 self.toolcontext.check_errors
183
184 if toolcontext.opt_only_parse.value then
185 self.toolcontext.info("*** ONLY PARSE...", 1)
186 exit(0)
187 end
188
189 return mmodules.to_a
190 end
191
192 # The list of directories to search for top level modules
193 # The list is initially set with:
194 #
195 # * the toolcontext --path option
196 # * the NIT_PATH environment variable
197 # * `toolcontext.nit_dir`
198 # Path can be added (or removed) by the client
199 var paths = new Array[String]
200
201 # Like (and used by) `get_mmodule_by_name` but just return the ModulePath
202 fun search_mmodule_by_name(anode: nullable ANode, mgroup: nullable MGroup, name: String): nullable ModulePath
203 do
204 # First, look in groups
205 var c = mgroup
206 while c != null do
207 var dirname = c.filepath
208 if dirname == null then break # virtual group
209 if dirname.has_suffix(".nit") then break # singleton project
210
211 # Second, try the directory to find a file
212 var try_file = dirname + "/" + name + ".nit"
213 if try_file.file_exists then
214 var res = self.identify_file(try_file.simplify_path)
215 assert res != null
216 return res
217 end
218
219 # Third, try if the requested module is itself a group
220 try_file = dirname + "/" + name + "/" + name + ".nit"
221 if try_file.file_exists then
222 var res = self.identify_file(try_file.simplify_path)
223 assert res != null
224 return res
225 end
226
227 c = c.parent
228 end
229
230 # Look at some known directories
231 var lookpaths = self.paths
232
233 # Look in the directory of the group project also (even if not explicitly in the path)
234 if mgroup != null then
235 # path of the root group
236 var dirname = mgroup.mproject.root.filepath
237 if dirname != null then
238 dirname = dirname.join_path("..").simplify_path
239 if not lookpaths.has(dirname) and dirname.file_exists then
240 lookpaths = lookpaths.to_a
241 lookpaths.add(dirname)
242 end
243 end
244 end
245
246 var candidate = search_module_in_paths(anode.hot_location, name, lookpaths)
247
248 if candidate == null then
249 if mgroup != null then
250 error(anode, "Error: cannot find module `{name}` from `{mgroup.name}`. Tried: {lookpaths.join(", ")}.")
251 else
252 error(anode, "Error: cannot find module `{name}`. Tried: {lookpaths.join(", ")}.")
253 end
254 return null
255 end
256 return candidate
257 end
258
259 # Get a module by its short name; if required, the module is loaded, parsed and its hierarchies computed.
260 # If `mgroup` is set, then the module search starts from it up to the top level (see `paths`);
261 # if `mgroup` is null then the module is searched in the top level only.
262 # If no module exists or there is a name conflict, then an error on `anode` is displayed and null is returned.
263 fun get_mmodule_by_name(anode: nullable ANode, mgroup: nullable MGroup, name: String): nullable MModule
264 do
265 var path = search_mmodule_by_name(anode, mgroup, name)
266 if path == null then return null # Forward error
267 var res = self.load_module(path.filepath)
268 if res == null then return null # Forward error
269 # Load imported module
270 build_module_importation(res)
271 return res.mmodule
272 end
273
274 # Search a module `name` from path `lookpaths`.
275 # If found, the path of the file is returned
276 private fun search_module_in_paths(location: nullable Location, name: String, lookpaths: Collection[String]): nullable ModulePath
277 do
278 var candidate: nullable String = null
279 for dirname in lookpaths do
280 var try_file = (dirname + "/" + name + ".nit").simplify_path
281 if try_file.file_exists then
282 if candidate == null then
283 candidate = try_file
284 else if candidate != try_file then
285 # try to disambiguate conflicting modules
286 var abs_candidate = module_absolute_path(candidate)
287 var abs_try_file = module_absolute_path(try_file)
288 if abs_candidate != abs_try_file then
289 toolcontext.error(location, "Error: conflicting module file for `{name}`: `{candidate}` `{try_file}`")
290 end
291 end
292 end
293 try_file = (dirname + "/" + name + "/" + name + ".nit").simplify_path
294 if try_file.file_exists then
295 if candidate == null then
296 candidate = try_file
297 else if candidate != try_file then
298 # try to disambiguate conflicting modules
299 var abs_candidate = module_absolute_path(candidate)
300 var abs_try_file = module_absolute_path(try_file)
301 if abs_candidate != abs_try_file then
302 toolcontext.error(location, "Error: conflicting module file for `{name}`: `{candidate}` `{try_file}`")
303 end
304 end
305 end
306 end
307 if candidate == null then return null
308 return identify_file(candidate)
309 end
310
311 # Cache for `identify_file` by realpath
312 private var identified_files_by_path = new HashMap[String, nullable ModulePath]
313
314 # All the currently identified modules.
315 # See `identify_file`.
316 var identified_files = new Array[ModulePath]
317
318 # Identify a source file
319 # Load the associated project and groups if required
320 #
321 # Silently return `null` if `path` is not a valid module path.
322 fun identify_file(path: String): nullable ModulePath
323 do
324 # special case for not a nit file
325 if path.file_extension != "nit" then
326 # search dirless files in known -I paths
327 if path.dirname == "" then
328 var res = search_module_in_paths(null, path, self.paths)
329 if res != null then return res
330 end
331
332 # Found nothing? maybe it is a group...
333 var candidate = null
334 if path.file_exists then
335 var mgroup = get_mgroup(path)
336 if mgroup != null then
337 var owner_path = mgroup.filepath.join_path(mgroup.name + ".nit")
338 if owner_path.file_exists then candidate = owner_path
339 end
340 end
341
342 if candidate == null then
343 return null
344 end
345 path = candidate
346 end
347
348 # Fast track, the path is already known
349 var pn = path.basename(".nit")
350 var rp = module_absolute_path(path)
351 if identified_files_by_path.has_key(rp) then return identified_files_by_path[rp]
352
353 # Search for a group
354 var mgrouppath = path.join_path("..").simplify_path
355 var mgroup = get_mgroup(mgrouppath)
356
357 if mgroup == null then
358 # singleton project
359 var mproject = new MProject(pn, model)
360 mgroup = new MGroup(pn, mproject, null) # same name for the root group
361 mgroup.filepath = path
362 mproject.root = mgroup
363 toolcontext.info("found project `{pn}` at {path}", 2)
364 end
365
366 var res = new ModulePath(pn, path, mgroup)
367 mgroup.module_paths.add(res)
368
369 identified_files_by_path[rp] = res
370 identified_files.add(res)
371 return res
372 end
373
374 # Groups by path
375 private var mgroups = new HashMap[String, nullable MGroup]
376
377 # Return the mgroup associated to a directory path.
378 # If the directory is not a group null is returned.
379 #
380 # Note: `paths` is also used to look for mgroups
381 fun get_mgroup(dirpath: String): nullable MGroup
382 do
383 if not dirpath.file_exists then do
384 for p in paths do
385 var try = p / dirpath
386 if try.file_exists then
387 dirpath = try
388 break label
389 end
390 end
391 return null
392 end label
393
394 var rdp = module_absolute_path(dirpath)
395 if mgroups.has_key(rdp) then
396 return mgroups[rdp]
397 end
398
399 # Hack, a group is determined by one of the following:
400 # * the presence of a honomymous nit file
401 # * the fact that the directory is named `src`
402 # * the fact that there is a sub-directory named `src`
403 var pn = rdp.basename(".nit")
404 var mp = dirpath.join_path(pn + ".nit").simplify_path
405
406 # dirpath2 is the root directory
407 # dirpath is the src subdirectory directory, if any, else it is the same that dirpath2
408 var dirpath2 = dirpath
409 if not mp.file_exists then
410 if pn == "src" then
411 # With a src directory, the group name is the name of the parent directory
412 dirpath2 = rdp.dirname
413 pn = dirpath2.basename("")
414 else
415 # Check a `src` subdirectory
416 dirpath = dirpath2 / "src"
417 if not dirpath.file_exists then
418 # All rules failed, so return null
419 return null
420 end
421 end
422 end
423
424 # check parent directory
425 var parentpath = dirpath2.join_path("..").simplify_path
426 var parent = get_mgroup(parentpath)
427
428 var mgroup
429 if parent == null then
430 # no parent, thus new project
431 var mproject = new MProject(pn, model)
432 mgroup = new MGroup(pn, mproject, null) # same name for the root group
433 mproject.root = mgroup
434 toolcontext.info("found project `{mproject}` at {dirpath}", 2)
435 else
436 mgroup = new MGroup(pn, parent.mproject, parent)
437 toolcontext.info("found sub group `{mgroup.full_name}` at {dirpath}", 2)
438 end
439
440 # search documentation
441 # in src first so the documentation of the project code can be distinct for the documentation of the project usage
442 var readme = dirpath.join_path("README.md")
443 if not readme.file_exists then readme = dirpath.join_path("README")
444 if not readme.file_exists then readme = dirpath2.join_path("README.md")
445 if not readme.file_exists then readme = dirpath2.join_path("README")
446 if readme.file_exists then
447 var mdoc = load_markdown(readme)
448 mgroup.mdoc = mdoc
449 mdoc.original_mentity = mgroup
450 end
451
452 mgroup.filepath = dirpath
453 mgroups[module_absolute_path(dirpath)] = mgroup
454 mgroups[module_absolute_path(dirpath2)] = mgroup
455 return mgroup
456 end
457
458 # Load a markdown file as a documentation object
459 fun load_markdown(filepath: String): MDoc
460 do
461 var mdoc = new MDoc(new Location(new SourceFile.from_string(filepath, ""),0,0,0,0))
462 var s = new FileReader.open(filepath)
463 while not s.eof do
464 mdoc.content.add(s.read_line)
465 end
466 return mdoc
467 end
468
469 # Force the identification of all ModulePath of the group and sub-groups.
470 fun visit_group(mgroup: MGroup) do
471 var p = mgroup.filepath
472 for f in p.files do
473 var fp = p/f
474 var g = get_mgroup(fp)
475 if g != null then visit_group(g)
476 identify_file(fp)
477 end
478 end
479
480 # Transform relative paths (starting with '../') into absolute paths
481 private fun module_absolute_path(path: String): String do
482 return getcwd.join_path(path).simplify_path
483 end
484
485 # Try to load a module AST using a path.
486 # Display an error if there is a problem (IO / lexer / parser) and return null
487 fun load_module_ast(filename: String): nullable AModule
488 do
489 if filename.file_extension != "nit" then
490 self.toolcontext.error(null, "Error: file `{filename}` is not a valid nit module.")
491 return null
492 end
493 if not filename.file_exists then
494 self.toolcontext.error(null, "Error: file `{filename}` not found.")
495 return null
496 end
497
498 self.toolcontext.info("load module {filename}", 2)
499
500 # Load the file
501 var file = new FileReader.open(filename)
502 var lexer = new Lexer(new SourceFile(filename, file))
503 var parser = new Parser(lexer)
504 var tree = parser.parse
505 file.close
506
507 # Handle lexer and parser error
508 var nmodule = tree.n_base
509 if nmodule == null then
510 var neof = tree.n_eof
511 assert neof isa AError
512 error(neof, neof.message)
513 return null
514 end
515
516 return nmodule
517 end
518
519 # Remove Nit source files from a list of arguments.
520 #
521 # Items of `args` that can be loaded as a nit file will be removed from `args` and returned.
522 fun filter_nit_source(args: Array[String]): Array[String]
523 do
524 var keep = new Array[String]
525 var res = new Array[String]
526 for a in args do
527 var l = identify_file(a)
528 if l == null then
529 keep.add a
530 else
531 res.add a
532 end
533 end
534 args.clear
535 args.add_all(keep)
536 return res
537 end
538
539 # Try to load a module using a path.
540 # Display an error if there is a problem (IO / lexer / parser) and return null.
541 # Note: usually, you do not need this method, use `get_mmodule_by_name` instead.
542 #
543 # The MModule is created however, the importation is not performed,
544 # therefore you should call `build_module_importation`.
545 fun load_module(filename: String): nullable AModule
546 do
547 # Look for the module
548 var file = identify_file(filename)
549 if file == null then
550 if filename.file_exists then
551 toolcontext.error(null, "Error: `{filename}` is not a Nit source file.")
552 else
553 toolcontext.error(null, "Error: cannot find module `{filename}`.")
554 end
555 return null
556 end
557
558 # Already known and loaded? then return it
559 var mmodule = file.mmodule
560 if mmodule != null then
561 return mmodule2nmodule[mmodule]
562 end
563
564 # Load it manually
565 var nmodule = load_module_ast(file.filepath)
566 if nmodule == null then return null # forward error
567
568 # build the mmodule and load imported modules
569 mmodule = build_a_mmodule(file.mgroup, file.name, nmodule)
570
571 if mmodule == null then return null # forward error
572
573 # Update the file information
574 file.mmodule = mmodule
575
576 return nmodule
577 end
578
579 # Injection of a new module without source.
580 # Used by the interpreter.
581 fun load_rt_module(parent: nullable MModule, nmodule: AModule, mod_name: String): nullable AModule
582 do
583 # Create the module
584
585 var mgroup = null
586 if parent != null then mgroup = parent.mgroup
587 var mmodule = new MModule(model, mgroup, mod_name, nmodule.location)
588 nmodule.mmodule = mmodule
589 nmodules.add(nmodule)
590 self.mmodule2nmodule[mmodule] = nmodule
591
592 if parent!= null then
593 var imported_modules = new Array[MModule]
594 imported_modules.add(parent)
595 mmodule.set_visibility_for(parent, intrude_visibility)
596 mmodule.set_imported_mmodules(imported_modules)
597 else
598 build_module_importation(nmodule)
599 end
600
601 return nmodule
602 end
603
604 # Visit the AST and create the `MModule` object
605 private fun build_a_mmodule(mgroup: nullable MGroup, mod_name: String, nmodule: AModule): nullable MModule
606 do
607 # Check the module name
608 var decl = nmodule.n_moduledecl
609 if decl != null then
610 var decl_name = decl.n_name.n_id.text
611 if decl_name != mod_name then
612 error(decl.n_name, "Error: module name mismatch; declared {decl_name} file named {mod_name}.")
613 end
614 end
615
616 # Check for conflicting module names in the project
617 if mgroup != null then
618 var others = model.get_mmodules_by_name(mod_name)
619 if others != null then for other in others do
620 if other.mgroup!= null and other.mgroup.mproject == mgroup.mproject then
621 var node: ANode
622 if decl == null then node = nmodule else node = decl.n_name
623 error(node, "Error: a module named `{other.full_name}` already exists at {other.location}.")
624 break
625 end
626 end
627 end
628
629 # Create the module
630 var mmodule = new MModule(model, mgroup, mod_name, nmodule.location)
631 nmodule.mmodule = mmodule
632 nmodules.add(nmodule)
633 self.mmodule2nmodule[mmodule] = nmodule
634
635 var source = nmodule.location.file
636 if source != null then
637 assert source.mmodule == null
638 source.mmodule = mmodule
639 end
640
641 if decl != null then
642 # Extract documentation
643 var ndoc = decl.n_doc
644 if ndoc != null then
645 var mdoc = ndoc.to_mdoc
646 mmodule.mdoc = mdoc
647 mdoc.original_mentity = mmodule
648 else
649 advice(decl, "missing-doc", "Documentation warning: Undocumented module `{mmodule}`")
650 end
651 # Is the module a test suite?
652 mmodule.is_test_suite = not decl.get_annotations("test_suite").is_empty
653 end
654
655 return mmodule
656 end
657
658 # Analyze the module importation and fill the module_importation_hierarchy
659 #
660 # Unless you used `load_module`, the importation is already done and this method does a no-op.
661 fun build_module_importation(nmodule: AModule)
662 do
663 if nmodule.is_importation_done then return
664 nmodule.is_importation_done = true
665 var mmodule = nmodule.mmodule.as(not null)
666 var stdimport = true
667 var imported_modules = new Array[MModule]
668 for aimport in nmodule.n_imports do
669 stdimport = false
670 if not aimport isa AStdImport then
671 continue
672 end
673 var mgroup = mmodule.mgroup
674 if aimport.n_name.n_quad != null then mgroup = null # Start from top level
675 for grp in aimport.n_name.n_path do
676 var path = search_mmodule_by_name(grp, mgroup, grp.text)
677 if path == null then
678 nmodule.mmodule = null # invalidate the module
679 return # Skip error
680 end
681 mgroup = path.mgroup
682 end
683 var mod_name = aimport.n_name.n_id.text
684 var sup = self.get_mmodule_by_name(aimport.n_name, mgroup, mod_name)
685 if sup == null then
686 nmodule.mmodule = null # invalidate the module
687 continue # Skip error
688 end
689 aimport.mmodule = sup
690 imported_modules.add(sup)
691 var mvisibility = aimport.n_visibility.mvisibility
692 if mvisibility == protected_visibility then
693 error(aimport.n_visibility, "Error: only properties can be protected.")
694 nmodule.mmodule = null # invalidate the module
695 return
696 end
697 if sup == mmodule then
698 error(aimport.n_name, "Error: dependency loop in module {mmodule}.")
699 nmodule.mmodule = null # invalidate the module
700 end
701 if sup.in_importation < mmodule then
702 error(aimport.n_name, "Error: dependency loop between modules {mmodule} and {sup}.")
703 nmodule.mmodule = null # invalidate the module
704 return
705 end
706 mmodule.set_visibility_for(sup, mvisibility)
707 end
708 if stdimport then
709 var mod_name = "standard"
710 var sup = self.get_mmodule_by_name(nmodule, null, mod_name)
711 if sup == null then
712 nmodule.mmodule = null # invalidate the module
713 else # Skip error
714 imported_modules.add(sup)
715 mmodule.set_visibility_for(sup, public_visibility)
716 end
717 end
718 self.toolcontext.info("{mmodule} imports {imported_modules.join(", ")}", 3)
719 mmodule.set_imported_mmodules(imported_modules)
720
721 # Force standard to be public if imported
722 for sup in mmodule.in_importation.greaters do
723 if sup.name == "standard" then
724 mmodule.set_visibility_for(sup, public_visibility)
725 end
726 end
727
728 # TODO: Correctly check for useless importation
729 # It is even doable?
730 var directs = mmodule.in_importation.direct_greaters
731 for nim in nmodule.n_imports do
732 if not nim isa AStdImport then continue
733 var im = nim.mmodule
734 if im == null then continue
735 if directs.has(im) then continue
736 # This generates so much noise that it is simpler to just comment it
737 #warning(nim, "Warning: possible useless importation of {im}")
738 end
739 end
740
741 # All the loaded modules
742 var nmodules = new Array[AModule]
743
744 # Register the nmodule associated to each mmodule
745 #
746 # Public clients need to use `mmodule2node` to access stuff.
747 private var mmodule2nmodule = new HashMap[MModule, AModule]
748
749 # Retrieve the associated AST node of a mmodule.
750 # This method is used to associate model entity with syntactic entities.
751 #
752 # If the module is not associated with a node, returns null.
753 fun mmodule2node(mmodule: MModule): nullable AModule
754 do
755 return mmodule2nmodule.get_or_null(mmodule)
756 end
757 end
758
759 # File-system location of a module (file) that is identified but not always loaded.
760 class ModulePath
761 # The name of the module
762 # (it's the basename of the filepath)
763 var name: String
764
765 # The human path of the module
766 var filepath: String
767
768 # The group (and the project) of the possible module
769 var mgroup: MGroup
770
771 # The loaded module (if any)
772 var mmodule: nullable MModule = null
773
774 redef fun to_s do return filepath
775 end
776
777 redef class MGroup
778 # Modules paths associated with the group
779 var module_paths = new Array[ModulePath]
780
781 # Is the group interesting for a final user?
782 #
783 # Groups are mandatory in the model but for simple projects they are not
784 # always interesting.
785 #
786 # A interesting group has, at least, one of the following true:
787 #
788 # * it has 2 modules or more
789 # * it has a subgroup
790 # * it has a documentation
791 fun is_interesting: Bool
792 do
793 return module_paths.length > 1 or mmodules.length > 1 or not in_nesting.direct_smallers.is_empty or mdoc != null
794 end
795
796 end
797
798 redef class SourceFile
799 # Associated mmodule, once created
800 var mmodule: nullable MModule = null
801 end
802
803 redef class AStdImport
804 # The imported module once determined
805 var mmodule: nullable MModule = null
806 end
807
808 redef class AModule
809 # The associated MModule once build by a `ModelBuilder`
810 var mmodule: nullable MModule
811 # Flag that indicate if the importation is already completed
812 var is_importation_done: Bool = false
813 end