Class: SyntaxTree::Begin

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

Overview

Begin represents a begin..end chain.

begin
  value
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(bodystmt:, location:) ⇒ Begin

Returns a new instance of Begin.



1879
1880
1881
1882
1883
# File 'lib/syntax_tree/node.rb', line 1879

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

Instance Attribute Details

#bodystmtObject (readonly)

BodyStmt

the bodystmt that contains the contents of this begin block



1874
1875
1876
# File 'lib/syntax_tree/node.rb', line 1874

def bodystmt
  @bodystmt
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



1877
1878
1879
# File 'lib/syntax_tree/node.rb', line 1877

def comments
  @comments
end

Instance Method Details

#===(other) ⇒ Object



1924
1925
1926
# File 'lib/syntax_tree/node.rb', line 1924

def ===(other)
  other.is_a?(Begin) && bodystmt === other.bodystmt
end

#accept(visitor) ⇒ Object



1885
1886
1887
# File 'lib/syntax_tree/node.rb', line 1885

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

#child_nodesObject Also known as: deconstruct



1889
1890
1891
# File 'lib/syntax_tree/node.rb', line 1889

def child_nodes
  [bodystmt]
end

#copy(bodystmt: nil, location: nil) ⇒ Object



1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
# File 'lib/syntax_tree/node.rb', line 1893

def copy(bodystmt: nil, location: nil)
  node =
    Begin.new(
      bodystmt: bodystmt || self.bodystmt,
      location: location || self.location
    )

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

#deconstruct_keys(_keys) ⇒ Object



1906
1907
1908
# File 'lib/syntax_tree/node.rb', line 1906

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

#format(q) ⇒ Object



1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
# File 'lib/syntax_tree/node.rb', line 1910

def format(q)
  q.text("begin")

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

  q.breakable_force
  q.text("end")
end