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.



7239
7240
7241
7242
7243
7244
7245
# File 'lib/syntax_tree.rb', line 7239

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



7237
7238
7239
# File 'lib/syntax_tree.rb', line 7237

def comments
  @comments
end

#consequentObject (readonly)

nil | In | Else

the next clause in the chain



7231
7232
7233
# File 'lib/syntax_tree.rb', line 7231

def consequent
  @consequent
end

#locationObject (readonly)

Location

the location of this node



7234
7235
7236
# File 'lib/syntax_tree.rb', line 7234

def location
  @location
end

#patternObject (readonly)

untyped

the pattern to check against



7225
7226
7227
# File 'lib/syntax_tree.rb', line 7225

def pattern
  @pattern
end

#statementsObject (readonly)

Statements

the expressions to execute if the pattern matched



7228
7229
7230
# File 'lib/syntax_tree.rb', line 7228

def statements
  @statements
end

Instance Method Details

#child_nodesObject



7247
7248
7249
# File 'lib/syntax_tree.rb', line 7247

def child_nodes
  [pattern, statements, consequent]
end

#format(q) ⇒ Object



7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
# File 'lib/syntax_tree.rb', line 7251

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



7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
# File 'lib/syntax_tree.rb', line 7272

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



7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
# File 'lib/syntax_tree.rb', line 7291

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