Class: SyntaxTree::Lambda
- Inherits:
-
Object
- Object
- SyntaxTree::Lambda
- Defined in:
- lib/syntax_tree.rb
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.
-
#location ⇒ Object
readonly
- Location
-
the location of this node.
-
#params ⇒ Object
readonly
- Params | Paren
-
the parameter declaration for this lambda.
-
#statements ⇒ Object
readonly
- BodyStmt | Statements
-
the expressions to be executed in this lambda.
Instance Method Summary collapse
- #child_nodes ⇒ Object
- #format(q) ⇒ Object
-
#initialize(params:, statements:, location:, comments: []) ⇒ Lambda
constructor
A new instance of Lambda.
- #pretty_print(q) ⇒ Object
- #to_json(*opts) ⇒ Object
Constructor Details
#initialize(params:, statements:, location:, comments: []) ⇒ Lambda
Returns a new instance of Lambda.
7761 7762 7763 7764 7765 7766 |
# File 'lib/syntax_tree.rb', line 7761 def initialize(params:, statements:, location:, comments: []) @params = params @statements = statements @location = location @comments = comments end |
Instance Attribute Details
#comments ⇒ Object (readonly)
- Array[ Comment | EmbDoc ]
-
the comments attached to this node
7759 7760 7761 |
# File 'lib/syntax_tree.rb', line 7759 def comments @comments end |
#location ⇒ Object (readonly)
- Location
-
the location of this node
7756 7757 7758 |
# File 'lib/syntax_tree.rb', line 7756 def location @location end |
#params ⇒ Object (readonly)
- Params | Paren
-
the parameter declaration for this lambda
7750 7751 7752 |
# File 'lib/syntax_tree.rb', line 7750 def params @params end |
#statements ⇒ Object (readonly)
- BodyStmt | Statements
-
the expressions to be executed in this lambda
7753 7754 7755 |
# File 'lib/syntax_tree.rb', line 7753 def statements @statements end |
Instance Method Details
#child_nodes ⇒ Object
7768 7769 7770 |
# File 'lib/syntax_tree.rb', line 7768 def child_nodes [params, statements] end |
#format(q) ⇒ Object
7772 7773 7774 7775 7776 7777 7778 7779 7780 7781 7782 7783 7784 7785 7786 7787 7788 7789 7790 7791 7792 7793 7794 7795 7796 7797 7798 7799 7800 7801 7802 7803 7804 7805 |
# File 'lib/syntax_tree.rb', line 7772 def format(q) q.group(0, "->") do if params.is_a?(Paren) q.format(params) unless params.contents.empty? 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 q.text(force_parens ? "{" : "do") q.indent do q.breakable q.format(statements) end q.breakable q.text(force_parens ? "}" : "end") end.if_flat do q.text("{ ") q.format(statements) q.text(" }") end end end |
#pretty_print(q) ⇒ Object
7807 7808 7809 7810 7811 7812 7813 7814 7815 7816 7817 7818 7819 |
# File 'lib/syntax_tree.rb', line 7807 def pretty_print(q) q.group(2, "(", ")") do q.text("lambda") q.breakable q.pp(params) q.breakable q.pp(statements) q.pp(Comment::List.new(comments)) end end |
#to_json(*opts) ⇒ Object
7821 7822 7823 7824 7825 7826 7827 7828 7829 |
# File 'lib/syntax_tree.rb', line 7821 def to_json(*opts) { type: :lambda, params: params, stmts: statements, loc: location, cmts: comments }.to_json(*opts) end |