Class: Ibex::Runtime::CST::SyntaxEditor

Inherits:
Object
  • Object
show all
Defined in:
lib/ibex/runtime/cst/editor.rb

Overview

Applies multiple occurrence-addressed replacements in one path-copying pass.

Instance Method Summary collapse

Constructor Details

#initialize(root) ⇒ SyntaxEditor

Returns a new instance of SyntaxEditor.



21
22
23
24
# File 'lib/ibex/runtime/cst/editor.rb', line 21

def initialize(root)
  @root = root
  @edits = []
end

Instance Method Details

#applyObject

Raises:

  • (TypeError)


46
47
48
49
50
51
52
# File 'lib/ibex/runtime/cst/editor.rb', line 46

def apply
  green = apply_element(@root)
  return @root if green.equal?(@root.green)
  raise TypeError, "a syntax root must remain a GreenNode" unless green.is_a?(GreenNode)

  Editing.red_root(@root, green)
end

#replace(target, replacement) ⇒ Object

Raises:

  • (ArgumentError)


27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/ibex/runtime/cst/editor.rb', line 27

def replace(target, replacement)
  raise ArgumentError, "edit target belongs to a different root" unless target.root.equal?(@root)

  green = Editing.green_element(replacement)
  duplicate = @edits.find { |existing, _value| same_occurrence?(existing, target) }
  if duplicate
    raise EditConflictError, "the same syntax occurrence has conflicting replacements" unless
      duplicate.fetch(1).equal?(green)

    return self
  end
  return self if @edits.any? { |existing, _value| ancestor?(existing, target) }

  @edits.reject! { |existing, _value| ancestor?(target, existing) }
  @edits << [target, green]
  self
end