1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Copyright 2006-2008 Floréal Morandat <morandat@lirmm.fr>
4 # Copyright 2008 Jean Privat <jean@pryen.org>
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
10 # http://www.apache.org/licenses/LICENSE-2.0
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
18 # This package is used to load a metamodel
24 # Global context for tools
28 readable attr _error_count
: Int = 0
31 readable attr _warning_count
: Int = 0
37 _error_count
= _error_count
+ 1
41 meth warning
(s
: String)
43 if _opt_warn
.value
== 0 then return
45 if _opt_warn
.value
== 1 then
46 _warning_count
= _warning_count
+ 1
48 _error_count
= _error_count
+ 1
52 # Paths where to locate modules files
53 readable attr _paths
: Array[String]
55 # List of module loaders
56 attr _loaders
: Array[ModuleLoader] = new Array[ModuleLoader]
58 # Global OptionContext
59 readable attr _option_context
: OptionContext = new OptionContext
62 readable attr _opt_warn
: OptionCount = new OptionCount("Show warnings", "-W", "--warn")
65 readable attr _opt_path
: OptionArray = new OptionArray("Set include path for loaders (may be used more than once)", "-I", "--path")
68 readable attr _opt_log
: OptionBool = new OptionBool("Generate various log files", "--log")
70 # Option --only-metamodel
71 readable attr _opt_only_metamodel
: OptionBool = new OptionBool("Stop after meta-model processing", "--only-metamodel")
74 readable attr _opt_only_parse
: OptionBool = new OptionBool("Only proceed to parse step of loaders", "--only-parse")
77 readable attr _opt_help
: OptionBool = new OptionBool("Show Help (This screen)", "-h", "-?", "--help")
82 option_context
.add_option
(opt_warn
, opt_path
, opt_log
, opt_only_parse
, opt_only_metamodel
, opt_help
)
85 # Parse and process the options given on the command line
89 option_context
.parse
(args
)
91 # Setup the paths value
92 _paths
= new Array[String]
93 paths
.append
(opt_path
.value
)
95 var path_env
= once
("NIT_PATH".to_symbol
).environ
96 if not path_env
.is_empty
then
97 paths
.append
(path_env
.split_with
(':'))
100 path_env
= once
("NIT_DIR".to_symbol
).environ
101 if not path_env
.is_empty
then
102 var libname
= "{path_env}/lib"
103 if libname
.file_exists
then paths
.add
(libname
)
106 var libname
= "{sys.program_name.dirname}/../lib"
107 if libname
.file_exists
then paths
.add
(libname
)
110 # Load and process a module in a directory (or a parent directory).
111 # If the module is already loaded, just return it without further processing.
112 # If no module is found, just return null without complaining.
113 private meth try_to_load
(module_name
: Symbol, dir
: MMDirectory): MMModule
115 # Look in the module directory
116 for m
in dir
.modules
do
117 if m
.name
== module_name
then return m
120 # print "try to load {module_name} in {dir.name} {_loaders.length}"
123 var dir2
= l
.try_to_load_dir
(module_name
, dir
)
125 var m
= try_to_load
(module_name
, dir2
)
133 if l
.can_handle
(module_name
, dir
) then
134 var full_name
= dir
.full_name_for
(module_name
)
135 if _processing_modules
.has
(full_name
) then
136 # FIXME: Generate better error
137 error
("Error: Dependency loop for module {full_name}")
141 _processing_modules
.add
(full_name
)
142 var m
= l
.load_and_process_module
(self, module_name
, dir
)
143 _processing_modules
.remove
(full_name
)
144 #if m != null then print "loaded {m.name} in {m.directory.name} -> {m.full_name} ({m.full_name.object_id.to_hex})"
152 # List of module currently processed.
153 # Used to prevent dependence loops.
154 attr _processing_modules
: HashSet[Symbol] = new HashSet[Symbol]
156 # Locate, load and analysis a module (and its supermodules) from its file name.
157 # If the module is already loaded, just return it without further processing.
158 # Beware, the files are automatically considered root of their directory.
159 meth get_module_from_filename
(filename
: String): MMModule
161 var path
= filename
.dirname
162 var module_name
= filename
.basename
(".nit").to_symbol
164 var dir
= directory_for
(path
)
166 if module_name
.to_s
== filename
then
167 # It's just a modulename
168 # look for it in the path directory "."
169 var m
= try_to_load
(module_name
, dir
)
170 if m
!= null then return m
172 # Else look for it in the path
173 return get_module
(module_name
, null)
176 if not filename
.file_exists
then
177 error
("Error: File {filename} not found.")
182 # Try to load the module where mentionned
183 var m
= try_to_load
(module_name
, dir
)
184 if m
!= null then return m
186 error
("Error: {filename} is not a NIT source module.")
191 # Locate, load and analysis a module (and its supermodules).
192 # If the module is already loaded, just return it without further processing.
193 meth get_module
(module_name
: Symbol, from
: MMModule): MMModule
197 var dir
= from
.directory
199 var m
= try_to_load
(module_name
, dir
)
200 if m
!= null then return m
206 var m
= try_to_load
(module_name
, directory_for
(p
))
207 if m
!= null then return m
209 # FIXME: Generate better error
210 error
("Error: No ressource found for module {module_name}.")
215 # Return the module directory associated with a given path
216 private meth directory_for
(path
: String): MMDirectory
218 if _path_dirs
.has_key
(path
) then return _path_dirs
[path
]
219 var dir
= new MMDirectory(path
.to_symbol
, path
, null)
220 _path_dirs
[path
] = dir
224 # Association bwtween plain path and module directories
225 attr _path_dirs
: Map[String, MMDirectory] = new HashMap[String, MMDirectory]
227 # Register a new module loader
228 meth register_loader
(ml
: ModuleLoader) do _loaders
.add
(ml
)
231 # A load handler know how to load a specific module type
233 # Type of module loaded by the loader
234 type MODULE: MMModule
236 # Extension that the loadhandler accepts
237 meth file_type
: String is abstract
239 # Try to load a new module directory
240 meth try_to_load_dir
(dirname
: Symbol, parent_dir
: MMDirectory): MMDirectory
242 var fname
= "{parent_dir.path}/{dirname}/"
243 if not fname
.file_exists
then return null
245 var dir
= new MMDirectory(parent_dir
.full_name_for
(dirname
), fname
, parent_dir
)
249 # Can the loadhandler load a given module?
250 # Return the file found
251 meth can_handle
(module_name
: Symbol, dir
: MMDirectory): Bool
253 var fname
= "{dir.path}/{module_name}.{file_type}"
254 if fname
.file_exists
then return true
258 # Load the module and process it
259 # filename is the result of can_handle
260 meth load_and_process_module
(context
: ToolContext, module_name
: Symbol, dir
: MMDirectory): MODULE
262 var filename
= "{dir.path}/{module_name}.{file_type}"
263 var m
= load_module
(context
, module_name
, dir
, filename
)
264 if not context
.opt_only_parse
.value
then process_metamodel
(context
, m
)
268 # Load an parse the module
269 private meth load_module
(context
: ToolContext, module_name
: Symbol, dir
: MMDirectory, filename
: String): MODULE
272 if filename
== "-" then
275 file
= new IFStream.open
(filename
.to_s
)
279 context
.error
("Error: Problem in opening file {filename}")
283 var m
= parse_file
(context
, file
, filename
, module_name
, dir
)
284 if file
!= stdin
then file
.close
288 # Parse the file to load a module
289 protected meth parse_file
(context
: ToolContext, file
: IFStream, filename
: String, module_name
: Symbol, dir
: MMDirectory): MODULE is abstract
291 # Process a parsed module
292 protected meth process_metamodel
(context
: ToolContext, module: MODULE) is abstract
296 # Recurcivelty process an import modules
297 meth import_supers_modules
(names
: Collection[Symbol])
300 assert c
isa ToolContext
301 var supers
= new Array[MMModule]
303 var m
= c
.get_module
(n
, self)
306 c
.add_module
(self,supers
)