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.



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

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



567
568
569
# File 'lib/syntax_tree.rb', line 567

def comments
  @comments
end

#lbraceObject (readonly)

LBrace

the left brace that is seen after the keyword



558
559
560
# File 'lib/syntax_tree.rb', line 558

def lbrace
  @lbrace
end

#locationObject (readonly)

Location

the location of this node



564
565
566
# File 'lib/syntax_tree.rb', line 564

def location
  @location
end

#statementsObject (readonly)

Statements

the expressions to be executed



561
562
563
# File 'lib/syntax_tree.rb', line 561

def statements
  @statements
end

Instance Method Details

#child_nodesObject



576
577
578
# File 'lib/syntax_tree.rb', line 576

def child_nodes
  [lbrace, statements]
end

#format(q) ⇒ Object



580
581
582
583
584
585
586
587
588
589
590
591
# File 'lib/syntax_tree.rb', line 580

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



593
594
595
596
597
598
599
600
601
602
# File 'lib/syntax_tree.rb', line 593

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



604
605
606
607
608
609
610
611
612
# File 'lib/syntax_tree.rb', line 604

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