@@ -101,127 +101,6 @@ def result
101101
102102 end # Noop
103103
104- # Preprocessor for dynamic string regexp and xtr nodes. Collapses adjacent string segments into one.
105- class CollapseStrChildren < self
106-
107- register :dstr
108- register :regexp
109- register :xstr
110-
111- # Return preprocessor result
112- #
113- # @return [Parser::AST::Node]
114- #
115- # @api private
116- #
117- def result
118- node . updated ( nil , collapsed_children )
119- end
120-
121- private
122-
123- # Return collapsed children
124- #
125- # @return [Array<Parser::AST::Node>]
126- #
127- # @api private
128- #
129- def collapsed_children
130- chunked_children . each_with_object ( [ ] ) do |( type , nodes ) , aggregate |
131- if type . equal? ( :str )
132- aggregate << s ( :str , nodes . map { |node | node . children . first } . join )
133- else
134- aggregate . concat ( nodes )
135- end
136- end
137- end
138- memoize :collapsed_children
139-
140- # Return chunked children
141- #
142- # @return [Array<Parser::AST::Node>]
143- #
144- # @api private
145- #
146- def chunked_children
147- visited_children . chunk ( &:type )
148- end
149-
150- end # CollapseStrChildren
151-
152- # Preprocessor flattening unneeded dstr nesting. This happens when a dstr is generated from
153- # implicit string concatenation, and one of the strings being concatenated is itself a dstr.
154- #
155- # Example: "foo" "bar #{baz}"
156- #
157- # Without this preprocessor, this turns into: "foo#{"bar#{baz}"}"
158- # With this preprocessor, this turns into: "foobar #{baz}"
159- class FlattenImplicitDSTR < self
160-
161- # No need to register :dsym, because Ruby doesn't do implicit concatenation of symbols
162- register :dstr
163-
164- FLATTEN_CHILDREN = IceNine . deep_freeze ( [ :dstr , :str ] )
165-
166- # Return preprocessor result
167- #
168- # @return [Parser::AST::Node]
169- #
170- # @api private
171- #
172- def result
173- return node unless implicit_dstr?
174-
175- flat_children = children . flat_map do |child |
176- child . type . equal? ( :dstr ) ? child . children : child
177- end
178- node . updated ( nil , flat_children )
179- end
180-
181- private
182-
183- # Test for implicit dstr
184- #
185- # This should only ever be true for dstr nodes that are a result of implicit concatenation
186- # (see the comments on this class). Any other dstr node would have a :begin node as a child.
187- #
188- # @return [Boolean]
189- #
190- # @api private
191- #
192- def implicit_dstr?
193- children . map ( &:type ) . all? ( &FLATTEN_CHILDREN . method ( :include? ) )
194- end
195-
196- end # FlattenImplicitDSTR
197-
198- # Preprocessor eliminating unneeded dstr nodes
199- class CompactDSTR < self
200-
201- register :dstr
202- register :dsym
203-
204- MAP = IceNine . deep_freeze (
205- dstr : :str ,
206- dsym : :sym
207- )
208-
209- # Return preprocessor result
210- #
211- # @return [Parser::AST::Node]
212- #
213- # @api private
214- #
215- def result
216- if children . any? && children . all? { |child | child . type . equal? ( :str ) }
217- node . updated ( MAP . fetch ( node . type ) , [ children . map { |child | child . children . first } . join ] )
218- else
219- node
220- end
221- end
222-
223- end # CompactDSTR
224-
225104 # Preprocessor transforming numeric nodes with infinity as value to round trippable aequivalent.
226105 class Infinity < self
227106
0 commit comments