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

Attributes

children[RW]
quantifier[RW]
reference[RW]
type[RW]

Public Class Methods

new(*children, reference: nil, type: :plain) click to toggle source
# File lib/js_regex/node.rb, line 19
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 34
def <<(node)
  children << node
  self
end
dropped?() click to toggle source
# File lib/js_regex/node.rb, line 39
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 25
def initialize_copy(*)
  self.children = children.map(&:clone)
end
to_s() click to toggle source
# File lib/js_regex/node.rb, line 45
def to_s
  case type
  when :dropped
    ''
  when :backref_num, :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 29
def transform(&block)
  children.map!(&block)
  self
end
update(attrs) click to toggle source
# File lib/js_regex/node.rb, line 58
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)
end