Class: SyntaxTree::If

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

Overview

If represents the first clause in an if chain.

if predicate
end

Instance Attribute Summary collapse

Attributes inherited from Node

#location

Instance Method Summary collapse

Methods inherited from Node

#construct_keys, #pretty_print, #to_json

Constructor Details

#initialize(predicate:, statements:, consequent:, location:, comments: []) ⇒ If

Returns a new instance of If.



5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
# File 'lib/syntax_tree/node.rb', line 5303

def initialize(
  predicate:,
  statements:,
  consequent:,
  location:,
  comments: []
)
  @predicate = predicate
  @statements = statements
  @consequent = consequent
  @location = location
  @comments = comments
end

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



5301
5302
5303
# File 'lib/syntax_tree/node.rb', line 5301

def comments
  @comments
end

#consequentObject (readonly)

nil, Elsif, Else

the next clause in the chain



5298
5299
5300
# File 'lib/syntax_tree/node.rb', line 5298

def consequent
  @consequent
end

#predicateObject (readonly)

untyped

the expression to be checked



5292
5293
5294
# File 'lib/syntax_tree/node.rb', line 5292

def predicate
  @predicate
end

#statementsObject (readonly)

Statements

the expressions to be executed



5295
5296
5297
# File 'lib/syntax_tree/node.rb', line 5295

def statements
  @statements
end

Instance Method Details

#accept(visitor) ⇒ Object



5317
5318
5319
# File 'lib/syntax_tree/node.rb', line 5317

def accept(visitor)
  visitor.visit_if(self)
end

#child_nodesObject Also known as: deconstruct



5321
5322
5323
# File 'lib/syntax_tree/node.rb', line 5321

def child_nodes
  [predicate, statements, consequent]
end

#deconstruct_keys(_keys) ⇒ Object



5327
5328
5329
5330
5331
5332
5333
5334
5335
# File 'lib/syntax_tree/node.rb', line 5327

def deconstruct_keys(_keys)
  {
    predicate: predicate,
    statements: statements,
    consequent: consequent,
    location: location,
    comments: comments
  }
end

#format(q) ⇒ Object



5337
5338
5339
# File 'lib/syntax_tree/node.rb', line 5337

def format(q)
  ConditionalFormatter.new("if", self).format(q)
end