Class: SyntaxTree::PinnedVarRef

Inherits:
Node
  • Object
show all
Defined in:
lib/syntax_tree/node.rb

Overview

PinnedVarRef represents a pinned variable reference within a pattern matching pattern.

case value
in ^variable
end

This can be a plain local variable like the example above. It can also be a a class variable, a global variable, or an instance variable.

Instance Attribute Summary collapse

Attributes inherited from Node

#location

Instance Method Summary collapse

Methods inherited from Node

#construct_keys, #pretty_print, #to_json

Constructor Details

#initialize(value:, location:) ⇒ PinnedVarRef

Returns a new instance of PinnedVarRef.



11491
11492
11493
11494
11495
# File 'lib/syntax_tree/node.rb', line 11491

def initialize(value:, location:)
  @value = value
  @location = location
  @comments = []
end

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



11489
11490
11491
# File 'lib/syntax_tree/node.rb', line 11489

def comments
  @comments
end

#valueObject (readonly)

VarRef

the value of this node



11486
11487
11488
# File 'lib/syntax_tree/node.rb', line 11486

def value
  @value
end

Instance Method Details

#===(other) ⇒ Object



11529
11530
11531
# File 'lib/syntax_tree/node.rb', line 11529

def ===(other)
  other.is_a?(PinnedVarRef) && value === other.value
end

#accept(visitor) ⇒ Object



11497
11498
11499
# File 'lib/syntax_tree/node.rb', line 11497

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

#child_nodesObject Also known as: deconstruct



11501
11502
11503
# File 'lib/syntax_tree/node.rb', line 11501

def child_nodes
  [value]
end

#copy(value: nil, location: nil) ⇒ Object



11505
11506
11507
11508
11509
11510
11511
11512
11513
11514
# File 'lib/syntax_tree/node.rb', line 11505

def copy(value: nil, location: nil)
  node =
    PinnedVarRef.new(
      value: value || self.value,
      location: location || self.location
    )

  node.comments.concat(comments.map(&:copy))
  node
end

#deconstruct_keys(_keys) ⇒ Object



11518
11519
11520
# File 'lib/syntax_tree/node.rb', line 11518

def deconstruct_keys(_keys)
  { value: value, location: location, comments: comments }
end

#format(q) ⇒ Object



11522
11523
11524
11525
11526
11527
# File 'lib/syntax_tree/node.rb', line 11522

def format(q)
  q.group do
    q.text("^")
    q.format(value)
  end
end