Class: SyntaxTree::Label

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

Overview

Label represents the use of an identifier to associate with an object. You can find it in a hash key, as in:

{ key: value }

In this case “key:” would be the body of the label. You can also find it in pattern matching, as in:

case value
in key:
end

In this case “key:” would be the body of the label.

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:) ⇒ Label

Returns a new instance of Label.



6929
6930
6931
6932
6933
# File 'lib/syntax_tree/node.rb', line 6929

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

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



6927
6928
6929
# File 'lib/syntax_tree/node.rb', line 6927

def comments
  @comments
end

#valueObject (readonly)

String

the value of the label



6924
6925
6926
# File 'lib/syntax_tree/node.rb', line 6924

def value
  @value
end

Instance Method Details

#===(other) ⇒ Object



6964
6965
6966
# File 'lib/syntax_tree/node.rb', line 6964

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

#accept(visitor) ⇒ Object



6935
6936
6937
# File 'lib/syntax_tree/node.rb', line 6935

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

#child_nodesObject Also known as: deconstruct



6939
6940
6941
# File 'lib/syntax_tree/node.rb', line 6939

def child_nodes
  []
end

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



6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
# File 'lib/syntax_tree/node.rb', line 6943

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

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

#deconstruct_keys(_keys) ⇒ Object



6956
6957
6958
# File 'lib/syntax_tree/node.rb', line 6956

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

#format(q) ⇒ Object



6960
6961
6962
# File 'lib/syntax_tree/node.rb', line 6960

def format(q)
  q.text(value)
end