Class: SyntaxTree::Lambda
Overview
Lambda represents using a lambda literal (not the lambda method call).
->(value) { value * 2 }
Instance Attribute Summary collapse
-
#comments ⇒ Object
readonly
- Array[ Comment | EmbDoc ]
-
the comments attached to this node.
-
#params ⇒ Object
readonly
- LambdaVar | Paren
-
the parameter declaration for this lambda.
-
#statements ⇒ Object
readonly
- BodyStmt | Statements
-
the expressions to be executed in this lambda.
Attributes inherited from Node
Instance Method Summary collapse
- #===(other) ⇒ Object
- #accept(visitor) ⇒ Object
- #child_nodes ⇒ Object (also: #deconstruct)
- #copy(params: nil, statements: nil, location: nil) ⇒ Object
- #deconstruct_keys(_keys) ⇒ Object
- #format(q) ⇒ Object
-
#initialize(params:, statements:, location:) ⇒ Lambda
constructor
A new instance of Lambda.
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.
7152 7153 7154 7155 7156 7157 |
# File 'lib/syntax_tree/node.rb', line 7152 def initialize(params:, statements:, location:) @params = params @statements = statements @location = location @comments = [] end |
Instance Attribute Details
#comments ⇒ Object (readonly)
- Array[ Comment | EmbDoc ]
-
the comments attached to this node
7150 7151 7152 |
# File 'lib/syntax_tree/node.rb', line 7150 def comments @comments end |
#params ⇒ Object (readonly)
- LambdaVar | Paren
-
the parameter declaration for this lambda
7144 7145 7146 |
# File 'lib/syntax_tree/node.rb', line 7144 def params @params end |
#statements ⇒ Object (readonly)
- BodyStmt | Statements
-
the expressions to be executed in this lambda
7147 7148 7149 |
# File 'lib/syntax_tree/node.rb', line 7147 def statements @statements end |
Instance Method Details
#===(other) ⇒ Object
7255 7256 7257 7258 |
# File 'lib/syntax_tree/node.rb', line 7255 def ===(other) other.is_a?(Lambda) && params === other.params && statements === other.statements end |
#accept(visitor) ⇒ Object
7159 7160 7161 |
# File 'lib/syntax_tree/node.rb', line 7159 def accept(visitor) visitor.visit_lambda(self) end |
#child_nodes ⇒ Object Also known as: deconstruct
7163 7164 7165 |
# File 'lib/syntax_tree/node.rb', line 7163 def child_nodes [params, statements] end |
#copy(params: nil, statements: nil, location: nil) ⇒ Object
7167 7168 7169 7170 7171 7172 7173 7174 7175 7176 7177 |
# File 'lib/syntax_tree/node.rb', line 7167 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
7181 7182 7183 7184 7185 7186 7187 7188 |
# File 'lib/syntax_tree/node.rb', line 7181 def deconstruct_keys(_keys) { params: params, statements: statements, location: location, comments: comments } end |
#format(q) ⇒ Object
7190 7191 7192 7193 7194 7195 7196 7197 7198 7199 7200 7201 7202 7203 7204 7205 7206 7207 7208 7209 7210 7211 7212 7213 7214 7215 7216 7217 7218 7219 7220 7221 7222 7223 7224 7225 7226 7227 7228 7229 7230 7231 7232 7233 7234 7235 7236 7237 7238 7239 7240 7241 7242 7243 7244 7245 7246 7247 7248 7249 7250 7251 7252 7253 |
# File 'lib/syntax_tree/node.rb', line 7190 def format(q) params = self.params 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 |