prism 1.9.0 → 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (258) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +144 -40
  3. data/CODE_OF_CONDUCT.md +1 -1
  4. data/LICENSE.md +1 -1
  5. data/Makefile +49 -20
  6. data/README.md +38 -98
  7. data/config.yml +174 -161
  8. data/docs/build_system.md +2 -2
  9. data/docs/configuration.md +5 -4
  10. data/docs/encoding.md +2 -2
  11. data/docs/fuzzing.md +9 -16
  12. data/docs/javascript.md +19 -4
  13. data/docs/mapping.md +1 -1
  14. data/docs/releasing.md +77 -5
  15. data/docs/relocation.md +2 -4
  16. data/docs/serialization.md +39 -28
  17. data/docs/testing.md +46 -29
  18. data/ext/prism/api_node.c +1527 -1467
  19. data/ext/prism/extconf.rb +3 -4
  20. data/ext/prism/extension.c +759 -418
  21. data/ext/prism/extension.h +3 -3
  22. data/include/prism/arena.h +45 -0
  23. data/include/prism/arena.hpp +42 -0
  24. data/include/prism/ast.h +2738 -611
  25. data/include/prism/ast.hpp +641 -0
  26. data/include/prism/buffer.h +60 -0
  27. data/include/prism/buffer.hpp +55 -0
  28. data/include/prism/comments.h +72 -0
  29. data/include/prism/compiler/accel.h +26 -0
  30. data/include/prism/compiler/align.h +36 -0
  31. data/include/prism/compiler/assume.h +25 -0
  32. data/include/prism/compiler/branch.h +26 -0
  33. data/include/prism/compiler/bswap.h +46 -0
  34. data/include/prism/compiler/cplusplus.hpp +16 -0
  35. data/include/prism/compiler/endianness.h +34 -0
  36. data/include/prism/compiler/exceptions.hpp +17 -0
  37. data/include/prism/compiler/exported.h +24 -0
  38. data/include/prism/compiler/fallthrough.h +22 -0
  39. data/include/prism/compiler/filesystem.h +32 -0
  40. data/include/prism/compiler/flex_array.h +19 -0
  41. data/include/prism/compiler/force_inline.h +21 -0
  42. data/include/prism/compiler/format.h +27 -0
  43. data/include/prism/compiler/inline.h +17 -0
  44. data/include/prism/compiler/lifetimebound.hpp +25 -0
  45. data/include/prism/compiler/nodiscard.h +22 -0
  46. data/include/prism/compiler/nonnull.h +18 -0
  47. data/include/prism/compiler/unused.h +18 -0
  48. data/include/prism/constant_pool.h +106 -0
  49. data/include/prism/diagnostic.h +55 -402
  50. data/include/prism/errors_format.h +61 -0
  51. data/include/prism/excludes.h +29 -0
  52. data/include/prism/integer.h +41 -0
  53. data/include/prism/internal/allocator.h +68 -0
  54. data/include/prism/internal/allocator_debug.h +88 -0
  55. data/include/prism/internal/arena.h +108 -0
  56. data/include/prism/internal/bit.h +42 -0
  57. data/include/prism/internal/buffer.h +94 -0
  58. data/include/prism/internal/char.h +145 -0
  59. data/include/prism/internal/comments.h +54 -0
  60. data/include/prism/internal/constant_pool.h +152 -0
  61. data/include/prism/internal/diagnostic.h +389 -0
  62. data/include/prism/{encoding.h → internal/encoding.h} +27 -68
  63. data/include/prism/internal/integer.h +68 -0
  64. data/include/prism/internal/isinf.h +16 -0
  65. data/include/prism/internal/line_offset_list.h +34 -0
  66. data/include/prism/{util/pm_list.h → internal/list.h} +11 -52
  67. data/include/prism/internal/magic_comments.h +23 -0
  68. data/include/prism/internal/memchr.h +15 -0
  69. data/include/prism/internal/node.h +42 -0
  70. data/include/prism/internal/options.h +232 -0
  71. data/include/prism/internal/parser.h +1015 -0
  72. data/include/prism/internal/regexp.h +26 -0
  73. data/include/prism/internal/serialize.h +39 -0
  74. data/include/prism/internal/source.h +72 -0
  75. data/include/prism/internal/static_literals.h +60 -0
  76. data/include/prism/internal/stringy.h +30 -0
  77. data/include/prism/{util/pm_strncasecmp.h → internal/strncasecmp.h} +3 -17
  78. data/include/prism/{util/pm_strpbrk.h → internal/strpbrk.h} +7 -20
  79. data/include/prism/internal/tokens.h +11 -0
  80. data/include/prism/internal/xxh3.h +16 -0
  81. data/include/prism/json.h +40 -0
  82. data/include/prism/line_offset_list.h +69 -0
  83. data/include/prism/magic_comments.h +53 -0
  84. data/include/prism/node.h +59 -49
  85. data/include/prism/node.hpp +272 -0
  86. data/include/prism/node_type.hpp +38 -0
  87. data/include/prism/options.h +154 -279
  88. data/include/prism/options.hpp +43 -0
  89. data/include/prism/parser.h +367 -870
  90. data/include/prism/parser.hpp +274 -0
  91. data/include/prism/prettyprint.h +14 -9
  92. data/include/prism/serialize.h +123 -0
  93. data/include/prism/source.h +163 -0
  94. data/include/prism/stream.h +36 -0
  95. data/include/prism/string_query.h +82 -0
  96. data/include/prism/stringy.h +61 -0
  97. data/include/prism/stringy.hpp +31 -0
  98. data/include/prism/version.h +20 -3
  99. data/include/prism.h +35 -320
  100. data/include/prism.hpp +24 -0
  101. data/lib/prism/compiler.rb +319 -305
  102. data/lib/prism/desugar_compiler.rb +105 -34
  103. data/lib/prism/dispatcher.rb +646 -473
  104. data/lib/prism/dot_visitor.rb +592 -511
  105. data/lib/prism/dsl.rb +391 -25
  106. data/lib/prism/ffi.rb +258 -90
  107. data/lib/prism/inspect_visitor.rb +357 -325
  108. data/lib/prism/lex_compat.rb +254 -212
  109. data/lib/prism/mutation_compiler.rb +313 -305
  110. data/lib/prism/node.rb +20966 -6992
  111. data/lib/prism/node_ext.rb +180 -187
  112. data/lib/prism/node_find.rb +215 -0
  113. data/lib/prism/parse_result/errors.rb +7 -1
  114. data/lib/prism/parse_result/newlines.rb +276 -8
  115. data/lib/prism/parse_result.rb +468 -139
  116. data/lib/prism/pattern.rb +65 -20
  117. data/lib/prism/reflection.rb +23 -12
  118. data/lib/prism/relocation.rb +149 -87
  119. data/lib/prism/serialize.rb +2889 -700
  120. data/lib/prism/string_query.rb +24 -1
  121. data/lib/prism/translation/parser/builder.rb +10 -2
  122. data/lib/prism/translation/parser/compiler.rb +44 -52
  123. data/lib/prism/translation/parser/lexer.rb +41 -92
  124. data/lib/prism/translation/parser.rb +1 -1
  125. data/lib/prism/translation/parser_current.rb +1 -1
  126. data/lib/prism/translation/ripper/lexer.rb +8 -10
  127. data/lib/prism/translation/ripper/sexp.rb +2 -10
  128. data/lib/prism/translation/ripper/shim.rb +2 -0
  129. data/lib/prism/translation/ripper.rb +967 -209
  130. data/lib/prism/translation/ruby_parser.rb +73 -316
  131. data/lib/prism/translation.rb +2 -0
  132. data/lib/prism/visitor.rb +632 -5
  133. data/lib/prism.rb +79 -9
  134. data/prism.gemspec +141 -62
  135. data/rbi/generated/prism/compiler.rbi +489 -0
  136. data/rbi/generated/prism/desugar_compiler.rbi +306 -0
  137. data/rbi/generated/prism/dispatcher.rbi +1138 -0
  138. data/rbi/generated/prism/dot_visitor.rbi +618 -0
  139. data/rbi/generated/prism/dsl.rbi +751 -0
  140. data/rbi/generated/prism/inspect_visitor.rbi +503 -0
  141. data/rbi/generated/prism/lex_compat.rbi +162 -0
  142. data/rbi/generated/prism/mutation_compiler.rbi +464 -0
  143. data/rbi/generated/prism/node.rbi +15143 -0
  144. data/rbi/generated/prism/node_ext.rbi +259 -0
  145. data/rbi/generated/prism/node_find.rbi +79 -0
  146. data/rbi/generated/prism/parse_result/errors.rbi +21 -0
  147. data/rbi/generated/prism/parse_result/newlines.rbi +208 -0
  148. data/rbi/generated/prism/parse_result.rbi +776 -0
  149. data/rbi/generated/prism/pattern.rbi +134 -0
  150. data/rbi/generated/prism/reflection.rbi +89 -0
  151. data/rbi/generated/prism/relocation.rbi +329 -0
  152. data/rbi/generated/prism/serialize.rbi +185 -0
  153. data/rbi/generated/prism/string_query.rbi +43 -0
  154. data/rbi/generated/prism/translation.rbi +7 -0
  155. data/rbi/generated/prism/visitor.rbi +650 -0
  156. data/rbi/generated/prism.rbi +86 -0
  157. data/rbi/prism/translation/parser_versions.rbi +5 -0
  158. data/rbi/rubyvm/node_find.rbi +14 -0
  159. data/sig/generated/prism/compiler.rbs +492 -0
  160. data/sig/generated/prism/desugar_compiler.rbs +305 -0
  161. data/sig/generated/prism/dispatcher.rbs +1292 -0
  162. data/sig/generated/prism/dot_visitor.rbs +635 -0
  163. data/sig/generated/prism/dsl.rbs +928 -0
  164. data/sig/generated/prism/inspect_visitor.rbs +509 -0
  165. data/sig/generated/prism/lex_compat.rbs +166 -0
  166. data/sig/{prism → generated/prism}/mutation_compiler.rbs +309 -4
  167. data/sig/generated/prism/node.rbs +21632 -0
  168. data/sig/generated/prism/node_ext.rbs +290 -0
  169. data/sig/generated/prism/node_find.rbs +90 -0
  170. data/sig/generated/prism/parse_result/errors.rbs +22 -0
  171. data/sig/generated/prism/parse_result/newlines.rbs +223 -0
  172. data/sig/generated/prism/parse_result.rbs +871 -0
  173. data/sig/generated/prism/pattern.rbs +156 -0
  174. data/sig/generated/prism/reflection.rbs +101 -0
  175. data/sig/generated/prism/relocation.rbs +428 -0
  176. data/sig/generated/prism/serialize.rbs +200 -0
  177. data/sig/generated/prism/string_query.rbs +43 -0
  178. data/sig/generated/prism/translation.rbs +8 -0
  179. data/sig/generated/prism/visitor.rbs +1111 -0
  180. data/sig/generated/prism.rbs +84 -0
  181. data/sig/manifest.yaml +2 -0
  182. data/src/arena.c +122 -0
  183. data/src/{util/pm_buffer.c → buffer.c} +53 -21
  184. data/src/{util/pm_char.c → char.c} +28 -57
  185. data/src/comments.c +455 -0
  186. data/src/constant_pool.c +406 -0
  187. data/src/diagnostic.c +120 -68
  188. data/src/encoding.c +8 -3
  189. data/src/errors_format.c +519 -0
  190. data/src/{util/pm_integer.c → integer.c} +38 -22
  191. data/src/json.c +5733 -0
  192. data/src/line_offset_list.c +100 -0
  193. data/src/list.c +24 -0
  194. data/src/{util/pm_memchr.c → memchr.c} +8 -6
  195. data/src/node.c +3465 -7787
  196. data/src/options.c +161 -54
  197. data/src/parser.c +371 -0
  198. data/src/prettyprint.c +421 -354
  199. data/src/prism.c +8503 -7280
  200. data/src/regexp.c +960 -45
  201. data/src/serialize.c +2434 -2048
  202. data/src/source.c +524 -0
  203. data/src/static_literals.c +303 -360
  204. data/src/string_query.c +180 -0
  205. data/src/stringy.c +61 -0
  206. data/src/{util/pm_strncasecmp.c → strncasecmp.c} +5 -4
  207. data/src/strpbrk.c +447 -0
  208. data/src/{token_type.c → tokens.c} +39 -18
  209. data/src/xxh3.c +292 -0
  210. metadata +141 -63
  211. data/ext/prism/api_pack.c +0 -276
  212. data/include/prism/defines.h +0 -260
  213. data/include/prism/pack.h +0 -163
  214. data/include/prism/regexp.h +0 -43
  215. data/include/prism/static_literals.h +0 -121
  216. data/include/prism/util/pm_buffer.h +0 -236
  217. data/include/prism/util/pm_char.h +0 -204
  218. data/include/prism/util/pm_constant_pool.h +0 -218
  219. data/include/prism/util/pm_integer.h +0 -130
  220. data/include/prism/util/pm_memchr.h +0 -29
  221. data/include/prism/util/pm_newline_list.h +0 -113
  222. data/include/prism/util/pm_string.h +0 -200
  223. data/lib/prism/pack.rb +0 -230
  224. data/lib/prism/parse_result/comments.rb +0 -188
  225. data/rbi/prism/compiler.rbi +0 -12
  226. data/rbi/prism/dsl.rbi +0 -524
  227. data/rbi/prism/inspect_visitor.rbi +0 -12
  228. data/rbi/prism/node.rbi +0 -8750
  229. data/rbi/prism/node_ext.rbi +0 -107
  230. data/rbi/prism/parse_result.rbi +0 -404
  231. data/rbi/prism/reflection.rbi +0 -58
  232. data/rbi/prism/string_query.rbi +0 -12
  233. data/rbi/prism/visitor.rbi +0 -473
  234. data/rbi/prism.rbi +0 -63
  235. data/sig/prism/compiler.rbs +0 -9
  236. data/sig/prism/dispatcher.rbs +0 -19
  237. data/sig/prism/dot_visitor.rbs +0 -6
  238. data/sig/prism/dsl.rbs +0 -351
  239. data/sig/prism/inspect_visitor.rbs +0 -22
  240. data/sig/prism/lex_compat.rbs +0 -10
  241. data/sig/prism/node.rbs +0 -4042
  242. data/sig/prism/node_ext.rbs +0 -149
  243. data/sig/prism/pack.rbs +0 -43
  244. data/sig/prism/parse_result/comments.rbs +0 -38
  245. data/sig/prism/parse_result.rbs +0 -197
  246. data/sig/prism/pattern.rbs +0 -13
  247. data/sig/prism/reflection.rbs +0 -50
  248. data/sig/prism/relocation.rbs +0 -185
  249. data/sig/prism/serialize.rbs +0 -8
  250. data/sig/prism/string_query.rbs +0 -11
  251. data/sig/prism/visitor.rbs +0 -169
  252. data/sig/prism.rbs +0 -272
  253. data/src/pack.c +0 -509
  254. data/src/util/pm_constant_pool.c +0 -342
  255. data/src/util/pm_list.c +0 -49
  256. data/src/util/pm_newline_list.c +0 -125
  257. data/src/util/pm_string.c +0 -381
  258. data/src/util/pm_strpbrk.c +0 -206
