Class: SyntaxTree::In

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

Overview

In represents using the in keyword within the Ruby 2.7+ pattern matching syntax.

case value
in pattern
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pattern:, statements:, consequent:, location:, comments: []) ⇒ In

Returns a new instance of In.



6447
6448
6449
6450
6451
6452
6453
# File 'lib/syntax_tree/node.rb', line 6447

def initialize(pattern:, statements:, consequent:, location:, comments: [])
  @pattern = pattern
  @statements = statements
  @consequent = consequent
  @location = location
  @comments = comments
end

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



6445
6446
6447
# File 'lib/syntax_tree/node.rb', line 6445

def comments
  @comments
end

#consequentObject (readonly)

nil | In | Else

the next clause in the chain



6439
6440
6441
# File 'lib/syntax_tree/node.rb', line 6439

def consequent
  @consequent
end

#locationObject (readonly)

Location

the location of this node



6442
6443
6444
# File 'lib/syntax_tree/node.rb', line 6442

def location
  @location
end

#patternObject (readonly)

untyped

the pattern to check against



6433
6434
6435
# File 'lib/syntax_tree/node.rb', line 6433

def pattern
  @pattern
end

#statementsObject (readonly)

Statements

the expressions to execute if the pattern matched



6436
6437
6438
# File 'lib/syntax_tree/node.rb', line 6436

def statements
  @statements
end

Instance Method Details

#child_nodesObject Also known as: deconstruct



6455
6456
6457
# File 'lib/syntax_tree/node.rb', line 6455

def child_nodes
  [pattern, statements, consequent]
end

#deconstruct_keys(keys) ⇒ Object



6461
6462
6463
6464
6465
6466
6467
6468
6469
# File 'lib/syntax_tree/node.rb', line 6461

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

#format(q) ⇒ Object



6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
# File 'lib/syntax_tree/node.rb', line 6471

def format(q)
  keyword = "in "

  q.group do
    q.text(keyword)
    q.nest(keyword.length) { q.format(pattern) }

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

    if consequent
      q.breakable(force: true)
      q.format(consequent)
    end
  end
end

#pretty_print(q) ⇒ Object



6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
# File 'lib/syntax_tree/node.rb', line 6492

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

    q.breakable
    q.pp(pattern)

    q.breakable
    q.pp(statements)

    if consequent
      q.breakable
      q.pp(consequent)
    end

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

#to_json(*opts) ⇒ Object



6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
# File 'lib/syntax_tree/node.rb', line 6511

def to_json(*opts)
  {
    type: :in,
    pattern: pattern,
    stmts: statements,
    cons: consequent,
    loc: location,
    cmts: comments
  }.to_json(*opts)
end