class JsRegex::Converter::Base

Template class. Implement convert_data in subclasses and return instance of String or Node from it.

Attributes

context[RW]
expression[RW]

Public Instance Methods

convert(expression, context) click to toggle source

returns instance of Node with quantifier attached.

# File lib/js_regex/converter/base.rb, line 9
def convert(expression, context)
  self.context    = context
  self.expression = expression

  node = convert_data
  node = Node.new(node) if node.instance_of?(String)
  apply_quantifier(node)
end

Private Instance Methods

apply_quantifier(node) click to toggle source
# File lib/js_regex/converter/base.rb, line 31
def apply_quantifier(node)
  return node if node.dropped? || (qtf = expression.quantifier).nil?

  if qtf.possessive?
    node.update(quantifier: qtf.dup.tap { |q| q.text = q.text[0..-2] })
    return wrap_in_backrefed_lookahead(node)
  elsif qtf.token == :interval && qtf.text[0..1] == "{,"
    node.update(quantifier: qtf.dup.tap { |q| q.text = "{0,#{q.max}}" })
  else
    node.update(quantifier: qtf)
  end

  node
end
convert_expression(expression) click to toggle source
# File lib/js_regex/converter/base.rb, line 50
def convert_expression(expression)
  Converter.convert(expression, context)
end
convert_subexpressions() click to toggle source
# File lib/js_regex/converter/base.rb, line 46
def convert_subexpressions
  Node.new(*expression.map { |subexp| convert_expression(subexp) })
end
data() click to toggle source
# File lib/js_regex/converter/base.rb, line 26
def data
  expression.text
end
Also aliased as: pass_through
drop() click to toggle source
# File lib/js_regex/converter/base.rb, line 73
def drop
  Node.new(type: :dropped)
end
Also aliased as: drop_without_warning
drop_without_warning()
Alias for: drop
pass_through()
Alias for: data
subtype() click to toggle source
# File lib/js_regex/converter/base.rb, line 22
def subtype
  expression.token
end
warn_of(text) click to toggle source
# File lib/js_regex/converter/base.rb, line 65
def warn_of(text)
  if context.fail_fast
    raise ConversionError, text.sub(/^Dropped /, '')
  else
    context.warnings << text
  end
end
warn_of_unsupported_feature(description = nil, min_target: nil) click to toggle source
# File lib/js_regex/converter/base.rb, line 54
def warn_of_unsupported_feature(description = nil, min_target: nil)
  description ||= "#{subtype} #{expression.type}".tr('_', ' ')
  full_text = "Dropped unsupported #{description} '#{expression}' "\
              "at index #{expression.ts}"
  if min_target
    full_text += " (requires at least `target: '#{min_target}'`)"
  end
  warn_of(full_text)
  drop
end
wrap_in_backrefed_lookahead(content) click to toggle source
# File lib/js_regex/converter/base.rb, line 78
def wrap_in_backrefed_lookahead(content)
  number = context.capturing_group_count + 1
  backref_node = Node.new("\\#{number}", reference: number, type: :backref)
  context.increment_local_capturing_group_count
  # an empty passive group (?:) is appended as literal digits may follow
  Node.new('(?=(', *content, '))', backref_node, '(?:)')
end