Class: SyntaxTree::In

Inherits:
Object
  • Object
show all
Defined in:
lib/syntax_tree.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.



7439
7440
7441
7442
7443
7444
7445
# File 'lib/syntax_tree.rb', line 7439

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



7437
7438
7439
# File 'lib/syntax_tree.rb', line 7437

def comments
  @comments
end

#consequentObject (readonly)

nil | In | Else

the next clause in the chain



7431
7432
7433
# File 'lib/syntax_tree.rb', line 7431

def consequent
  @consequent
end

#locationObject (readonly)

Location

the location of this node



7434
7435
7436
# File 'lib/syntax_tree.rb', line 7434

def location
  @location
end

#patternObject (readonly)

untyped

the pattern to check against



7425
7426
7427
# File 'lib/syntax_tree.rb', line 7425

def pattern
  @pattern
end

#statementsObject (readonly)

Statements

the expressions to execute if the pattern matched



7428
7429
7430
# File 'lib/syntax_tree.rb', line 7428

def statements
  @statements
end

Instance Method Details

#child_nodesObject



7447
7448
7449
# File 'lib/syntax_tree.rb', line 7447

def child_nodes
  [pattern, statements, consequent]
end

#format(q) ⇒ Object



7451
7452
7453
7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
7468
7469
7470
# File 'lib/syntax_tree.rb', line 7451

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



7472
7473
7474
7475
7476
7477
7478
7479
7480
7481
7482
7483
7484
7485
7486
7487
7488
7489
# File 'lib/syntax_tree.rb', line 7472

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



7491
7492
7493
7494
7495
7496
7497
7498
7499
7500
# File 'lib/syntax_tree.rb', line 7491

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