Class: SyntaxTree::Kw

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

Overview

Kw represents the use of a keyword. It can be almost anywhere in the syntax tree, so you end up seeing it quite a lot.

if value
end

In the above example, there would be two Kw nodes: one for the if and one for the end. Note that anything that matches the list of keywords in Ruby will use a Kw, so if you use a keyword in a symbol literal for instance:

:if

then the contents of the symbol node will contain a Kw node.

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

Returns a new instance of Kw.



6818
6819
6820
6821
6822
6823
# File 'lib/syntax_tree/node.rb', line 6818

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

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



6816
6817
6818
# File 'lib/syntax_tree/node.rb', line 6816

def comments
  @comments
end

#nameObject (readonly)

Symbol

the symbol version of the value



6813
6814
6815
# File 'lib/syntax_tree/node.rb', line 6813

def name
  @name
end

#valueObject (readonly)

String

the value of the keyword



6810
6811
6812
# File 'lib/syntax_tree/node.rb', line 6810

def value
  @value
end

Instance Method Details

#===(other) ⇒ Object



6851
6852
6853
# File 'lib/syntax_tree/node.rb', line 6851

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

#accept(visitor) ⇒ Object



6825
6826
6827
# File 'lib/syntax_tree/node.rb', line 6825

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

#child_nodesObject Also known as: deconstruct



6829
6830
6831
# File 'lib/syntax_tree/node.rb', line 6829

def child_nodes
  []
end

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



6833
6834
6835
6836
6837
6838
6839
# File 'lib/syntax_tree/node.rb', line 6833

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

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

#deconstruct_keys(_keys) ⇒ Object



6843
6844
6845
# File 'lib/syntax_tree/node.rb', line 6843

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

#format(q) ⇒ Object



6847
6848
6849
# File 'lib/syntax_tree/node.rb', line 6847

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