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.



8683
8684
8685
8686
8687
# File 'lib/syntax_tree.rb', line 8683

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



8681
8682
8683
# File 'lib/syntax_tree.rb', line 8681

def comments
  @comments
end

#locationObject (readonly)

Location

the location of this node



8678
8679
8680
# File 'lib/syntax_tree.rb', line 8678

def location
  @location
end

#valueObject (readonly)

String

the operator



8675
8676
8677
# File 'lib/syntax_tree.rb', line 8675

def value
  @value
end

Instance Method Details

#child_nodesObject



8689
8690
8691
# File 'lib/syntax_tree.rb', line 8689

def child_nodes
  []
end

#format(q) ⇒ Object



8693
8694
8695
# File 'lib/syntax_tree.rb', line 8693

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

#pretty_print(q) ⇒ Object



8697
8698
8699
8700
8701
8702
8703
8704
8705
8706
# File 'lib/syntax_tree.rb', line 8697

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



8708
8709
8710
# File 'lib/syntax_tree.rb', line 8708

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