Class: Prism::BeginNode

Inherits:
PrismNode
  • Object
show all
Defined in:
lib/prism/node.rb,
lib/prism/parse_result/newlines.rb,
ext/prism/api_node.c

Overview

Represents a begin statement.

begin
  foo
end
^^^^^

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, node_id, location, flags, begin_keyword_loc, statements, rescue_clause, else_clause, ensure_clause, end_keyword_loc) ⇒ BeginNode

Initialize a new BeginNode node.



1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
# File 'lib/prism/node.rb', line 1589

def initialize(source, node_id, location, flags, begin_keyword_loc, statements, rescue_clause, else_clause, ensure_clause, end_keyword_loc)
  @source = source
  @node_id = node_id
  @location = location
  @flags = flags
  @begin_keyword_loc = begin_keyword_loc
  @statements = statements
  @rescue_clause = rescue_clause
  @else_clause = else_clause
  @ensure_clause = ensure_clause
  @end_keyword_loc = end_keyword_loc
end

Instance Attribute Details

#else_clauseObject (readonly)

Represents the else clause within the begin block.

begin x; rescue y; else z; end
                   ^^^^^^


1688
1689
1690
# File 'lib/prism/node.rb', line 1688

def else_clause
  @else_clause
end

#ensure_clauseObject (readonly)

Represents the ensure clause within the begin block.

begin x; ensure y; end
         ^^^^^^^^


1694
1695
1696
# File 'lib/prism/node.rb', line 1694

def ensure_clause
  @ensure_clause
end

#rescue_clauseObject (readonly)

Represents the rescue clause within the begin block.

begin x; rescue y; end
         ^^^^^^^^


1682
1683
1684
# File 'lib/prism/node.rb', line 1682

def rescue_clause
  @rescue_clause
end

#statementsObject (readonly)

Represents the statements within the begin block.

begin x end
      ^


1676
1677
1678
# File 'lib/prism/node.rb', line 1676

def statements
  @statements
end

Class Method Details

.typeObject

Return a symbol representation of this node type. See Node::type.



1739
1740
1741
# File 'lib/prism/node.rb', line 1739

def self.type
  :begin_node
end

Instance Method Details

#===(other) ⇒ Object

Implements case-equality for the node. This is effectively == but without comparing the value of locations. Locations are checked only for presence.



1745
1746
1747
1748
1749
1750
1751
1752
1753
# File 'lib/prism/node.rb', line 1745

def ===(other)
  other.is_a?(BeginNode) &&
    (begin_keyword_loc.nil? == other.begin_keyword_loc.nil?) &&
    (statements === other.statements) &&
    (rescue_clause === other.rescue_clause) &&
    (else_clause === other.else_clause) &&
    (ensure_clause === other.ensure_clause) &&
    (end_keyword_loc.nil? == other.end_keyword_loc.nil?)
end

#accept(visitor) ⇒ Object

def accept: (Visitor visitor) -> void



1603
1604
1605
# File 'lib/prism/node.rb', line 1603

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

#begin_keywordObject

def begin_keyword: () -> String?



1719
1720
1721
# File 'lib/prism/node.rb', line 1719

def begin_keyword
  begin_keyword_loc&.slice
end

#begin_keyword_locObject

Represents the location of the begin keyword.

begin x end
^^^^^


1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
# File 'lib/prism/node.rb', line 1654

def begin_keyword_loc
  location = @begin_keyword_loc
  case location
  when nil
    nil
  when Location
    location
  else
    @begin_keyword_loc = Location.new(source, location >> 32, location & 0xFFFFFFFF)
  end
end

#child_nodesObject Also known as: deconstruct

def child_nodes: () -> Array



1608
1609
1610
# File 'lib/prism/node.rb', line 1608

def child_nodes
  [statements, rescue_clause, else_clause, ensure_clause]
end

#comment_targetsObject

def comment_targets: () -> Array[Node | Location]



1633
1634
1635
# File 'lib/prism/node.rb', line 1633

def comment_targets
  [*begin_keyword_loc, *statements, *rescue_clause, *else_clause, *ensure_clause, *end_keyword_loc] #: Array[Prism::node | Location]
end

#compact_child_nodesObject

def compact_child_nodes: () -> Array



1623
1624
1625
1626
1627
1628
1629
1630
# File 'lib/prism/node.rb', line 1623

