Class: Ibex::Runtime::CST::SyntaxEditor
- Inherits:
-
Object
- Object
- Ibex::Runtime::CST::SyntaxEditor
- Defined in:
- lib/json5/generated_parser.rb
Overview
Applies multiple occurrence-addressed replacements in one path-copying pass.
Instance Method Summary collapse
- #apply ⇒ Object
-
#initialize(root) ⇒ SyntaxEditor
constructor
A new instance of SyntaxEditor.
- #replace(target, replacement) ⇒ Object
Constructor Details
#initialize(root) ⇒ SyntaxEditor
Returns a new instance of SyntaxEditor.
1959 1960 1961 1962 |
# File 'lib/json5/generated_parser.rb', line 1959 def initialize(root) @root = root @edits = [] end |
Instance Method Details
#apply ⇒ Object
1984 1985 1986 1987 1988 1989 1990 |
# File 'lib/json5/generated_parser.rb', line 1984 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
1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 |
# File 'lib/json5/generated_parser.rb', line 1965 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 |