Class: SyntaxTree::ENDBlock

Inherits:
Node
  • Object
show all
Defined in:
lib/syntax_tree/node.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

Attributes inherited from Node

#location

Instance Method Summary collapse

Methods inherited from Node

#pretty_print, #to_json

Constructor Details

#initialize(lbrace:, statements:, location:, comments: []) ⇒ ENDBlock

Returns a new instance of ENDBlock.



214
215
216
217
218
219
# File 'lib/syntax_tree/node.rb', line 214

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



212
213
214
# File 'lib/syntax_tree/node.rb', line 212

def comments
  @comments
end

#lbraceObject (readonly)

LBrace

the left brace that is seen after the keyword



206
207
208
# File 'lib/syntax_tree/node.rb', line 206

def lbrace
  @lbrace
end

#statementsObject (readonly)

Statements

the expressions to be executed



209
210
211
# File 'lib/syntax_tree/node.rb', line 209

def statements
  @statements
end

Instance Method Details

#accept(visitor) ⇒ Object



221
222
223
# File 'lib/syntax_tree/node.rb', line 221

def accept(visitor)
  visitor.visit_END(self)
end

#child_nodesObject Also known as: deconstruct



225
226
227
# File 'lib/syntax_tree/node.rb', line 225

def child_nodes
  [lbrace, statements]
end

#deconstruct_keys(keys) ⇒ Object



231
232
233
234
235
236
237
238
# File 'lib/syntax_tree/node.rb', line 231

def deconstruct_keys(keys)
  {
    lbrace: lbrace,
    statements: statements,
    location: location,
    comments: comments
  }
end

#format(q) ⇒ Object



240
241
242
243
244
245
246
247
248
249
250
251
# File 'lib/syntax_tree/node.rb', line 240

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