Class: SyntaxTree::Unary

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

Overview

Unary represents a unary method being called on an expression, as in ! or ~.

!value

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(operator:, statement:, location:, comments: []) ⇒ Unary

Returns a new instance of Unary.



12147
12148
12149
12150
12151
12152
# File 'lib/syntax_tree.rb', line 12147

def initialize(operator:, statement:, location:, comments: [])
  @operator = operator
  @statement = statement
  @location = location
  @comments = comments
end

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



12145
12146
12147
# File 'lib/syntax_tree.rb', line 12145

def comments
  @comments
end

#locationObject (readonly)

Location

the location of this node



12142
12143
12144
# File 'lib/syntax_tree.rb', line 12142

def location
  @location
end

#operatorObject (readonly)

String

the operator being used



12136
12137
12138
# File 'lib/syntax_tree.rb', line 12136

def operator
  @operator
end

#statementObject (readonly)

untyped

the statement on which to operate



12139
12140
12141
# File 'lib/syntax_tree.rb', line 12139

def statement
  @statement
end

Instance Method Details

#child_nodesObject



12154
12155
12156
# File 'lib/syntax_tree.rb', line 12154

def child_nodes
  [statement]
end

#format(q) ⇒ Object



12158
12159
12160
12161
# File 'lib/syntax_tree.rb', line 12158

def format(q)
  q.text(operator)
  q.format(statement)
end

#pretty_print(q) ⇒ Object



12163
12164
12165
12166
12167
12168
12169
12170
12171
12172
12173
12174
12175
# File 'lib/syntax_tree.rb', line 12163

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

    q.breakable
    q.pp(operator)

    q.breakable
    q.pp(statement)

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

#to_json(*opts) ⇒ Object



12177
12178
12179
12180
12181
12182
12183
12184
12185
# File 'lib/syntax_tree.rb', line 12177

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