Class: SyntaxTree::Else

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

Overview

Else represents the end of an if, unless, or case chain.

if variable
else
end

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(keyword:, statements:, location:) ⇒ Else

Returns a new instance of Else.



4729
4730
4731
4732
4733
4734
# File 'lib/syntax_tree/node.rb', line 4729

def initialize(keyword:, statements:, location:)
  @keyword = keyword
  @statements = statements
  @location = location
  @comments = []
end

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



4727
4728
4729
# File 'lib/syntax_tree/node.rb', line 4727

def comments
  @comments
end

#keywordObject (readonly)

Kw

the else keyword



4721
4722
4723
# File 'lib/syntax_tree/node.rb', line 4721

def keyword
  @keyword
end

#statementsObject (readonly)

Statements

the expressions to be executed



4724
4725
4726
# File 'lib/syntax_tree/node.rb', line 4724

def statements
  @statements
end

Instance Method Details

#===(other) ⇒ Object



4780
4781
4782
4783
# File 'lib/syntax_tree/node.rb', line 4780

def ===(other)
  other.is_a?(Else) && keyword === other.keyword &&
    statements === other.statements
end

#accept(visitor) ⇒ Object



4736
4737
4738
# File 'lib/syntax_tree/node.rb', line 4736

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

#child_nodesObject Also known as: deconstruct



4740
4741
4742
# File 'lib/syntax_tree/node.rb', line 4740

def child_nodes
  [keyword, statements]
end

#copy(keyword: nil, statements: nil, location: nil) ⇒ Object



4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
# File 'lib/syntax_tree/node.rb', line 4744

def copy(keyword: nil, statements: nil, location: nil)
  node =
    Else.new(
      keyword: keyword || self.keyword,
      statements: statements || self.statements,
      location: location || self.location
    )

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

#deconstruct_keys(_keys) ⇒ Object



4758
4759
4760
4761
4762
4763
4764
4765
# File 'lib/syntax_tree/node.rb', line 4758

def deconstruct_keys(_keys)
  {
    keyword: keyword,
    statements: statements,
    location: location,
    comments: comments
  }
end

#format(q) ⇒ Object



4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
# File 'lib/syntax_tree/node.rb', line 4767

def format(q)
  q.group do
    q.format(keyword)

    unless statements.empty?
      q.indent do
        q.breakable_force
        q.format(statements)
      end
    end
  end
end