Class: SyntaxTree::In

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

Overview

In represents using the in keyword within the Ruby 2.7+ pattern matching syntax.

case value
in pattern
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pattern:, statements:, consequent:, location:, comments: []) ⇒ In

Returns a new instance of In.



6869
6870
6871
6872
6873
6874
6875
# File 'lib/syntax_tree.rb', line 6869

def initialize(pattern:, statements:, consequent:, location:, comments: [])
  @pattern = pattern
  @statements = statements
  @consequent = consequent
  @location = location
  @comments = comments
end

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



6867
6868
6869
# File 'lib/syntax_tree.rb', line 6867

def comments
  @comments
end

#consequentObject (readonly)

nil | In | Else

the next clause in the chain



6861
6862
6863
# File 'lib/syntax_tree.rb', line 6861

def consequent
  @consequent
end

#locationObject (readonly)

Location

the location of this node



6864
6865
6866
# File 'lib/syntax_tree.rb', line 6864

def location
  @location
end

#patternObject (readonly)

untyped

the pattern to check against



6855
6856
6857
# File 'lib/syntax_tree.rb', line 6855

def pattern
  @pattern
end

#statementsObject (readonly)

Statements

the expressions to execute if the pattern matched



6858
6859
6860
# File 'lib/syntax_tree.rb', line 6858

def statements
  @statements
end

Instance Method Details

#child_nodesObject



6877
6878
6879
# File 'lib/syntax_tree.rb', line 6877

def child_nodes
  [pattern, statements, consequent]
end

#format(q) ⇒ Object



6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
# File 'lib/syntax_tree.rb', line 6881

def format(q)
  keyword = "in "

  q.group do
    q.text(keyword)
    q.nest(keyword.length) { q.format(pattern) }

    unless statements.empty?
      q.indent do
        q.breakable(force: true)
        q.format(statements)
      end
    end

    if consequent
      q.breakable(force: true)
      q.format(consequent)
    end
  end
end

#pretty_print(q) ⇒ Object



6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
# File 'lib/syntax_tree.rb', line 6902

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

    q.breakable
    q.pp(pattern)

    q.breakable
    q.pp(statements)

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

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

#to_json(*opts) ⇒ Object



6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
# File 'lib/syntax_tree.rb', line 6921

def to_json(*opts)
  {
    type: :in,
    pattern: pattern,
    stmts: statements,
    cons: consequent,
    loc: location,
    cmts: comments
  }.to_json(*opts)
end