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, #end_char, #pretty_print, #start_char, #to_json, #to_mermaid

Constructor Details

#initialize(value:, location:) ⇒ Label

Returns a new instance of Label.



7059
7060
7061
7062
7063
# File 'lib/syntax_tree/node.rb', line 7059

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

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



7057
7058
7059
# File 'lib/syntax_tree/node.rb', line 7057

def comments
  @comments
end

#valueObject (readonly)

String

the value of the label



7054
7055
7056
# File 'lib/syntax_tree/node.rb', line 7054

def value
  @value
end

Instance Method Details

#===(other) ⇒ Object



7094
7095
7096
# File 'lib/syntax_tree/node.rb', line 7094

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

#accept(visitor) ⇒ Object



7065
7066
7067
# File 'lib/syntax_tree/node.rb', line 7065

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

#child_nodesObject Also known as: deconstruct



7069
7070
7071
# File 'lib/syntax_tree/node.rb', line 7069

def child_nodes
  []
end

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



7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
# File 'lib/syntax_tree/node.rb', line 7073

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



7086
7087
7088
# File 'lib/syntax_tree/node.rb', line 7086

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

#format(q) ⇒ Object



7090
7091
7092
# File 'lib/syntax_tree/node.rb', line 7090

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