Class: SyntaxTree::HshPtn

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

Overview

HshPtn represents matching against a hash pattern using the Ruby 2.7+ pattern matching syntax.

case value
in { key: }
end

Defined Under Namespace

Classes: KeywordFormatter, KeywordRestFormatter

Instance Attribute Summary collapse

Attributes inherited from Node

#location

Instance Method Summary collapse

Methods inherited from Node

#pretty_print, #to_json

Constructor Details

#initialize(constant:, keywords:, keyword_rest:, location:, comments: []) ⇒ HshPtn

Returns a new instance of HshPtn.



4508
4509
4510
4511
4512
4513
4514
# File 'lib/syntax_tree/node.rb', line 4508

def initialize(constant:, keywords:, keyword_rest:, location:, comments: [])
  @constant = constant
  @keywords = keywords
  @keyword_rest = keyword_rest
  @location = location
  @comments = comments
end

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



4506
4507
4508
# File 'lib/syntax_tree/node.rb', line 4506

def comments
  @comments
end

#constantObject (readonly)

nil | untyped

the optional constant wrapper



4496
4497
4498
# File 'lib/syntax_tree/node.rb', line 4496

def constant
  @constant
end

#keyword_restObject (readonly)

nil | VarField

an optional parameter to gather up all remaining keywords



4503
4504
4505
# File 'lib/syntax_tree/node.rb', line 4503

def keyword_rest
  @keyword_rest
end

#keywordsObject (readonly)

Array[ [Label, untyped

]] the set of tuples representing the keywords

that should be matched against in the pattern



4500
4501
4502
# File 'lib/syntax_tree/node.rb', line 4500

def keywords
  @keywords
end

Instance Method Details

#accept(visitor) ⇒ Object



4516
4517
4518
# File 'lib/syntax_tree/node.rb', line 4516

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

#child_nodesObject Also known as: deconstruct



4520
4521
4522
# File 'lib/syntax_tree/node.rb', line 4520

def child_nodes
  [constant, *keywords.flatten(1), keyword_rest]
end

#deconstruct_keys(keys) ⇒ Object



4526
4527
4528
4529
4530
4531
4532
4533
4534
# File 'lib/syntax_tree/node.rb', line 4526

def deconstruct_keys(keys)
  {
    constant: constant,
    keywords: keywords,
    keyword_rest: keyword_rest,
    location: location,
    comments: comments
  }
end

#format(q) ⇒ Object



4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
# File 'lib/syntax_tree/node.rb', line 4536

def format(q)
  parts = keywords.map { |(key, value)| KeywordFormatter.new(key, value) }
  parts << KeywordRestFormatter.new(keyword_rest) if keyword_rest
  contents = -> do
    q.seplist(parts) { |part| q.format(part, stackable: false) }
  end

  if constant
    q.format(constant)
    q.group(0, "[", "]", &contents)
    return
  end

  parent = q.parent
  if PATTERNS.include?(parent.class)
    q.text("{ ")
    contents.call
    q.text(" }")
  else
    contents.call
  end
end