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.



2301
2302
2303
2304
2305
# File 'lib/syntax_tree.rb', line 2301

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



2293
2294
2295
# File 'lib/syntax_tree.rb', line 2293

def bodystmt
  @bodystmt
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



2299
2300
2301
# File 'lib/syntax_tree.rb', line 2299

def comments
  @comments
end

#locationObject (readonly)

Location

the location of this node



2296
2297
2298
# File 'lib/syntax_tree.rb', line 2296

def location
  @location
end

Instance Method Details

#child_nodesObject



2307
2308
2309
# File 'lib/syntax_tree.rb', line 2307

def child_nodes
  [bodystmt]
end

#format(q) ⇒ Object



2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
# File 'lib/syntax_tree.rb', line 2311

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



2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
# File 'lib/syntax_tree.rb', line 2325

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



2336
2337
2338
2339
2340
2341
2342
2343
# File 'lib/syntax_tree.rb', line 2336

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