Class: Prism::WhileNode

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

Overview

Represents the use of the while keyword, either in the block form or the modifier form.

bar while foo
^^^^^^^^^^^^^

while foo do bar end
^^^^^^^^^^^^^^^^^^^^

Explore code sample

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, node_id, location, flags, while_keyword_loc, do_keyword_loc, end_keyword_loc, predicate, statements) ⇒ WhileNode

Initialize a new WhileNode node.

: (Source source, Integer node_id, Location location, Integer flags, Location while_keyword_loc, Location? do_keyword_loc, Location? end_keyword_loc, Prism::node predicate, StatementsNode? statements) -> void

Parameters:



32980
32981
32982
32983
32984
32985
32986
32987
32988
32989
32990
# File 'lib/prism/node.rb', line 32980

def initialize(source, node_id, location, flags, while_keyword_loc, do_keyword_loc, end_keyword_loc, predicate, statements)
  @source = source
  @node_id = node_id
  @location = location
  @flags = flags
  @while_keyword_loc = while_keyword_loc
  @do_keyword_loc = do_keyword_loc
  @end_keyword_loc = end_keyword_loc
  @predicate = predicate
  @statements = statements
end

Class Method Details

.type ⇒ :while_node

See Node.type.

: () -> :while_node

Returns:

  • (:while_node)


33080
33081
33082
# File 'lib/prism/node.rb', line 33080

def self.type
  :while_node
end

Instance Method Details

#===(other) ⇒ boolish

: (untyped other) -> boolish

Parameters:

  • other (Object)

Returns:

  • (boolish)


33230
33231
33232
33233
33234
33235
33236
33237
33238
# File 'lib/prism/node.rb', line 33230

def ===(other) # :nodoc:
  other.is_a?(WhileNode) &&
    (flags === other.flags) &&
    (while_keyword_loc.nil? == other.while_keyword_loc.nil?) &&
    (do_keyword_loc.nil? == other.do_keyword_loc.nil?) &&
    (end_keyword_loc.nil? == other.end_keyword_loc.nil?) &&
    (predicate === other.predicate) &&
    (statements === other.statements)
end

#accept(visitor) ⇒ Object

See Node.accept.

: (_Visitor visitor) -> untyped

Parameters:

Returns:

  • (Object)


33006
33007
33008
# File 'lib/prism/node.rb', line 33006

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

#begin_modifier? ⇒ Boolean

:category: Flags a loop after a begin statement, so the body is executed first before the condition

: () -> bool

Returns:

  • (Boolean)


33095
33096
33097
# File 'lib/prism/node.rb', line 33095

def begin_modifier?
  flags.anybits?(LoopFlags::BEGIN_MODIFIER)
end

#child_nodes ⇒ Array[node?] Also known as: deconstruct

See Node.child_nodes.

: () -> Array

Returns:

  • (Array[node?])


33013
33014
33015
# File 'lib/prism/node.rb', line 33013

def child_nodes
  [predicate, statements]
end

#closing ⇒ String?

: () -> String?

Returns:

  • (String, nil)


482
483
484
# File 'lib/prism/node_ext.rb', line 482

def closing # :nodoc
  end_keyword
end

#closing_loc ⇒ Location?

: () -> Location?

Returns:



487
488
489
# File 'lib/prism/node_ext.rb', line 487

def closing_loc # :nodoc
  end_keyword_loc
end

#compact_child_nodes ⇒ Array[node]

See Node.compact_child_nodes.

: () -> Array

Returns:

  • (Array[node])


33031
33032
33033
33034
33035
33036
# File 'lib/prism/node.rb', line 33031

def compact_child_nodes
  compact = [] #: Array[Prism::node]
  compact << predicate
  if (statements = self.statements); compact << statements; end
  compact
end

#copy(node_id: self.node_id, location: self.location, flags: self.flags, while_keyword_loc: self.while_keyword_loc, do_keyword_loc: self.do_keyword_loc, end_keyword_loc: self.end_keyword_loc, predicate: self.predicate, statements: self.statements) ⇒ WhileNode

:call-seq:

copy(**fields) -> WhileNode

Creates a copy of self with the given fields, using self as the template.

: (?node_id: Integer, ?location: Location, ?flags: Integer, ?while_keyword_loc: Location, ?do_keyword_loc: Location?, ?end_keyword_loc: Location?, ?predicate: Prism::node, ?statements: StatementsNode?) -> WhileNode

