Class: Carbon::Compiler::Metanostic

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/carbon/compiler/metanostic.rb,
lib/carbon/compiler/metanostic/list.rb,
lib/carbon/compiler/metanostic/mode.rb,
lib/carbon/compiler/metanostic/state.rb,
lib/carbon/compiler/metanostic/defaults.rb,
lib/carbon/compiler/metanostic/diagnostic.rb

Overview

A “Metanostic.” A “metanostic” has information about what a diagnostic could be about. For example, a ‘Diagnostic/Unknown` diagnostic has metanostic information; the name itself, the default mode, and the allowed modes are a part of that metanostic information. Diagnostics are generated from both metanostic information and environmental information.

Defined Under Namespace

Modules: Defaults, Mode Classes: Diagnostic, List, State

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ Metanostic

Initialize the diagnostic with the given data.

Parameters:

  • data (::Hash{::Symbol,::String => ::Object})

Options Hash (data):

  • :name (::String)

    The name of the metanostic.

  • :allowed (::Integer)

    The allowed modes of derived diagnostics.

  • :default (::Integer)

    The default mode of derived diagnostics.

  • :message (::String)

    The default message of derived diagnostics.

Raises:

  • (::KeyError)

    If one of the option keys is not present.



59
60
61
62
63
64
# File 'lib/carbon/compiler/metanostic.rb', line 59

def initialize(data)
  @name        = data.fetch(:name) { data.fetch("name") }
  @allowed     = data.fetch(:allowed) { data.fetch("allowed") }
  self.default = data.fetch(:default) { data.fetch("default") }
  @message     = data.fetch(:message) { data.fetch("message") }
end

Instance Attribute Details

#allowed::Integer (readonly)

The allowed modes of a metanostic or diagnostic. This limits how a diagnostic can be categorized. This is a bitfield of modes. If a metanostic can be any mode, this is ‘Metanostic::Mode::ALL`; otherwise, it is a union of all of the allowed modes of a diagnostic.

Returns:

  • (::Integer)

See Also:



34
35
36
# File 'lib/carbon/compiler/metanostic.rb', line 34

def allowed
  @allowed
end

#default::Integer

The default mode of derived diagnostics. This is and must be an allowed diagnostic.

Returns:

  • (::Integer)

See Also:



41
42
43
# File 'lib/carbon/compiler/metanostic.rb', line 41

def default
  @default
end

#message::String (readonly)

The default message of derived diagnostics, if the diagnostic does not provide one. This is used to provide a generic message.

Returns:

  • (::String)


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

def message
  @message
end

#name::String (readonly)

The name of the metanostic. This is used as a unique identifier for the metanostic. This is a string, and is normally represented as a path of CamelCase names. For example, a name might be ‘Diagnostic/Unknown`.

Returns:

  • (::String)


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

def name
  @name
end

Instance Method Details

#<=>(other) ⇒ ::Numeric

Compares this with another metanostic. This should only be used for equality; it makes no sense otherwise.

Parameters:

  • other (Metanostic)

    The metanostic to compare to.

Returns:

  • (::Numeric)


71
72
73
74
# File 'lib/carbon/compiler/metanostic.rb', line 71

def <=>(other)
  fail ArgumentError, "Expected Metanostic" unless other.is_a?(Metanostic)
  to_a <=> other.to_a
end

#can_be?(mode) ⇒ Boolean

Checks to see if the metanostic or any derived diagnostics can have the given mode.

Parameters:

  • mode (Integer)

    The mode.

Returns:

  • (Boolean)

See Also:



118
119
120
# File 'lib/carbon/compiler/metanostic.rb', line 118

def can_be?(mode)
  Mode.singular?(mode) && Mode.is?(allowed, mode)
end

#hash::Numeric

Returns a numeric representation of this class for use of hashing.

Returns:

  • (::Numeric)


79
80
81
# File 'lib/carbon/compiler/metanostic.rb', line 79

def hash
  to_a.hash
end

#inspect::String

Pretty inspect.

Returns:

  • (::String)


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

def inspect
  "#<#{self.class} #{@name}(#{Mode.to_s(@default)}) " \
    "[#{Mode.to_s(@allowed)}]>"
end

#to_a(::Class, ::String, ::Integer, ::Integer, ::String)

Returns an array representation of this class. This is frozen.

Returns:

  • ((::Class, ::String, ::Integer, ::Integer, ::String))


86
87
88
# File 'lib/carbon/compiler/metanostic.rb', line 86

def to_a
  [self.class, @name, @allowed, @default, @message].freeze
end