def compact_child_nodes
  compact = [] #: Array[Prism::node]
  compact << statements if statements
  compact << rescue_clause if rescue_clause
  compact << else_clause if else_clause
  compact << ensure_clause if ensure_clause
  compact
end

#copy(node_id: self.node_id, location: self.location, flags: self.flags, begin_keyword_loc: self.begin_keyword_loc, statements: self.statements, rescue_clause: self.rescue_clause, else_clause: self.else_clause, ensure_clause: self.ensure_clause, end_keyword_loc: self.end_keyword_loc) ⇒ Object

def copy: (?node_id: Integer, ?location: Location, ?flags: Integer, ?begin_keyword_loc: Location?, ?statements: StatementsNode?, ?rescue_clause: RescueNode?, ?else_clause: ElseNode?, ?ensure_clause: EnsureNode?, ?end_keyword_loc: Location?) -> BeginNode



1638
1639
1640
# File 'lib/prism/node.rb', line 1638

def copy(node_id: self.node_id, location: self.location, flags: self.flags, begin_keyword_loc: self.begin_keyword_loc, statements: self.statements, rescue_clause: self.rescue_clause, else_clause: self.else_clause, ensure_clause: self.ensure_clause, end_keyword_loc: self.end_keyword_loc)
  BeginNode.new(source, node_id, location, flags, begin_keyword_loc, statements, rescue_clause, else_clause, ensure_clause, end_keyword_loc)
end

#deconstruct_keys(keys) ⇒ Object

def deconstruct_keys: (Array keys) -> { node_id: Integer, location: Location, begin_keyword_loc: Location?, statements: StatementsNode?, rescue_clause: RescueNode?, else_clause: ElseNode?, ensure_clause: EnsureNode?, end_keyword_loc: Location? }



1646
1647
1648
# File 'lib/prism/node.rb', line 1646

def deconstruct_keys(keys)
  { node_id: node_id, location: location, begin_keyword_loc: begin_keyword_loc, statements: statements, rescue_clause: rescue_clause, else_clause: else_clause, ensure_clause: ensure_clause, end_keyword_loc: end_keyword_loc }
end

#each_child_node {|statements| ... } ⇒ Object

def each_child_node: () { (Prism::node) -> void } -> void | () -> Enumerator

Yields:



1613
1614
1615
1616
1617
1618
1619
1620
# File 'lib/prism/node.rb', line 1613

def each_child_node
  return to_enum(:each_child_node) unless block_given?

  yield statements if statements
  yield rescue_clause if rescue_clause
  yield else_clause if else_clause
  yield ensure_clause if ensure_clause
end

#end_keywordObject

def end_keyword: () -> String?



1724
1725
1726
# File 'lib/prism/node.rb', line 1724

def end_keyword
  end_keyword_loc&.slice
end

#end_keyword_locObject

Represents the location of the end keyword.

begin x end
        ^^^


1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
# File 'lib/prism/node.rb', line 1700

def end_keyword_loc
  location = @end_keyword_loc
  case location
  when nil
    nil
  when Location
    location
  else
    @end_keyword_loc = Location.new(source, location >> 32, location & 0xFFFFFFFF)
  end
end

#inspectObject

def inspect -> String



1729
1730
1731
# File 'lib/prism/node.rb', line 1729

def inspect
  InspectVisitor.compose(self)
end

#newline_flag!(lines) ⇒ Object

:nodoc:



80
81
82
# File 'lib/prism/parse_result/newlines.rb', line 80

def newline_flag!(lines) # :nodoc:
  # Never mark BeginNode with a newline flag, mark children instead.
end

#save_begin_keyword_loc(repository) ⇒ Object

Save the begin_keyword_loc location using the given saved source so that it can be retrieved later.



1668
1669
1670
# File 'lib/prism/node.rb', line 1668

def save_begin_keyword_loc(repository)
  repository.enter(node_id, :begin_keyword_loc) unless @begin_keyword_loc.nil?
end

#save_end_keyword_loc(repository) ⇒ Object

Save the end_keyword_loc location using the given saved source so that it can be retrieved later.



1714
1715
1716
# File 'lib/prism/node.rb', line 1714

def save_end_keyword_loc(repository)
  repository.enter(node_id, :end_keyword_loc) unless @end_keyword_loc.nil?
end

#typeObject

Return a symbol representation of this node type. See ‘Node#type`.



1734
1735
1736
# File 'lib/prism/node.rb', line 1734

def type
  :begin_node
end