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.



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

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



1901
1902
1903
# File 'lib/syntax_tree/node.rb', line 1901

def bodystmt
  @bodystmt
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



1904
1905
1906
# File 'lib/syntax_tree/node.rb', line 1904

def comments
  @comments
end

Instance Method Details

#===(other) ⇒ Object



1951
1952
1953
# File 'lib/syntax_tree/node.rb', line 1951

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

#accept(visitor) ⇒ Object



1912
1913
1914
# File 'lib/syntax_tree/node.rb', line 1912

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

#child_nodesObject Also known as: deconstruct



1916
1917
1918
# File 'lib/syntax_tree/node.rb', line 1916

def child_nodes
  [bodystmt]
end

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



1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
# File 'lib/syntax_tree/node.rb', line 1920

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



1933
1934
1935
# File 'lib/syntax_tree/node.rb', line 1933

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

#format(q) ⇒ Object



1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
# File 'lib/syntax_tree/node.rb', line 1937

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