@@ -99,6 +99,10 @@ function triple_string_roughly_equal(fl_str, str)
99
99
return true
100
100
end
101
101
102
+ function exprs_equal_no_linenum (fl_ex, ex)
103
+ remove_all_linenums! (deepcopy (ex)) == remove_all_linenums! (deepcopy (fl_ex))
104
+ end
105
+
102
106
# Compare Expr from reference parser expression to JuliaSyntax parser, ignoring
103
107
# differences due to bugs in the reference parser.
104
108
function exprs_roughly_equal (fl_ex, ex)
@@ -143,6 +147,15 @@ function exprs_roughly_equal(fl_ex, ex)
143
147
if (h == :global || h == :local ) && length (args) == 1 && Meta. isexpr (args[1 ], :tuple )
144
148
# Allow invalid syntax like `global (x, y)`
145
149
args = args[1 ]. args
150
+ elseif h == :function && Meta. isexpr (fl_args[1 ], :block )
151
+ blockargs = filter (x-> ! (x isa LineNumberNode), fl_args[1 ]. args)
152
+ ps = blockargs[2 : end ]
153
+ for i = 1 : length (ps)
154
+ if Meta. isexpr (ps[i], :(= ))
155
+ ps[i] = Expr (:kw , ps[i]. args... )
156
+ end
157
+ end
158
+ fl_args[1 ] = Expr (:tuple , Expr (:parameters , ps... ), blockargs[1 ])
146
159
end
147
160
if length (fl_args) != length (args)
148
161
return false
@@ -163,7 +176,8 @@ function exprs_roughly_equal(fl_ex, ex)
163
176
return true
164
177
end
165
178
166
- function parsers_agree_on_file (filename; show_diff= false )
179
+ function parsers_agree_on_file (filename; exprs_equal= exprs_equal_no_linenum,
180
+ show_diff= false )
167
181
text = try
168
182
read (filename, String)
169
183
catch
@@ -185,9 +199,7 @@ function parsers_agree_on_file(filename; show_diff=false)
185
199
if show_diff && ex != fl_ex
186
200
show_expr_text_diff (stdout , show, ex, fl_ex)
187
201
end
188
- return ! JuliaSyntax. any_error (stream) &&
189
- JuliaSyntax. remove_linenums! (ex) ==
190
- JuliaSyntax. remove_linenums! (fl_ex)
202
+ return ! JuliaSyntax. any_error (stream) && exprs_equal (fl_ex, ex)
191
203
# Could alternatively use
192
204
# exprs_roughly_equal(fl_ex, ex)
193
205
catch exc
@@ -218,27 +230,29 @@ end
218
230
219
231
# Check whether a given SyntaxNode converts to the same Expr as the flisp
220
232
# parser produces from the source text of the node.
221
- function equals_flisp_parse (tree)
233
+ function equals_flisp_parse (exprs_equal, tree)
222
234
node_text = sourcetext (tree)
223
235
# Reparse with JuliaSyntax. This is a crude way to ensure we're not missing
224
236
# some context from the parent node.
225
- ex = parseall (Expr, node_text)
226
- fl_ex = fl_parseall (node_text)
227
- if Meta. isexpr (fl_ex, :error )
228
- return true # Something went wrong in reduction; ignore these cases 😬
237
+ fl_ex = fl_parseall (node_text, filename= " none" )
238
+ if Meta. isexpr (fl_ex, :error ) || (Meta. isexpr (fl_ex, :toplevel ) &&
239
+ length (fl_ex. args) >= 1 &&
240
+ Meta. isexpr (fl_ex. args[end ], :error ))
241
+ return true # Something went wrong in reduction; ignore these cases 😬
229
242
end
230
- remove_all_linenums! (ex) == remove_all_linenums! (fl_ex)
243
+ ex = parseall (Expr, node_text, filename= " none" , ignore_errors= true )
244
+ exprs_equal (fl_ex, ex)
231
245
end
232
246
233
247
"""
234
- reduce_test(text::AbstractString)
235
- reduce_test(tree::SyntaxNode)
248
+ reduce_test(text::AbstractString; exprs_equal=exprs_equal_no_linenum )
249
+ reduce_test(tree::SyntaxNode; exprs_equal=exprs_equal_no_linenum )
236
250
237
251
Select minimal subtrees of `text` or `tree` which are inconsistent between
238
252
flisp and JuliaSyntax parsers.
239
253
"""
240
- function reduce_test (failing_subtrees, tree)
241
- if equals_flisp_parse (tree)
254
+ function reduce_test (failing_subtrees, tree; exprs_equal = exprs_equal_no_linenum )
255
+ if equals_flisp_parse (exprs_equal, tree)
242
256
return false
243
257
end
244
258
if ! haschildren (tree)
@@ -251,7 +265,7 @@ function reduce_test(failing_subtrees, tree)
251
265
if is_trivia (child) || ! haschildren (child)
252
266
continue
253
267
end
254
- had_failing_subtrees |= reduce_test (failing_subtrees, child)
268
+ had_failing_subtrees |= reduce_test (failing_subtrees, child; exprs_equal = exprs_equal )
255
269
end
256
270
end
257
271
if ! had_failing_subtrees
@@ -260,15 +274,15 @@ function reduce_test(failing_subtrees, tree)
260
274
return true
261
275
end
262
276
263
- function reduce_test (tree:: SyntaxNode )
277
+ function reduce_test (tree:: SyntaxNode ; kws ... )
264
278
subtrees = Vector {typeof(tree)} ()
265
- reduce_test (subtrees, tree)
279
+ reduce_test (subtrees, tree; kws ... )
266
280
subtrees
267
281
end
268
282
269
- function reduce_test (text:: AbstractString )
283
+ function reduce_test (text:: AbstractString ; kws ... )
270
284
tree = parseall (SyntaxNode, text)
271
- reduce_test (tree)
285
+ reduce_test (tree; kws ... )
272
286
end
273
287
274
288
0 commit comments