-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
- if location == null then
- return text
- else
- return "{location}: {text}"
- end
- end
-end
-
-# Global context for tools
-class ToolContext
-special MMContext
- # Number of errors
- readable var _error_count: Int = 0
-
- # Number of warnings
- readable var _warning_count: Int = 0
-
- # 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(l: nullable Location, s: String)
- do
- _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(l: nullable Location, s: String)
- do
- if _opt_warn.value == 0 then return
- _messages.add(new Message(l,s))
- if _opt_warn.value == 1 then
- _warning_count = _warning_count + 1
- else
- _error_count = _error_count + 1
- end
- end
-
- # Display an info
- fun info(s: String, level: Int)
- do
- if level <= verbose_level then
- print "{s}"
- end
- end