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.



1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
# File 'lib/prism/node.rb', line 1315

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)

attr_reader else_clause: ElseNode?



1386
1387
1388
# File 'lib/prism/node.rb', line 1386

def else_clause
  @else_clause
end

#ensure_clauseObject (readonly)

attr_reader ensure_clause: EnsureNode?



1389
1390
1391
# File 'lib/prism/node.rb', line 1389

def ensure_clause
  @ensure_clause
end

#rescue_clauseObject (readonly)

attr_reader rescue_clause: RescueNode?



1383
1384
1385
# File 'lib/prism/node.rb', line 1383

def rescue_clause
  @rescue_clause
end

#statementsObject (readonly)

attr_reader statements: StatementsNode?



1380
1381
1382
# File 'lib/prism/node.rb', line 1380

def statements
  @statements
end

Class Method Details

.typeObject

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



1425
1426
1427
# File 'lib/prism/node.rb', line 1425

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.



1431
1432
1433
1434
1435
1436
1437
1438
1439
# File 'lib/prism/node.rb', line 1431

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



1329
1330
1331
# File 'lib/prism/node.rb', line 1329

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

#begin_keywordObject

def begin_keyword: () -> String?



1405
1406
1407
# File 'lib/prism/node.rb', line 1405

def begin_keyword
  begin_keyword_loc&.slice
end

#begin_keyword_locObject

attr_reader begin_keyword_loc: Location?



1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
# File 'lib/prism/node.rb', line 1367

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]



1334
1335
1336
# File 'lib/prism/node.rb', line 1334

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

#comment_targetsObject

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



1349
1350
1351
# File 'lib/prism/node.rb', line 1349

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



1339
1340
1341
1342
1343
1344
1345
1346
# File 'lib/prism/node.rb', line 1339

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



1354
1355
1356
# File 'lib/prism/node.rb', line 1354

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



1362
1363
1364
# File 'lib/prism/node.rb', line 1362

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?



1410
1411
1412
# File 'lib/prism/node.rb', line 1410

def end_keyword
  end_keyword_loc&.slice
end

#end_keyword_locObject

attr_reader end_keyword_loc: Location?



1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
# File 'lib/prism/node.rb', line 1392

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



1415
1416
1417
# File 'lib/prism/node.rb', line 1415

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

#typeObject

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



1420
1421
1422
# File 'lib/prism/node.rb', line 1420

def type
  :begin_node
end