5133747d792f7a1ef4df14773a97fc95b69fbcee
[nit.git] / src / mmloader.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
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>
6 #
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
10 #
11 # http://www.apache.org/licenses/LICENSE-2.0
12 #
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.
18
19 # This module is used to load a metamodel
20 package mmloader
21
22 import metamodel
23 import opts
24 import location
25
26 class Message
27 super Comparable
28 redef type OTHER: Message
29
30 readable var _location: nullable Location
31 readable var _text: String
32
33 redef fun <(other: OTHER): Bool do
34 if location == null then return true
35 if other.location == null then return false
36
37 return location.as(not null) < other.location.as(not null)
38 end
39
40 redef fun to_s: String do
41 var l = location
42 if l == null then
43 return text
44 else
45 return "{l}: {text}"
46 end
47 end
48 end
49
50 # Global context for tools
51 class ToolContext
52 super MMContext
53 # Number of errors
54 readable var _error_count: Int = 0
55
56 # Number of warnings
57 readable var _warning_count: Int = 0
58
59 # Directory where to generate log files
60 readable var _log_directory: String = "logs"
61
62 # Messages
63 var _messages: Array[Message] = new Array[Message]
64 var _message_sorter: ComparableSorter[Message] = new ComparableSorter[Message]
65
66 fun check_errors
67 do
68 if _messages.length > 0 then
69 _message_sorter.sort(_messages)
70
71 for m in _messages do
72 stderr.write("{m}\n")
73 end
74
75 _messages.clear
76 end
77
78 if error_count > 0 then exit(1)
79 end
80
81 # Display an error
82 fun error(l: nullable Location, s: String)
83 do
84 _messages.add(new Message(l,s))
85 _error_count = _error_count + 1
86 if opt_stop_on_first_error.value then check_errors
87 end
88
89 # Add an error, show errors and quit
90 fun fatal_error(l: nullable Location, s: String)
91 do
92 error(l,s)
93 check_errors
94 end
95
96 # Display a warning
97 fun warning(l: nullable Location, s: String)
98 do
99 if _opt_warn.value == 0 then return
100 _messages.add(new Message(l,s))
101 if _opt_warn.value == 1 then
102 _warning_count = _warning_count + 1
103 else
104 _error_count = _error_count + 1
105 end
106 if opt_stop_on_first_error.value then check_errors
107 end
108
109 # Display an info
110 fun info(s: String, level: Int)
111 do
112 if level <= verbose_level then
113 print "{s}"
114 end
115 end
116
117 # Paths where to locate modules files
118 readable var _paths: Array[String] = new Array[String]
119
120 # List of module loaders
121 var _loaders: Array[ModuleLoader] = new Array[ModuleLoader]
122
123 # Global OptionContext
124 readable var _option_context: OptionContext = new OptionContext
125
126 # Option --warn
127 readable var _opt_warn: OptionCount = new OptionCount("Show warnings", "-W", "--warn")
128
129 # Option --path
130 readable var _opt_path: OptionArray = new OptionArray("Set include path for loaders (may be used more than once)", "-I", "--path")
131
132 # Option --log
133 readable var _opt_log: OptionBool = new OptionBool("Generate various log files", "--log")
134
135 # Option --log-dir
136 readable var _opt_log_dir: OptionString = new OptionString("Directory where to generate log files", "--log-dir")
137
138 # Option --only-metamodel
139 readable var _opt_only_metamodel: OptionBool = new OptionBool("Stop after meta-model processing", "--only-metamodel")
140
141 # Option --only-parse
142 readable var _opt_only_parse: OptionBool = new OptionBool("Only proceed to parse step of loaders", "--only-parse")
143
144 # Option --help
145 readable var _opt_help: OptionBool = new OptionBool("Show Help (This screen)", "-h", "-?", "--help")
146
147 # Option --version
148 readable var _opt_version: OptionBool = new OptionBool("Show version and exit", "--version")
149
150 # Option --verbose
151 readable var _opt_verbose: OptionCount = new OptionCount("Verbose", "-v", "--verbose")
152
153 # Option --stop-on-first-error
154 readable var _opt_stop_on_first_error: OptionBool = new OptionBool("Stop on first error", "--stop-on-first-error")
155
156 # Verbose level
157 readable var _verbose_level: Int = 0
158
159 init
160 do
161 super
162 option_context.add_option(opt_warn, opt_stop_on_first_error, opt_path, opt_log, opt_log_dir, opt_only_parse, opt_only_metamodel, opt_help, opt_version, opt_verbose)
163 end
164
165 # Parse and process the options given on the command line
166 fun process_options
167 do
168 # init options
169 option_context.parse(args)
170
171 # Set verbose level
172 _verbose_level = opt_verbose.value
173
174 # Setup the paths value
175 paths.append(opt_path.value)
176
177 var path_env = once ("NIT_PATH".to_symbol).environ
178 if not path_env.is_empty then
179 paths.append(path_env.split_with(':'))
180 end
181
182 path_env = once ("NIT_DIR".to_symbol).environ
183 if not path_env.is_empty then
184 var libname = "{path_env}/lib"
185 if libname.file_exists then paths.add(libname)
186 end
187
188 var libname = "{sys.program_name.dirname}/../lib"
189 if libname.file_exists then paths.add(libname)
190
191 if opt_log_dir.value != null then _log_directory = opt_log_dir.value.as(not null)
192 if _opt_log.value then
193 # Make sure the output directory exists
194 log_directory.mkdir
195 end
196 end
197
198 # Load and process a module in a directory (or a parent directory).
199 # If the module is already loaded, just return it without further processing.
200 # If no module is found, just return null without complaining.
201 private fun try_to_load(module_name: Symbol, dir: MMDirectory): nullable MMModule
202 do
203 # Look in the module directory
204 for m in dir.modules do
205 if m.name == module_name then return m
206 end
207
208 # print "try to load {module_name} in {dir.name} {_loaders.length}"
209
210 for l in _loaders do
211 var dir2 = l.try_to_load_dir(module_name, dir)
212 if dir2 != null then
213 var m = try_to_load(module_name, dir2)
214 if m != null then
215 dir2.owner = m
216 dir.add_module(m)
217 return m
218 end
219 end
220
221 if l.can_handle(module_name, dir) then
222 var full_name = dir.full_name_for(module_name)
223 if _processing_modules.has(full_name) then
224 # FIXME: Generate better error
225 fatal_error(null, "Error: Dependency loop for module {full_name}")
226 end
227 _processing_modules.add(full_name)
228 var m = l.load_and_process_module(self, module_name, dir)
229 _processing_modules.remove(full_name)
230 #if m != null then print "loaded {m.name} in {m.directory.name} -> {m.full_name} ({m.full_name.object_id.to_hex})"
231 dir.add_module(m)
232 return m
233 end
234 end
235 return null
236 end
237
238 # List of module currently processed.
239 # Used to prevent dependence loops.
240 var _processing_modules: HashSet[Symbol] = new HashSet[Symbol]
241
242 # Locate, load and analysis a module (and its supermodules) from its file name.
243 # If the module is already loaded, just return it without further processing.
244 # Beware, the files are automatically considered root of their directory.
245 fun get_module_from_filename(filename: String): MMModule
246 do
247 var path = filename.dirname
248 var module_name = filename.basename(".nit").to_symbol
249
250 var dir = directory_for(path)
251
252 if module_name.to_s == filename then
253 # It's just a modulename
254 # look for it in the path directory "."
255 var m = try_to_load(module_name, dir)
256 if m != null then return m
257
258 # Else look for it in the path
259 return get_module(module_name, null)
260 end
261
262 if not filename.file_exists then
263 fatal_error(null, "Error: File {filename} not found.")
264 abort
265 end
266
267 # Try to load the module where mentionned
268 var m = try_to_load(module_name, dir)
269 if m != null then return m
270
271 fatal_error(null, "Error: {filename} is not a NIT source module.")
272 abort
273 end
274
275 # Locate, load and analysis a module (and its supermodules).
276 # If the module is already loaded, just return it without further processing.
277 fun get_module(module_name: Symbol, from: nullable MMModule): MMModule
278 do
279 if from != null then
280 var dir: nullable MMDirectory = from.directory
281 while dir != null do
282 var m = try_to_load(module_name, dir)
283 if m != null then return m
284 dir = dir.parent
285 end
286 end
287
288 for p in paths do
289 var m = try_to_load(module_name, directory_for(p))
290 if m != null then return m
291 end
292 # FIXME: Generate better error
293 fatal_error(null, "Error: No ressource found for module {module_name}.")
294 abort
295 end
296
297 # Return the module directory associated with a given path
298 private fun directory_for(path: String): MMDirectory
299 do
300 if _path_dirs.has_key(path) then return _path_dirs[path]
301 var dir = new MMDirectory(path.to_symbol, path, null)
302 _path_dirs[path] = dir
303 return dir
304 end
305
306 # Association bwtween plain path and module directories
307 var _path_dirs: Map[String, MMDirectory] = new HashMap[String, MMDirectory]
308
309 # Register a new module loader
310 fun register_loader(ml: ModuleLoader) do _loaders.add(ml)
311 end
312
313 # A load handler know how to load a specific module type
314 class ModuleLoader
315 # Type of module loaded by the loader
316 type MODULE: MMModule
317
318 # Extension that the loadhandler accepts
319 fun file_type: String is abstract
320
321 # Try to load a new module directory
322 fun try_to_load_dir(dirname: Symbol, parent_dir: MMDirectory): nullable MMDirectory
323 do
324 var fname = "{parent_dir.path}/{dirname}/"
325 if not fname.file_exists then return null
326
327 var dir = new MMDirectory(parent_dir.full_name_for(dirname), fname, parent_dir)
328 return dir
329 end
330
331 # Can the loadhandler load a given module?
332 # Return the file found
333 fun can_handle(module_name: Symbol, dir: MMDirectory): Bool
334 do
335 var fname = "{dir.path}/{module_name}.{file_type}"
336 if fname.file_exists then return true
337 return false
338 end
339
340 # Load the module and process it
341 # filename is the result of can_handle
342 fun load_and_process_module(context: ToolContext, module_name: Symbol, dir: MMDirectory): MODULE
343 do
344 var filename = "{dir.path}/{module_name}.{file_type}"
345 var m = load_module(context, module_name, dir, filename)
346 if not context.opt_only_parse.value then process_metamodel(context, m)
347 return m
348 end
349
350 # Load an parse the module
351 private fun load_module(context: ToolContext, module_name: Symbol, dir: MMDirectory, filename: String): MODULE
352 do
353 var file: IFStream
354 if filename == "-" then
355 file = stdin
356 else
357 file = new IFStream.open(filename.to_s)
358 end
359
360 if file.eof then
361 context.fatal_error(null, "Error: Problem in opening file {filename}")
362 end
363 var m = parse_file(context, file, filename, module_name, dir)
364 if file != stdin then file.close
365 return m
366 end
367
368 # Parse the file to load a module
369 protected fun parse_file(context: ToolContext, file: IFStream, filename: String, module_name: Symbol, dir: MMDirectory): MODULE is abstract
370
371 # Process a parsed module
372 protected fun process_metamodel(context: ToolContext, mod: MODULE) is abstract
373 end
374
375 redef class MMModule
376 # Recurcivelty process an import modules
377 fun import_supers_modules(names: Collection[Symbol])
378 do
379 var c = context
380 assert c isa ToolContext
381 var supers = new Array[MMModule]
382 for n in names do
383 var m = c.get_module(n, self)
384 supers.add(m)
385 end
386 c.add_module(self,supers)
387 end
388 end