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
  captured_group
  conditional
  dropped
  keep_mark
  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.



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

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



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

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

#dropped?Boolean

Returns:

  • (Boolean)


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



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

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

#to_sObject



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



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

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

#update(attrs) ⇒ Object



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