|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +module Parser |
| 4 | + module Source |
| 5 | + class TreeRewriter |
| 6 | + ## |
| 7 | + # Base class for objects responsible to handle rewriting conflicts |
| 8 | + # At a minimum, `ignore?` must be defined. |
| 9 | + # Other methods may be overwritten. |
| 10 | + # |
| 11 | + class Enforcer |
| 12 | + # |
| 13 | + # @param [Symbol] one of :crossing_deletions, :different_replacements, :swallowed_insertions, |
| 14 | + # For future-proofing, `ignore?` must accept other events and return `true`. |
| 15 | + def ignore?(event) |
| 16 | + raise NotImplementedError |
| 17 | + end |
| 18 | + |
| 19 | + def on_crossing_insertions(range, conflict_range) |
| 20 | + on(:crossing_insertions, range, conflict: conflict_range) |
| 21 | + end |
| 22 | + |
| 23 | + def on_crossing_deletions(range, conflict_range) |
| 24 | + on(:crossing_deletions, range, conflict: conflict_range) |
| 25 | + end |
| 26 | + |
| 27 | + def on_swallowed_insertions(range, conflict_range) |
| 28 | + on(:swallowed_insertions, range, conflict: conflict_range) |
| 29 | + end |
| 30 | + |
| 31 | + def on_different_replacements(range, replacement, other_replacement) |
| 32 | + on(:different_replacements, range, replacement: replacement, other_replacement: other_replacement) |
| 33 | + end |
| 34 | + |
| 35 | + protected |
| 36 | + |
| 37 | + def on(_event, _range, **_args) |
| 38 | + reject |
| 39 | + end |
| 40 | + |
| 41 | + def reject |
| 42 | + raise Parser::ClobberingError, "Parser::Source::TreeRewriter detected clobbering" |
| 43 | + end |
| 44 | + |
| 45 | + class WithPolicy < Enforcer |
| 46 | + ACTIONS = %i[accept warn raise].freeze |
| 47 | + |
| 48 | + def initialize( |
| 49 | + crossing_deletions: :accept, |
| 50 | + different_replacements: :accept, |
| 51 | + swallowed_insertions: :accept |
| 52 | + ) |
| 53 | + @crossing_deletions = validate_policy(crossing_deletions ) |
| 54 | + @different_replacements = validate_policy(different_replacements) |
| 55 | + @swallowed_insertions = validate_policy(swallowed_insertions ) |
| 56 | + end |
| 57 | + |
| 58 | + def ignore?(event) |
| 59 | + policy(event) == :accept |
| 60 | + end |
| 61 | + |
| 62 | + def diagnostics |
| 63 | + @diagnostics ||= Diagnostic::Engine.new(-> diag { $stderr.puts diag.render }) |
| 64 | + end |
| 65 | + |
| 66 | + protected |
| 67 | + |
| 68 | + def validate_policy(action) |
| 69 | + raise ArgumentError, "Invalid policy value: #{action}" unless ACTIONS.include?(action) |
| 70 | + |
| 71 | + action |
| 72 | + end |
| 73 | + |
| 74 | + def policy(event) |
| 75 | + case event |
| 76 | + when :crossing_insertions then :raise |
| 77 | + when :crossing_deletions then @crossing_deletions |
| 78 | + when :different_replacements then @different_replacements |
| 79 | + when :swallowed_insertions then @swallowed_insertions |
| 80 | + else :accept # Example of future proofing |
| 81 | + end |
| 82 | + end |
| 83 | + |
| 84 | + POLICY_TO_LEVEL = {warn: :warning, raise: :error}.freeze |
| 85 | + def on(event, range, conflict: nil, **arguments) |
| 86 | + action = policy(event) |
| 87 | + severity = POLICY_TO_LEVEL.fetch(action) |
| 88 | + diag = Parser::Diagnostic.new(severity, event, arguments, range) |
| 89 | + diagnostics.process(diag) |
| 90 | + if conflict |
| 91 | + range, *highlights = conflict |
| 92 | + diag = Parser::Diagnostic.new(severity, :"#{event}_conflict", arguments, range, highlights) |
| 93 | + diagnostics.process(diag) |
| 94 | + end |
| 95 | + raise Parser::ClobberingError, "Parser::Source::TreeRewriter detected clobbering" if action == :raise |
| 96 | + end |
| 97 | + end |
| 98 | + |
| 99 | + DEFAULT = WithPolicy.new.freeze |
| 100 | + end |
| 101 | + end |
| 102 | + end |
| 103 | +end |
0 commit comments