Class: SyntaxTree::ENDBlock

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

Overview

ENDBlock represents the use of the END keyword, which hooks into the lifecycle of the interpreter. Whatever is inside the block will get executed when the program ends.

END {
}

Interestingly, the END 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: []) ⇒ ENDBlock

Returns a new instance of ENDBlock.



533
534
535
536
537
538
# File 'lib/syntax_tree.rb', line 533

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



531
532
533
# File 'lib/syntax_tree.rb', line 531

def comments
  @comments
end

#lbraceObject (readonly)

LBrace

the left brace that is seen after the keyword



522
523
524
# File 'lib/syntax_tree.rb', line 522

def lbrace
  @lbrace
end

#locationObject (readonly)

Location

the location of this node



528
529
530
# File 'lib/syntax_tree.rb', line 528

def location
  @location
end

#statementsObject (readonly)

Statements

the expressions to be executed



525
526
527
# File 'lib/syntax_tree.rb', line 525

def statements
  @statements
end

Instance Method Details

#child_nodesObject



540
541
542
# File 'lib/syntax_tree.rb', line 540

def child_nodes
  [lbrace, statements]
end

#format(q) ⇒ Object



544
545
546
547
548
549
550
551
552
553
554
555
# File 'lib/syntax_tree.rb', line 544

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

#pretty_print(q) ⇒ Object



557
558
559
560
561
562
563
564
565
566
# File 'lib/syntax_tree.rb', line 557

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

    q.breakable
    q.pp(statements)

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

#to_json(*opts) ⇒ Object



568
569
570
571
572
573
574
575
576
# File 'lib/syntax_tree.rb', line 568

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