Class: SyntaxTree::BEGINBlock
- Inherits:
-
Object
- Object
- SyntaxTree::BEGINBlock
- Defined in:
- lib/syntax_tree.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
-
#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.
-
#location ⇒ Object
readonly
- Location
-
the location of this node.
-
#statements ⇒ Object
readonly
- Statements
-
the expressions to be executed.
Instance Method Summary collapse
- #child_nodes ⇒ Object
- #format(q) ⇒ Object
-
#initialize(lbrace:, statements:, location:, comments: []) ⇒ BEGINBlock
constructor
A new instance of BEGINBlock.
- #pretty_print(q) ⇒ Object
- #to_json(*opts) ⇒ Object
Constructor Details
#initialize(lbrace:, statements:, location:, comments: []) ⇒ BEGINBlock
Returns a new instance of BEGINBlock.
431 432 433 434 435 436 |
# File 'lib/syntax_tree.rb', line 431 def initialize(lbrace:, statements:, location:, comments: []) @lbrace = lbrace @statements = statements @location = location @comments = comments end |
Instance Attribute Details
#comments ⇒ Object (readonly)
- Array[ Comment | EmbDoc ]
-
the comments attached to this node
429 430 431 |
# File 'lib/syntax_tree.rb', line 429 def comments @comments end |
#lbrace ⇒ Object (readonly)
- LBrace
-
the left brace that is seen after the keyword
420 421 422 |
# File 'lib/syntax_tree.rb', line 420 def lbrace @lbrace end |
#location ⇒ Object (readonly)
- Location
-
the location of this node
426 427 428 |
# File 'lib/syntax_tree.rb', line 426 def location @location end |
#statements ⇒ Object (readonly)
- Statements
-
the expressions to be executed
423 424 425 |
# File 'lib/syntax_tree.rb', line 423 def statements @statements end |
Instance Method Details
#child_nodes ⇒ Object
438 439 440 |
# File 'lib/syntax_tree.rb', line 438 def child_nodes [lbrace, statements] end |
#format(q) ⇒ Object
442 443 444 445 446 447 448 449 450 451 452 453 |
# File 'lib/syntax_tree.rb', line 442 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 |
#pretty_print(q) ⇒ Object
455 456 457 458 459 460 461 462 463 464 |
# File 'lib/syntax_tree.rb', line 455 def pretty_print(q) q.group(2, "(", ")") do q.text("BEGIN") q.breakable q.pp(statements) q.pp(Comment::List.new(comments)) end end |
#to_json(*opts) ⇒ Object
466 467 468 469 470 471 472 473 474 |
# File 'lib/syntax_tree.rb', line 466 def to_json(*opts) { type: :BEGIN, lbrace: lbrace, stmts: statements, loc: location, cmts: comments }.to_json(*opts) end |