From 79dc927fa576754a9559a38cf087c21007c24e36 Mon Sep 17 00:00:00 2001 From: Fredrik Ekre Date: Fri, 7 Feb 2025 09:16:06 +0100 Subject: [PATCH] Include filename in parser errors, closes #132. (#133) --- src/Runic.jl | 16 ++++++++++------ src/juliac.jl | 1 + src/main.jl | 10 +++++++--- test/maintests.jl | 14 +++++++++++++- 4 files changed, 31 insertions(+), 10 deletions(-) diff --git a/src/Runic.jl b/src/Runic.jl index 11ba192..df2e98b 100644 --- a/src/Runic.jl +++ b/src/Runic.jl @@ -137,6 +137,7 @@ mutable struct Context check::Bool diff::Bool filemode::Bool + filename::String line_ranges::Vector{UnitRange{Int}} # Global state indent_level::Int # track (hard) indentation level @@ -239,7 +240,7 @@ end function Context( src_str::String; assert::Bool = true, debug::Bool = false, verbose::Bool = debug, diff::Bool = false, check::Bool = false, quiet::Bool = false, filemode::Bool = true, - line_ranges::Vector{UnitRange{Int}} = UnitRange{Int}[] + line_ranges::Vector{UnitRange{Int}} = UnitRange{Int}[], filename::String = "-", ) if !isempty(line_ranges) # If formatting is limited to certain line ranges we modify the source string to @@ -250,7 +251,10 @@ function Context( # TODO: If parsing here fails, and we have line ranges, perhaps try to parse without the # markers to check whether the markers are the cause of the failure. src_tree = Node( - JuliaSyntax.parseall(JuliaSyntax.GreenNode, src_str; ignore_warnings = true, version = v"2-") + JuliaSyntax.parseall( + JuliaSyntax.GreenNode, src_str; + filename = filename, ignore_warnings = true, version = v"2-" + ) ) normalize_tree!(src_tree) fmt_io = IOBuffer() @@ -276,8 +280,8 @@ function Context( format_on = true return Context( src_str, src_tree, src_io, fmt_io, fmt_tree, quiet, verbose, assert, debug, check, - diff, filemode, line_ranges, indent_level, call_depth, format_on, prev_sibling, next_sibling, - lineage_kinds, lineage_macros + diff, filemode, filename, line_ranges, indent_level, call_depth, format_on, + prev_sibling, next_sibling, lineage_kinds, lineage_macros ) end @@ -625,7 +629,7 @@ end Format string `str` and return the formatted string. """ function format_string(str::AbstractString; filemode::Bool = false) - ctx = Context(str; filemode = filemode) + ctx = Context(str; filemode = filemode, filename = "string") format_tree!(ctx) return String(take!(ctx.fmt_io)) end @@ -651,7 +655,7 @@ function format_file(inputfile::AbstractString, outputfile::AbstractString = inp error("input and output must not be the same when `inplace = false`") end # Format it - ctx = Context(str) + ctx = Context(str; filename = inputfile) format_tree!(ctx) # Write the output but skip if it text didn't change changed = ctx.fmt_tree !== nothing diff --git a/src/juliac.jl b/src/juliac.jl index 616fdce..910b513 100644 --- a/src/juliac.jl +++ b/src/juliac.jl @@ -57,6 +57,7 @@ end supports_color(io::RawIO) = isatty(io) # juliac-compatible `Base.showerror` +# TODO: Special case for JuliaSyntax.ParseError function sprint_showerror_juliac(err::Exception) if err isa SystemError return "SystemError: " * err.prefix * ": " * Libc.strerror(err.errnum) diff --git a/src/main.jl b/src/main.jl index 634cd56..7a4c717 100644 --- a/src/main.jl +++ b/src/main.jl @@ -402,20 +402,24 @@ function main(argv) end # Call the library to format the text + inputfile_pretty = inputfile == "-" ? "stdin" : inputfile ctx = try - ctx′ = Context(sourcetext; quiet, verbose, debug, diff, check, line_ranges) + ctx′ = Context( + sourcetext; quiet, verbose, debug, diff, check, line_ranges, + filename = inputfile_pretty, + ) format_tree!(ctx′) ctx′ catch err print_progress && errln() if err isa JuliaSyntax.ParseError - panic("failed to parse input: ", err) + panic(string("failed to parse input from ", inputfile_pretty, ": "), err) continue elseif err isa MainError panic(err.msg) continue end - msg = "failed to format input: " + msg = string("failed to format input from ", inputfile_pretty, ": ") @static if juliac rc = panic(msg, err) else diff --git a/test/maintests.jl b/test/maintests.jl index 369cded..7538012 100644 --- a/test/maintests.jl +++ b/test/maintests.jl @@ -474,7 +474,19 @@ function maintests(f::R) where {R} rc, fd1, fd2 = runic(["--check", f_in]) @test rc == 1 @test isempty(fd1) - @test occursin("failed to parse input", fd2) + @test occursin("failed to parse input from in.jl: ", fd2) + # TODO: Not juliac-compatible + # @test occursin("Error @ in.jl:1:7", fd2) # Relies on JuliaSyntax output + end + + # runic --check < unparseable.jl + cdtmp() do + rc, fd1, fd2 = runic(["--check"], "syntax error") + @test rc == 1 + @test isempty(fd1) + @test occursin("failed to parse input from stdin: ", fd2) + # TODO: Not juliac-compatible + # @test occursin("Error @ stdin:1:7", fd2) # Relies on JuliaSyntax output end # runic -o readonly.jl in.jl