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

Attributes inherited from Node

#location

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of In.



6231
6232
6233
6234
6235
6236
6237
# File 'lib/syntax_tree/node.rb', line 6231

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



6229
6230
6231
# File 'lib/syntax_tree/node.rb', line 6229

def comments
  @comments
end

#consequentObject (readonly)

nil | In | Else

the next clause in the chain



6226
6227
6228
# File 'lib/syntax_tree/node.rb', line 6226

def consequent
  @consequent
end

#patternObject (readonly)

untyped

the pattern to check against



6220
6221
6222
# File 'lib/syntax_tree/node.rb', line 6220

def pattern
  @pattern
end

#statementsObject (readonly)

Statements

the expressions to execute if the pattern matched



6223
6224
6225
# File 'lib/syntax_tree/node.rb', line 6223

def statements
  @statements
end

Instance Method Details

#child_nodesObject Also known as: deconstruct



6239
6240
6241
# File 'lib/syntax_tree/node.rb', line 6239

def child_nodes
  [pattern, statements, consequent]
end

#deconstruct_keys(keys) ⇒ Object



6245
6246
6247
6248
6249
6250
6251
6252
6253
# File 'lib/syntax_tree/node.rb', line 6245

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

#format(q) ⇒ Object



6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
# File 'lib/syntax_tree/node.rb', line 6255

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



6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
# File 'lib/syntax_tree/node.rb', line 6276

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



6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
# File 'lib/syntax_tree/node.rb', line 6295

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