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

Constructor Details

#initialize(keyword:, statements:, location:) ⇒ Else

Returns a new instance of Else.



4817
4818
4819
4820
4821
4822
# File 'lib/syntax_tree/node.rb', line 4817

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



4815
4816
4817
# File 'lib/syntax_tree/node.rb', line 4815

def comments
  @comments
end

#keywordObject (readonly)

Kw

the else keyword



4809
4810
4811
# File 'lib/syntax_tree/node.rb', line 4809

def keyword
  @keyword
end

#statementsObject (readonly)

Statements

the expressions to be executed



4812
4813
4814
# File 'lib/syntax_tree/node.rb', line 4812

def statements
  @statements
end

Instance Method Details

#===(other) ⇒ Object



4868
4869
4870
4871
# File 'lib/syntax_tree/node.rb', line 4868

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

#accept(visitor) ⇒ Object



4824
4825
4826
# File 'lib/syntax_tree/node.rb', line 4824

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

#child_nodesObject Also known as: deconstruct



4828
4829
4830
# File 'lib/syntax_tree/node.rb', line 4828

def child_nodes
  [keyword, statements]
end

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



4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
# File 'lib/syntax_tree/node.rb', line 4832

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



4846
4847
4848
4849
4850
4851
4852
4853
# File 'lib/syntax_tree/node.rb', line 4846

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

#format(q) ⇒ Object



4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
# File 'lib/syntax_tree/node.rb', line 4855

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