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.



431
432
433
434
435
436
# File 'lib/syntax_tree.rb', line 431

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



429
430
431
# File 'lib/syntax_tree.rb', line 429

def comments
  @comments
end

#lbraceObject (readonly)

LBrace

the left brace that is seen after the keyword



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

def lbrace
  @lbrace
end

#locationObject (readonly)

Location

the location of this node



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

def location
  @location
end

#statementsObject (readonly)

Statements

the expressions to be executed



423
424
425
# File 'lib/syntax_tree.rb', line 423

def statements
  @statements
end

Instance Method Details

#child_nodesObject



438
439
440
# File 'lib/syntax_tree.rb', line 438

def child_nodes
  [lbrace, statements]
end

#format(q) ⇒ Object



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

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



455
456
457
458
459
460
461
462
463
464
# File 'lib/syntax_tree.rb', line 455

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



466
467
468
469
470
471
472
473
474
# File 'lib/syntax_tree.rb', line 466

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