Class: SyntaxTree::BEGINBlock
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
-
#comments ⇒ Object
readonly
- Array[ Comment | EmbDoc ]
-
the comments attached to this node.
-
#lbrace ⇒ Object
readonly
- LBrace
-
the left brace that is seen after the keyword.
-
#statements ⇒ Object
readonly
- Statements
-
the expressions to be executed.
Attributes inherited from Node
Instance Method Summary collapse
- #===(other) ⇒ Object
- #accept(visitor) ⇒ Object
- #child_nodes ⇒ Object (also: #deconstruct)
- #copy(lbrace: nil, statements: nil, location: nil) ⇒ Object
- #deconstruct_keys(_keys) ⇒ Object
- #format(q) ⇒ Object
-
#initialize(lbrace:, statements:, location:) ⇒ BEGINBlock
constructor
A new instance of BEGINBlock.
Methods inherited from Node
#construct_keys, #pretty_print, #to_json
Constructor Details
#initialize(lbrace:, statements:, location:) ⇒ BEGINBlock
Returns a new instance of BEGINBlock.
175 176 177 178 179 180 |
# File 'lib/syntax_tree/node.rb', line 175 def initialize(lbrace:, statements:, location:) @lbrace = lbrace @statements = statements @location = location @comments = [] end |
Instance Attribute Details
#comments ⇒ Object (readonly)
- Array[ Comment | EmbDoc ]
-
the comments attached to this node
173 174 175 |
# File 'lib/syntax_tree/node.rb', line 173 def comments @comments end |
#lbrace ⇒ Object (readonly)
- LBrace
-
the left brace that is seen after the keyword
167 168 169 |
# File 'lib/syntax_tree/node.rb', line 167 def lbrace @lbrace end |
#statements ⇒ Object (readonly)
- Statements
-
the expressions to be executed
170 171 172 |
# File 'lib/syntax_tree/node.rb', line 170 def statements @statements end |
Instance Method Details
#===(other) ⇒ Object
226 227 228 229 |
# File 'lib/syntax_tree/node.rb', line 226 def ===(other) other.is_a?(BEGINBlock) && lbrace === other.lbrace && statements === other.statements end |
#accept(visitor) ⇒ Object
182 183 184 |
# File 'lib/syntax_tree/node.rb', line 182 def accept(visitor) visitor.visit_BEGIN(self) end |
#child_nodes ⇒ Object Also known as: deconstruct
186 187 188 |
# File 'lib/syntax_tree/node.rb', line 186 def child_nodes [lbrace, statements] end |
#copy(lbrace: nil, statements: nil, location: nil) ⇒ Object
190 191 192 193 194 195 196 197 198 199 200 |
# File 'lib/syntax_tree/node.rb', line 190 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
204 205 206 207 208 209 210 211 |
# File 'lib/syntax_tree/node.rb', line 204 def deconstruct_keys(_keys) { lbrace: lbrace, statements: statements, location: location, comments: comments } end |
#format(q) ⇒ Object
213 214 215 216 217 218 219 220 221 222 223 224 |
# File 'lib/syntax_tree/node.rb', line 213 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 |