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.



3145
3146
3147
3148
3149
3150
3151
# File 'lib/syntax_tree.rb', line 3145

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



3143
3144
3145
# File 'lib/syntax_tree.rb', line 3143

def comments
  @comments
end

#locationObject (readonly)

Location

the location of this node



3140
3141
3142
# File 'lib/syntax_tree.rb', line 3140

def location
  @location
end

#operatorObject (readonly)

Kw | Op

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

either => or in



3134
3135
3136
# File 'lib/syntax_tree.rb', line 3134

def operator
  @operator
end

#patternObject (readonly)

untyped

the pattern on the right-hand side of the expression



3137
3138
3139
# File 'lib/syntax_tree.rb', line 3137

def pattern
  @pattern
end

#valueObject (readonly)

untyped

the left-hand expression



3130
3131
3132
# File 'lib/syntax_tree.rb', line 3130

def value
  @value
end

Instance Method Details

#child_nodesObject



3153
3154
3155
# File 'lib/syntax_tree.rb', line 3153

def child_nodes
  [value, operator, pattern]
end

#format(q) ⇒ Object



3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
# File 'lib/syntax_tree.rb', line 3157

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



3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
# File 'lib/syntax_tree.rb', line 3171

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



3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
# File 'lib/syntax_tree.rb', line 3188

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