Class: SyntaxTree::RangeNode

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

Overview

RangeNode represents using the .. or the … operator between two expressions. Usually this is to create a range object.

1..2

Sometimes this operator is used to create a flip-flop.

if value == 5 .. value == 10
end

One of the sides of the expression may be nil, but not both.

Instance Attribute Summary collapse

Attributes inherited from Node

#location

Instance Method Summary collapse

Methods inherited from Node

#construct_keys, #end_char, #pretty_print, #start_char, #to_json, #to_mermaid

Constructor Details

#initialize(left:, operator:, right:, location:) ⇒ RangeNode

Returns a new instance of RangeNode.



4501
4502
4503
4504
4505
4506
4507
# File 'lib/syntax_tree/node.rb', line 4501

def initialize(left:, operator:, right:, location:)
  @left = left
  @operator = operator
  @right = right
  @location = location
  @comments = []
end

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



4499
4500
4501
# File 'lib/syntax_tree/node.rb', line 4499

def comments
  @comments
end

#leftObject (readonly)

nil | Node

the left side of the expression



4490
4491
4492
# File 'lib/syntax_tree/node.rb', line 4490

def left
  @left
end

#operatorObject (readonly)

Op

the operator used for this range



4493
4494
4495
# File 'lib/syntax_tree/node.rb', line 4493

def operator
  @operator
end

#rightObject (readonly)

nil | Node

the right side of the expression



4496
4497
4498
# File 'lib/syntax_tree/node.rb', line 4496

def right
  @right
end

Instance Method Details

#===(other) ⇒ Object



4555
4556
4557
4558
# File 'lib/syntax_tree/node.rb', line 4555

def ===(other)
  other.is_a?(RangeNode) && left === other.left &&
    operator === other.operator && right === other.right
end

#accept(visitor) ⇒ Object



4509
4510
4511
# File 'lib/syntax_tree/node.rb', line 4509

def accept(visitor)
  visitor.visit_range(self)
end

#child_nodesObject Also known as: deconstruct



4513
4514
4515
# File 'lib/syntax_tree/node.rb', line 4513

def child_nodes
  [left, right]
end

#copy(left: nil, operator: nil, right: nil, location: nil) ⇒ Object



4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
# File 'lib/syntax_tree/node.rb', line 4517

def copy(left: nil, operator: nil, right: nil, location: nil)
  node =
    RangeNode.new(
      left: left || self.left,
      operator: operator || self.operator,
      right: right || self.right,
      location: location || self.location
    )

  node.comments.concat(comments.map(&:copy))
  node
end

#deconstruct_keys(_keys) ⇒ Object



4532
4533
4534
4535
4536
4537
4538
4539
4540
# File 'lib/syntax_tree/node.rb', line 4532

def deconstruct_keys(_keys)
  {
    left: left,
    operator: operator,
    right: right,
    location: location,
    comments: comments
  }
end

#format(q) ⇒ Object



4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
# File 'lib/syntax_tree/node.rb', line 4542

def format(q)
  q.format(left) if left

  case q.parent
  when IfNode, UnlessNode
    q.text(" #{operator.value} ")
  else
    q.text(operator.value)
  end

  q.format(right) if right
end