Class: SyntaxTree::Op

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



8270
8271
8272
8273
8274
# File 'lib/syntax_tree.rb', line 8270

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



8268
8269
8270
# File 'lib/syntax_tree.rb', line 8268

def comments
  @comments
end

#locationObject (readonly)

Location

the location of this node



8265
8266
8267
# File 'lib/syntax_tree.rb', line 8265

def location
  @location
end

#valueObject (readonly)

String

the operator



8262
8263
8264
# File 'lib/syntax_tree.rb', line 8262

def value
  @value
end

Instance Method Details

#child_nodesObject



8276
8277
8278
# File 'lib/syntax_tree.rb', line 8276

def child_nodes
  []
end

#format(q) ⇒ Object



8280
8281
8282
# File 'lib/syntax_tree.rb', line 8280

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

#pretty_print(q) ⇒ Object



8284
8285
8286
8287
8288
8289
8290
8291
8292
8293
# File 'lib/syntax_tree.rb', line 8284

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



8295
8296
8297
# File 'lib/syntax_tree.rb', line 8295

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