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.text[0..-2])
    return wrap_in_backrefed_lookahead(node)
  else
    node.update(quantifier: qtf)
  end

  node
end
convert_expression(expression) click to toggle source
# File lib/js_regex/converter/base.rb, line 48
def convert_expression(expression)
  Converter.convert(expression, context)
end
convert_subexpressions() click to toggle source
# File lib/js_regex/converter/base.rb, line 44
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 63
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 59
def warn_of(text)
  context.warnings << text
end
warn_of_unsupported_feature(description = nil) click to toggle source
# File lib/js_regex/converter/base.rb, line 52
def warn_of_unsupported_feature(description = nil)
  description ||= "#{subtype} #{expression.type}".tr('_', ' ')
  full_desc = "#{description} '#{expression}'"
  warn_of("Dropped unsupported #{full_desc} at index #{expression.ts}")
  drop
end
wrap_in_backrefed_lookahead(content) click to toggle source
# File lib/js_regex/converter/base.rb, line 68
def wrap_in_backrefed_lookahead(content)
  backref_num = context.capturing_group_count + 1
  backref_num_node = Node.new(backref_num.to_s, type: :backref_num)
  context.increment_local_capturing_group_count
  # an empty passive group (?:) is appended as literal digits may follow
  Node.new('(?=(', *content, '))\\', backref_num_node, '(?:)')
end