Class: SyntaxTree::BodyStmt
- Inherits:
-
Object
- Object
- SyntaxTree::BodyStmt
- Defined in:
- lib/syntax_tree.rb
Overview
bodystmt can’t actually determine its bounds appropriately because it doesn’t necessarily know where it started. So the parent node needs to report back down into this one where it goes.
Instance Attribute Summary collapse
-
#comments ⇒ Object
readonly
- Array[ Comment | EmbDoc ]
-
the comments attached to this node.
-
#else_clause ⇒ Object
readonly
- nil | Statements
-
the optional set of statements inside the else clause.
-
#ensure_clause ⇒ Object
readonly
- nil | Ensure
-
the optional ensure clause.
-
#location ⇒ Object
readonly
- Location
-
the location of this node.
-
#rescue_clause ⇒ Object
readonly
- nil | Rescue
-
the optional rescue chain attached to the begin clause.
-
#statements ⇒ Object
readonly
- Statements
-
the list of statements inside the begin clause.
Instance Method Summary collapse
- #bind(start_char, end_char) ⇒ Object
- #child_nodes ⇒ Object
- #empty? ⇒ Boolean
- #format(q) ⇒ Object
-
#initialize(statements:, rescue_clause:, else_clause:, ensure_clause:, location:, comments: []) ⇒ BodyStmt
constructor
A new instance of BodyStmt.
- #pretty_print(q) ⇒ Object
- #to_json(*opts) ⇒ Object
Constructor Details
#initialize(statements:, rescue_clause:, else_clause:, ensure_clause:, location:, comments: []) ⇒ BodyStmt
Returns a new instance of BodyStmt.
2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 |
# File 'lib/syntax_tree.rb', line 2774 def initialize( statements:, rescue_clause:, else_clause:, ensure_clause:, location:, comments: [] ) @statements = statements @rescue_clause = rescue_clause @else_clause = else_clause @ensure_clause = ensure_clause @location = location @comments = comments end |
Instance Attribute Details
#comments ⇒ Object (readonly)
- Array[ Comment | EmbDoc ]
-
the comments attached to this node
2772 2773 2774 |
# File 'lib/syntax_tree.rb', line 2772 def comments @comments end |
#else_clause ⇒ Object (readonly)
- nil | Statements
-
the optional set of statements inside the else clause
2763 2764 2765 |
# File 'lib/syntax_tree.rb', line 2763 def else_clause @else_clause end |
#ensure_clause ⇒ Object (readonly)
- nil | Ensure
-
the optional ensure clause
2766 2767 2768 |
# File 'lib/syntax_tree.rb', line 2766 def ensure_clause @ensure_clause end |
#location ⇒ Object (readonly)
- Location
-
the location of this node
2769 2770 2771 |
# File 'lib/syntax_tree.rb', line 2769 def location @location end |
#rescue_clause ⇒ Object (readonly)
- nil | Rescue
-
the optional rescue chain attached to the begin clause
2760 2761 2762 |
# File 'lib/syntax_tree.rb', line 2760 def rescue_clause @rescue_clause end |
#statements ⇒ Object (readonly)
- Statements
-
the list of statements inside the begin clause
2757 2758 2759 |
# File 'lib/syntax_tree.rb', line 2757 def statements @statements end |
Instance Method Details
#bind(start_char, end_char) ⇒ Object
2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 |
# File 'lib/syntax_tree.rb', line 2790 def bind(start_char, end_char) @location = Location.new( start_line: location.start_line, start_char: start_char, end_line: location.end_line, end_char: end_char ) parts = [rescue_clause, else_clause, ensure_clause] # Here we're going to determine the bounds for the statements consequent = parts.compact.first statements.bind( start_char, consequent ? consequent.location.start_char : end_char ) # Next we're going to determine the rescue clause if there is one if rescue_clause consequent = parts.drop(1).compact.first rescue_clause.bind_end( consequent ? consequent.location.start_char : end_char ) end end |
#child_nodes ⇒ Object
2821 2822 2823 |
# File 'lib/syntax_tree.rb', line 2821 def child_nodes [statements, rescue_clause, else_clause, ensure_clause] end |
#empty? ⇒ Boolean
2817 2818 2819 |
# File 'lib/syntax_tree.rb', line 2817 def empty? statements.empty? && !rescue_clause && !else_clause && !ensure_clause end |
#format(q) ⇒ Object
2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 |
# File 'lib/syntax_tree.rb', line 2825 def format(q) q.group do q.format(statements) unless statements.empty? if rescue_clause q.nest(-2) do q.breakable(force: true) q.format(rescue_clause) end end if else_clause q.nest(-2) do q.breakable(force: true) q.text("else") end q.breakable(force: true) q.format(else_clause) end if ensure_clause q.nest(-2) do q.breakable(force: true) q.format(ensure_clause) end end end end |
#pretty_print(q) ⇒ Object
2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 |
# File 'lib/syntax_tree.rb', line 2854 def pretty_print(q) q.group(2, "(", ")") do q.text("bodystmt") q.breakable q.pp(statements) if rescue_clause q.breakable q.pp(rescue_clause) end if else_clause q.breakable q.pp(else_clause) end if ensure_clause q.breakable q.pp(ensure_clause) end q.pp(Comment::List.new(comments)) end end |
#to_json(*opts) ⇒ Object
2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 |
# File 'lib/syntax_tree.rb', line 2880 def to_json(*opts) { type: :bodystmt, stmts: statements, rsc: rescue_clause, els: else_clause, ens: ensure_clause, loc: location, cmts: comments }.to_json(*opts) end |