Class: Ibex::Runtime::SyntaxRepairEdit

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

Overview

One syntax-only projection of a runtime repair edit. It intentionally carries source bytes and token identity, never an application value.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(kind:, position:, token_id:, token_name:, cost:, start_byte:, end_byte:, original_text:, replacement_text:) ⇒ SyntaxRepairEdit

rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity

RBS:

  • (kind: Symbol, position: Integer, token_id: Integer, token_name: String, cost: Integer, start_byte: Integer, end_byte: Integer, original_text: String, replacement_text: String?) -> void

Parameters:

  • kind: (Symbol)
  • position: (Integer)
  • token_id: (Integer)
  • token_name: (String)
  • cost: (Integer)
  • start_byte: (Integer)
  • end_byte: (Integer)
  • original_text: (String)
  • replacement_text: (String, nil)


35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/ibex/runtime/syntax_repair.rb', line 35

def initialize(kind:, position:, token_id:, token_name:, cost:, start_byte:, end_byte:, original_text:,
               replacement_text:)
  raise ArgumentError, "unknown syntax repair edit #{kind.inspect}" unless RepairEdit::KINDS.include?(kind)
  unless position.is_a?(Integer) && position >= 0
    raise ArgumentError, "syntax repair edit position must be nonnegative"
  end
  unless token_id.is_a?(Integer) && token_id >= 0
    raise ArgumentError, "syntax repair token id must be nonnegative"
  end
  unless token_name.is_a?(String) && !token_name.empty?
    raise ArgumentError, "syntax repair token name must be a nonempty String"
  end
  raise ArgumentError, "syntax repair edit cost must be positive" unless cost.is_a?(Integer) && cost.positive?
  unless start_byte.is_a?(Integer) && end_byte.is_a?(Integer) && start_byte >= 0 && end_byte >= start_byte
    raise ArgumentError, "syntax repair byte range is invalid"
  end
  unless original_text.is_a?(String) && (replacement_text.nil? || replacement_text.is_a?(String))
    raise ArgumentError, "syntax repair edit text must be String values"
  end
  if kind == :insert && (start_byte != end_byte || !original_text.empty?)
    raise ArgumentError, "syntax insertion must have an empty source range"
  end
  if kind == :delete && replacement_text != ""
    raise ArgumentError, "syntax deletion must have an empty replacement"
  end
  unless original_text.bytesize == end_byte - start_byte
    raise ArgumentError, "syntax repair original text does not match its byte range"
  end

  @kind = kind
  @position = position
  @token_id = token_id
  @token_name = token_name.dup.freeze
  @cost = cost
  @start_byte = start_byte
  @end_byte = end_byte
  @original_text = original_text.b.freeze
  @replacement_text = replacement_text&.b&.freeze
  freeze
end

Instance Attribute Details

#costInteger (readonly)

Signature:

  • Integer

Returns:

  • (Integer)


26
27
28
# File 'lib/ibex/runtime/syntax_repair.rb', line 26

def cost
  @cost
end

#end_byteInteger (readonly)

Signature:

  • Integer

Returns:

  • (Integer)


28
29
30
# File 'lib/ibex/runtime/syntax_repair.rb', line 28

def end_byte
  @end_byte
end

#kindSymbol (readonly)

Signature:

  • Symbol

Returns:

  • (Symbol)


22
23
24
# File 'lib/ibex/runtime/syntax_repair.rb', line 22

def kind
  @kind
end

#original_textString (readonly)

Signature:

  • String

Returns:

  • (String)


29
30
31
# File 'lib/ibex/runtime/syntax_repair.rb', line 29

def original_text
  @original_text
end

#positionInteger (readonly)

Signature:

  • Integer

Returns:

  • (Integer)


23
24
25
# File 'lib/ibex/runtime/syntax_repair.rb', line 23

def position
  @position
end

#replacement_textString? (readonly)

Signature:

  • String?

Returns:

  • (String, nil)


30
31
32
# File 'lib/ibex/runtime/syntax_repair.rb', line 30

def replacement_text
  @replacement_text
end

#start_byteInteger (readonly)

Signature:

  • Integer

Returns:

  • (Integer)


27
28
29
# File 'lib/ibex/runtime/syntax_repair.rb', line 27

def start_byte
  @start_byte
end

#token_idInteger (readonly)

Signature:

  • Integer

Returns:

  • (Integer)


24
25
26
# File 'lib/ibex/runtime/syntax_repair.rb', line 24

def token_id
  @token_id
end

#token_nameString (readonly)

Signature:

  • String

Returns:

  • (String)


25
26
27
# File 'lib/ibex/runtime/syntax_repair.rb', line 25

def token_name
  @token_name
end

Instance Method Details

#to_hsyntax_edit_document

RBS:

  • () -> syntax_edit_document

Returns:

  • (syntax_edit_document)


78
79
80
81
82
83
84
85
# File 'lib/ibex/runtime/syntax_repair.rb', line 78

def to_h
  value = {
    kind: @kind, position: @position, token_id: @token_id, token_name: @token_name, cost: @cost,
    start_byte: @start_byte, end_byte: @end_byte, original_text: @original_text,
    replacement_text: @replacement_text
  } # @type var value: syntax_edit_document
  value.freeze
end