Class: SyntaxTree::Begin

Inherits:
Object
  • Object
show all
Defined in:
lib/syntax_tree.rb

Overview

Begin represents a begin..end chain.

begin
  value
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(bodystmt:, location:, comments: []) ⇒ Begin

Returns a new instance of Begin.



2146
2147
2148
2149
2150
# File 'lib/syntax_tree.rb', line 2146

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

Instance Attribute Details

#bodystmtObject (readonly)

BodyStmt

the bodystmt that contains the contents of this begin block



2138
2139
2140
# File 'lib/syntax_tree.rb', line 2138

def bodystmt
  @bodystmt
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



2144
2145
2146
# File 'lib/syntax_tree.rb', line 2144

def comments
  @comments
end

#locationObject (readonly)

Location

the location of this node



2141
2142
2143
# File 'lib/syntax_tree.rb', line 2141

def location
  @location
end

Instance Method Details

#child_nodesObject



2152
2153
2154
# File 'lib/syntax_tree.rb', line 2152

def child_nodes
  [bodystmt]
end

#format(q) ⇒ Object



2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
# File 'lib/syntax_tree.rb', line 2156

def format(q)
  q.text("begin")

  unless bodystmt.empty?
    q.indent do
      q.breakable(force: true) unless bodystmt.statements.empty?
      q.format(bodystmt)
    end
  end

  q.breakable(force: true)
  q.text("end")
end

#pretty_print(q) ⇒ Object



2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
# File 'lib/syntax_tree.rb', line 2170

def pretty_print(q)
  q.group(2, "(", ")") do
    q.text("begin")

    q.breakable
    q.pp(bodystmt)

    q.pp(Comment::List.new(comments))
  end
end

#to_json(*opts) ⇒ Object



2181
2182
2183
2184
2185
2186
2187
2188
# File 'lib/syntax_tree.rb', line 2181

def to_json(*opts)
  {
    type: :begin,
    bodystmt: bodystmt,
    loc: location,
    cmts: comments
  }.to_json(*opts)
end