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

#pretty_print, #to_json

Constructor Details

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

Returns a new instance of BEGINBlock.



126
127
128
129
130
131
# File 'lib/syntax_tree/node.rb', line 126

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



124
125
126
# File 'lib/syntax_tree/node.rb', line 124

def comments
  @comments
end

#lbraceObject (readonly)

LBrace

the left brace that is seen after the keyword



118
119
120
# File 'lib/syntax_tree/node.rb', line 118

def lbrace
  @lbrace
end

#statementsObject (readonly)

Statements

the expressions to be executed



121
122
123
# File 'lib/syntax_tree/node.rb', line 121

def statements
  @statements
end

Instance Method Details

#accept(visitor) ⇒ Object



133
134
135
# File 'lib/syntax_tree/node.rb', line 133

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

#child_nodesObject Also known as: deconstruct



137
138
139
# File 'lib/syntax_tree/node.rb', line 137

def child_nodes
  [lbrace, statements]
end

#deconstruct_keys(keys) ⇒ Object



143
144
145
146
147
148
149
150
# File 'lib/syntax_tree/node.rb', line 143

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

#format(q) ⇒ Object



152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/syntax_tree/node.rb', line 152

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