Parameters:

  • node_id: (Integer) (defaults to: self.node_id)
  • location: (Location) (defaults to: self.location)
  • flags: (Integer) (defaults to: self.flags)
  • while_keyword_loc: (Location) (defaults to: self.while_keyword_loc)
  • do_keyword_loc: (Location, nil) (defaults to: self.do_keyword_loc)
  • end_keyword_loc: (Location, nil) (defaults to: self.end_keyword_loc)
  • predicate: (Prism::node) (defaults to: self.predicate)
  • statements: (StatementsNode, nil) (defaults to: self.statements)

Returns:



33044
33045
33046
# File 'lib/prism/node.rb', line 33044

def copy(node_id: self.node_id, location: self.location, flags: self.flags, while_keyword_loc: self.while_keyword_loc, do_keyword_loc: self.do_keyword_loc, end_keyword_loc: self.end_keyword_loc, predicate: self.predicate, statements: self.statements)
  WhileNode.new(source, node_id, location, flags, while_keyword_loc, do_keyword_loc, end_keyword_loc, predicate, statements)
end

#deconstruct_keys(keys) ⇒ Hash[Symbol, untyped]

: (Array? keys) -> Hash[Symbol, untyped]

Parameters:

  • keys (Array[Symbol], nil)

Returns:

  • (Hash[Symbol, untyped])


33051
33052
33053
33054
33055
33056
33057
33058
33059
33060
33061
33062
33063
33064
33065
33066
33067
33068
# File 'lib/prism/node.rb', line 33051

def deconstruct_keys(keys) # :nodoc:
  (keys || %i[node_id location while_keyword_loc do_keyword_loc end_keyword_loc predicate statements while_keyword do_keyword end_keyword]).each_with_object(
    {} #: Hash[Symbol, untyped]
  ) do |key, deconstructed|
    case key
    when :node_id                 then deconstructed[:node_id] = self.node_id
    when :location                then deconstructed[:location] = self.location
    when :while_keyword_loc       then deconstructed[:while_keyword_loc] = self.while_keyword_loc
    when :do_keyword_loc          then deconstructed[:do_keyword_loc] = self.do_keyword_loc
    when :end_keyword_loc         then deconstructed[:end_keyword_loc] = self.end_keyword_loc
    when :predicate               then deconstructed[:predicate] = self.predicate
    when :statements              then deconstructed[:statements] = self.statements
    when :while_keyword           then deconstructed[:while_keyword] = self.while_keyword
    when :do_keyword              then deconstructed[:do_keyword] = self.do_keyword
    when :end_keyword             then deconstructed[:end_keyword] = self.end_keyword
    end
  end
end

#do_keyword ⇒ String?

:call-seq:

do_keyword -> String | nil

Slice the location of do_keyword_loc from the source.

: () -> String?

Returns:

  • (String, nil)


33213
33214
33215
# File 'lib/prism/node.rb', line 33213

def do_keyword
  do_keyword_loc&.slice
end

#do_keyword_loc ⇒ Location?

:category: Locations :call-seq:

do_keyword_loc -> Location | nil

Returns the Location represented by do_keyword_loc.

: () -> Location?

Returns:



33128
33129
33130
33131
33132
33133
33134
33135
33136
33137
33138
# File 'lib/prism/node.rb', line 33128

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

#each_child_node ⇒ Enumerator[node, void] #each_child_node ⇒ void

See Node.each_child_node.

: () -> Enumerator[node, void] : () { (node) -> void } -> void

Overloads:

  • #each_child_node ⇒ Enumerator[node, void]

    Returns:

    • (Enumerator[node, void])
  • #each_child_node ⇒ void

    This method returns an undefined value.

Yields:

Yield Parameters:

  • arg0 (node)

Yield Returns:

  • (void)


33021
33022
33023
33024
33025
33026
# File 'lib/prism/node.rb', line 33021

def each_child_node(&blk)
  return to_enum(:each_child_node) unless block_given?

  yield predicate
  if (statements = self.statements); yield statements; end
end

#end_keyword ⇒ String?

:call-seq:

end_keyword -> String | nil

Slice the location of end_keyword_loc from the source.

: () -> String?

