Class: SyntaxTree::While

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

Overview

While represents a while loop.

while predicate
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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



13352
13353
13354
13355
13356
13357
# File 'lib/syntax_tree.rb', line 13352

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

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



13350
13351
13352
# File 'lib/syntax_tree.rb', line 13350

def comments
  @comments
end

#locationObject (readonly)

Location

the location of this node



13347
13348
13349
# File 'lib/syntax_tree.rb', line 13347

def location
  @location
end

#predicateObject (readonly)

untyped

the expression to be checked



13341
13342
13343
# File 'lib/syntax_tree.rb', line 13341

def predicate
  @predicate
end

#statementsObject (readonly)

Statements

the expressions to be executed



13344
13345
13346
# File 'lib/syntax_tree.rb', line 13344

def statements
  @statements
end

Instance Method Details

#child_nodesObject



13359
13360
13361
# File 'lib/syntax_tree.rb', line 13359

def child_nodes
  [predicate, statements]
end

#format(q) ⇒ Object



13363
13364
13365
13366
13367
13368
13369
13370
13371
13372
13373
13374
13375
13376
# File 'lib/syntax_tree.rb', line 13363

def format(q)
  if statements.empty?
    keyword = "while "

    q.group do
      q.text(keyword)
      q.nest(keyword.length) { q.format(predicate) }
      q.breakable(force: true)
      q.text("end")
    end
  else
    LoopFormatter.new("while", self, statements).format(q)
  end
end

#pretty_print(q) ⇒ Object



13378
13379
13380
13381
13382
13383
13384
13385
13386
13387
13388
13389
13390
# File 'lib/syntax_tree.rb', line 13378

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

    q.breakable
    q.pp(predicate)

    q.breakable
    q.pp(statements)

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

#to_json(*opts) ⇒ Object



13392
13393
13394
13395
13396
13397
13398
13399
13400
# File 'lib/syntax_tree.rb', line 13392

def to_json(*opts)
  {
    type: :while,
    pred: predicate,
    stmts: statements,
    loc: location,
    cmts: comments
  }.to_json(*opts)
end