Class: SyntaxTree::Statements
- Inherits:
-
Object
- Object
- SyntaxTree::Statements
- Defined in:
- lib/syntax_tree.rb
Overview
Everything that has a block of code inside of it has a list of statements. Normally we would just track those as a node that has an array body, but we have some special handling in order to handle empty statement lists. They need to have the right location information, so all of the parent node of stmts nodes will report back down the location information. We then propagate that onto void_stmt nodes inside the stmts in order to make sure all comments get printed appropriately.
Instance Attribute Summary collapse
-
#body ⇒ Object
readonly
- Array[ untyped ]
-
the list of expressions contained within this node.
-
#comments ⇒ Object
readonly
- Array[ Comment | EmbDoc ]
-
the comments attached to this node.
-
#location ⇒ Object
readonly
- Location
-
the location of this node.
-
#parser ⇒ Object
readonly
- SyntaxTree
-
the parser that is generating this node.
Instance Method Summary collapse
- #bind(start_char, end_char) ⇒ Object
- #bind_end(end_char) ⇒ Object
- #child_nodes ⇒ Object
- #empty? ⇒ Boolean
- #format(q) ⇒ Object
-
#initialize(parser, body:, location:, comments: []) ⇒ Statements
constructor
A new instance of Statements.
- #pretty_print(q) ⇒ Object
- #to_json(*opts) ⇒ Object
Constructor Details
#initialize(parser, body:, location:, comments: []) ⇒ Statements
Returns a new instance of Statements.
10838 10839 10840 10841 10842 10843 |
# File 'lib/syntax_tree.rb', line 10838 def initialize(parser, body:, location:, comments: []) @parser = parser @body = body @location = location @comments = comments end |
Instance Attribute Details
#body ⇒ Object (readonly)
- Array[ untyped ]
-
the list of expressions contained within this node
10830 10831 10832 |
# File 'lib/syntax_tree.rb', line 10830 def body @body end |
#comments ⇒ Object (readonly)
- Array[ Comment | EmbDoc ]
-
the comments attached to this node
10836 10837 10838 |
# File 'lib/syntax_tree.rb', line 10836 def comments @comments end |
#location ⇒ Object (readonly)
- Location
-
the location of this node
10833 10834 10835 |
# File 'lib/syntax_tree.rb', line 10833 def location @location end |
#parser ⇒ Object (readonly)
- SyntaxTree
-
the parser that is generating this node
10827 10828 10829 |
# File 'lib/syntax_tree.rb', line 10827 def parser @parser end |
Instance Method Details
#bind(start_char, end_char) ⇒ Object
10845 10846 10847 10848 10849 10850 10851 10852 10853 10854 10855 10856 10857 10858 10859 10860 10861 10862 10863 10864 10865 10866 10867 10868 |
# File 'lib/syntax_tree.rb', line 10845 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 ) if body[0].is_a?(VoidStmt) location = body[0].location location = Location.new( start_line: location.start_line, start_char: start_char, end_line: location.end_line, end_char: start_char ) body[0] = VoidStmt.new(location: location) end attach_comments(start_char, end_char) end |
#bind_end(end_char) ⇒ Object
10870 10871 10872 10873 10874 10875 10876 10877 10878 |
# File 'lib/syntax_tree.rb', line 10870 def bind_end(end_char) @location = Location.new( start_line: location.start_line, start_char: location.start_char, end_line: location.end_line, end_char: end_char ) end |
#child_nodes ⇒ Object
10886 10887 10888 |
# File 'lib/syntax_tree.rb', line 10886 def child_nodes body end |
#empty? ⇒ Boolean
10880 10881 10882 10883 10884 |
# File 'lib/syntax_tree.rb', line 10880 def empty? body.all? do |statement| statement.is_a?(VoidStmt) && statement.comments.empty? end end |
#format(q) ⇒ Object
10890 10891 10892 10893 10894 10895 10896 10897 10898 10899 10900 10901 10902 10903 10904 10905 10906 10907 10908 10909 10910 10911 10912 10913 10914 10915 10916 10917 10918 10919 10920 10921 10922 10923 10924 10925 10926 10927 10928 10929 10930 10931 10932 10933 10934 10935 10936 10937 10938 10939 |
# File 'lib/syntax_tree.rb', line 10890 def format(q) line = nil # This handles a special case where you've got a block of statements where # the only value is a comment. In that case a lot of nodes like # brace_block will attempt to format as a single line, but since that # wouldn't work with a comment, we intentionally break the parent group. if body.length == 2 void_stmt, comment = body if void_stmt.is_a?(VoidStmt) && comment.is_a?(Comment) q.format(comment) q.break_parent return end end access_controls = Hash.new do |hash, node| hash[node] = node.is_a?(VCall) && %w[private protected public].include?(node.value.value) end body.each_with_index do |statement, index| next if statement.is_a?(VoidStmt) if line.nil? q.format(statement) elsif (statement.location.start_line - line) > 1 q.breakable(force: true) q.breakable(force: true) q.format(statement) elsif access_controls[statement] || access_controls[body[index - 1]] q.breakable(force: true) q.breakable(force: true) q.format(statement) elsif statement.location.start_line != line q.breakable(force: true) q.format(statement) elsif !q.parent.is_a?(StringEmbExpr) q.breakable(force: true) q.format(statement) else q.text("; ") q.format(statement) end line = statement.location.end_line end end |
#pretty_print(q) ⇒ Object
10941 10942 10943 10944 10945 10946 10947 10948 10949 10950 |
# File 'lib/syntax_tree.rb', line 10941 def pretty_print(q) q.group(2, "(", ")") do q.text("statements") q.breakable q.seplist(body) { |statement| q.pp(statement) } q.pp(Comment::List.new(comments)) end end |
#to_json(*opts) ⇒ Object
10952 10953 10954 10955 10956 |
# File 'lib/syntax_tree.rb', line 10952 def to_json(*opts) { type: :statements, body: body, loc: location, cmts: comments }.to_json( *opts ) end |