Class: SyntaxTree::Kw

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

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Kw.



7543
7544
7545
7546
7547
# File 'lib/syntax_tree.rb', line 7543

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



7541
7542
7543
# File 'lib/syntax_tree.rb', line 7541

def comments
  @comments
end

#locationObject (readonly)

Location

the location of this node



7538
7539
7540
# File 'lib/syntax_tree.rb', line 7538

def location
  @location
end

#valueObject (readonly)

String

the value of the keyword



7535
7536
7537
# File 'lib/syntax_tree.rb', line 7535

def value
  @value
end

Instance Method Details

#child_nodesObject



7549
7550
7551
# File 'lib/syntax_tree.rb', line 7549

def child_nodes
  []
end

#format(q) ⇒ Object



7553
7554
7555
# File 'lib/syntax_tree.rb', line 7553

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

#pretty_print(q) ⇒ Object



7557
7558
7559
7560
7561
7562
7563
7564
7565
7566
# File 'lib/syntax_tree.rb', line 7557

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

    q.breakable
    q.pp(value)

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

#to_json(*opts) ⇒ Object



7568
7569
7570
# File 'lib/syntax_tree.rb', line 7568

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