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.



7307
7308
7309
7310
7311
7312
7313
# File 'lib/syntax_tree.rb', line 7307

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



7305
7306
7307
# File 'lib/syntax_tree.rb', line 7305

def comments
  @comments
end

#consequentObject (readonly)

nil | In | Else

the next clause in the chain



7299
7300
7301
# File 'lib/syntax_tree.rb', line 7299

def consequent
  @consequent
end

#locationObject (readonly)

Location

the location of this node



7302
7303
7304
# File 'lib/syntax_tree.rb', line 7302

def location
  @location
end

#patternObject (readonly)

untyped

the pattern to check against



7293
7294
7295
# File 'lib/syntax_tree.rb', line 7293

def pattern
  @pattern
end

#statementsObject (readonly)

Statements

the expressions to execute if the pattern matched



7296
7297
7298
# File 'lib/syntax_tree.rb', line 7296

def statements
  @statements
end

Instance Method Details

#child_nodesObject



7315
7316
7317
# File 'lib/syntax_tree.rb', line 7315

def child_nodes
  [pattern, statements, consequent]
end

#format(q) ⇒ Object



7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
# File 'lib/syntax_tree.rb', line 7319

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



7340
7341
7342
7343
7344
7345
7346
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
# File 'lib/syntax_tree.rb', line 7340

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



7359
7360
7361
7362
7363
7364
7365
7366
7367
7368
# File 'lib/syntax_tree.rb', line 7359

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