Class: SyntaxTree::Lambda

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

Overview

Lambda represents using a lambda literal (not the lambda method call).

->(value) { value * 2 }

Instance Attribute Summary collapse

Attributes inherited from Node

#location

Instance Method Summary collapse

Methods inherited from Node

#construct_keys, #end_char, #pretty_print, #start_char, #to_json, #to_mermaid

Constructor Details

#initialize(params:, statements:, location:) ⇒ Lambda

Returns a new instance of Lambda.



7097
7098
7099
7100
7101
7102
# File 'lib/syntax_tree/node.rb', line 7097

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

Instance Attribute Details

#commentsObject (readonly)

Array[ Comment | EmbDoc ]

the comments attached to this node



7095
7096
7097
# File 'lib/syntax_tree/node.rb', line 7095

def comments
  @comments
end

#paramsObject (readonly)

LambdaVar | Paren

the parameter declaration for this lambda



7089
7090
7091
# File 'lib/syntax_tree/node.rb', line 7089

def params
  @params
end

#statementsObject (readonly)

BodyStmt | Statements

the expressions to be executed in this lambda



7092
7093
7094
# File 'lib/syntax_tree/node.rb', line 7092

def statements
  @statements
end

Instance Method Details

#===(other) ⇒ Object



7198
7199
7200
7201
# File 'lib/syntax_tree/node.rb', line 7198

def ===(other)
  other.is_a?(Lambda) && params === other.params &&
    statements === other.statements
end

#accept(visitor) ⇒ Object



7104
7105
7106
# File 'lib/syntax_tree/node.rb', line 7104

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

#child_nodesObject Also known as: deconstruct



7108
7109
7110
# File 'lib/syntax_tree/node.rb', line 7108

def child_nodes
  [params, statements]
end

#copy(params: nil, statements: nil, location: nil) ⇒ Object



7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
# File 'lib/syntax_tree/node.rb', line 7112

def copy(params: nil, statements: nil, location: nil)
  node =
    Lambda.new(
      params: params || self.params,
      statements: statements || self.statements,
      location: location || self.location
    )

  node.comments.concat(comments.map(&:copy))
  node
end

#deconstruct_keys(_keys) ⇒ Object



7126
7127
7128
7129
7130
7131
7132
7133
# File 'lib/syntax_tree/node.rb', line 7126

def deconstruct_keys(_keys)
  {
    params: params,
    statements: statements,
    location: location,
    comments: comments
  }
end

#format(q) ⇒ Object



7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
# File 'lib/syntax_tree/node.rb', line 7135

def format(q)
  q.text("->")
  q.group do
    if params.is_a?(Paren)
      q.format(params) unless params.contents.empty?
    elsif params.empty? && params.comments.any?
      q.format(params)
    elsif !params.empty?
      q.group do
        q.text("(")
        q.format(params)
        q.text(")")
      end
    end

    q.text(" ")
    q
      .if_break do
        force_parens =
          q.parents.any? do |node|
            node.is_a?(Command) || node.is_a?(CommandCall)
          end

        if force_parens
          q.text("{")

          unless statements.empty?
            q.indent do
              q.breakable_space
              q.format(statements)
            end
            q.breakable_space
          end

          q.text("}")
        else
          q.text("do")

          unless statements.empty?
            q.indent do
              q.breakable_space
              q.format(statements)
            end
          end

          q.breakable_space
          q.text("end")
        end
      end
      .if_flat do
        q.text("{")

        unless statements.empty?
          q.text(" ")
          q.format(statements)
          q.text(" ")
        end

        q.text("}")
      end
  end
end