@@ -233,28 +233,6 @@ function _core_parser_hook(code, filename, lineno, offset, options)
233
233
end
234
234
end
235
235
236
- # Call the flisp parser
237
- function _fl_parse_hook (code, filename, lineno, offset, options)
238
- @static if VERSION >= v " 1.8.0-DEV.1370" # https://github.com/JuliaLang/julia/pull/43876
239
- return Core. Compiler. fl_parse (code, filename, lineno, offset, options)
240
- elseif VERSION >= v " 1.6"
241
- return Core. Compiler. fl_parse (code, filename, offset, options)
242
- else
243
- if options === :all
244
- ex = Base. parse_input_line (String (code), filename= filename, depwarn= false )
245
- if ! Meta. isexpr (ex, :toplevel )
246
- ex = Expr (:toplevel , ex)
247
- end
248
- return ex, sizeof (code)
249
- elseif options === :statement || options == :atom
250
- ex, pos = Meta. parse (code, offset+ 1 , greedy= options== :statement , raise= false )
251
- return ex, pos- 1
252
- else
253
- error (" Unknown parse options $options " )
254
- end
255
- end
256
- end
257
-
258
236
# Hack:
259
237
# Meta.parse() attempts to construct a ParseError from a string if it receives
260
238
# `Expr(:error)`. Add an override to the ParseError constructor to prevent this.
@@ -292,3 +270,79 @@ function enable_in_core!(enable=true; freeze_world_age = true,
292
270
_set_core_parse_hook (enable ? core_parser_hook : _default_parser)
293
271
nothing
294
272
end
273
+
274
+
275
+ # -------------------------------------------------------------------------------
276
+ # Tools to call the reference flisp parser
277
+ #
278
+ # Call the flisp parser
279
+ function _fl_parse_hook (code, filename, lineno, offset, options)
280
+ @static if VERSION >= v " 1.8.0-DEV.1370" # https://github.com/JuliaLang/julia/pull/43876
281
+ return Core. Compiler. fl_parse (code, filename, lineno, offset, options)
282
+ elseif VERSION >= v " 1.6"
283
+ return Core. Compiler. fl_parse (code, filename, offset, options)
284
+ else
285
+ if options === :all
286
+ ex = Base. parse_input_line (String (code), filename= filename, depwarn= false )
287
+ if ! Meta. isexpr (ex, :toplevel )
288
+ ex = Expr (:toplevel , ex)
289
+ end
290
+ return ex, sizeof (code)
291
+ elseif options === :statement || options == :atom
292
+ ex, pos = Meta. parse (code, offset+ 1 , greedy= options== :statement , raise= false )
293
+ return ex, pos- 1
294
+ else
295
+ error (" Unknown parse options $options " )
296
+ end
297
+ end
298
+ end
299
+
300
+ # ------------------------------------------------
301
+ # Copy of the Meta.parse() API, but ensuring that we call the flisp parser
302
+ # rather than using Meta.parse() which may be using the JuliaSyntax parser.
303
+
304
+ """
305
+ Like Meta.parse() but always call the flisp reference parser.
306
+ """
307
+ function fl_parse (str:: AbstractString ; raise:: Bool = true , depwarn:: Bool = true )
308
+ ex, pos = fl_parse (str, 1 , greedy= true , raise= raise, depwarn= depwarn)
309
+ if isa (ex,Expr) && ex. head === :error
310
+ return ex
311
+ end
312
+ if pos <= ncodeunits (str)
313
+ raise && throw (Meta. ParseError (" extra token after end of expression" ))
314
+ return Expr (:error , " extra token after end of expression" )
315
+ end
316
+ return ex
317
+ end
318
+
319
+ function fl_parse (str:: AbstractString , pos:: Integer ; greedy:: Bool = true , raise:: Bool = true ,
320
+ depwarn:: Bool = true )
321
+ ex, pos = _fl_parse_string (str, " none" , 1 , pos, greedy ? :statement : :atom )
322
+ if raise && isa (ex,Expr) && ex. head === :error
323
+ throw (Meta. ParseError (ex. args[1 ]))
324
+ end
325
+ return ex, pos
326
+ end
327
+
328
+ """
329
+ Like Meta.parseall() but always call the flisp reference parser.
330
+ """
331
+ function fl_parseall (text:: AbstractString ; filename= " none" , lineno= 1 )
332
+ ex,_ = _fl_parse_string (text, String (filename), lineno, 1 , :all )
333
+ return ex
334
+ end
335
+
336
+ function _fl_parse_string (text:: AbstractString , filename:: AbstractString ,
337
+ lineno:: Integer , index:: Integer , options)
338
+ if index < 1 || index > ncodeunits (text) + 1
339
+ throw (BoundsError (text, index))
340
+ end
341
+ ex, offset:: Int = _fl_parse_hook (text, filename, lineno, index- 1 , options)
342
+ ex, offset+ 1
343
+ end
344
+
345
+ # Convenience functions to mirror `JuliaSyntax.parse(Expr, ...)` in simple cases.
346
+ fl_parse (:: Type{Expr} , args... ; kws... ) = fl_parse (args... ; kws... )
347
+ fl_parseall (:: Type{Expr} , args... ; kws... ) = fl_parseall (args... ; kws... )
348
+
0 commit comments