Class: Ibex::Runtime::SyntaxRepairEdit

Inherits:
Object
  • Object
show all
Defined in:
lib/json5/generated_parser.rb

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

Raises:

  • (ArgumentError)


5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
# File 'lib/json5/generated_parser.rb', line 5101

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

#costObject (readonly)

Signature:

  • Integer



5092
5093
5094
# File 'lib/json5/generated_parser.rb', line 5092

def cost
  @cost
end

#end_byteObject (readonly)

Signature:

  • Integer



5094
5095
5096
# File 'lib/json5/generated_parser.rb', line 5094

def end_byte
  @end_byte
end

#kindObject (readonly)

Signature:

  • Symbol



5088
5089
5090
# File 'lib/json5/generated_parser.rb', line 5088

def kind
  @kind
end

#original_textObject (readonly)

Signature:

  • String



5095
5096
5097
# File 'lib/json5/generated_parser.rb', line 5095

def original_text
  @original_text
end

#positionObject (readonly)

Signature:

  • Integer



5089
5090
5091
# File 'lib/json5/generated_parser.rb', line 5089

def position
  @position
end

#replacement_textObject (readonly)

Signature:

  • String?



5096
5097
5098
# File 'lib/json5/generated_parser.rb', line 5096

def replacement_text
  @replacement_text
end

#start_byteObject (readonly)

Signature:

  • Integer



5093
5094
5095
# File 'lib/json5/generated_parser.rb', line 5093

def start_byte
  @start_byte
end

#token_idObject (readonly)

Signature:

  • Integer



5090
5091
5092
# File 'lib/json5/generated_parser.rb', line 5090

def token_id
  @token_id
end

#token_nameObject (readonly)

Signature:

  • String



5091
5092
5093
# File 'lib/json5/generated_parser.rb', line 5091

def token_name
  @token_name
end

Instance Method Details

#to_hObject

RBS:

  • () -> syntax_edit_document



5144
5145
5146
5147
5148
5149
5150
5151
# File 'lib/json5/generated_parser.rb', line 5144

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