Class: Ibex::Frontend::Diagnostic

Inherits:
Object
  • Object
show all
Defined in:
lib/ibex/frontend/diagnostic.rb,
sig/ibex/frontend/diagnostic.rbs

Overview

An immutable machine-readable frontend error.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(code:, phase:, message:, location:, span: nil, expected: [], received: nil, rendered: nil) ⇒ Diagnostic

Returns a new instance of Diagnostic.

RBS:

  • (code: String, phase: Symbol, message: String, location: Location, ?span: SourceSpan?, ?expected: Array[String], ?received: String?, ?rendered: String?) -> void

Parameters:

  • code: (String)
  • phase: (Symbol)
  • message: (String)
  • location: (Location)
  • span: (SourceSpan, nil) (defaults to: nil)
  • expected: (Array[String]) (defaults to: [])
  • received: (String, nil) (defaults to: nil)
  • rendered: (String, nil) (defaults to: nil)


17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/ibex/frontend/diagnostic.rb', line 17

def initialize(code:, phase:, message:, location:, span: nil, expected: [], received: nil, rendered: nil)
  @code = code.dup.freeze
  @phase = phase
  @message = message.dup.freeze
  @location = Location.new(
    file: location.file.dup.freeze, line: location.line, column: location.column
  ).freeze
  @span = span
  @expected = expected.map { |value| value.dup.freeze }.freeze
  @received = received&.dup&.freeze
  @rendered = (rendered || "#{location}: #{message}").dup.freeze
  freeze
end

Instance Attribute Details

#codeString (readonly)

Signature:

  • String

Returns:

  • (String)


7
8
9
# File 'lib/ibex/frontend/diagnostic.rb', line 7

def code
  @code
end

#expectedArray[String] (readonly)

Signature:

  • Array[String]

Returns:

  • (Array[String])


12
13
14
# File 'lib/ibex/frontend/diagnostic.rb', line 12

def expected
  @expected
end

#locationLocation (readonly)

Signature:

  • Location

Returns:



10
11
12
# File 'lib/ibex/frontend/diagnostic.rb', line 10

def location
  @location
end

#messageString (readonly)

Signature:

  • String

Returns:

  • (String)


9
10
11
# File 'lib/ibex/frontend/diagnostic.rb', line 9

def message
  @message
end

#phaseSymbol (readonly)

Signature:

  • Symbol

Returns:

  • (Symbol)


8
9
10
# File 'lib/ibex/frontend/diagnostic.rb', line 8

def phase
  @phase
end

#receivedString? (readonly)

Signature:

  • String?

Returns:

  • (String, nil)


13
14
15
# File 'lib/ibex/frontend/diagnostic.rb', line 13

def received
  @received
end

#spanSourceSpan? (readonly)

Signature:

  • SourceSpan?

Returns:



11
12
13
# File 'lib/ibex/frontend/diagnostic.rb', line 11

def span
  @span
end

Instance Method Details

#severityString

RBS:

  • () -> String

Returns:

  • (String)


32
33
34
# File 'lib/ibex/frontend/diagnostic.rb', line 32

def severity
  "error"
end

#to_hHash[Symbol, untyped]

RBS:

  • () -> Hash[Symbol, untyped]

Returns:

  • (Hash[Symbol, untyped])


42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/ibex/frontend/diagnostic.rb', line 42

def to_h
  {
    code: code,
    severity: severity,
    phase: phase.to_s,
    message: message,
    location: location.to_h,
    span: span&.to_h,
    expected: expected,
    received: received
  }
end

#to_sString

RBS:

  • () -> String

Returns:

  • (String)


37
38
39
# File 'lib/ibex/frontend/diagnostic.rb', line 37

def to_s
  @rendered
end