Class: SyntaxTree::BEGINBlock

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

Overview

BEGINBlock represents the use of the BEGIN keyword, which hooks into the lifecycle of the interpreter. Whatever is inside the block will get executed when the program starts.

BEGIN {
}

Interestingly, the BEGIN keyword doesn’t allow the do and end keywords for the block. Only braces are permitted.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(lbrace:, statements:, location:, comments: []) ⇒ BEGINBlock

Returns a new instance of BEGINBlock.



418
419
420
421
422
423
# File 'lib/syntax_tree.rb', line 418

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

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



416
417
418
# File 'lib/syntax_tree.rb', line 416

def comments
  @comments
end

#lbraceObject (readonly)

LBrace

the left brace that is seen after the keyword



407
408
409
# File 'lib/syntax_tree.rb', line 407

def lbrace
  @lbrace
end

#locationObject (readonly)

Location

the location of this node



413
414
415
# File 'lib/syntax_tree.rb', line 413

def location
  @location
end

#statementsObject (readonly)

Statements

the expressions to be executed



410
411
412
# File 'lib/syntax_tree.rb', line 410

def statements
  @statements
end

Instance Method Details

#child_nodesObject



425
426
427
# File 'lib/syntax_tree.rb', line 425

def child_nodes
  [lbrace, statements]
end

#format(q) ⇒ Object



429
430
431
432
433
434
435
436
437
438
439
440
# File 'lib/syntax_tree.rb', line 429

def format(q)
  q.group do
    q.text("BEGIN ")
    q.format(lbrace)
    q.indent do
      q.breakable
      q.format(statements)
    end
    q.breakable
    q.text("}")
  end
end

#pretty_print(q) ⇒ Object



442
443
444
445
446
447
448
449
450
451
# File 'lib/syntax_tree.rb', line 442

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

    q.breakable
    q.pp(statements)

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

#to_json(*opts) ⇒ Object



453
454
455
456
457
458
459
460
461
# File 'lib/syntax_tree.rb', line 453

def to_json(*opts)
  {
    type: :BEGIN,
    lbrace: lbrace,
    stmts: statements,
    loc: location,
    cmts: comments
  }.to_json(*opts)
end