Returns:

  • (String, nil)


33223
33224
33225
# File 'lib/prism/node.rb', line 33223

def end_keyword
  end_keyword_loc&.slice
end

#end_keyword_loc ⇒ Location?

:category: Locations :call-seq:

end_keyword_loc -> Location | nil

Returns the Location represented by end_keyword_loc.

: () -> Location?

Returns:



33155
33156
33157
33158
33159
33160
33161
33162
33163
33164
33165
# File 'lib/prism/node.rb', line 33155

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

#inspect ⇒ String

: () -> String

Returns:

  • (String)


33085
33086
33087
# File 'lib/prism/node.rb', line 33085

def inspect # :nodoc:
  InspectVisitor.compose(self)
end

#keyword ⇒ String

: () -> String

Returns:

  • (String)


472
473
474
# File 'lib/prism/node_ext.rb', line 472

def keyword # :nodoc
  while_keyword
end

#keyword_loc ⇒ Location

: () -> Location

Returns:



477
478
479
# File 'lib/prism/node_ext.rb', line 477

def keyword_loc # :nodoc
  while_keyword_loc
end

#newline_flag!(lines) ⇒ void

This method returns an undefined value.

: (Array lines) -> void

Parameters:

  • lines (Array[bool])


270
271
272
273
274
275
276
277
278
# File 'lib/prism/parse_result/newlines.rb', line 270

def newline_flag!(lines) # :nodoc:
  if location.start_offset == keyword_loc.start_offset && predicate.is_a?(ParenthesesNode)
    # A parenthesized predicate emits its own line event when it is
    # compiled at the end of the loop, in addition to this one.
    super
  else
    predicate.newline_flag!(lines)
  end
end

#predicate ⇒ Prism::node

:call-seq:

predicate -> Node

Returns the predicate attribute.

: () -> Prism::node

Returns:

  • (Prism::node)


33181
33182
33183
# File 'lib/prism/node.rb', line 33181

def predicate
  @predicate
end

#save_do_keyword_loc(repository) ⇒ Relocation::Entry?

:category: Repository Save the do_keyword_loc location using the given saved source so that it can be retrieved later.

: (_Repository repository) -> Relocation::Entry?

Parameters:

Returns:



33145
33146
33147
# File 'lib/prism/node.rb', line 33145

def save_do_keyword_loc(repository)
  repository.enter(node_id, :do_keyword_loc) unless @do_keyword_loc.nil?
end

#save_end_keyword_loc(repository) ⇒ Relocation::Entry?

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

: (_Repository repository) -> Relocation::Entry?

Parameters:

Returns:



33172
33173
33174
# File 'lib/prism/node.rb', line 33172

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

#save_while_keyword_loc(repository) ⇒ Relocation::Entry

:category: Repository Save the while_keyword_loc location using the given saved source so that it can be retrieved later.

: (_Repository repository) -> Relocation::Entry

Parameters:

Returns:



33117
33118
33119
# File 'lib/prism/node.rb', line 33117

def save_while_keyword_loc(repository)
  repository.enter(node_id, :while_keyword_loc)
end

#statements ⇒ StatementsNode?

:call-seq:

statements -> StatementsNode | nil

Returns the statements attribute.

: () -> StatementsNode?

Returns:



33191
33192
33193
# File 'lib/prism/node.rb', line 33191

def statements
  @statements
end

#type ⇒ :while_node

See Node#type.

: () -> :while_node

Returns:

  • (:while_node)


33073
33074
33075
# File 'lib/prism/node.rb', line 33073

def type
  :while_node
end

#while_keyword ⇒ String

:call-seq:

while_keyword -> String

Slice the location of while_keyword_loc from the source.

: () -> String

Returns:

  • (String)


33203
33204
33205
# File 'lib/prism/node.rb', line 33203

def while_keyword
  while_keyword_loc.slice
end

#while_keyword_loc ⇒ Location

:category: Locations :call-seq:

while_keyword_loc -> Location

Returns the Location represented by while_keyword_loc.

: () -> Location

Returns:



33106
33107
33108
33109
33110
# File 'lib/prism/node.rb', line 33106

def while_keyword_loc
  location = @while_keyword_loc
  return location if location.is_a?(Location)
  @while_keyword_loc = Location.new(source, location >> 32, location & 0xFFFFFFFF)
end