Class: SyntaxTree::FndPtn

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

Overview

FndPtn represents matching against a pattern where you find a pattern in an array using the Ruby 3.0+ pattern matching syntax.

case value
in [*, 7, *]
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(constant:, left:, values:, right:, location:, comments: []) ⇒ FndPtn

Returns a new instance of FndPtn.



6046
6047
6048
6049
6050
6051
6052
6053
# File 'lib/syntax_tree.rb', line 6046

def initialize(constant:, left:, values:, right:, location:, comments: [])
  @constant = constant
  @left = left
  @values = values
  @right = right
  @location = location
  @comments = comments
end

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



6044
6045
6046
# File 'lib/syntax_tree.rb', line 6044

def comments
  @comments
end

#constantObject (readonly)

nil | untyped

the optional constant wrapper



6028
6029
6030
# File 'lib/syntax_tree.rb', line 6028

def constant
  @constant
end

#leftObject (readonly)

VarField

the splat on the left-hand side



6031
6032
6033
# File 'lib/syntax_tree.rb', line 6031

def left
  @left
end

#locationObject (readonly)

Location

the location of this node



6041
6042
6043
# File 'lib/syntax_tree.rb', line 6041

def location
  @location
end

#rightObject (readonly)

VarField

the splat on the right-hand side



6038
6039
6040
# File 'lib/syntax_tree.rb', line 6038

def right
  @right
end

#valuesObject (readonly)

Array[ untyped ]

the list of positional expressions in the pattern that

are being matched



6035
6036
6037
# File 'lib/syntax_tree.rb', line 6035

def values
  @values
end

Instance Method Details

#child_nodesObject



6055
6056
6057
# File 'lib/syntax_tree.rb', line 6055

def child_nodes
  [constant, left, *values, right]
end

#format(q) ⇒ Object



6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
# File 'lib/syntax_tree.rb', line 6059

def format(q)
  q.format(constant) if constant
  q.group(0, "[", "]") do
    q.text("*")
    q.format(left)
    q.comma_breakable

    q.seplist(values) { |value| q.format(value) }
    q.comma_breakable

    q.text("*")
    q.format(right)
  end
end

#pretty_print(q) ⇒ Object



6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
# File 'lib/syntax_tree.rb', line 6074

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

    if constant
      q.breakable
      q.pp(constant)
    end

    q.breakable
    q.pp(left)

    q.breakable
    q.group(2, "(", ")") { q.seplist(values) { |value| q.pp(value) } }

    q.breakable
    q.pp(right)

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

#to_json(*opts) ⇒ Object



6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
# File 'lib/syntax_tree.rb', line 6096

def to_json(*opts)
  {
    type: :fndptn,
    constant: constant,
    left: left,
    values: values,
    right: right,
    loc: location,
    cmts: comments
  }.to_json(*opts)
end