1
1
# 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)
3
3
try
4
4
# TODO : Check that we do all this input wrangling without copying the
5
5
# code buffer
@@ -13,19 +13,32 @@ function core_parser_hook(code, filename, offset, options)
13
13
14
14
stream = ParseStream (io)
15
15
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
16
21
JuliaSyntax. parse (stream; rule= rule)
17
22
18
- ex = if any_error (stream)
23
+ if any_error (stream)
19
24
e = Expr (:error , ParseError (SourceFile (code), stream. diagnostics))
20
- options === :all ? Expr (:toplevel , e) : e
25
+ ex = options === :all ? Expr (:toplevel , e) : e
21
26
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
23
33
end
24
34
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)
26
39
27
40
# Rewrap result in an svec for use by the C code
28
- return Core. svec (ex, pos )
41
+ return Core. svec (ex, last_offset )
29
42
catch exc
30
43
@error (" JuliaSyntax parser failed — falling back to flisp!" ,
31
44
exception= (exc,catch_backtrace ()),
@@ -35,6 +48,18 @@ function core_parser_hook(code, filename, offset, options)
35
48
return Core. Compiler. fl_parse (code, filename, offset, options)
36
49
end
37
50
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
+
38
63
"""
39
64
Connect the JuliaSyntax parser to the Julia runtime so that it replaces the
40
65
flisp parser for all parsing work.
0 commit comments