Debian Codegraph @

lib/prism/parse_result/newlines.rb

raw symbols SCIP
1 # frozen_string_literal: true
2 # :markup: markdown
3
4 module Prism
5 class ParseResult < Result
6 # The :line tracepoint event gets fired whenever the Ruby VM encounters an
7 # expression on a new line. The types of expressions that can trigger this
8 # event are:
9 #
10 # * if statements
11 # * unless statements
12 # * nodes that are children of statements lists
13 #
14 # In order to keep track of the newlines, we have a list of offsets that
15 # come back from the parser. We assign these offsets to the first nodes that
16 # we find in the tree that are on those lines.
17 #
18 # Note that the logic in this file should be kept in sync with the Java
19 # MarkNewlinesVisitor, since that visitor is responsible for marking the
20 # newlines for JRuby/TruffleRuby.
21 #
22 # This file is autoloaded only when `mark_newlines!` is called, so the
23 # re-opening of the various nodes in this file will only be performed in
24 # that case. We do that to avoid storing the extra `@newline` instance
25 # variable on every node if we don't need it.
26 class Newlines < Visitor
27 # Create a new Newlines visitor with the given newline offsets.
28 def initialize(lines)
29 # @type var lines: Integer
30 @lines = Array.new(1 + lines, false)
31 end
32
33 # Permit block/lambda nodes to mark newlines within themselves.
34 def visit_block_node(node)
35 old_lines = @lines
36 @lines = Array.new(old_lines.size, false)
37
38 begin
39 super(node)
40 ensure
41 @lines = old_lines
42 end
43 end
44
45 alias_method :visit_lambda_node, :visit_block_node
46
47 # Mark if/unless nodes as newlines.
48 def visit_if_node(node)
49 node.newline_flag!(@lines)
50 super(node)
51 end
52
53 alias_method :visit_unless_node, :visit_if_node
54
55 # Permit statements lists to mark newlines within themselves.
56 def visit_statements_node(node)
57 node.body.each do |child|
58 child.newline_flag!(@lines)
59 end
60 super(node)
61 end
62 end
63 end
64
65 class Node
66 def newline_flag? # :nodoc:
67 !!defined?(@newline_flag)
68 end
69
70 def newline_flag!(lines) # :nodoc:
71 line = location.start_line
72 unless lines[line]
73 lines[line] = true
74 @newline_flag = true
75 end
76 end
77 end
78
79 class BeginNode < Node
80 def newline_flag!(lines) # :nodoc:
81 # Never mark BeginNode with a newline flag, mark children instead.
82 end
83 end
84
85 class ParenthesesNode < Node
86 def newline_flag!(lines) # :nodoc:
87 # Never mark ParenthesesNode with a newline flag, mark children instead.
88 end
89 end
90
91 class IfNode < Node
92 def newline_flag!(lines) # :nodoc:
93 predicate.newline_flag!(lines)
94 end
95 end
96
97 class UnlessNode < Node
98 def newline_flag!(lines) # :nodoc:
99 predicate.newline_flag!(lines)
100 end
101 end
102
103 class UntilNode < Node
104 def newline_flag!(lines) # :nodoc:
105 predicate.newline_flag!(lines)
106 end
107 end
108
109 class WhileNode < Node
110 def newline_flag!(lines) # :nodoc:
111 predicate.newline_flag!(lines)
112 end
113 end
114
115 class RescueModifierNode < Node
116 def newline_flag!(lines) # :nodoc:
117 expression.newline_flag!(lines)
118 end
119 end
120
121 class InterpolatedMatchLastLineNode < Node
122 def newline_flag!(lines) # :nodoc:
123 first = parts.first
124 first.newline_flag!(lines) if first
125 end
126 end
127
128 class InterpolatedRegularExpressionNode < Node
129 def newline_flag!(lines) # :nodoc:
130 first = parts.first
131 first.newline_flag!(lines) if first
132 end
133 end
134
135 class InterpolatedStringNode < Node
136 def newline_flag!(lines) # :nodoc:
137 first = parts.first
138 first.newline_flag!(lines) if first
139 end
140 end
141
142 class InterpolatedSymbolNode < Node
143 def newline_flag!(lines) # :nodoc:
144 first = parts.first
145 first.newline_flag!(lines) if first
146 end
147 end
148
149 class InterpolatedXStringNode < Node
150 def newline_flag!(lines) # :nodoc:
151 first = parts.first
152 first.newline_flag!(lines) if first
153 end
154 end
155 end