Class: SyntaxTree::RAssign

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

Overview

RAssign represents a single-line pattern match.

value in pattern
value => pattern

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value:, operator:, pattern:, location:, comments: []) ⇒ RAssign

Returns a new instance of RAssign.



3415
3416
3417
3418
3419
3420
3421
# File 'lib/syntax_tree.rb', line 3415

def initialize(value:, operator:, pattern:, location:, comments: [])
  @value = value
  @operator = operator
  @pattern = pattern
  @location = location
  @comments = comments
end

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



3413
3414
3415
# File 'lib/syntax_tree.rb', line 3413

def comments
  @comments
end

#locationObject (readonly)

Location

the location of this node



3410
3411
3412
# File 'lib/syntax_tree.rb', line 3410

def location
  @location
end

#operatorObject (readonly)

Kw | Op

the operator being used to match against the pattern, which is

either => or in



3404
3405
3406
# File 'lib/syntax_tree.rb', line 3404

def operator
  @operator
end

#patternObject (readonly)

untyped

the pattern on the right-hand side of the expression



3407
3408
3409
# File 'lib/syntax_tree.rb', line 3407

def pattern
  @pattern
end

#valueObject (readonly)

untyped

the left-hand expression



3400
3401
3402
# File 'lib/syntax_tree.rb', line 3400

def value
  @value
end

Instance Method Details

#child_nodesObject



3423
3424
3425
# File 'lib/syntax_tree.rb', line 3423

def child_nodes
  [value, operator, pattern]
end

#format(q) ⇒ Object



3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
# File 'lib/syntax_tree.rb', line 3427

def format(q)
  q.group do
    q.format(value)
    q.text(" ")
    q.format(operator)
    q.group do
      q.indent do
        q.breakable
        q.format(pattern)
      end
    end
  end
end

#pretty_print(q) ⇒ Object



3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
# File 'lib/syntax_tree.rb', line 3441

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

    q.breakable
    q.pp(value)

    q.breakable
    q.pp(operator)

    q.breakable
    q.pp(pattern)

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

#to_json(*opts) ⇒ Object



3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
# File 'lib/syntax_tree.rb', line 3458

def to_json(*opts)
  {
    type: :rassign,
    value: value,
    op: operator,
    pattern: pattern,
    loc: location,
    cmts: comments
  }.to_json(*opts)
end