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.



1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
# File 'lib/prism/node.rb', line 1485

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


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

def else_clause
  @else_clause
end

#ensure_clauseObject (readonly)

Represents the ensure clause within the begin block.

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


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

def ensure_clause
  @ensure_clause
end

#rescue_clauseObject (readonly)

Represents the rescue clause within the begin block.

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


1568
1569
1570
# File 'lib/prism/node.rb', line 1568

def rescue_clause
  @rescue_clause
end

#statementsObject (readonly)

Represents the statements within the begin block.

begin x end
      ^


1562
1563
1564
# File 'lib/prism/node.rb', line 1562

def statements
  @statements
end

Class Method Details

.typeObject

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



1625
1626
1627
# File 'lib/prism/node.rb', line 1625

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.



1631
1632
1633
1634
1635
1636
1637
1638
1639
# File 'lib/prism/node.rb', line 1631

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



1499
1500
1501
# File 'lib/prism/node.rb', line 1499

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

#begin_keywordObject

def begin_keyword: () -> String?



1605
1606
1607
# File 'lib/prism/node.rb', line 1605

def begin_keyword
  begin_keyword_loc&.slice
end

#begin_keyword_locObject

Represents the location of the ‘begin` keyword.

begin x end
^^^^^


1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
# File 'lib/prism/node.rb', line 1540

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[nil | Node]



1504
1505
1506
# File 'lib/prism/node.rb', line 1504

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

#comment_targetsObject

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



1519
1520
1521
# File 'lib/prism/node.rb', line 1519

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



1509
1510
1511
1512
1513
1514
1515
1516
# File 'lib/prism/node.rb', line 1509

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



1524
1525
1526
# File 'lib/prism/node.rb', line 1524

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



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

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?



1610
1611
1612
# File 'lib/prism/node.rb', line 1610

def end_keyword
  end_keyword_loc&.slice
end

#end_keyword_locObject

Represents the location of the ‘end` keyword.

begin x end
        ^^^


1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
# File 'lib/prism/node.rb', line 1586

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



1615
1616
1617
# File 'lib/prism/node.rb', line 1615

def inspect
  InspectVisitor.compose(self)
end

#newline_flag!(lines) ⇒ Object

:nodoc:



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

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.



1554
1555
1556
# File 'lib/prism/node.rb', line 1554

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.



1600
1601
1602
# File 'lib/prism/node.rb', line 1600

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



1620
1621
1622
# File 'lib/prism/node.rb', line 1620

def type
  :begin_node
end