Class: JsRegex::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/js_regex/node.rb

Overview

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

Constant Summary collapse

TYPES =
%i[
  backref_num
  captured_group
  conditional
  dropped
  plain
].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*children, reference: nil, type: :plain) ⇒ Node

Returns a new instance of Node.



19
20
21
22
23
# 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

Instance Attribute Details

#childrenObject

Returns the value of attribute children.



9
10
11
# File 'lib/js_regex/node.rb', line 9

def children
  @children
end

#quantifierObject

Returns the value of attribute quantifier.



9
10
11
# File 'lib/js_regex/node.rb', line 9

def quantifier
  @quantifier
end

#referenceObject

Returns the value of attribute reference.



9
10
11
# File 'lib/js_regex/node.rb', line 9

def reference
  @reference
end

#typeObject

Returns the value of attribute type.



9
10
11
# File 'lib/js_regex/node.rb', line 9

def type
  @type
end

Instance Method Details

#<<(node) ⇒ Object



34
35
36
37
# File 'lib/js_regex/node.rb', line 34

def <<(node)
  children << node
  self
end

#dropped?Boolean

Returns:

  • (Boolean)


39
40
41
42
43
# 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_copyObject



25
26
27
# File 'lib/js_regex/node.rb', line 25

def initialize_copy(*)
  self.children = children.map(&:clone)
end

#to_sObject



45
46
47
48
49
50
51
52
53
54
55
56
# 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) ⇒ Object



29
30
31
32
# File 'lib/js_regex/node.rb', line 29

def transform(&block)
  children.map!(&block)
  self
end

#update(attrs) ⇒ Object



58
59
60
61
62
# 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