11# Adaptor for the API/ABI expected by the Julia runtime code.
2- function core_parser_hook (code, filename, offset, options)
2+ function core_parser_hook (code, filename, lineno, offset, options)
33 try
44 # TODO : Check that we do all this input wrangling without copying the
55 # code buffer
@@ -13,19 +13,32 @@ function core_parser_hook(code, filename, offset, options)
1313
1414 stream = ParseStream (io)
1515 rule = options === :all ? :toplevel : options
16+ if rule != = :toplevel
17+ # To copy the flisp parser driver, we ignore leading trivia when
18+ # parsing statements or atoms
19+ bump_trivia (stream)
20+ end
1621 JuliaSyntax. parse (stream; rule= rule)
1722
18- ex = if any_error (stream)
23+ if any_error (stream)
1924 e = Expr (:error , ParseError (SourceFile (code), stream. diagnostics))
20- options === :all ? Expr (:toplevel , e) : e
25+ ex = options === :all ? Expr (:toplevel , e) : e
2126 else
22- build_tree (Expr, stream)
27+ ex = build_tree (Expr, stream, wrap_toplevel_as_kind= K " None" )
28+ if Meta. isexpr (ex, :None )
29+ # The None wrapping is only to give somewhere for trivia to be
30+ # attached; unwrap!
31+ ex = only (ex. args)
32+ end
2333 end
2434
25- pos = last_byte (stream) - 1
35+ # Note the next byte in 1-based indexing is `last_byte(stream) + 1` but
36+ # the Core hook must return an offset (ie, it's 0-based) so the factors
37+ # of one cancel here.
38+ last_offset = last_byte (stream)
2639
2740 # Rewrap result in an svec for use by the C code
28- return Core. svec (ex, pos )
41+ return Core. svec (ex, last_offset )
2942 catch exc
3043 @error (" JuliaSyntax parser failed — falling back to flisp!" ,
3144 exception= (exc,catch_backtrace ()),
@@ -35,6 +48,18 @@ function core_parser_hook(code, filename, offset, options)
3548 return Core. Compiler. fl_parse (code, filename, offset, options)
3649end
3750
51+ # Core._parse gained a `lineno` argument in
52+ # https://github.com/JuliaLang/julia/pull/43876
53+ # Prior to this, the following signature was needed:
54+ function core_parser_hook (code, filename, offset, options)
55+ core_parser_hook (code, filename, LineNumberNode (0 ), offset, options)
56+ end
57+
58+ # Hack:
59+ # Meta.parse() attempts to construct a ParseError from a string if it receives
60+ # `Expr(:error)`.
61+ Base. Meta. ParseError (e:: JuliaSyntax.ParseError ) = e
62+
3863"""
3964Connect the JuliaSyntax parser to the Julia runtime so that it replaces the
4065flisp parser for all parsing work.
0 commit comments