Class: YARP::BlockNode

Inherits:
YARPNode
  • Object
show all
Defined in:
lib/yarp/node.rb,
ext/yarp/api_node.c

Overview

Represents a block of ruby code.

[1, 2, 3].each { |i| puts x }

^^^^^^^^^^^^^^

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(locals, parameters, body, opening_loc, closing_loc, location) ⇒ BlockNode

def initialize: (locals: Array, parameters: BlockParametersNode?, body: Node?, opening_loc: Location, closing_loc: Location, location: Location) -> void



911
912
913
914
915
916
917
918
# File 'lib/yarp/node.rb', line 911

def initialize(locals, parameters, body, opening_loc, closing_loc, location)
  @locals = locals
  @parameters = parameters
  @body = body
  @opening_loc = opening_loc
  @closing_loc = closing_loc
  @location = location
end

Instance Attribute Details

#bodyObject (readonly)

attr_reader body: Node?



902
903
904
# File 'lib/yarp/node.rb', line 902

def body
  @body
end

#closing_locObject (readonly)

attr_reader closing_loc: Location



908
909
910
# File 'lib/yarp/node.rb', line 908

def closing_loc
  @closing_loc
end

#localsObject (readonly)

attr_reader locals: Array



896
897
898
# File 'lib/yarp/node.rb', line 896

def locals
  @locals
end

#opening_locObject (readonly)

attr_reader opening_loc: Location



905
906
907
# File 'lib/yarp/node.rb', line 905

def opening_loc
  @opening_loc
end

#parametersObject (readonly)

attr_reader parameters: BlockParametersNode?



899
900
901
# File 'lib/yarp/node.rb', line 899

def parameters
  @parameters
end

Instance Method Details

#accept(visitor) ⇒ Object

def accept: (visitor: Visitor) -> void



921
922
923
# File 'lib/yarp/node.rb', line 921

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

#child_nodesObject Also known as: deconstruct

def child_nodes: () -> Array[nil | Node]



926
927
928
# File 'lib/yarp/node.rb', line 926

def child_nodes
  [parameters, body]
end

#closingObject

def closing: () -> String



961
962
963
# File 'lib/yarp/node.rb', line 961

def closing
  closing_loc.slice
end

#comment_targetsObject

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



931
932
933
# File 'lib/yarp/node.rb', line 931

def comment_targets
  [*parameters, *body, opening_loc, closing_loc]
end

#copy(**params) ⇒ Object

def copy: (**params) -> BlockNode



936
937
938
939
940
941
942
943
944
945
# File 'lib/yarp/node.rb', line 936

def copy(**params)
  BlockNode.new(
    params.fetch(:locals) { locals },
    params.fetch(:parameters) { parameters },
    params.fetch(:body) { body },
    params.fetch(:opening_loc) { opening_loc },
    params.fetch(:closing_loc) { closing_loc },
    params.fetch(:location) { location },
  )
end

#deconstruct_keys(keys) ⇒ Object

def deconstruct_keys: (keys: Array) -> Hash[Symbol, nil | Node | Array | String | Token | Array | Location]



951
952
953
# File 'lib/yarp/node.rb', line 951

def deconstruct_keys(keys)
  { locals: locals, parameters: parameters, body: body, opening_loc: opening_loc, closing_loc: closing_loc, location: location }
end

#inspect(inspector = NodeInspector.new) ⇒ Object



965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
# File 'lib/yarp/node.rb', line 965

def inspect(inspector = NodeInspector.new)
  inspector << inspector.header(self)
  inspector << "├── locals: #{locals.inspect}\n"
  if (parameters = self.parameters).nil?
    inspector << "├── parameters: ∅\n"
  else
    inspector << "├── parameters:\n"
    inspector << parameters.inspect(inspector.child_inspector("")).delete_prefix(inspector.prefix)
  end
  if (body = self.body).nil?
    inspector << "├── body: ∅\n"
  else
    inspector << "├── body:\n"
    inspector << body.inspect(inspector.child_inspector("")).delete_prefix(inspector.prefix)
  end
  inspector << "├── opening_loc: #{inspector.location(opening_loc)}\n"
  inspector << "└── closing_loc: #{inspector.location(closing_loc)}\n"
  inspector.to_str
end

#openingObject

def opening: () -> String



956
957
958
# File 'lib/yarp/node.rb', line 956

def opening
  opening_loc.slice
end