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.
7688 7689 7690 7691 7692 7693 |
# File 'lib/syntax_tree.rb', line 7688 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
7686 7687 7688 |
# File 'lib/syntax_tree.rb', line 7686 def comments @comments end |
#location ⇒ Object (readonly)
- Location
-
the location of this node
7683 7684 7685 |
# File 'lib/syntax_tree.rb', line 7683 def location @location end |
#params ⇒ Object (readonly)
- Params | Paren
-
the parameter declaration for this lambda
7677 7678 7679 |
# File 'lib/syntax_tree.rb', line 7677 def params @params end |
#statements ⇒ Object (readonly)
- BodyStmt | Statements
-
the expressions to be executed in this lambda
7680 7681 7682 |
# File 'lib/syntax_tree.rb', line 7680 def statements @statements end |
Instance Method Details
#child_nodes ⇒ Object
7695 7696 7697 |
# File 'lib/syntax_tree.rb', line 7695 def child_nodes [params, statements] end |
#format(q) ⇒ Object
7699 7700 7701 7702 7703 7704 7705 7706 7707 7708 7709 7710 7711 7712 7713 7714 7715 7716 7717 7718 7719 7720 7721 7722 7723 7724 7725 7726 7727 7728 7729 7730 7731 7732 |
# File 'lib/syntax_tree.rb', line 7699 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
7734 7735 7736 7737 7738 7739 7740 7741 7742 7743 7744 7745 7746 |
# File 'lib/syntax_tree.rb', line 7734 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
7748 7749 7750 7751 7752 7753 7754 7755 7756 |
# File 'lib/syntax_tree.rb', line 7748 def to_json(*opts) { type: :lambda, params: params, stmts: statements, loc: location, cmts: comments }.to_json(*opts) end |