/
parse_helper.rb
318 lines (256 loc) · 9.16 KB
/
parse_helper.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
# frozen_string_literal: true
module ParseHelper
include AST::Sexp
require 'parser/all'
require 'parser/macruby'
require 'parser/rubymotion'
ALL_VERSIONS = %w(1.8 1.9 2.0 2.1 2.2 2.3 2.4 2.5 2.6 2.7 2.8 mac ios)
def setup
@diagnostics = []
super if defined?(super)
end
def parser_for_ruby_version(version)
case version
when '1.8' then parser = Parser::Ruby18.new
when '1.9' then parser = Parser::Ruby19.new
when '2.0' then parser = Parser::Ruby20.new
when '2.1' then parser = Parser::Ruby21.new
when '2.2' then parser = Parser::Ruby22.new
when '2.3' then parser = Parser::Ruby23.new
when '2.4' then parser = Parser::Ruby24.new
when '2.5' then parser = Parser::Ruby25.new
when '2.6' then parser = Parser::Ruby26.new
when '2.7' then parser = Parser::Ruby27.new
when '2.8' then parser = Parser::Ruby28.new
when 'mac' then parser = Parser::MacRuby.new
when 'ios' then parser = Parser::RubyMotion.new
else raise "Unrecognized Ruby version #{version}"
end
parser.diagnostics.consumer = lambda do |diagnostic|
@diagnostics << diagnostic
end
parser
end
def with_versions(versions)
(versions & ALL_VERSIONS).each do |version|
@diagnostics.clear
parser = parser_for_ruby_version(version)
yield version, parser
end
end
def assert_source_range(begin_pos, end_pos, range, version, what)
assert range.is_a?(Parser::Source::Range),
"(#{version}) #{range.inspect}.is_a?(Source::Range) for #{what}"
assert_equal begin_pos, range.begin_pos,
"(#{version}) begin of #{what}"
assert_equal end_pos, range.end_pos,
"(#{version}) end of #{what}"
end
# Use like this:
# ~~~
# assert_parses(
# s(:send, s(:lit, 10), :+, s(:lit, 20))
# %q{10 + 20},
# %q{~~~~~~~ expression
# | ^ operator
# | ~~ expression (lit)
# },
# %w(1.8 1.9) # optional
# )
# ~~~
def assert_parses(ast, code, source_maps='', versions=ALL_VERSIONS)
with_versions(versions) do |version, parser|
try_parsing(ast, code, parser, source_maps, version)
end
# Also try parsing with lexer set to use UTF-32LE internally
with_versions(versions) do |version, parser|
parser.instance_eval { @lexer.force_utf32 = true }
try_parsing(ast, code, parser, source_maps, version)
end
end
def try_parsing(ast, code, parser, source_maps, version)
source_file = Parser::Source::Buffer.new('(assert_parses)')
source_file.source = code
begin
parsed_ast = parser.parse(source_file)
rescue => exc
backtrace = exc.backtrace
Exception.instance_method(:initialize).bind(exc).
call("(#{version}) #{exc.message}")
exc.set_backtrace(backtrace)
raise
end
if ast.nil?
assert_nil parsed_ast, "(#{version}) AST equality"
return
end
assert_equal ast, parsed_ast,
"(#{version}) AST equality"
parse_source_map_descriptions(source_maps) do |begin_pos, end_pos, map_field, ast_path, line|
astlet = traverse_ast(parsed_ast, ast_path)
if astlet.nil?
# This is a testsuite bug.
raise "No entity with AST path #{ast_path} in #{parsed_ast.inspect}"
end
assert astlet.frozen?
assert astlet.location.respond_to?(map_field),
"(#{version}) #{astlet.location.inspect}.respond_to?(#{map_field.inspect}) for:\n#{parsed_ast.inspect}"
range = astlet.location.send(map_field)
assert_source_range(begin_pos, end_pos, range, version, line.inspect)
end
assert parser.instance_eval { @lexer }.cmdarg.empty?,
"(#{version}) expected cmdarg to be empty after parsing"
assert_equal 0, parser.instance_eval { @lexer.instance_eval { @paren_nest } },
"(#{version}) expected paren_nest to be 0 after parsing"
end
# Use like this:
# ~~~
# assert_diagnoses(
# [:warning, :ambiguous_prefix, { prefix: '*' }],
# %q{foo *bar},
# %q{ ^ location
# | ~~~ highlights (0)})
# ~~~
def assert_diagnoses(diagnostic, code, source_maps='', versions=ALL_VERSIONS)
with_versions(versions) do |version, parser|
source_file = Parser::Source::Buffer.new('(assert_diagnoses)')
source_file.source = code
begin
parser = parser.parse(source_file)
rescue Parser::SyntaxError
# do nothing; the diagnostic was reported
end
assert_equal 1, @diagnostics.count,
"(#{version}) emits a single diagnostic, not\n" \
"#{@diagnostics.map(&:render).join("\n")}"
emitted_diagnostic = @diagnostics.first
level, reason, arguments = diagnostic
arguments ||= {}
message = Parser::Messages.compile(reason, arguments)
assert_equal level, emitted_diagnostic.level
assert_equal reason, emitted_diagnostic.reason
assert_equal arguments, emitted_diagnostic.arguments
assert_equal message, emitted_diagnostic.message
parse_source_map_descriptions(source_maps) do |begin_pos, end_pos, map_field, ast_path, line|
case map_field
when 'location'
assert_source_range begin_pos, end_pos,
emitted_diagnostic.location,
version, 'location'
when 'highlights'
index = ast_path.first.to_i
assert_source_range begin_pos, end_pos,
emitted_diagnostic.highlights[index],
version, "#{index}th highlight"
else
raise "Unknown diagnostic range #{map_field}"
end
end
end
end
# Use like this:
# ~~~
# assert_diagnoses_many(
# [
# [:warning, :ambiguous_literal],
# [:error, :unexpected_token, { :token => :tLCURLY }]
# ],
# %q{m /foo/ {}},
# SINCE_2_4)
# ~~~
def assert_diagnoses_many(diagnostics, code, versions=ALL_VERSIONS)
with_versions(versions) do |version, parser|
source_file = Parser::Source::Buffer.new('(assert_diagnoses_many)')
source_file.source = code
begin
parser = parser.parse(source_file)
rescue Parser::SyntaxError
# do nothing; the diagnostic was reported
end
assert_equal diagnostics.count, @diagnostics.count
diagnostics.zip(@diagnostics) do |expected_diagnostic, actual_diagnostic|
level, reason, arguments = expected_diagnostic
arguments ||= {}
message = Parser::Messages.compile(reason, arguments)
assert_equal level, actual_diagnostic.level
assert_equal reason, actual_diagnostic.reason
assert_equal arguments, actual_diagnostic.arguments
assert_equal message, actual_diagnostic.message
end
end
end
def refute_diagnoses(code, versions=ALL_VERSIONS)
with_versions(versions) do |version, parser|
source_file = Parser::Source::Buffer.new('(refute_diagnoses)')
source_file.source = code
begin
parser = parser.parse(source_file)
rescue Parser::SyntaxError
# do nothing; the diagnostic was reported
end
assert_empty @diagnostics,
"(#{version}) emits no diagnostics, not\n" \
"#{@diagnostics.map(&:render).join("\n")}"
end
end
def assert_context(context, code, versions=ALL_VERSIONS)
with_versions(versions) do |version, parser|
source_file = Parser::Source::Buffer.new('(assert_context)')
source_file.source = code
begin
parser.parse(source_file)
rescue Parser::SyntaxError
# do nothing; the diagnostic was reported
end
assert_equal parser.context.stack, context, "(#{version}) parsing context"
end
end
SOURCE_MAP_DESCRIPTION_RE =
/(?x)
^(?# $1 skip) ^(\s*)
(?# $2 highlight) ([~\^]+)
\s+
(?# $3 source_map_field) ([a-z_]+)
(?# $5 ast_path) (\s+\(([a-z_.\/0-9]+)\))?
$/
def parse_source_map_descriptions(descriptions)
unless block_given?
return to_enum(:parse_source_map_descriptions, descriptions)
end
descriptions.each_line do |line|
# Remove leading " |", if it exists.
line = line.sub(/^\s*\|/, '').rstrip
next if line.empty?
if (match = SOURCE_MAP_DESCRIPTION_RE.match(line))
begin_pos = match[1].length
end_pos = begin_pos + match[2].length
source_map_field = match[3]
if match[5]
ast_path = match[5].split('.')
else
ast_path = []
end
yield begin_pos, end_pos, source_map_field, ast_path, line
else
raise "Cannot parse source map description line: #{line.inspect}."
end
end
end
def traverse_ast(ast, path)
path.inject(ast) do |astlet, path_component|
# Split "dstr/2" to :dstr and 1
type_str, index_str = path_component.split('/')
type = type_str.to_sym
if index_str.nil?
index = 0
else
index = index_str.to_i - 1
end
matching_children = \
astlet.children.select do |child|
AST::Node === child && child.type == type
end
matching_children[index]
end
end
end