Class: SyntaxTree::Dot2

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

Overview

Dot2 represents using 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

Instance Method Summary collapse

Constructor Details

#initialize(left:, right:, location:, comments: []) ⇒ Dot2

Returns a new instance of Dot2.



4617
4618
4619
4620
4621
4622
# File 'lib/syntax_tree.rb', line 4617

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

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



4615
4616
4617
# File 'lib/syntax_tree.rb', line 4615

def comments
  @comments
end

#leftObject (readonly)

nil | untyped

the left side of the expression



4606
4607
4608
# File 'lib/syntax_tree.rb', line 4606

def left
  @left
end

#locationObject (readonly)

Location

the location of this node



4612
4613
4614
# File 'lib/syntax_tree.rb', line 4612

def location
  @location
end

#rightObject (readonly)

nil | untyped

the right side of the expression



4609
4610
4611
# File 'lib/syntax_tree.rb', line 4609

def right
  @right
end

Instance Method Details

#child_nodesObject



4624
4625
4626
# File 'lib/syntax_tree.rb', line 4624

def child_nodes
  [left, right]
end

#format(q) ⇒ Object



4628
4629
4630
# File 'lib/syntax_tree.rb', line 4628

def format(q)
  DotFormatter.new("..", self).format(q)
end

#pretty_print(q) ⇒ Object



4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
# File 'lib/syntax_tree.rb', line 4632

def pretty_print(q)
  q.group(2, "(", ")") do
    q.text("dot2")

    if left
      q.breakable
      q.pp(left)
    end

    if right
      q.breakable
      q.pp(right)
    end

    q.pp(Comment::List.new(comments))
  end
end

#to_json(*opts) ⇒ Object



4650
4651
4652
4653
4654
4655
4656
4657
4658
# File 'lib/syntax_tree.rb', line 4650

def to_json(*opts)
  {
    type: :dot2,
    left: left,
    right: right,
    loc: location,
    cmts: comments
  }.to_json(*opts)
end