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

Instance Method Summary collapse

Constructor Details

#initialize(value:, location:, comments: []) ⇒ Label

Returns a new instance of Label.



6772
6773
6774
6775
6776
# File 'lib/syntax_tree/node.rb', line 6772

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

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



6770
6771
6772
# File 'lib/syntax_tree/node.rb', line 6770

def comments
  @comments
end

#locationObject (readonly)

Location

the location of this node



6767
6768
6769
# File 'lib/syntax_tree/node.rb', line 6767

def location
  @location
end

#valueObject (readonly)

String

the value of the label



6764
6765
6766
# File 'lib/syntax_tree/node.rb', line 6764

def value
  @value
end

Instance Method Details

#child_nodesObject Also known as: deconstruct



6778
6779
6780
# File 'lib/syntax_tree/node.rb', line 6778

def child_nodes
  []
end

#deconstruct_keys(keys) ⇒ Object



6784
6785
6786
# File 'lib/syntax_tree/node.rb', line 6784

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

#format(q) ⇒ Object



6788
6789
6790
# File 'lib/syntax_tree/node.rb', line 6788

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

#pretty_print(q) ⇒ Object



6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
# File 'lib/syntax_tree/node.rb', line 6792

def pretty_print(q)
  q.group(2, "(", ")") do
    q.text("label")

    q.breakable
    q.text(":")
    q.text(value[0...-1])

    q.pp(Comment::List.new(comments))
  end
end

#to_json(*opts) ⇒ Object



6804
6805
6806
6807
6808
# File 'lib/syntax_tree/node.rb', line 6804

def to_json(*opts)
  { type: :label, value: value, loc: location, cmts: comments }.to_json(
    *opts
  )
end