Class: SyntaxTree::RAssign

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

Overview

RAssign represents a single-line pattern match.

value in pattern
value => pattern

Instance Attribute Summary collapse

Attributes inherited from Node

#location

Instance Method Summary collapse

Methods inherited from Node

#construct_keys, #pretty_print, #to_json

Constructor Details

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

Returns a new instance of RAssign.



2739
2740
2741
2742
2743
2744
2745
# File 'lib/syntax_tree/node.rb', line 2739

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



2737
2738
2739
# File 'lib/syntax_tree/node.rb', line 2737

def comments
  @comments
end

#operatorObject (readonly)

Kw | Op

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

either => or in



2731
2732
2733
# File 'lib/syntax_tree/node.rb', line 2731

def operator
  @operator
end

#patternObject (readonly)

untyped

the pattern on the right-hand side of the expression



2734
2735
2736
# File 'lib/syntax_tree/node.rb', line 2734

def pattern
  @pattern
end

#valueObject (readonly)

untyped

the left-hand expression



2727
2728
2729
# File 'lib/syntax_tree/node.rb', line 2727

def value
  @value
end

Instance Method Details

#accept(visitor) ⇒ Object



2747
2748
2749
# File 'lib/syntax_tree/node.rb', line 2747

def accept(visitor)
  visitor.visit_rassign(self)
end

#child_nodesObject Also known as: deconstruct



2751
2752
2753
# File 'lib/syntax_tree/node.rb', line 2751

def child_nodes
  [value, operator, pattern]
end

#deconstruct_keys(_keys) ⇒ Object



2757
2758
2759
2760
2761
2762
2763
2764
2765
# File 'lib/syntax_tree/node.rb', line 2757

def deconstruct_keys(_keys)
  {
    value: value,
    operator: operator,
    pattern: pattern,
    location: location,
    comments: comments
  }
end

#format(q) ⇒ Object



2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
# File 'lib/syntax_tree/node.rb', line 2767

def format(q)
  q.group do
    q.format(value)
    q.text(" ")
    q.format(operator)

    case pattern
    in AryPtn | FndPtn | HshPtn
      q.text(" ")
      q.format(pattern)
    else
      q.group do
        q.indent do
          q.breakable
          q.format(pattern)
        end
      end
    end
  end
end