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.

Returns:

  • (Integer)
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.

Returns:

  • (Integer)
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.

Returns:

  • (Integer)
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.

Returns:

  • (Integer)
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.

Returns:

  • (Integer)
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.

Returns:

  • (Integer)
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.

Returns:

  • (Integer)
PANIC | ERROR | WARNING | INFO | IGNORE
MODES =

An associative list of modes to convert from strings.

Returns:

  • ({String => Integer})
{
  "none" => NONE, "ignore" => IGNORE, "info" => INFO,
  "warning" => WARNING, "error" => ERROR, "panic" => PANIC,
  "all" => ALL
}.freeze

Class Method Summary collapse

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.

Parameters:

  • value (Array, String, Numeric)

Returns:

  • (Integer)

Raises:

  • (ArgumentError)

    If value is not an Array, a String, or a Numeric.



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.

Parameters:

  • value (<String, Numeric>)

Returns:

  • (Integer)


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.

Parameters:

  • value (String)

Returns:

  • (Integer)

Raises:

  • KeyError If value is not a valid mode.



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.

Parameters:

Returns:

  • (Boolean)


113
114
115
# File 'lib/carbon/compiler/metanostic/mode.rb', line 113

def self.is?(mode, type)
  (mode & type) != 0
end

.singular?(mode) ⇒ Boolean

Determines if a given mode is a singular mode (i.e. only one of IGNORE. INFO, WARNING, ERROR, or PANIC). It does this via doing a population count.

Parameters:

  • mode (Integer)

Returns:

  • (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?).

Parameters:

  • mode (Integer)

Returns:

  • (Rainbow)

See Also:



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

.to_s(value) ⇒ String

Converts the given mode into a string. If the value is ALL, it retuns ‘“All”`; if the value is NONE, it returns `“None”`; otherwise, it attempts to find all of the modes that are represented by the value.

Returns:

  • (String)


135
136
137
138
139
140
141
142
# File 'lib/carbon/compiler/metanostic/mode.rb', line 135

def self.to_s(value)
  return "All" if value == ALL
  return "None" if value == NONE
  MODES.select { |_, v| is?(value, v) && v != ALL }
       .map(&:first)
       .map(&:capitalize)
       .join(", ")
end