Browse Source

Include filename in parser errors, closes #132. (#133)

pull/134/head
Fredrik Ekre 10 months ago committed by GitHub
parent
commit
79dc927fa5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 16
      src/Runic.jl
  2. 1
      src/juliac.jl
  3. 10
      src/main.jl
  4. 14
      test/maintests.jl

16
src/Runic.jl

@ -137,6 +137,7 @@ mutable struct Context @@ -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 @@ -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( @@ -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( @@ -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 @@ -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 @@ -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

1
src/juliac.jl

@ -57,6 +57,7 @@ end @@ -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)

10
src/main.jl

@ -402,20 +402,24 @@ function main(argv) @@ -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

14
test/maintests.jl

@ -474,7 +474,19 @@ function maintests(f::R) where {R} @@ -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

Loading…
Cancel
Save