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.



5082
5083
5084
5085
5086
5087
# File 'lib/syntax_tree.rb', line 5082

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



5080
5081
5082
# File 'lib/syntax_tree.rb', line 5080

def comments
  @comments
end

#leftObject (readonly)

[nil | untyped] the left side of the expression



5071
5072
5073
# File 'lib/syntax_tree.rb', line 5071

def left
  @left
end

#locationObject (readonly)

[Location] the location of this node



5077
5078
5079
# File 'lib/syntax_tree.rb', line 5077

def location
  @location
end

#rightObject (readonly)

[nil | untyped] the right side of the expression



5074
5075
5076
# File 'lib/syntax_tree.rb', line 5074

def right
  @right
end

Instance Method Details

#child_nodesObject



5089
5090
5091
# File 'lib/syntax_tree.rb', line 5089

def child_nodes
  [left, right]
end

#format(q) ⇒ Object



5093
5094
5095
# File 'lib/syntax_tree.rb', line 5093

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

#pretty_print(q) ⇒ Object



5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
# File 'lib/syntax_tree.rb', line 5097

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



5115
5116
5117
5118
5119
5120
5121
5122
5123
# File 'lib/syntax_tree.rb', line 5115

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