Class: Ibex::Codegen::ActionLocations

Inherits:
Object
  • Object
show all
Defined in:
lib/ibex/codegen/action_locations.rb

Overview

Rewrites semantic-location expressions without touching Ruby literals, comments, heredoc bodies, or ordinary instance/class variables.

Instance Method Summary collapse

Constructor Details

#initialize(source, maximum:, location:) ⇒ ActionLocations

Returns a new instance of ActionLocations.



17
18
19
20
21
# File 'lib/ibex/codegen/action_locations.rb', line 17

def initialize(source, maximum:, location:)
  @source = source
  @maximum = maximum
  @location = location
end

Instance Method Details

#references?Boolean

Returns:

  • (Boolean)


36
37
38
39
40
41
42
43
44
45
# File 'lib/ibex/codegen/action_locations.rb', line 36

def references?
  return true if @source.match?(/@(?:\$|\d+)/) && !semantic_references.empty?
  return false unless @source.match?(/\b(?:loc|result_loc)\b/)

  require "ripper"
  tokens = Object.const_get(:Ripper).__send__(:lex, lexable_source)
  tokens.any? do |_position, type, token, _state|
    type == :on_ident && SEMANTIC_HELPERS.include?(token)
  end
end

#rewriteObject



24
25
26
27
28
29
30
31
32
33
# File 'lib/ibex/codegen/action_locations.rb', line 24

def rewrite
  rewritten = mutable_binary_source_copy
  return rewritten.force_encoding(@source.encoding) unless @source.match?(/@(?:\$|\d+)/)

  replacements = semantic_references.map do |offset, spelling|
    [offset, spelling.bytesize, replacement_for(spelling)]
  end #: Array[[Integer, Integer, String]]
  replacements.reverse_each { |offset, length, replacement| rewritten[offset, length] = replacement }
  rewritten.force_encoding(@source.encoding)
end