doc: use 'module' instead of 'package' in comments
[nit.git] / src / mmloader.nit
index 17066c4..c63455f 100644 (file)
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-# This package is used to load a metamodel 
+# This module is used to load a metamodel
 package mmloader
 
 import metamodel
 import opts
+import location
 
-class Location
-       readable var _file: String
-       readable var _line_start: Int
-       readable var _line_end: Int
-       readable var _column_start: Int
-       readable var _column_end: Int
-
-       redef meth to_s: String do
-               if line_start == line_end then
-                       if column_start == column_end then
-                               return "{file}:{line_start},{column_start}"
-                       else
-                               return "{file}:{line_start},{column_start}--{column_end}"
-                       end
+class Message
+special Comparable
+       redef type OTHER: Message
+
+       readable var _location: nullable Location
+       readable var _text: String
+
+       redef fun <(other: OTHER): Bool do
+               if location == null then return true
+               if other.location == null then return false
+
+               return location.as(not null) < other.location.as(not null)
+       end
+
+       redef fun to_s: String do
+               var l = location
+               if l == null then
+                       return text
                else
-                       return "{file}:{line_start},{column_start}--{line_end}:{column_end}"
+                       return "{l}: {text}"
                end
        end
 end
@@ -46,23 +51,52 @@ end
 class ToolContext
 special MMContext
        # Number of errors
-       readable var _error_count: Int = 0 
+       readable var _error_count: Int = 0
 
        # Number of warnings
        readable var _warning_count: Int = 0
 
+       # Directory where to generate log files
+       readable var _log_directory: String = "logs"
+
+       # Messages
+       var _messages: Array[Message] = new Array[Message]
+       var _message_sorter: ComparableSorter[Message] = new ComparableSorter[Message]
+
+       fun check_errors
+       do
+               if _messages.length > 0 then
+                       _message_sorter.sort(_messages)
+
+                       for m in _messages do
+                               stderr.write("{m}\n")
+                       end
+
+                       _messages.clear
+               end
+
+               if error_count > 0 then exit(1)
+       end
+
        # Display an error
-       fun error(s: String)
+       fun error(l: nullable Location, s: String)
        do
-               stderr.write("{s}\n")
+               _messages.add(new Message(l,s))
                _error_count = _error_count + 1
        end
 
+       # Add an error, show errors and quit
+       fun fatal_error(l: nullable Location, s: String)
+       do
+               error(l,s)
+               check_errors
+       end
+
        # Display a warning
-       fun warning(s: String)
+       fun warning(l: nullable Location, s: String)
        do
                if _opt_warn.value == 0 then return
-               stderr.write("{s}\n")
+               _messages.add(new Message(l,s))
                if _opt_warn.value == 1 then
                        _warning_count = _warning_count + 1
                else
@@ -71,7 +105,7 @@ special MMContext
        end
 
        # Display an info
-       meth info(s: String, level: Int)
+       fun info(s: String, level: Int)
        do
                if level <= verbose_level then
                        print "{s}"
@@ -93,9 +127,12 @@ special MMContext
        # Option --path
        readable var _opt_path: OptionArray = new OptionArray("Set include path for loaders (may be used more than once)", "-I", "--path")
 
-       # Option --lop
+       # Option --log
        readable var _opt_log: OptionBool = new OptionBool("Generate various log files", "--log")
-       
+
+       # Option --log-dir
+       readable var _opt_log_dir: OptionString = new OptionString("Directory where to generate log files", "--log-dir")
+
        # Option --only-metamodel
        readable var _opt_only_metamodel: OptionBool = new OptionBool("Stop after meta-model processing", "--only-metamodel")
 
@@ -117,7 +154,7 @@ special MMContext
        init
        do
                super
-               option_context.add_option(opt_warn, opt_path, opt_log, opt_only_parse, opt_only_metamodel, opt_help, opt_version, opt_verbose)
+               option_context.add_option(opt_warn, opt_path, opt_log, opt_log_dir, opt_only_parse, opt_only_metamodel, opt_help, opt_version, opt_verbose)
        end
 
        # Parse and process the options given on the command line
@@ -145,6 +182,12 @@ special MMContext
 
                var libname = "{sys.program_name.dirname}/../lib"
                if libname.file_exists then paths.add(libname)
+
+               if opt_log_dir.value != null then _log_directory = opt_log_dir.value.as(not null)
+               if _opt_log.value then
+                       # Make sure the output directory exists
+                       log_directory.mkdir
+               end
        end
 
        # Load and process a module in a directory (or a parent directory).
@@ -174,9 +217,7 @@ special MMContext
                                var full_name = dir.full_name_for(module_name)
                                if _processing_modules.has(full_name) then
                                        # FIXME: Generate better error
-                                       error("Error: Dependency loop for module {full_name}")
-                                       exit(1)
-                                       abort
+                                       fatal_error(null, "Error: Dependency loop for module {full_name}")
                                end
                                _processing_modules.add(full_name)
                                var m = l.load_and_process_module(self, module_name, dir)
@@ -214,8 +255,7 @@ special MMContext
                end
 
                if not filename.file_exists then
-                       error("Error: File {filename} not found.")
-                       exit(1)
+                       fatal_error(null, "Error: File {filename} not found.")
                        abort
                end
 
@@ -223,8 +263,7 @@ special MMContext
                var m = try_to_load(module_name, dir)
                if m != null then return m
 
-               error("Error: {filename} is not a NIT source module.")
-               exit(1)
+               fatal_error(null, "Error: {filename} is not a NIT source module.")
                abort
        end
 
@@ -232,7 +271,6 @@ special MMContext
        # If the module is already loaded, just return it without further processing.
        fun get_module(module_name: Symbol, from: nullable MMModule): MMModule
        do
-               var m: MMModule
                if from != null then
                        var dir: nullable MMDirectory = from.directory
                        while dir != null do
@@ -247,8 +285,7 @@ special MMContext
                        if m != null then return m
                end
                # FIXME: Generate better error
-               error("Error: No ressource found for module {module_name}.")
-               exit(1)
+               fatal_error(null, "Error: No ressource found for module {module_name}.")
                abort
        end
 
@@ -316,9 +353,7 @@ class ModuleLoader
                end
 
                if file.eof then
-                       context.error("Error: Problem in opening file {filename}")
-                       exit(1)
-                       abort
+                       context.fatal_error(null, "Error: Problem in opening file {filename}")
                end
                var m = parse_file(context, file, filename, module_name, dir)
                if file != stdin then file.close
@@ -329,7 +364,7 @@ class ModuleLoader
        protected fun parse_file(context: ToolContext, file: IFStream, filename: String, module_name: Symbol, dir: MMDirectory): MODULE is abstract
 
        # Process a parsed module
-       protected fun process_metamodel(context: ToolContext, module: MODULE) is abstract
+       protected fun process_metamodel(context: ToolContext, mod: MODULE) is abstract
 end
 
 redef class MMModule