- option_context.add_option(opt_path, opt_only_parse, opt_only_metamodel)
- end
-
- fun modelbuilder: ModelBuilder do return modelbuilder_real.as(not null)
- private var modelbuilder_real: nullable ModelBuilder = null
-
-end
-
-# A model builder knows how to load nit source files and build the associated model
-class ModelBuilder
- # The model where new modules, classes and properties are added
- var model: Model
-
- # The toolcontext used to control the interaction with the user (getting options and displaying messages)
- var toolcontext: ToolContext
-
- # Run phases on all loaded modules
- fun run_phases
- do
- var mmodules = model.mmodules.to_a
- model.mmodule_importation_hierarchy.sort(mmodules)
- var nmodules = new Array[AModule]
- for mm in mmodules do
- nmodules.add(mmodule2nmodule[mm])
- end
- toolcontext.run_phases(nmodules)
- end
-
- # Instantiate a modelbuilder for a model and a toolcontext
- # Important, the options of the toolcontext must be correctly set (parse_option already called)
- init(model: Model, toolcontext: ToolContext)
- do
- self.model = model
- self.toolcontext = toolcontext
- assert toolcontext.modelbuilder_real == null
- toolcontext.modelbuilder_real = self
-
- # Setup the paths value
- paths.append(toolcontext.opt_path.value)
-
- var path_env = "NIT_PATH".environ
- if not path_env.is_empty then
- paths.append(path_env.split_with(':'))
- end
-
- path_env = "NIT_DIR".environ
- if not path_env.is_empty then
- var libname = "{path_env}/lib"
- if libname.file_exists then paths.add(libname)
- end
-
- var libname = "{sys.program_name.dirname}/../lib"
- if libname.file_exists then paths.add(libname.simplify_path)
- end
-
- # Load a bunch of modules.
- # `modules' can contains filenames or module names.
- # Imported modules are automatically loaded and modelized.
- # The result is the corresponding model elements.
- # Errors and warnings are printed with the toolcontext.
- #
- # Note: class and property model element are not analysed.
- fun parse(modules: Sequence[String]): Array[MModule]
- do
- var time0 = get_time
- # Parse and recursively load
- self.toolcontext.info("*** PARSE ***", 1)
- var mmodules = new Array[MModule]
- for a in modules do
- var nmodule = self.load_module(null, a)
- if nmodule == null then continue # Skip error
- mmodules.add(nmodule.mmodule.as(not null))
- end
- var time1 = get_time
- self.toolcontext.info("*** END PARSE: {time1-time0} ***", 2)
-
- self.toolcontext.check_errors
- return mmodules
- end
-
- # Return a class named `name' visible by the module `mmodule'.
- # Visibility in modules is correctly handled.
- # If no such a class exists, then null is returned.
- # If more than one class exists, then an error on `anode' is displayed and null is returned.
- # FIXME: add a way to handle class name conflict
- fun try_get_mclass_by_name(anode: ANode, mmodule: MModule, name: String): nullable MClass
- do
- var classes = model.get_mclasses_by_name(name)
- if classes == null then
- return null
- end
-
- var res: nullable MClass = null
- for mclass in classes do
- if not mmodule.in_importation <= mclass.intro_mmodule then continue
- if not mmodule.is_visible(mclass.intro_mmodule, mclass.visibility) then continue
- if res == null then
- res = mclass
- else
- error(anode, "Ambigous class name '{name}'; conflict between {mclass.full_name} and {res.full_name}")
- return null
- end
- end
- return res