class JsRegex::Converter::SetConverter

Template class implementation.

Unlike other converters, this one does not recurse on subexpressions, since many are unsupported by JavaScript. If it detects incompatible children, it uses the `character_set` gem to establish the codepoints matched by the whole set and build a completely new set string.

Private Instance Methods

all_children_directly_compatible?() click to toggle source
# File lib/js_regex/converter/set_converter.rb, line 36
def all_children_directly_compatible?
  # note that #each_expression is recursive
  expression.each_expression.all? { |ch| child_directly_compatible?(ch) }
end
casefolding_needed?() click to toggle source
# File lib/js_regex/converter/set_converter.rb, line 56
def casefolding_needed?
  expression.case_insensitive? ^ context.case_insensitive_root
end
child_directly_compatible?(exp) click to toggle source
# File lib/js_regex/converter/set_converter.rb, line 41
def child_directly_compatible?(exp)
  case exp.type
  when :literal
    # surrogate pair substitution needed if astral
    exp.text.ord <= 0xFFFF
  when :set
    # conversion needed for nested sets, intersections
    exp.token.equal?(:range)
  when :type
    TypeConverter.directly_compatible?(exp)
  when :escape
    EscapeConverter::ESCAPES_SHARED_BY_RUBY_AND_JS.include?(exp.token)
  end
end
convert_data() click to toggle source
# File lib/js_regex/converter/set_converter.rb, line 19
def convert_data
  return pass_through_with_escaping if directly_compatible?

  content = CharacterSet.of_expression(expression)
  if expression.case_insensitive? && !context.case_insensitive_root
    content = content.case_insensitive
  elsif !expression.case_insensitive? && context.case_insensitive_root
    warn_of_unsupported_feature('nested case-sensitive set')
  end

  content.to_s_with_surrogate_ranges
end
directly_compatible?() click to toggle source
# File lib/js_regex/converter/set_converter.rb, line 32
def directly_compatible?
  all_children_directly_compatible? && !casefolding_needed?
end
pass_through_with_escaping() click to toggle source
# File lib/js_regex/converter/set_converter.rb, line 60
def pass_through_with_escaping
  string = expression.to_s(:base)
  LiteralConverter.escape_incompatible_bmp_literals(string)
end