ed-precompiled_prism 1.5.2
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.
- checksums.yaml +7 -0
- data/BSDmakefile +58 -0
- data/CHANGELOG.md +723 -0
- data/CODE_OF_CONDUCT.md +76 -0
- data/CONTRIBUTING.md +58 -0
- data/LICENSE.md +7 -0
- data/Makefile +110 -0
- data/README.md +143 -0
- data/config.yml +4714 -0
- data/docs/build_system.md +119 -0
- data/docs/configuration.md +68 -0
- data/docs/cruby_compilation.md +27 -0
- data/docs/design.md +53 -0
- data/docs/encoding.md +121 -0
- data/docs/fuzzing.md +88 -0
- data/docs/heredocs.md +36 -0
- data/docs/javascript.md +118 -0
- data/docs/local_variable_depth.md +229 -0
- data/docs/mapping.md +117 -0
- data/docs/parser_translation.md +24 -0
- data/docs/parsing_rules.md +22 -0
- data/docs/releasing.md +98 -0
- data/docs/relocation.md +34 -0
- data/docs/ripper_translation.md +72 -0
- data/docs/ruby_api.md +44 -0
- data/docs/ruby_parser_translation.md +19 -0
- data/docs/serialization.md +233 -0
- data/docs/testing.md +55 -0
- data/ext/prism/api_node.c +6941 -0
- data/ext/prism/api_pack.c +276 -0
- data/ext/prism/extconf.rb +127 -0
- data/ext/prism/extension.c +1419 -0
- data/ext/prism/extension.h +19 -0
- data/include/prism/ast.h +8220 -0
- data/include/prism/defines.h +260 -0
- data/include/prism/diagnostic.h +456 -0
- data/include/prism/encoding.h +283 -0
- data/include/prism/node.h +129 -0
- data/include/prism/options.h +482 -0
- data/include/prism/pack.h +163 -0
- data/include/prism/parser.h +933 -0
- data/include/prism/prettyprint.h +34 -0
- data/include/prism/regexp.h +43 -0
- data/include/prism/static_literals.h +121 -0
- data/include/prism/util/pm_buffer.h +236 -0
- data/include/prism/util/pm_char.h +204 -0
- data/include/prism/util/pm_constant_pool.h +218 -0
- data/include/prism/util/pm_integer.h +130 -0
- data/include/prism/util/pm_list.h +103 -0
- data/include/prism/util/pm_memchr.h +29 -0
- data/include/prism/util/pm_newline_list.h +113 -0
- data/include/prism/util/pm_string.h +200 -0
- data/include/prism/util/pm_strncasecmp.h +32 -0
- data/include/prism/util/pm_strpbrk.h +46 -0
- data/include/prism/version.h +29 -0
- data/include/prism.h +408 -0
- data/lib/prism/compiler.rb +801 -0
- data/lib/prism/desugar_compiler.rb +392 -0
- data/lib/prism/dispatcher.rb +2210 -0
- data/lib/prism/dot_visitor.rb +4762 -0
- data/lib/prism/dsl.rb +1003 -0
- data/lib/prism/ffi.rb +570 -0
- data/lib/prism/inspect_visitor.rb +2392 -0
- data/lib/prism/lex_compat.rb +928 -0
- data/lib/prism/mutation_compiler.rb +772 -0
- data/lib/prism/node.rb +18816 -0
- data/lib/prism/node_ext.rb +511 -0
- data/lib/prism/pack.rb +230 -0
- data/lib/prism/parse_result/comments.rb +188 -0
- data/lib/prism/parse_result/errors.rb +66 -0
- data/lib/prism/parse_result/newlines.rb +155 -0
- data/lib/prism/parse_result.rb +911 -0
- data/lib/prism/pattern.rb +269 -0
- data/lib/prism/polyfill/append_as_bytes.rb +15 -0
- data/lib/prism/polyfill/byteindex.rb +13 -0
- data/lib/prism/polyfill/scan_byte.rb +14 -0
- data/lib/prism/polyfill/unpack1.rb +14 -0
- data/lib/prism/polyfill/warn.rb +36 -0
- data/lib/prism/reflection.rb +416 -0
- data/lib/prism/relocation.rb +505 -0
- data/lib/prism/serialize.rb +2398 -0
- data/lib/prism/string_query.rb +31 -0
- data/lib/prism/translation/parser/builder.rb +62 -0
- data/lib/prism/translation/parser/compiler.rb +2234 -0
- data/lib/prism/translation/parser/lexer.rb +820 -0
- data/lib/prism/translation/parser.rb +374 -0
- data/lib/prism/translation/parser33.rb +13 -0
- data/lib/prism/translation/parser34.rb +13 -0
- data/lib/prism/translation/parser35.rb +13 -0
- data/lib/prism/translation/parser_current.rb +24 -0
- data/lib/prism/translation/ripper/sexp.rb +126 -0
- data/lib/prism/translation/ripper/shim.rb +5 -0
- data/lib/prism/translation/ripper.rb +3474 -0
- data/lib/prism/translation/ruby_parser.rb +1929 -0
- data/lib/prism/translation.rb +16 -0
- data/lib/prism/visitor.rb +813 -0
- data/lib/prism.rb +97 -0
- data/prism.gemspec +174 -0
- data/rbi/prism/compiler.rbi +12 -0
- data/rbi/prism/dsl.rbi +524 -0
- data/rbi/prism/inspect_visitor.rbi +12 -0
- data/rbi/prism/node.rbi +8734 -0
- data/rbi/prism/node_ext.rbi +107 -0
- data/rbi/prism/parse_result.rbi +404 -0
- data/rbi/prism/reflection.rbi +58 -0
- data/rbi/prism/string_query.rbi +12 -0
- data/rbi/prism/translation/parser.rbi +11 -0
- data/rbi/prism/translation/parser33.rbi +6 -0
- data/rbi/prism/translation/parser34.rbi +6 -0
- data/rbi/prism/translation/parser35.rbi +6 -0
- data/rbi/prism/translation/ripper.rbi +15 -0
- data/rbi/prism/visitor.rbi +473 -0
- data/rbi/prism.rbi +66 -0
- data/sig/prism/compiler.rbs +9 -0
- data/sig/prism/dispatcher.rbs +19 -0
- data/sig/prism/dot_visitor.rbs +6 -0
- data/sig/prism/dsl.rbs +351 -0
- data/sig/prism/inspect_visitor.rbs +22 -0
- data/sig/prism/lex_compat.rbs +10 -0
- data/sig/prism/mutation_compiler.rbs +159 -0
- data/sig/prism/node.rbs +4028 -0
- data/sig/prism/node_ext.rbs +149 -0
- data/sig/prism/pack.rbs +43 -0
- data/sig/prism/parse_result/comments.rbs +38 -0
- data/sig/prism/parse_result.rbs +196 -0
- data/sig/prism/pattern.rbs +13 -0
- data/sig/prism/reflection.rbs +50 -0
- data/sig/prism/relocation.rbs +185 -0
- data/sig/prism/serialize.rbs +8 -0
- data/sig/prism/string_query.rbs +11 -0
- data/sig/prism/visitor.rbs +169 -0
- data/sig/prism.rbs +254 -0
- data/src/diagnostic.c +850 -0
- data/src/encoding.c +5235 -0
- data/src/node.c +8676 -0
- data/src/options.c +328 -0
- data/src/pack.c +509 -0
- data/src/prettyprint.c +8941 -0
- data/src/prism.c +23361 -0
- data/src/regexp.c +790 -0
- data/src/serialize.c +2268 -0
- data/src/static_literals.c +617 -0
- data/src/token_type.c +703 -0
- data/src/util/pm_buffer.c +357 -0
- data/src/util/pm_char.c +318 -0
- data/src/util/pm_constant_pool.c +342 -0
- data/src/util/pm_integer.c +670 -0
- data/src/util/pm_list.c +49 -0
- data/src/util/pm_memchr.c +35 -0
- data/src/util/pm_newline_list.c +125 -0
- data/src/util/pm_string.c +381 -0
- data/src/util/pm_strncasecmp.c +36 -0
- data/src/util/pm_strpbrk.c +206 -0
- metadata +195 -0
data/lib/prism/pack.rb
ADDED
|
@@ -0,0 +1,230 @@
|
|
|
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
|
|
@@ -0,0 +1,188 @@
|
|
|
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
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
# :markup: markdown
|
|
3
|
+
|
|
4
|
+
require "stringio"
|
|
5
|
+
|
|
6
|
+
module Prism
|
|
7
|
+
class ParseResult < Result
|
|
8
|
+
# An object to represent the set of errors on a parse result. This object
|
|
9
|
+
# can be used to format the errors in a human-readable way.
|
|
10
|
+
class Errors
|
|
11
|
+
# The parse result that contains the errors.
|
|
12
|
+
attr_reader :parse_result
|
|
13
|
+
|
|
14
|
+
# Initialize a new set of errors from the given parse result.
|
|
15
|
+
def initialize(parse_result)
|
|
16
|
+
@parse_result = parse_result
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
# Formats the errors in a human-readable way and return them as a string.
|
|
20
|
+
def format
|
|
21
|
+
error_lines = {} #: Hash[Integer, Array[ParseError]]
|
|
22
|
+
parse_result.errors.each do |error|
|
|
23
|
+
location = error.location
|
|
24
|
+
(location.start_line..location.end_line).each do |line|
|
|
25
|
+
error_lines[line] ||= []
|
|
26
|
+
error_lines[line] << error
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
source_lines = parse_result.source.source.lines
|
|
31
|
+
source_lines << "" if error_lines.key?(source_lines.size + 1)
|
|
32
|
+
|
|
33
|
+
io = StringIO.new
|
|
34
|
+
source_lines.each.with_index(1) do |line, line_number|
|
|
35
|
+
io.puts(line)
|
|
36
|
+
|
|
37
|
+
(error_lines.delete(line_number) || []).each do |error|
|
|
38
|
+
location = error.location
|
|
39
|
+
|
|
40
|
+
case line_number
|
|
41
|
+
when location.start_line
|
|
42
|
+
io.print(" " * location.start_column + "^")
|
|
43
|
+
|
|
44
|
+
if location.start_line == location.end_line
|
|
45
|
+
if location.start_column != location.end_column
|
|
46
|
+
io.print("~" * (location.end_column - location.start_column - 1))
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
io.puts(" " + error.message)
|
|
50
|
+
else
|
|
51
|
+
io.puts("~" * (line.bytesize - location.start_column))
|
|
52
|
+
end
|
|
53
|
+
when location.end_line
|
|
54
|
+
io.puts("~" * location.end_column + " " + error.message)
|
|
55
|
+
else
|
|
56
|
+
io.puts("~" * line.bytesize)
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
io.puts
|
|
62
|
+
io.string
|
|
63
|
+
end
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
end
|
|
@@ -0,0 +1,155 @@
|
|
|
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
|