Class: JsRegex::Node
- Inherits:
-
Object
- Object
- JsRegex::Node
- 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
-
#children ⇒ Object
readonly
Returns the value of attribute children.
-
#quantifier ⇒ Object
readonly
Returns the value of attribute quantifier.
-
#reference ⇒ Object
readonly
Returns the value of attribute reference.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Instance Method Summary collapse
- #<<(node) ⇒ Object
- #dropped? ⇒ Boolean
-
#initialize(*children, reference: nil, type: :plain) ⇒ Node
constructor
A new instance of Node.
- #initialize_copy ⇒ Object
- #to_s ⇒ Object
- #transform(&block) ⇒ Object
- #update(attrs) ⇒ Object
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
#children ⇒ Object
Returns the value of attribute children.
9 10 11 |
# File 'lib/js_regex/node.rb', line 9 def children @children end |
#quantifier ⇒ Object
Returns the value of attribute quantifier.
9 10 11 |
# File 'lib/js_regex/node.rb', line 9 def quantifier @quantifier end |
#reference ⇒ Object
Returns the value of attribute reference.
9 10 11 |
# File 'lib/js_regex/node.rb', line 9 def reference @reference end |
#type ⇒ Object
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
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_copy ⇒ Object
26 27 28 |
# File 'lib/js_regex/node.rb', line 26 def initialize_copy(*) self.children = children.map(&:clone) end |
#to_s ⇒ Object
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 |