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.



1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
# File 'lib/prism/node.rb', line 1497

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
                   ^^^^^^


1586
1587
1588
# File 'lib/prism/node.rb', line 1586

def else_clause
  @else_clause
end

#ensure_clauseObject (readonly)

Represents the ensure clause within the begin block.

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


1592
1593
1594
# File 'lib/prism/node.rb', line 1592

def ensure_clause
  @ensure_clause
end

#rescue_clauseObject (readonly)

Represents the rescue clause within the begin block.

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


1580
1581
1582
# File 'lib/prism/node.rb', line 1580

def rescue_clause
  @rescue_clause
end

#statementsObject (readonly)

Represents the statements within the begin block.

begin x end
      ^


1574
1575
1576
# File 'lib/prism/node.rb', line 1574

def statements
  @statements
end

Class Method Details

.typeObject

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



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

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.



1643
1644
1645
1646
1647
1648
1649
1650
1651
# File 'lib/prism/node.rb', line 1643

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



1511
1512
1513
# File 'lib/prism/node.rb', line 1511

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

#begin_keywordObject

def begin_keyword: () -> String?



1617
1618
1619
# File 'lib/prism/node.rb', line 1617

def begin_keyword
  begin_keyword_loc&.slice
end

#begin_keyword_locObject

Represents the location of the ‘begin` keyword.

begin x end
^^^^^


1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
# File 'lib/prism/node.rb', line 1552

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



1516
1517
1518
# File 'lib/prism/node.rb', line 1516

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

#comment_targetsObject

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



1531
1532
1533
# File 'lib/prism/node.rb', line 1531

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



1521
1522
1523
1524
1525
1526
1527
1528
# File 'lib/prism/node.rb', line 1521

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



1536
1537
1538
# File 'lib/prism/node.rb', line 1536

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? }



1544
1545
1546
# File 'lib/prism/node.rb', line 1544

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

#end_keywordObject

def end_keyword: () -> String?



1622
1623
1624
# File 'lib/prism/node.rb', line 1622

def end_keyword
  end_keyword_loc&.slice
end

#end_keyword_locObject

Represents the location of the ‘end` keyword.

begin x end
        ^^^


1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
# File 'lib/prism/node.rb', line 1598

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



1627
1628
1629
# File 'lib/prism/node.rb', line 1627

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.



1566
1567
1568
# File 'lib/prism/node.rb', line 1566

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.



1612
1613
1614
# File 'lib/prism/node.rb', line 1612

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`.



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

def type
  :begin_node
end