Module: Carbon::Compiler::Metanostic::Mode
- Defined in:
- lib/carbon/compiler/metanostic/mode.rb
Overview
The “mode” of a metanostic or diagnostic. This determines how a diagnostic should be treated.
Constant Summary collapse
- NONE =
No mode. This is rarely used.
0x0- IGNORE =
Ignore mode. This means that by default, the diagnostic should be ignored by the compiler; with a default or less verbose level, the diagnostic is not output to the compiler.
0b1- INFO =
Informational mode. This means that the diagnostic is output for the purpose of informing a developer of a certain thing; for example, the original definition of a function. At the default verbose level, it is output to the compiler.
0b10- WARNING =
Warning mode. This means that the diagnostic is output to inform the developer of a potential issue. Enough diagnostic warnings causes a panic. At the default verbose level, it is output to the compiler.
0b100- ERROR =
Error mode. This means that the diagnostic is output to inform the developer of a definite issue. The compiler will not be able to finish, and enough diagnostic errors causes a panic. At the default verbose level, it is output to the compiler.
0b1000- PANIC =
Panic mode. This means that the diagnostic is output to inform the developer of a fatal issue. Once the panic is emitted, all execution stops, and all diagnostics output.
0b10000- ALL =
All of the above. This is mostly used for metanostics to note that all of the above are allowable modes for a given diagnostic.
PANIC | ERROR | WARNING | INFO | IGNORE
- MODES =
An associative list of modes to convert from strings.
{ "none" => NONE, "ignore" => IGNORE, "info" => INFO, "warning" => WARNING, "error" => ERROR, "panic" => PANIC, "all" => ALL }.freeze
Class Method Summary collapse
-
.from(value) ⇒ Integer
Converts from a given value into a mode.
-
.from_array(value) ⇒ Integer
Converts the given Array value into a mode.
-
.from_string(value) ⇒ Integer
Converts the given String value into a mode.
-
.is?(mode, type) ⇒ Boolean
Checks if the given type is a mode.
- .singular?(mode) ⇒ Boolean
-
.to_rainbow(mode) ⇒ Rainbow
Creates a rainbow representation of the given mode.
-
.to_s(value) ⇒ String
Converts the given mode into a string.
Class Method Details
.from(value) ⇒ Integer
Converts from a given value into a mode. If the value is an array, the return value of the function is the result of converting each element into a mode and unioning the result. If the value is an array, MODES is used to convert the string. If the value is numeric, it is returned.
73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/carbon/compiler/metanostic/mode.rb', line 73 def self.from(value) if value.is_a?(::Array) from_array(value) elsif value.is_a?(::String) from_string(value) elsif value.is_a?(::Numeric) value else fail ArgumentError, "Unexpected value #{value.class}" end end |
.from_array(value) ⇒ Integer
Converts the given Array value into a mode. It does this by converting each element into a mode, and unioning the modes.
90 91 92 |
# File 'lib/carbon/compiler/metanostic/mode.rb', line 90 def self.from_array(value) value.inject(NONE) { |a, e| a | from(e) } end |
.from_string(value) ⇒ Integer
Converts the given String value into a mode. It does this by fetching the mode from the MODES constant.
100 101 102 |
# File 'lib/carbon/compiler/metanostic/mode.rb', line 100 def self.from_string(value) MODES.fetch(value.downcase) end |
.is?(mode, type) ⇒ Boolean
Checks if the given type is a mode. This is done by performing a bitwise and, and comparing the result against zero. The order of the arguments does not matter.
113 114 115 |
# File 'lib/carbon/compiler/metanostic/mode.rb', line 113 def self.is?(mode, type) (mode & type) != 0 end |
.singular?(mode) ⇒ Boolean
123 124 125 126 127 |
# File 'lib/carbon/compiler/metanostic/mode.rb', line 123 def self.singular?(mode) count = 0 ((count += mode & 1) && mode >>= 1) until mode == 0 count < 2 end |
.to_rainbow(mode) ⇒ Rainbow
Creates a rainbow representation of the given mode. The mode must be singular (see singular?).
150 151 152 153 154 155 156 157 158 |
# File 'lib/carbon/compiler/metanostic/mode.rb', line 150 def self.to_rainbow(mode) case mode when IGNORE then Rainbow("Ignore").color(:white).bright when INFO then Rainbow("Info").color(:blue).bright when WARNING then Rainbow("Warning").color(:yellow).bright when ERROR then Rainbow("Error").color(:red).bright when PANIC then Rainbow("Panic").color(:magenta).bright end end |