class JsRegex::Converter::BackreferenceConverter

Template class implementation.

Private Instance Methods

convert_call() click to toggle source
# File lib/js_regex/converter/backreference_converter.rb, line 43
def convert_call
  if context.recursions(expression) >= 5
    warn_of("Recursion for '#{expression}' curtailed at 5 levels")
    return ''
  end

  context.count_recursion(expression)
  context.increment_local_capturing_group_count
  target_copy = expression.referenced_expression.unquantified_clone
  # avoid "Duplicate capture group name" error in JS
  target_copy.token = :capture if target_copy.is?(:named, :group)
  result = convert_expression(target_copy)
  # wrap in group if it is a full-pattern recursion
  expression.reference == 0 ? Node.new('(?:', result, ')') : result
end
convert_data() click to toggle source
# File lib/js_regex/converter/backreference_converter.rb, line 11
def convert_data
  case subtype
  when :name_ref then convert_name_ref
  when :number, :number_ref, :number_rel_ref then convert_to_plain_num_ref
  when :name_call, :number_call, :number_rel_call then convert_call
  else # name_recursion_ref, number_recursion_ref, ...
    warn_of_unsupported_feature
  end
end
convert_name_ref() click to toggle source
# File lib/js_regex/converter/backreference_converter.rb, line 21
def convert_name_ref
  if context.es_2018_or_higher?
    # ES 2018+ supports named backrefs, but only the angled-bracket syntax
    Node.new("\\k<#{expression.name}>", reference: new_position, type: :backref)
  else
    convert_to_plain_num_ref
  end
end
convert_to_plain_num_ref() click to toggle source
# File lib/js_regex/converter/backreference_converter.rb, line 30
def convert_to_plain_num_ref
  position = new_position
  Node.new("\\#{position}", reference: position, type: :backref)
end
new_position() click to toggle source
# File lib/js_regex/converter/backreference_converter.rb, line 35
def new_position
  context.new_capturing_group_position(target_position)
end
target_position() click to toggle source
# File lib/js_regex/converter/backreference_converter.rb, line 39
def target_position
  expression.referenced_expression.number
end