Class: SyntaxTree::BEGINBlock

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

Attributes inherited from Node

#location

Instance Method Summary collapse

Methods inherited from Node

#construct_keys, #end_char, #pretty_print, #start_char, #to_json, #to_mermaid

Constructor Details

#initialize(lbrace:, statements:, location:) ⇒ BEGINBlock

Returns a new instance of BEGINBlock.



185
186
187
188
189
190
# File 'lib/syntax_tree/node.rb', line 185

def initialize(lbrace:, statements:, location:)
  @lbrace = lbrace
  @statements = statements
  @location = location
  @comments = []
end

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



183
184
185
# File 'lib/syntax_tree/node.rb', line 183

def comments
  @comments
end

#lbraceObject (readonly)

LBrace

the left brace that is seen after the keyword



177
178
179
# File 'lib/syntax_tree/node.rb', line 177

def lbrace
  @lbrace
end

#statementsObject (readonly)

Statements

the expressions to be executed



180
181
182
# File 'lib/syntax_tree/node.rb', line 180

def statements
  @statements
end

Instance Method Details

#===(other) ⇒ Object



236
237
238
239
# File 'lib/syntax_tree/node.rb', line 236

def ===(other)
  other.is_a?(BEGINBlock) && lbrace === other.lbrace &&
    statements === other.statements
end

#accept(visitor) ⇒ Object



192
193
194
# File 'lib/syntax_tree/node.rb', line 192

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

#child_nodesObject Also known as: deconstruct



196
197
198
# File 'lib/syntax_tree/node.rb', line 196

def child_nodes
  [lbrace, statements]
end

#copy(lbrace: nil, statements: nil, location: nil) ⇒ Object



200
201
202
203
204
205
206
207
208
209
210
# File 'lib/syntax_tree/node.rb', line 200

def copy(lbrace: nil, statements: nil, location: nil)
  node =
    BEGINBlock.new(
      lbrace: lbrace || self.lbrace,
      statements: statements || self.statements,
      location: location || self.location
    )

  node.comments.concat(comments.map(&:copy))
  node
end

#deconstruct_keys(_keys) ⇒ Object



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

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

#format(q) ⇒ Object



223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/syntax_tree/node.rb', line 223

def format(q)
  q.group do
    q.text("BEGIN ")
    q.format(lbrace)
    q.indent do
      q.breakable_space
      q.format(statements)
    end
    q.breakable_space
    q.text("}")
  end
end