Class: SyntaxTree::Label

Inherits:
Object
  • Object
show all
Defined in:
lib/syntax_tree.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.



7596
7597
7598
7599
7600
# File 'lib/syntax_tree.rb', line 7596

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



7594
7595
7596
# File 'lib/syntax_tree.rb', line 7594

def comments
  @comments
end

#locationObject (readonly)

Location

the location of this node



7591
7592
7593
# File 'lib/syntax_tree.rb', line 7591

def location
  @location
end

#valueObject (readonly)

String

the value of the label



7588
7589
7590
# File 'lib/syntax_tree.rb', line 7588

def value
  @value
end

Instance Method Details

#child_nodesObject



7602
7603
7604
# File 'lib/syntax_tree.rb', line 7602

def child_nodes
  []
end

#format(q) ⇒ Object



7606
7607
7608
# File 'lib/syntax_tree.rb', line 7606

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

#pretty_print(q) ⇒ Object



7610
7611
7612
7613
7614
7615
7616
7617
7618
7619
7620
# File 'lib/syntax_tree.rb', line 7610

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



7622
7623
7624
7625
7626
# File 'lib/syntax_tree.rb', line 7622

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