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.



582
583
584
585
586
587
# File 'lib/syntax_tree.rb', line 582

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



580
581
582
# File 'lib/syntax_tree.rb', line 580

def comments
  @comments
end

#lbraceObject (readonly)

LBrace

the left brace that is seen after the keyword



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

def lbrace
  @lbrace
end

#locationObject (readonly)

Location

the location of this node



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

def location
  @location
end

#statementsObject (readonly)

Statements

the expressions to be executed



574
575
576
# File 'lib/syntax_tree.rb', line 574

def statements
  @statements
end

Instance Method Details

#child_nodesObject



589
590
591
# File 'lib/syntax_tree.rb', line 589

def child_nodes
  [lbrace, statements]
end

#format(q) ⇒ Object



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

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



606
607
608
609
610
611
612
613
614
615
# File 'lib/syntax_tree.rb', line 606

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



617
618
619
620
621
622
623
624
625
# File 'lib/syntax_tree.rb', line 617

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