class JsRegex::Node

Converter#convert result. Represents a branch or leaf node with an optional quantifier as well as type and reference annotations for SecondPass.

Constants

TYPES
TypeError

Attributes

children[R]
quantifier[R]
reference[R]
type[R]

Public Class Methods

new(*children, reference: nil, type: :plain) click to toggle source
# File lib/js_regex/node.rb, line 20
def initialize(*children, reference: nil, type: :plain)
  self.children = children
  self.reference = reference
  self.type = type
end

Public Instance Methods

<<(node) click to toggle source
# File lib/js_regex/node.rb, line 35
def <<(node)
  children << node
  self
end
dropped?() click to toggle source
# File lib/js_regex/node.rb, line 40
def dropped?
  # keep everything else, including empty or depleted capturing groups
  # so as not to not mess with reference numbers (e.g. backrefs)
  type.equal?(:dropped)
end
initialize_copy(*) click to toggle source
# File lib/js_regex/node.rb, line 26
def initialize_copy(*)
  self.children = children.map(&:clone)
end
to_s() click to toggle source
# File lib/js_regex/node.rb, line 46
def to_s
  case type
  when :dropped
    ''
  when :backref, :captured_group, :plain
    children.join << quantifier.to_s
  else
    raise TypeError.new(
      "#{type} must be substituted before stringification"
    ).extend(JsRegex::Error)
  end
end
transform(&block) click to toggle source
# File lib/js_regex/node.rb, line 30
def transform(&block)
  children.map!(&block)
  self
end
update(attrs) click to toggle source
# File lib/js_regex/node.rb, line 59
def update(attrs)
  self.children   = attrs.fetch(:children)   if attrs.key?(:children)
  self.quantifier = attrs.fetch(:quantifier) if attrs.key?(:quantifier)
  self.type       = attrs.fetch(:type)       if attrs.key?(:type)
  self
end

Private Instance Methods

children=(arg) click to toggle source
# File lib/js_regex/node.rb, line 76
def children=(arg)
  arg.class == Array ||
    raise(TypeError, "unsupported type #{arg.class} for #{__method__}")
  @children = arg
end
quantifier=(arg) click to toggle source
# File lib/js_regex/node.rb, line 82
def quantifier=(arg)
  arg.nil? || arg.class == Regexp::Expression::Quantifier ||
    raise(TypeError, "unsupported type #{arg.class} for #{__method__}")
  @quantifier = arg
end
reference=(arg) click to toggle source
# File lib/js_regex/node.rb, line 88
def reference=(arg)
  arg.nil? || arg.is_a?(Numeric) ||
    raise(TypeError, "unsupported type #{arg.class} for #{__method__}")
  @reference = arg
end
type=(arg) click to toggle source
# File lib/js_regex/node.rb, line 70
def type=(arg)
  arg.nil? || TYPES.include?(arg) ||
    raise(TypeError, "unsupported type #{arg.class} for #{__method__}")
  @type = arg
end