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 # Load nit source files and build the associated model
20 import modelbuilder_base
22 redef class ToolContext
24 var opt_path
= new OptionArray("Set include path for loaders (may be used more than once)", "-I", "--path")
26 # Option --only-metamodel
27 var opt_only_metamodel
= new OptionBool("Stop after meta-model processing", "--only-metamodel")
30 var opt_only_parse
= new OptionBool("Only proceed to parse step of loaders", "--only-parse")
35 option_context
.add_option
(opt_path
, opt_only_parse
, opt_only_metamodel
)
39 redef class ModelBuilder
44 # Setup the paths value
45 paths
.append
(toolcontext
.opt_path
.value
)
47 var path_env
= "NIT_PATH".environ
48 if not path_env
.is_empty
then
49 paths
.append
(path_env
.split_with
(':'))
52 var nit_dir
= toolcontext
.nit_dir
53 var libname
= "{nit_dir}/lib"
54 if libname
.file_exists
then paths
.add
(libname
)
57 # Load a bunch of modules.
58 # `modules` can contains filenames or module names.
59 # Imported modules are automatically loaded and modelized.
60 # The result is the corresponding model elements.
61 # Errors and warnings are printed with the toolcontext.
63 # Note: class and property model element are not analysed.
64 fun parse
(modules
: Sequence[String]): Array[MModule]
67 # Parse and recursively load
68 self.toolcontext
.info
("*** PARSE ***", 1)
69 var mmodules
= new ArraySet[MModule]
71 var nmodule
= self.load_module
(a
)
72 if nmodule
== null then continue # Skip error
73 # Load imported module
74 build_module_importation
(nmodule
)
76 mmodules
.add
(nmodule
.mmodule
.as(not null))
79 self.toolcontext
.info
("*** END PARSE: {time1-time0} ***", 2)
81 self.toolcontext
.check_errors
83 if toolcontext
.opt_only_parse
.value
then
84 self.toolcontext
.info
("*** ONLY PARSE...", 1)
91 # The list of directories to search for top level modules
92 # The list is initially set with :
93 # * the toolcontext --path option
94 # * the NIT_PATH environment variable
95 # * `toolcontext.nit_dir`
96 # Path can be added (or removed) by the client
97 var paths
= new Array[String]
99 # Like (an used by) `get_mmodule_by_name` but just return the ModulePath
100 private fun search_mmodule_by_name
(anode
: nullable ANode, mgroup
: nullable MGroup, name
: String): nullable ModulePath
102 # First, look in groups
105 var dirname
= c
.filepath
106 if dirname
== null then break # virtual group
107 if dirname
.has_suffix
(".nit") then break # singleton project
109 # Second, try the directory to find a file
110 var try_file
= dirname
+ "/" + name
+ ".nit"
111 if try_file
.file_exists
then
112 var res
= self.identify_file
(try_file
.simplify_path
)
117 # Third, try if the requested module is itself a group
118 try_file
= dirname
+ "/" + name
+ "/" + name
+ ".nit"
119 if try_file
.file_exists
then
120 var res
= self.identify_file
(try_file
.simplify_path
)
128 # Look at some known directories
129 var lookpaths
= self.paths
131 # Look in the directory of the group project also (even if not explicitely in the path)
132 if mgroup
!= null then
133 # path of the root group
134 var dirname
= mgroup
.mproject
.root
.filepath
135 if dirname
!= null then
136 dirname
= dirname
.join_path
("..").simplify_path
137 if not lookpaths
.has
(dirname
) and dirname
.file_exists
then
138 lookpaths
= lookpaths
.to_a
139 lookpaths
.add
(dirname
)
144 var candidate
= search_module_in_paths
(anode
.hot_location
, name
, lookpaths
)
146 if candidate
== null then
147 if mgroup
!= null then
148 error
(anode
, "Error: cannot find module {name} from {mgroup.name}. tried {lookpaths.join(", ")}")
150 error
(anode
, "Error: cannot find module {name}. tried {lookpaths.join(", ")}")
157 # Get a module by its short name; if required, the module is loaded, parsed and its hierarchies computed.
158 # If `mgroup` is set, then the module search starts from it up to the top level (see `paths`);
159 # if `mgroup` is null then the module is searched in the top level only.
160 # If no module exists or there is a name conflict, then an error on `anode` is displayed and null is returned.
161 fun get_mmodule_by_name
(anode
: nullable ANode, mgroup
: nullable MGroup, name
: String): nullable MModule
163 var path
= search_mmodule_by_name
(anode
, mgroup
, name
)
164 if path
== null then return null # Forward error
165 var res
= self.load_module
(path
.filepath
)
166 if res
== null then return null # Forward error
167 # Load imported module
168 build_module_importation
(res
)
169 return res
.mmodule
.as(not null)
172 # Search a module `name` from path `lookpaths`.
173 # If found, the path of the file is returned
174 private fun search_module_in_paths
(location
: nullable Location, name
: String, lookpaths
: Collection[String]): nullable ModulePath
176 var candidate
: nullable String = null
177 for dirname
in lookpaths
do
178 var try_file
= (dirname
+ "/" + name
+ ".nit").simplify_path
179 if try_file
.file_exists
then
180 if candidate
== null then
182 else if candidate
!= try_file
then
183 # try to disambiguate conflicting modules
184 var abs_candidate
= module_absolute_path
(candidate
)
185 var abs_try_file
= module_absolute_path
(try_file
)
186 if abs_candidate
!= abs_try_file
then
187 toolcontext
.error
(location
, "Error: conflicting module file for {name}: {candidate} {try_file}")
191 try_file
= (dirname
+ "/" + name
+ "/" + name
+ ".nit").simplify_path
192 if try_file
.file_exists
then
193 if candidate
== null then
195 else if candidate
!= try_file
then
196 # try to disambiguate conflicting modules
197 var abs_candidate
= module_absolute_path
(candidate
)
198 var abs_try_file
= module_absolute_path
(try_file
)
199 if abs_candidate
!= abs_try_file
then
200 toolcontext
.error
(location
, "Error: conflicting module file for {name}: {candidate} {try_file}")
205 if candidate
== null then return null
206 return identify_file
(candidate
)
209 # cache for `identify_file` by realpath
210 private var identified_files
= new HashMap[String, nullable ModulePath]
212 # Identify a source file
213 # Load the associated project and groups if required
215 # Silently return `null` if `path` is not a valid module path.
216 private fun identify_file
(path
: String): nullable ModulePath
218 # special case for not a nit file
219 if path
.file_extension
!= "nit" then
220 # search in known -I paths
221 var res
= search_module_in_paths
(null, path
, self.paths
)
222 if res
!= null then return res
224 # Found nothins? maybe it is a group...
226 if path
.file_exists
then
227 var mgroup
= get_mgroup
(path
)
228 if mgroup
!= null then
229 var owner_path
= mgroup
.filepath
.join_path
(mgroup
.name
+ ".nit")
230 if owner_path
.file_exists
then candidate
= owner_path
234 if candidate
== null then
240 # Fast track, the path is already known
241 var pn
= path
.basename
(".nit")
242 var rp
= module_absolute_path
(path
)
243 if identified_files
.has_key
(rp
) then return identified_files
[rp
]
246 var mgrouppath
= path
.join_path
("..").simplify_path
247 var mgroup
= get_mgroup
(mgrouppath
)
249 if mgroup
== null then
251 var mproject
= new MProject(pn
, model
)
252 mgroup
= new MGroup(pn
, mproject
, null) # same name for the root group
253 mgroup
.filepath
= path
254 mproject
.root
= mgroup
255 toolcontext
.info
("found project `{pn}` at {path}", 2)
258 var res
= new ModulePath(pn
, path
, mgroup
)
259 mgroup
.module_paths
.add
(res
)
261 identified_files
[rp
] = res
266 private var mgroups
= new HashMap[String, nullable MGroup]
268 # return the mgroup associated to a directory path
269 # if the directory is not a group null is returned
270 private fun get_mgroup
(dirpath
: String): nullable MGroup
272 var rdp
= module_absolute_path
(dirpath
)
273 if mgroups
.has_key
(rdp
) then
277 # Hack, a group is determined by:
278 # * the presence of a honomymous nit file
279 # * the fact that the directory is named `src`
280 var pn
= rdp
.basename
(".nit")
281 var mp
= dirpath
.join_path
(pn
+ ".nit").simplify_path
283 var dirpath2
= dirpath
284 if not mp
.file_exists
then
286 # With a src directory, the group name is the name of the parent directory
287 dirpath2
= rdp
.dirname
288 pn
= dirpath2
.basename
("")
294 # check parent directory
295 var parentpath
= dirpath
.join_path
("..").simplify_path
296 var parent
= get_mgroup
(parentpath
)
299 if parent
== null then
300 # no parent, thus new project
301 var mproject
= new MProject(pn
, model
)
302 mgroup
= new MGroup(pn
, mproject
, null) # same name for the root group
303 mproject
.root
= mgroup
304 toolcontext
.info
("found project `{mproject}` at {dirpath}", 2)
306 mgroup
= new MGroup(pn
, parent
.mproject
, parent
)
307 toolcontext
.info
("found sub group `{mgroup.full_name}` at {dirpath}", 2)
309 var readme
= dirpath2
.join_path
("README.md")
310 if not readme
.file_exists
then readme
= dirpath2
.join_path
("README")
311 if readme
.file_exists
then
313 var s
= new IFStream.open
(readme
)
315 mdoc
.content
.add
(s
.read_line
)
318 mdoc
.original_mentity
= mgroup
320 mgroup
.filepath
= dirpath
321 mgroups
[rdp
] = mgroup
325 # Transform relative paths (starting with '../') into absolute paths
326 private fun module_absolute_path
(path
: String): String do
327 return getcwd
.join_path
(path
).simplify_path
330 # Try to load a module AST using a path.
331 # Display an error if there is a problem (IO / lexer / parser) and return null
332 fun load_module_ast
(filename
: String): nullable AModule
334 if filename
.file_extension
!= "nit" then
335 self.toolcontext
.error
(null, "Error: file {filename} is not a valid nit module.")
338 if not filename
.file_exists
then
339 self.toolcontext
.error
(null, "Error: file {filename} not found.")
343 self.toolcontext
.info
("load module {filename}", 2)
346 var file
= new IFStream.open
(filename
)
347 var lexer
= new Lexer(new SourceFile(filename
, file
))
348 var parser
= new Parser(lexer
)
349 var tree
= parser
.parse
352 # Handle lexer and parser error
353 var nmodule
= tree
.n_base
354 if nmodule
== null then
355 var neof
= tree
.n_eof
356 assert neof
isa AError
357 error
(neof
, neof
.message
)
364 # Try to load a module modules using a path.
365 # Display an error if there is a problem (IO / lexer / parser) and return null.
366 # Note: usually, you do not need this method, use `get_mmodule_by_name` instead.
368 # The MModule is created however, the importation is not performed,
369 # therefore you should call `build_module_importation`.
370 fun load_module
(filename
: String): nullable AModule
372 # Look for the module
373 var file
= identify_file
(filename
)
375 toolcontext
.error
(null, "Error: cannot find module `{filename}`.")
379 # Already known and loaded? then return it
380 var mmodule
= file
.mmodule
381 if mmodule
!= null then
382 return mmodule2nmodule
[mmodule
]
386 var nmodule
= load_module_ast
(file
.filepath
)
387 if nmodule
== null then return null # forward error
389 # build the mmodule and load imported modules
390 mmodule
= build_a_mmodule
(file
.mgroup
, file
.name
, nmodule
)
392 if mmodule
== null then return null # forward error
394 # Update the file information
395 file
.mmodule
= mmodule
400 # Injection of a new module without source.
401 # Used by the interpreted
402 fun load_rt_module
(parent
: nullable MModule, nmodule
: AModule, mod_name
: String): nullable AModule
407 if parent
!= null then mgroup
= parent
.mgroup
408 var mmodule
= new MModule(model
, mgroup
, mod_name
, nmodule
.location
)
409 nmodule
.mmodule
= mmodule
410 nmodules
.add
(nmodule
)
411 self.mmodule2nmodule
[mmodule
] = nmodule
413 if parent
!= null then
414 var imported_modules
= new Array[MModule]
415 imported_modules
.add
(parent
)
416 mmodule
.set_visibility_for
(parent
, intrude_visibility
)
417 mmodule
.set_imported_mmodules
(imported_modules
)
419 build_module_importation
(nmodule
)
425 # Visit the AST and create the `MModule` object
426 private fun build_a_mmodule
(mgroup
: nullable MGroup, mod_name
: String, nmodule
: AModule): nullable MModule
428 # Check the module name
429 var decl
= nmodule
.n_moduledecl
431 #warning(nmodule, "Warning: Missing 'module' keyword") #FIXME: NOT YET FOR COMPATIBILITY
433 var decl_name
= decl
.n_name
.n_id
.text
434 if decl_name
!= mod_name
then
435 error
(decl
.n_name
, "Error: module name missmatch; declared {decl_name} file named {mod_name}")
440 var mmodule
= new MModule(model
, mgroup
, mod_name
, nmodule
.location
)
441 nmodule
.mmodule
= mmodule
442 nmodules
.add
(nmodule
)
443 self.mmodule2nmodule
[mmodule
] = nmodule
446 var ndoc
= decl
.n_doc
448 var mdoc
= ndoc
.to_mdoc
450 mdoc
.original_mentity
= mmodule
452 advice
(decl
, "missing-doc", "Documentation warning: Undocumented module `{mmodule}`")
459 # Analysis the module importation and fill the module_importation_hierarchy
461 # Unless you used `load_module`, the importation is already done and this method does a no-op.
462 fun build_module_importation
(nmodule
: AModule)
464 if nmodule
.is_importation_done
then return
465 nmodule
.is_importation_done
= true
466 var mmodule
= nmodule
.mmodule
.as(not null)
468 var imported_modules
= new Array[MModule]
469 for aimport
in nmodule
.n_imports
do
471 if not aimport
isa AStdImport then
474 var mgroup
= mmodule
.mgroup
475 if aimport
.n_name
.n_quad
!= null then mgroup
= null # Start from top level
476 for grp
in aimport
.n_name
.n_path
do
477 var path
= search_mmodule_by_name
(grp
, mgroup
, grp
.text
)
478 if path
== null then return # Skip error
481 var mod_name
= aimport
.n_name
.n_id
.text
482 var sup
= self.get_mmodule_by_name
(aimport
.n_name
, mgroup
, mod_name
)
483 if sup
== null then continue # Skip error
484 aimport
.mmodule
= sup
485 imported_modules
.add
(sup
)
486 var mvisibility
= aimport
.n_visibility
.mvisibility
487 if mvisibility
== protected_visibility
then
488 error
(aimport
.n_visibility
, "Error: only properties can be protected.")
491 if sup
== mmodule
then
492 error
(aimport
.n_name
, "Error: Dependency loop in module {mmodule}.")
494 if sup
.in_importation
< mmodule
then
495 error
(aimport
.n_name
, "Error: Dependency loop between modules {mmodule} and {sup}.")
498 mmodule
.set_visibility_for
(sup
, mvisibility
)
501 var mod_name
= "standard"
502 var sup
= self.get_mmodule_by_name
(nmodule
, null, mod_name
)
503 if sup
!= null then # Skip error
504 imported_modules
.add
(sup
)
505 mmodule
.set_visibility_for
(sup
, public_visibility
)
508 self.toolcontext
.info
("{mmodule} imports {imported_modules.join(", ")}", 3)
509 mmodule
.set_imported_mmodules
(imported_modules
)
511 # Force standard to be public if imported
512 for sup
in mmodule
.in_importation
.greaters
do
513 if sup
.name
== "standard" then
514 mmodule
.set_visibility_for
(sup
, public_visibility
)
518 # TODO: Correctly check for useless importation
520 var directs
= mmodule
.in_importation
.direct_greaters
521 for nim
in nmodule
.n_imports
do
522 if not nim
isa AStdImport then continue
524 if im
== null then continue
525 if directs
.has
(im
) then continue
526 # This generates so much noise that it is simpler to just comment it
527 #warning(nim, "Warning: possible useless importation of {im}")
531 # All the loaded modules
532 var nmodules
= new Array[AModule]
534 # Register the nmodule associated to each mmodule
535 # FIXME: why not refine the `MModule` class with a nullable attribute?
536 var mmodule2nmodule
= new HashMap[MModule, AModule]
539 # placeholder to a module file identified but not always loaded in a project
540 private class ModulePath
541 # The name of the module
542 # (it's the basename of the filepath)
545 # The human path of the module
548 # The group (and the project) of the possible module
551 # The loaded module (if any)
552 var mmodule
: nullable MModule = null
554 redef fun to_s
do return filepath
558 # modules paths associated with the group
559 private var module_paths
= new Array[ModulePath]
561 # Is the group interesting for a final user?
563 # groups are mandatory in the model but for simple projects they are not
564 # always interesting.
566 # A interesting group has, at least, one of the following true:
568 # * it has 2 modules or more
569 # * it has a subgroup
570 # * it has a documentation
571 fun is_interesting
: Bool
573 return module_paths
.length
> 1 or mmodules
.length
> 1 or not in_nesting
.direct_smallers
.is_empty
or mdoc
!= null
578 redef class AStdImport
579 # The imported module once determined
580 var mmodule
: nullable MModule = null
584 # The associated MModule once build by a `ModelBuilder`
585 var mmodule
: nullable MModule
586 # Flag that indicate if the importation is already completed
587 var is_importation_done
: Bool = false