data/lib/prism/pack.rb DELETED
@@ -1,230 +0,0 @@
1
- # frozen_string_literal: true
2
- # :markup: markdown
3
- # typed: ignore
4
-
5
- #
6
- module Prism
7
- # A parser for the pack template language.
8
- module Pack
9
- %i[
10
- SPACE
11
- COMMENT
12
- INTEGER
13
- UTF8
14
- BER
15
- FLOAT
16
- STRING_SPACE_PADDED
17
- STRING_NULL_PADDED
18
- STRING_NULL_TERMINATED
19
- STRING_MSB
20
- STRING_LSB
21
- STRING_HEX_HIGH
22
- STRING_HEX_LOW
23
- STRING_UU
24
- STRING_MIME
25
- STRING_BASE64
26
- STRING_FIXED
27
- STRING_POINTER
28
- MOVE
29
- BACK
30
- NULL
31
-
32
- UNSIGNED
33
- SIGNED
34
- SIGNED_NA
35
-
36
- AGNOSTIC_ENDIAN
37
- LITTLE_ENDIAN
38
- BIG_ENDIAN
39
- NATIVE_ENDIAN
40
- ENDIAN_NA
41
-
42
- SIZE_SHORT
43
- SIZE_INT
44
- SIZE_LONG
45
- SIZE_LONG_LONG
46
- SIZE_8
47
- SIZE_16
48
- SIZE_32
49
- SIZE_64
50
- SIZE_P
51
- SIZE_NA
52
-
53
- LENGTH_FIXED
54
- LENGTH_MAX
55
- LENGTH_RELATIVE
56
- LENGTH_NA
57
- ].each do |const|
58
- const_set(const, const)
59
- end
60
-
61
- # A directive in the pack template language.
62
- class Directive
63
- # A symbol representing the version of Ruby.
64
- attr_reader :version
65
-
66
- # A symbol representing whether or not we are packing or unpacking.
67
- attr_reader :variant
68
-
69
- # A byteslice of the source string that this directive represents.
70
- attr_reader :source
71
-
72
- # The type of the directive.
73
- attr_reader :type
74
-
75
- # The type of signedness of the directive.
76
- attr_reader :signed
77
-
78
- # The type of endianness of the directive.
79
- attr_reader :endian
80
-
81
- # The size of the directive.
82
- attr_reader :size
83
-
84
- # The length type of this directive (used for integers).
85
- attr_reader :length_type
86
-
87
- # The length of this directive (used for integers).
88
- attr_reader :length
89
-
90
- # Initialize a new directive with the given values.
91
- def initialize(version, variant, source, type, signed, endian, size, length_type, length)
92
- @version = version
93
- @variant = variant
94
- @source = source
95
- @type = type
96
- @signed = signed
97
- @endian = endian
98
- @size = size
99
- @length_type = length_type
100
- @length = length
101
- end
102
-
103
- # The descriptions of the various types of endianness.
104
- ENDIAN_DESCRIPTIONS = {
105
- AGNOSTIC_ENDIAN: "agnostic",
106
- LITTLE_ENDIAN: "little-endian (VAX)",
107
- BIG_ENDIAN: "big-endian (network)",
108
- NATIVE_ENDIAN: "native-endian",
109
- ENDIAN_NA: "n/a"
110
- }
111
-
112
- # The descriptions of the various types of signedness.
113
- SIGNED_DESCRIPTIONS = {
114
- UNSIGNED: "unsigned",
115
- SIGNED: "signed",
116
- SIGNED_NA: "n/a"
117
- }
118
-
119
- # The descriptions of the various types of sizes.
120
- SIZE_DESCRIPTIONS = {
121
- SIZE_SHORT: "short",
122
- SIZE_INT: "int-width",
123
- SIZE_LONG: "long",
124
- SIZE_LONG_LONG: "long long",
125
- SIZE_8: "8-bit",
126
- SIZE_16: "16-bit",
127
- SIZE_32: "32-bit",
128
- SIZE_64: "64-bit",
129
- SIZE_P: "pointer-width"
130
- }
131
-
132
- # Provide a human-readable description of the directive.
133
- def describe
134
- case type
135
- when SPACE
136
- "whitespace"
137
- when COMMENT
138
- "comment"
139
- when INTEGER
140
- if size == SIZE_8
141
- base = "#{SIGNED_DESCRIPTIONS[signed]} #{SIZE_DESCRIPTIONS[size]} integer"
142
- else
143
- base = "#{SIGNED_DESCRIPTIONS[signed]} #{SIZE_DESCRIPTIONS[size]} #{ENDIAN_DESCRIPTIONS[endian]} integer"
144
- end
145
- case length_type
146
- when LENGTH_FIXED
147
- if length > 1
148
- base + ", x#{length}"
149
- else
150
- base
151
- end
152
- when LENGTH_MAX
153
- base + ", as many as possible"
154
- else
155
- raise
156
- end
157
- when UTF8
158
- "UTF-8 character"
159
- when BER
160
- "BER-compressed integer"
161
- when FLOAT
162
- "#{SIZE_DESCRIPTIONS[size]} #{ENDIAN_DESCRIPTIONS[endian]} float"
163
- when STRING_SPACE_PADDED
164
- "arbitrary binary string (space padded)"
165
- when STRING_NULL_PADDED
166
- "arbitrary binary string (null padded, count is width)"
167
- when STRING_NULL_TERMINATED
168
- "arbitrary binary string (null padded, count is width), except that null is added with *"
169
- when STRING_MSB
170
- "bit string (MSB first)"
171
- when STRING_LSB
172
- "bit string (LSB first)"
173
- when STRING_HEX_HIGH
174
- "hex string (high nibble first)"
175
- when STRING_HEX_LOW
176
- "hex string (low nibble first)"
177
- when STRING_UU
178
- "UU-encoded string"
179
- when STRING_MIME
180
- "quoted printable, MIME encoding"
181
- when STRING_BASE64
182
- "base64 encoded string"
183
- when STRING_FIXED
184
- "pointer to a structure (fixed-length string)"
185
- when STRING_POINTER
186
- "pointer to a null-terminated string"
187
- when MOVE
188
- "move to absolute position"
189
- when BACK
190
- "back up a byte"
191
- when NULL
192
- "null byte"
193
- else
194
- raise
195
- end
196
- end
197
- end
198
-
199
- # The result of parsing a pack template.
200
- class Format
201
- # A list of the directives in the template.
202
- attr_reader :directives
203
-
204
- # The encoding of the template.
205
- attr_reader :encoding
206
-
207
- # Create a new Format with the given directives and encoding.
208
- def initialize(directives, encoding)
209
- @directives = directives
210
- @encoding = encoding
211
- end
212
-
213
- # Provide a human-readable description of the format.
214
- def describe
215
- source_width = directives.map { |d| d.source.inspect.length }.max
216
- directive_lines = directives.map do |directive|
217
- if directive.type == SPACE
218
- source = directive.source.inspect
219
- else
220
- source = directive.source
221
- end
222
- # @type var source_width: Integer
223
- " #{source.ljust(source_width)} #{directive.describe}"
224
- end
225
-
226
- (["Directives:"] + directive_lines + ["Encoding:", " #{encoding}"]).join("\n")
227
- end
228
- end
229
- end
230
- end
@@ -1,188 +0,0 @@
1
- # frozen_string_literal: true
2
- # :markup: markdown
3
-
4
- module Prism
5
- class ParseResult < Result
6
- # When we've parsed the source, we have both the syntax tree and the list of
7
- # comments that we found in the source. This class is responsible for
8
- # walking the tree and finding the nearest location to attach each comment.
9
- #
10
- # It does this by first finding the nearest locations to each comment.
11
- # Locations can either come from nodes directly or from location fields on
12
- # nodes. For example, a `ClassNode` has an overall location encompassing the
13
- # entire class, but it also has a location for the `class` keyword.
14
- #
15
- # Once the nearest locations are found, it determines which one to attach
16
- # to. If it's a trailing comment (a comment on the same line as other source
17
- # code), it will favor attaching to the nearest location that occurs before
18
- # the comment. Otherwise it will favor attaching to the nearest location
19
- # that is after the comment.
20
- class Comments
21
- # A target for attaching comments that is based on a specific node's
22
- # location.
23
- class NodeTarget # :nodoc:
24
- attr_reader :node
25
-
26
- def initialize(node)
27
- @node = node
28
- end
29
-
30
- def start_offset
31
- node.start_offset
32
- end
33
-
34
- def end_offset
35
- node.end_offset
36
- end
37
-
38
- def encloses?(comment)
39
- start_offset <= comment.location.start_offset &&
40
- comment.location.end_offset <= end_offset
41
- end
42
-
43
- def leading_comment(comment)
44
- node.location.leading_comment(comment)
45
- end
46
-
47
- def trailing_comment(comment)
48
- node.location.trailing_comment(comment)
49
- end
50
- end
51
-
52
- # A target for attaching comments that is based on a location field on a
53
- # node. For example, the `end` token of a ClassNode.
54
- class LocationTarget # :nodoc:
55
- attr_reader :location
56
-
57
- def initialize(location)
58
- @location = location
59
- end
60
-
61
- def start_offset
62
- location.start_offset
63
- end
64
-
65
- def end_offset
66
- location.end_offset
67
- end
68
-
69
- def encloses?(comment)
70
- false
71
- end
72
-
73
- def leading_comment(comment)
74
- location.leading_comment(comment)
75
- end
76
-
77
- def trailing_comment(comment)
78
- location.trailing_comment(comment)
79
- end
80
- end
81
-
82
- # The parse result that we are attaching comments to.
83
- attr_reader :parse_result
84
-
85
- # Create a new Comments object that will attach comments to the given
86
- # parse result.
87
- def initialize(parse_result)
88
- @parse_result = parse_result
89
- end
90
-
91
- # Attach the comments to their respective locations in the tree by
92
- # mutating the parse result.
93
- def attach!
94
- parse_result.comments.each do |comment|
95
- preceding, enclosing, following = nearest_targets(parse_result.value, comment)
96
-
97
- if comment.trailing?
98
- if preceding
99
- preceding.trailing_comment(comment)
100
- else
101
- (following || enclosing || NodeTarget.new(parse_result.value)).leading_comment(comment)
102
- end
103
- else
104
- # If a comment exists on its own line, prefer a leading comment.
105
- if following
106
- following.leading_comment(comment)
107
- elsif preceding
108
- preceding.trailing_comment(comment)
109
- else
110
- (enclosing || NodeTarget.new(parse_result.value)).leading_comment(comment)
111
- end
112
- end
113
- end
114
- end
115
-
116
- private
117
-
118
- # Responsible for finding the nearest targets to the given comment within
119
- # the context of the given encapsulating node.
120
- def nearest_targets(node, comment)
121
- comment_start = comment.location.start_offset
122
- comment_end = comment.location.end_offset
123
-
124
- targets = [] #: Array[_Target]
125
- node.comment_targets.map do |value|
126
- case value
127
- when StatementsNode
128
- targets.concat(value.body.map { |node| NodeTarget.new(node) })
129
- when Node
130
- targets << NodeTarget.new(value)
131
- when Location
132
- targets << LocationTarget.new(value)
133
- end
134
- end
135
-
136
- targets.sort_by!(&:start_offset)
137
- preceding = nil #: _Target?
138
- following = nil #: _Target?
139
-
140
- left = 0
141
- right = targets.length
142
-
143
- # This is a custom binary search that finds the nearest nodes to the
144
- # given comment. When it finds a node that completely encapsulates the
145
- # comment, it recurses downward into the tree.
146
- while left < right
147
- middle = (left + right) / 2
148
- target = targets[middle]
149
-
150
- target_start = target.start_offset
151
- target_end = target.end_offset
152
-
153
- if target.encloses?(comment)
154
- # @type var target: NodeTarget
155
- # The comment is completely contained by this target. Abandon the
156
- # binary search at this level.
157
- return nearest_targets(target.node, comment)
158
- end
159
-
160
- if target_end <= comment_start
161
- # This target falls completely before the comment. Because we will
162
- # never consider this target or any targets before it again, this
163
- # target must be the closest preceding target we have encountered so
164
- # far.
165
- preceding = target
166
- left = middle + 1
167
- next
168
- end
169
-
170
- if comment_end <= target_start
171
- # This target falls completely after the comment. Because we will
172
- # never consider this target or any targets after it again, this
173
- # target must be the closest following target we have encountered so
174
- # far.
175
- following = target
176
- right = middle
177
- next
178
- end
179
-
180
- # This should only happen if there is a bug in this parser.
181
- raise "Comment location overlaps with a target location"
182
- end
183
-
184
- [preceding, NodeTarget.new(node), following]
185
- end
186
- end
187
- end
188
- end
@@ -1,12 +0,0 @@
1
- # typed: strict
2
-
3
- class Prism::Compiler
4
- sig { params(node: T.nilable(Prism::Node)).returns(T.untyped) }
5
- def visit(node); end
6
-
7
- sig { params(nodes: T::Array[T.nilable(Prism::Node)]).returns(T::Array[T.untyped]) }
8
- def visit_all(nodes); end
9
-
10
- sig { params(node: Prism::Node).returns(T::Array[T.untyped]) }
11
- def visit_child_nodes(node); end
12
- end