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.



5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
# File 'lib/syntax_tree/node.rb', line 5347

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



5345
5346
5347
# File 'lib/syntax_tree/node.rb', line 5345

def comments
  @comments
end

#consequentObject (readonly)

nil, Elsif, Else

the next clause in the chain



5342
5343
5344
# File 'lib/syntax_tree/node.rb', line 5342

def consequent
  @consequent
end

#predicateObject (readonly)

untyped

the expression to be checked



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

def predicate
  @predicate
end

#statementsObject (readonly)

Statements

the expressions to be executed



5339
5340
5341
# File 'lib/syntax_tree/node.rb', line 5339

def statements
  @statements
end

Instance Method Details

#accept(visitor) ⇒ Object



5361
5362
5363
# File 'lib/syntax_tree/node.rb', line 5361

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

#child_nodesObject Also known as: deconstruct



5365
5366
5367
# File 'lib/syntax_tree/node.rb', line 5365

def child_nodes
  [predicate, statements, consequent]
end

#deconstruct_keys(_keys) ⇒ Object



5371
5372
5373
5374
5375
5376
5377
5378
5379
# File 'lib/syntax_tree/node.rb', line 5371

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

#format(q) ⇒ Object



5381
5382
5383
# File 'lib/syntax_tree/node.rb', line 5381

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