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.



2256
2257
2258
2259
2260
# File 'lib/syntax_tree.rb', line 2256

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



2248
2249
2250
# File 'lib/syntax_tree.rb', line 2248

def bodystmt
  @bodystmt
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



2254
2255
2256
# File 'lib/syntax_tree.rb', line 2254

def comments
  @comments
end

#locationObject (readonly)

Location

the location of this node



2251
2252
2253
# File 'lib/syntax_tree.rb', line 2251

def location
  @location
end

Instance Method Details

#child_nodesObject



2262
2263
2264
# File 'lib/syntax_tree.rb', line 2262

def child_nodes
  [bodystmt]
end

#format(q) ⇒ Object



2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
# File 'lib/syntax_tree.rb', line 2266

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



2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
# File 'lib/syntax_tree.rb', line 2280

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



2291
2292
2293
2294
2295
2296
2297
2298
# File 'lib/syntax_tree.rb', line 2291

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