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>
5 # Copyright 2009 Jean-Sebastien Gelinas <calestar@gmail.com>
7 # Licensed under the Apache License, Version 2.0 (the "License");
8 # you may not use this file except in compliance with the License.
9 # You may obtain a copy of the License at
11 # http://www.apache.org/licenses/LICENSE-2.0
13 # Unless required by applicable law or agreed to in writing, software
14 # distributed under the License is distributed on an "AS IS" BASIS,
15 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 # See the License for the specific language governing permissions and
17 # limitations under the License.
19 # This package is used to load a metamodel
28 redef type OTHER: Message
30 readable attr _location
: nullable Location
31 readable attr _text
: String
33 redef fun <(other
: OTHER): Bool do
34 if location
== null then return true
35 if other
.location
== null then return false
37 return location
.as(not null) < other
.location
.as(not null)
40 redef fun to_s
: String do
41 if location
== null then
44 return "{location}: {text}"
49 # Global context for tools
53 readable var _error_count
: Int = 0
56 readable var _warning_count
: Int = 0
59 var _messages
: Array[Message] = new Array[Message]
60 var _message_sorter
: ComparableSorter[Message] = new ComparableSorter[Message]
64 if _messages
.length
> 0 then
65 _message_sorter
.sort
(_messages
)
74 if error_count
> 0 then exit
(1)
78 fun error
(l
: nullable Location, s
: String)
80 _messages
.add
(new Message(l
,s
))
81 _error_count
= _error_count
+ 1
84 # Add an error, show errors and quit
85 fun fatal_error
(l
: nullable Location, s
: String)
92 fun warning
(l
: nullable Location, s
: String)
94 if _opt_warn
.value
== 0 then return
95 _messages
.add
(new Message(l
,s
))
96 if _opt_warn
.value
== 1 then
97 _warning_count
= _warning_count
+ 1
99 _error_count
= _error_count
+ 1
104 meth info
(s
: String, level
: Int)
106 if level
<= verbose_level
then
111 # Paths where to locate modules files
112 readable var _paths
: Array[String] = new Array[String]
114 # List of module loaders
115 var _loaders
: Array[ModuleLoader] = new Array[ModuleLoader]
117 # Global OptionContext
118 readable var _option_context
: OptionContext = new OptionContext
121 readable var _opt_warn
: OptionCount = new OptionCount("Show warnings", "-W", "--warn")
124 readable var _opt_path
: OptionArray = new OptionArray("Set include path for loaders (may be used more than once)", "-I", "--path")
127 readable var _opt_log
: OptionBool = new OptionBool("Generate various log files", "--log")
129 # Option --only-metamodel
130 readable var _opt_only_metamodel
: OptionBool = new OptionBool("Stop after meta-model processing", "--only-metamodel")
132 # Option --only-parse
133 readable var _opt_only_parse
: OptionBool = new OptionBool("Only proceed to parse step of loaders", "--only-parse")
136 readable var _opt_help
: OptionBool = new OptionBool("Show Help (This screen)", "-h", "-?", "--help")
139 readable var _opt_version
: OptionBool = new OptionBool("Show version and exit", "--version")
142 readable var _opt_verbose
: OptionCount = new OptionCount("Verbose", "-v", "--verbose")
145 readable var _verbose_level
: Int = 0
150 option_context
.add_option
(opt_warn
, opt_path
, opt_log
, opt_only_parse
, opt_only_metamodel
, opt_help
, opt_version
, opt_verbose
)
153 # Parse and process the options given on the command line
157 option_context
.parse
(args
)
160 _verbose_level
= opt_verbose
.value
162 # Setup the paths value
163 paths
.append
(opt_path
.value
)
165 var path_env
= once
("NIT_PATH".to_symbol
).environ
166 if not path_env
.is_empty
then
167 paths
.append
(path_env
.split_with
(':'))
170 path_env
= once
("NIT_DIR".to_symbol
).environ
171 if not path_env
.is_empty
then
172 var libname
= "{path_env}/lib"
173 if libname
.file_exists
then paths
.add
(libname
)
176 var libname
= "{sys.program_name.dirname}/../lib"
177 if libname
.file_exists
then paths
.add
(libname
)
180 # Load and process a module in a directory (or a parent directory).
181 # If the module is already loaded, just return it without further processing.
182 # If no module is found, just return null without complaining.
183 private fun try_to_load
(module_name
: Symbol, dir
: MMDirectory): nullable MMModule
185 # Look in the module directory
186 for m
in dir
.modules
do
187 if m
.name
== module_name
then return m
190 # print "try to load {module_name} in {dir.name} {_loaders.length}"
193 var dir2
= l
.try_to_load_dir
(module_name
, dir
)
195 var m
= try_to_load
(module_name
, dir2
)
203 if l
.can_handle
(module_name
, dir
) then
204 var full_name
= dir
.full_name_for
(module_name
)
205 if _processing_modules
.has
(full_name
) then
206 # FIXME: Generate better error
207 fatal_error
(null, "Error: Dependency loop for module {full_name}")
209 _processing_modules
.add
(full_name
)
210 var m
= l
.load_and_process_module
(self, module_name
, dir
)
211 _processing_modules
.remove
(full_name
)
212 #if m != null then print "loaded {m.name} in {m.directory.name} -> {m.full_name} ({m.full_name.object_id.to_hex})"
220 # List of module currently processed.
221 # Used to prevent dependence loops.
222 var _processing_modules
: HashSet[Symbol] = new HashSet[Symbol]
224 # Locate, load and analysis a module (and its supermodules) from its file name.
225 # If the module is already loaded, just return it without further processing.
226 # Beware, the files are automatically considered root of their directory.
227 fun get_module_from_filename
(filename
: String): MMModule
229 var path
= filename
.dirname
230 var module_name
= filename
.basename
(".nit").to_symbol
232 var dir
= directory_for
(path
)
234 if module_name
.to_s
== filename
then
235 # It's just a modulename
236 # look for it in the path directory "."
237 var m
= try_to_load
(module_name
, dir
)
238 if m
!= null then return m
240 # Else look for it in the path
241 return get_module
(module_name
, null)
244 if not filename
.file_exists
then
245 fatal_error
(null, "Error: File {filename} not found.")
249 # Try to load the module where mentionned
250 var m
= try_to_load
(module_name
, dir
)
251 if m
!= null then return m
253 fatal_error
(null, "Error: {filename} is not a NIT source module.")
257 # Locate, load and analysis a module (and its supermodules).
258 # If the module is already loaded, just return it without further processing.
259 fun get_module
(module_name
: Symbol, from
: nullable MMModule): MMModule
263 var dir
: nullable MMDirectory = from
.directory
265 var m
= try_to_load
(module_name
, dir
)
266 if m
!= null then return m
272 var m
= try_to_load
(module_name
, directory_for
(p
))
273 if m
!= null then return m
275 # FIXME: Generate better error
276 fatal_error
(null, "Error: No ressource found for module {module_name}.")
280 # Return the module directory associated with a given path
281 private fun directory_for
(path
: String): MMDirectory
283 if _path_dirs
.has_key
(path
) then return _path_dirs
[path
]
284 var dir
= new MMDirectory(path
.to_symbol
, path
, null)
285 _path_dirs
[path
] = dir
289 # Association bwtween plain path and module directories
290 var _path_dirs
: Map[String, MMDirectory] = new HashMap[String, MMDirectory]
292 # Register a new module loader
293 fun register_loader
(ml
: ModuleLoader) do _loaders
.add
(ml
)
296 # A load handler know how to load a specific module type
298 # Type of module loaded by the loader
299 type MODULE: MMModule
301 # Extension that the loadhandler accepts
302 fun file_type
: String is abstract
304 # Try to load a new module directory
305 fun try_to_load_dir
(dirname
: Symbol, parent_dir
: MMDirectory): nullable MMDirectory
307 var fname
= "{parent_dir.path}/{dirname}/"
308 if not fname
.file_exists
then return null
310 var dir
= new MMDirectory(parent_dir
.full_name_for
(dirname
), fname
, parent_dir
)
314 # Can the loadhandler load a given module?
315 # Return the file found
316 fun can_handle
(module_name
: Symbol, dir
: MMDirectory): Bool
318 var fname
= "{dir.path}/{module_name}.{file_type}"
319 if fname
.file_exists
then return true
323 # Load the module and process it
324 # filename is the result of can_handle
325 fun load_and_process_module
(context
: ToolContext, module_name
: Symbol, dir
: MMDirectory): MODULE
327 var filename
= "{dir.path}/{module_name}.{file_type}"
328 var m
= load_module
(context
, module_name
, dir
, filename
)
329 if not context
.opt_only_parse
.value
then process_metamodel
(context
, m
)
333 # Load an parse the module
334 private fun load_module
(context
: ToolContext, module_name
: Symbol, dir
: MMDirectory, filename
: String): MODULE
337 if filename
== "-" then
340 file
= new IFStream.open
(filename
.to_s
)
344 context
.fatal_error
(null, "Error: Problem in opening file {filename}")
346 var m
= parse_file
(context
, file
, filename
, module_name
, dir
)
347 if file
!= stdin
then file
.close
351 # Parse the file to load a module
352 protected fun parse_file
(context
: ToolContext, file
: IFStream, filename
: String, module_name
: Symbol, dir
: MMDirectory): MODULE is abstract
354 # Process a parsed module
355 protected fun process_metamodel
(context
: ToolContext, module: MODULE) is abstract
359 # Recurcivelty process an import modules
360 fun import_supers_modules
(names
: Collection[Symbol])
363 assert c
isa ToolContext
364 var supers
= new Array[MMModule]
366 var m
= c
.get_module
(n
, self)
369 c
.add_module
(self,supers
)