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, #pretty_print, #to_json

Constructor Details

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

Returns a new instance of RangeNode.



4466
4467
4468
4469
4470
4471
4472
# File 'lib/syntax_tree/node.rb', line 4466

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



4464
4465
4466
# File 'lib/syntax_tree/node.rb', line 4464

def comments
  @comments
end

#leftObject (readonly)

nil | untyped

the left side of the expression



4455
4456
4457
# File 'lib/syntax_tree/node.rb', line 4455

def left
  @left
end

#operatorObject (readonly)

Op

the operator used for this range



4458
4459
4460
# File 'lib/syntax_tree/node.rb', line 4458

def operator
  @operator
end

#rightObject (readonly)

nil | untyped

the right side of the expression



4461
4462
4463
# File 'lib/syntax_tree/node.rb', line 4461

def right
  @right
end

Instance Method Details

#===(other) ⇒ Object



4520
4521
4522
4523
# File 'lib/syntax_tree/node.rb', line 4520

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

#accept(visitor) ⇒ Object



4474
4475
4476
# File 'lib/syntax_tree/node.rb', line 4474

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

#child_nodesObject Also known as: deconstruct



4478
4479
4480
# File 'lib/syntax_tree/node.rb', line 4478

def child_nodes
  [left, right]
end

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



4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
# File 'lib/syntax_tree/node.rb', line 4482

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



4497
4498
4499
4500
4501
4502
4503
4504
4505
# File 'lib/syntax_tree/node.rb', line 4497

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

#format(q) ⇒ Object



4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
# File 'lib/syntax_tree/node.rb', line 4507

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