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.



2273
2274
2275
2276
2277
# File 'lib/syntax_tree.rb', line 2273

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



2265
2266
2267
# File 'lib/syntax_tree.rb', line 2265

def bodystmt
  @bodystmt
end

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



2271
2272
2273
# File 'lib/syntax_tree.rb', line 2271

def comments
  @comments
end

#locationObject (readonly)

Location

the location of this node



2268
2269
2270
# File 'lib/syntax_tree.rb', line 2268

def location
  @location
end

Instance Method Details

#child_nodesObject



2279
2280
2281
# File 'lib/syntax_tree.rb', line 2279

def child_nodes
  [bodystmt]
end

#format(q) ⇒ Object



2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
# File 'lib/syntax_tree.rb', line 2283

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



2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
# File 'lib/syntax_tree.rb', line 2297

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



2308
2309
2310
2311
2312
2313
2314
2315
# File 'lib/syntax_tree.rb', line 2308

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