# 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 location
class Message
-special Comparable
+ super Comparable
redef type OTHER: Message
- readable attr _location: nullable Location
- readable attr _text: String
+ readable var _location: nullable Location
+ readable var _text: String
redef fun <(other: OTHER): Bool do
if location == null then return true
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 "{l}: {text}"
+ end
+ end
+
+ fun to_color_string: String
+ do
+ var esc = 27.ascii
+ var red = "{esc}[0;31m"
+ var bred = "{esc}[1;31m"
+ var green = "{esc}[0;32m"
+ var yellow = "{esc}[0;33m"
+ var def = "{esc}[0m"
+
+ var l = location
+ if l == null then
+ return text
+ else if l.file == null then
+ return "{yellow}{l}{def}: {text}"
+ else
+ var i = location.line_start
+ var line_start = l.file.line_starts[i-1]
+ var line_end = line_start
+ var string = l.file.string
+ while line_end+1 < string.length and string[line_end+1] != '\n' and string[line_end+1] != '\r' do
+ line_end += 1
+ end
+ var lstart = string.substring(line_start, location.column_start - 1)
+ var cend
+ if i != location.line_end then
+ cend = line_end - line_start + 1
+ else
+ cend = location.column_end
+ end
+ var lmid = string.substring(line_start + location.column_start - 1, cend - location.column_start + 1)
+ var lend = string.substring(line_start + cend, line_end - line_start - cend + 1)
+ var indent = new Buffer
+ for j in [line_start..line_start+location.column_start-1[ do
+ if string[j] == '\t' then
+ indent.add '\t'
+ else
+ indent.add ' '
+ end
+ end
+ return "{yellow}{l}{def}: {text}\n\t{lstart}{bred}{lmid}{def}{lend}\n\t{indent}^"
+ end
+ end
end
# Global context for tools
class ToolContext
-special MMContext
+ super MMContext
# Number of errors
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]
_message_sorter.sort(_messages)
for m in _messages do
- stderr.write("{m.text}\n")
+ if opt_no_color.value then
+ stderr.write("{m}\n")
+ else
+ stderr.write("{m.to_color_string}\n")
+ end
end
_messages.clear
do
_messages.add(new Message(l,s))
_error_count = _error_count + 1
+ if opt_stop_on_first_error.value then check_errors
+ 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
else
_error_count = _error_count + 1
end
+ if opt_stop_on_first_error.value then check_errors
end
# Display an info
- meth info(s: String, level: Int)
+ fun info(s: String, level: Int)
do
if level <= verbose_level then
print "{s}"
# 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")
# Option --verbose
readable var _opt_verbose: OptionCount = new OptionCount("Verbose", "-v", "--verbose")
+ # Option --stop-on-first-error
+ readable var _opt_stop_on_first_error: OptionBool = new OptionBool("Stop on first error", "--stop-on-first-error")
+
+ # Option --no-color
+ readable var _opt_no_color: OptionBool = new OptionBool("Do not use color to display errors and warnings", "--no-color")
+
# Verbose level
readable var _verbose_level: Int = 0
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_stop_on_first_error, opt_no_color, 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
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).
var full_name = dir.full_name_for(module_name)
if _processing_modules.has(full_name) then
# FIXME: Generate better error
- error(null, "Error: Dependency loop for module {full_name}")
- check_errors
+ 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)
end
if not filename.file_exists then
- error(null, "Error: File {filename} not found.")
- check_errors
+ fatal_error(null, "Error: File {filename} not found.")
abort
end
var m = try_to_load(module_name, dir)
if m != null then return m
- error(null, "Error: {filename} is not a NIT source module.")
- check_errors
+ fatal_error(null, "Error: {filename} is not a NIT source module.")
abort
end
# 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
if m != null then return m
end
# FIXME: Generate better error
- error(null, "Error: No ressource found for module {module_name}.")
- check_errors
+ fatal_error(null, "Error: No ressource found for module {module_name}.")
abort
end
end
if file.eof then
- context.error(null, "Error: Problem in opening file {filename}")
- context.check_errors
+ 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
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
-end
-
-redef class MMModule
- # Recurcivelty process an import modules
- fun import_supers_modules(names: Collection[Symbol])
- do
- var c = context
- assert c isa ToolContext
- var supers = new Array[MMModule]
- for n in names do
- var m = c.get_module(n, self)
- supers.add(m)
- end
- c.add_module(self,supers)
- end
+ protected fun process_metamodel(context: ToolContext, mod: MODULE) is abstract
end