Class: Carbon::Compiler::Metanostic::List

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable
Defined in:
lib/carbon/compiler/metanostic/list.rb

Overview

A list of diagnostics. This handles setting up the modes of new, emitted diagnostics, and reporting diagnostic types.

Constant Summary collapse

THRESHOLDS =
{
  "warnings" => 100,
  "errors" => 20
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeList

Initialize the list.



36
37
38
39
40
41
# File 'lib/carbon/compiler/metanostic/list.rb', line 36

def initialize
  @defaults = Metanostic::Defaults.defaults
  @state = State.new
  @list = Concurrent::Array.new
  @files = Concurrent::Hash.new
end

Instance Attribute Details

#defaults{::Hash => Metanostic} (readonly)

The default hash of metanostics.

Returns:

See Also:



21
22
23
# File 'lib/carbon/compiler/metanostic/list.rb', line 21

def defaults
  @defaults
end

#filesObject (readonly)

The project files for the metanostic list. This is to provide context for diagnostic errors.



26
27
28
# File 'lib/carbon/compiler/metanostic/list.rb', line 26

def files
  @files
end

#stateState (readonly)

The current state. This contains information about what modes each diagnostic should inherit.

Returns:



16
17
18
# File 'lib/carbon/compiler/metanostic/list.rb', line 16

def state
  @state
end

Instance Method Details

#emit(name, location = Location.default, format = []) ⇒ Object Also known as: <<, push

Emits a diagnostic to the list. It sets up the data to pass to the diagnostic. If the diagnostic cannot be found, it instead emits a ‘System/Error` diagnostic.



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/carbon/compiler/metanostic/list.rb', line 67

def emit(name, location = Location.default, format = [])
  mode, metanostic = @state.fetch(name) do
    return emit("System/Error", location, [name])
  end

  message = format.is_a?(::String) ? format :
    sprintf(metanostic.message, *format)

  stack = caller[2..7]
  diagnostic = Diagnostic.new(meta: metanostic, location: location,
    message: message, mode: mode, list: self, stack: stack)
  @list << diagnostic
  diagnostic.output($stderr)
  diagnostic_check
end

#errors<Diagnostic>

Returns a list of diagnostics that have the Mode::ERROR mode.

Returns:



53
54
55
# File 'lib/carbon/compiler/metanostic/list.rb', line 53

def errors
  select { |d| d.metanostic.default == Mode::ERROR }
end

#output(io) ⇒ void

This method returns an undefined value.

Outputs all of the diagnostics in this list. Does nothing if Carbon.quiet? is true.

Parameters:

  • io (#<<)

    The IO device to output to.

See Also:



92
93
94
95
# File 'lib/carbon/compiler/metanostic/list.rb', line 92

def output(io)
  return if Carbon.quiet?
  each { |i| i.output(io, @files) }
end

#panics<Diagnostic>

Returns a list of diagnostics that have the Mode::PANIC mode.

Returns:



46
47
48
# File 'lib/carbon/compiler/metanostic/list.rb', line 46

def panics
  select { |d| d.metanostic.default == Mode::PANIC }
end

#warnings<Diagnostic>

Returns a list of diagnostics that have the Mode::WARNING mode.

Returns:



60
61
62
# File 'lib/carbon/compiler/metanostic/list.rb', line 60

def warnings
  select { |d| d.metanostic.default == Mode::WARNING }
end