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

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of BEGINBlock.



103
104
105
106
107
108
# File 'lib/syntax_tree/node.rb', line 103

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



101
102
103
# File 'lib/syntax_tree/node.rb', line 101

def comments
  @comments
end

#lbraceObject (readonly)

LBrace

the left brace that is seen after the keyword



92
93
94
# File 'lib/syntax_tree/node.rb', line 92

def lbrace
  @lbrace
end

#locationObject (readonly)

Location

the location of this node



98
99
100
# File 'lib/syntax_tree/node.rb', line 98

def location
  @location
end

#statementsObject (readonly)

Statements

the expressions to be executed



95
96
97
# File 'lib/syntax_tree/node.rb', line 95

def statements
  @statements
end

Instance Method Details

#child_nodesObject Also known as: deconstruct



110
111
112
# File 'lib/syntax_tree/node.rb', line 110

def child_nodes
  [lbrace, statements]
end

#deconstruct_keys(keys) ⇒ Object



116
117
118
119
120
121
122
123
# File 'lib/syntax_tree/node.rb', line 116

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

#format(q) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/syntax_tree/node.rb', line 125

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



138
139
140
141
142
143
144
145
146
147
# File 'lib/syntax_tree/node.rb', line 138

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



149
150
151
152
153
154
155
156
157
# File 'lib/syntax_tree/node.rb', line 149

def to_json(*opts)
  {
    type: :BEGIN,
    lbrace: lbrace,
    stmts: statements,
    loc: location,
    cmts: comments
  }.to_json(*opts)
end