Class: SyntaxTree::Op

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

Overview

Op represents an operator literal in the source.

1 + 2

In the example above, the Op node represents the + operator.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Op.



7582
7583
7584
7585
7586
# File 'lib/syntax_tree/node.rb', line 7582

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



7580
7581
7582
# File 'lib/syntax_tree/node.rb', line 7580

def comments
  @comments
end

#locationObject (readonly)

Location

the location of this node



7577
7578
7579
# File 'lib/syntax_tree/node.rb', line 7577

def location
  @location
end

#valueObject (readonly)

String

the operator



7574
7575
7576
# File 'lib/syntax_tree/node.rb', line 7574

def value
  @value
end

Instance Method Details

#child_nodesObject Also known as: deconstruct



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

def child_nodes
  []
end

#deconstruct_keys(keys) ⇒ Object



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

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

#format(q) ⇒ Object



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

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

#pretty_print(q) ⇒ Object



7602
7603
7604
7605
7606
7607
7608
7609
7610
7611
# File 'lib/syntax_tree/node.rb', line 7602

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

    q.breakable
    q.pp(value)

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

#to_json(*opts) ⇒ Object



7613
7614
7615
# File 'lib/syntax_tree/node.rb', line 7613

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