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



382
383
384
385
386
387
# File 'lib/syntax_tree.rb', line 382

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



380
381
382
# File 'lib/syntax_tree.rb', line 380

def comments
  @comments
end

#lbraceObject (readonly)

LBrace

the left brace that is seen after the keyword



371
372
373
# File 'lib/syntax_tree.rb', line 371

def lbrace
  @lbrace
end

#locationObject (readonly)

Location

the location of this node



377
378
379
# File 'lib/syntax_tree.rb', line 377

def location
  @location
end

#statementsObject (readonly)

Statements

the expressions to be executed



374
375
376
# File 'lib/syntax_tree.rb', line 374

def statements
  @statements
end

Instance Method Details

#child_nodesObject



389
390
391
# File 'lib/syntax_tree.rb', line 389

def child_nodes
  [lbrace, statements]
end

#format(q) ⇒ Object



393
394
395
396
397
398
399
400
401
402
403
404
# File 'lib/syntax_tree.rb', line 393

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



406
407
408
409
410
411
412
413
414
415
# File 'lib/syntax_tree.rb', line 406

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



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

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