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

Attributes inherited from Node

#location

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Op.



7312
7313
7314
7315
7316
# File 'lib/syntax_tree/node.rb', line 7312

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



7310
7311
7312
# File 'lib/syntax_tree/node.rb', line 7310

def comments
  @comments
end

#valueObject (readonly)

String

the operator



7307
7308
7309
# File 'lib/syntax_tree/node.rb', line 7307

def value
  @value
end

Instance Method Details

#child_nodesObject Also known as: deconstruct



7318
7319
7320
# File 'lib/syntax_tree/node.rb', line 7318

def child_nodes
  []
end

#deconstruct_keys(keys) ⇒ Object



7324
7325
7326
# File 'lib/syntax_tree/node.rb', line 7324

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

#format(q) ⇒ Object



7328
7329
7330
# File 'lib/syntax_tree/node.rb', line 7328

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

#pretty_print(q) ⇒ Object



7332
7333
7334
7335
7336
7337
7338
7339
7340
7341
# File 'lib/syntax_tree/node.rb', line 7332

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



7343
7344
7345
# File 'lib/syntax_tree/node.rb', line 7343

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