@@ -22,28 +22,34 @@ function Base.getproperty(er::ToplevelErrorReport, sym::Symbol)
2222end
2323
2424struct SyntaxErrorReport <: ToplevelErrorReport
25- err:: Exception
25+ err:: JuliaSyntax.ParseError
2626 file:: String
2727 line:: Int
28- function SyntaxErrorReport (@nospecialize (err), file, line)
29- isa (err, Exception) || (err = ErrorException (err))
30- return new (err, file, line)
28+ function SyntaxErrorReport (err:: JuliaSyntax.ParseError )
29+ lnn = JuliaSyntax. source_location (LineNumberNode, err. source,
30+ JuliaSyntax. first_byte (first (err. diagnostics)))
31+ return new (err, String (lnn. file:: Symbol ), lnn. line)
3132 end
3233end
3334# don't show stacktrace for syntax errors
3435print_report (io:: IO , report:: SyntaxErrorReport ) = showerror (io, report. err)
3536
37+ # TODO Use JuliaLowering.jl here
38+ struct LoweringErrorReport <: ToplevelErrorReport
39+ msg:: String
40+ file:: String
41+ line:: Int
42+ end
43+ # don't show stacktrace for lowering errors
44+ print_report (io:: IO , report:: LoweringErrorReport ) = showerror (io, ErrorException (lazy " syntax: $(report.msg)" ))
45+
3646# wraps general errors from actual execution
3747struct ActualErrorWrapped <: ToplevelErrorReport
3848 err
3949 st:: Base.StackTraces.StackTrace
4050 file:: String
4151 line:: Int
4252 function ActualErrorWrapped (@nospecialize (err), st, file, line)
43- if isa (err, ErrorException) && startswith (err. msg, " syntax: " )
44- # forward syntax error
45- return SyntaxErrorReport (err. msg, file, line)
46- end
4753 return new (err, st, file, line)
4854 end
4955end
@@ -580,16 +586,19 @@ function _virtual_process!(res::VirtualProcessResult,
580586 end
581587
582588 s = String (s):: String
583- toplevelex = Base. parse_input_line (s; filename)
589+ parsed = try
590+ JuliaSyntax. parseall (Expr, s; filename)
591+ catch err
592+ err isa JuliaSyntax. ParseError || rethrow (err)
593+ err
594+ end
584595
585- if isexpr (toplevelex, ( :error , :incomplete ))
596+ if parsed isa JuliaSyntax . ParseError
586597 # if there's any syntax error, try to identify all the syntax error location
587- report_syntax_errors! (res, s, filename)
588- elseif isnothing (toplevelex)
589- # just return if there is nothing to analyze
598+ push! (res. toplevel_error_reports, SyntaxErrorReport (parsed))
590599 else
591- @assert isexpr (toplevelex , :toplevel )
592- _virtual_process! (res, toplevelex , filename, analyzer, config, context, pkg_mod_depth)
600+ @assert isexpr (parsed , :toplevel )
601+ _virtual_process! (res, parsed , filename, analyzer, config, context, pkg_mod_depth)
593602 end
594603
595604 with_toplevel_logger (config) do @nospecialize (io)
@@ -642,8 +651,8 @@ function _virtual_process!(res::VirtualProcessResult,
642651 lwr = lower (mod, x)
643652 # here we should capture syntax errors found during lowering
644653 if isexpr (lwr, :error )
645- msg = first (lwr. args)
646- push! (res. toplevel_error_reports, SyntaxErrorReport ( lazy " syntax: $ msg" , filename, lnnref[]. line))
654+ msg = first (lwr. args):: String
655+ push! (res. toplevel_error_reports, LoweringErrorReport ( msg, filename, lnnref[]. line))
647656 return nothing
648657 end
649658 return lwr
@@ -1530,35 +1539,6 @@ let s = string(nameof(AbstractGlobal))
15301539 end
15311540end
15321541
1533- function report_syntax_errors! (res, s, filename)
1534- index = line = 1
1535- while begin
1536- ex, nextindex = Base. Meta. _parse_string (s, filename, line, index, :statement )
1537- ! isnothing (ex)
1538- end
1539- line += count (== (' \n ' ), s[index: nextindex- 1 ])
1540- if isexpr (ex, :error )
1541- err = only (ex. args)
1542- if (@static JULIA_SYNTAX_ENABLED && true ) && isa (err, ParseError)
1543- report = SyntaxErrorReport (err, filename, line)
1544- else
1545- report = SyntaxErrorReport (lazy " syntax: $err" , filename, line)
1546- end
1547- elseif isexpr (ex, :incomplete )
1548- err = only (ex. args)
1549- if (@static JULIA_SYNTAX_ENABLED && true ) && isa (err, ParseError)
1550- report = SyntaxErrorReport (err, filename, line)
1551- else
1552- report = SyntaxErrorReport (lazy " syntax: $err" , filename, line)
1553- end
1554- else
1555- report = nothing
1556- end
1557- isnothing (report) || push! (res. toplevel_error_reports, report)
1558- index = nextindex
1559- end
1560- end
1561-
15621542# a bridge to abstract interpretation
15631543function analyze_toplevel! (analyzer:: AbstractAnalyzer , src:: CodeInfo )
15641544 # construct toplevel `MethodInstance`
0 commit comments