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.



7001
7002
7003
7004
7005
# File 'lib/syntax_tree/node.rb', line 7001

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

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



6999
7000
7001
# File 'lib/syntax_tree/node.rb', line 6999

def comments
  @comments
end

#valueObject (readonly)

String

the value of the label



6996
6997
6998
# File 'lib/syntax_tree/node.rb', line 6996

def value
  @value
end

Instance Method Details

#===(other) ⇒ Object



7036
7037
7038
# File 'lib/syntax_tree/node.rb', line 7036

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

#accept(visitor) ⇒ Object



7007
7008
7009
# File 'lib/syntax_tree/node.rb', line 7007

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

#child_nodesObject Also known as: deconstruct



7011
7012
7013
# File 'lib/syntax_tree/node.rb', line 7011

def child_nodes
  []
end

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



7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
# File 'lib/syntax_tree/node.rb', line 7015

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



7028
7029
7030
# File 'lib/syntax_tree/node.rb', line 7028

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

#format(q) ⇒ Object



7032
7033
7034
# File 'lib/syntax_tree/node.rb', line 7032

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