Skip to content

Commit 2197b66

Browse files
committed
make showprov() able to take optional keyword arguments for
`highlight()` So that user of `showprov` can tweak options of `highlight`, e.g, `color` and `context_lines_before`.
1 parent 4b12ab1 commit 2197b66

File tree

2 files changed

+19
-19
lines changed

2 files changed

+19
-19
lines changed

README.md

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ This work is intended to
1313
* Bring precise code provenance to Julia's lowered form (and eventually
1414
downstream in type inference, stack traces, etc). This has many benefits
1515
- Talk to users precisely about their code via character-precise error and
16-
diagnostic messages from lowering
16+
diagnostic messages from lowering
1717
- Greatly simplify the implementation of critical tools like Revise.jl
1818
which rely on analyzing how the user's source maps to the compiler's data
1919
structures
@@ -109,10 +109,10 @@ For example when parsing a source file we have
109109
```julia
110110
julia> ex = parsestmt(SyntaxTree, "a + b", filename="foo.jl")
111111
SyntaxTree with attributes kind,value,name_val,syntax_flags,source
112-
[call-i] │
113-
a │
114-
+
115-
b │
112+
[call-i] │
113+
a │
114+
+
115+
b │
116116

117117
julia> ex[3].source
118118
a + b
@@ -157,9 +157,9 @@ The tree which arises from macro expanding this is pretty simple:
157157
```julia
158158
julia> expanded = JuliaLowering.macroexpand(Main, parsestmt(SyntaxTree, "M.@outer()"))
159159
SyntaxTree with attributes scope_layer,kind,value,var_id,name_val,syntax_flags,source
160-
[tuple-p] │
161-
1
162-
2
160+
[tuple-p] │
161+
1
162+
2
163163
```
164164

165165
but the provenance information recorded for the second element `2` of this
@@ -777,7 +777,7 @@ The final lowered IR is expressed as `CodeInfo` objects which are a sequence of
777777
* Restricted forms of `Expr` (with semantics different from surface syntax,
778778
even for the same `head`! for example the arguments to `Expr(:call)` in IR
779779
must be "simple" and aren't evaluated in order)
780-
* `Core.SlotNumber`
780+
* `Core.SlotNumber`
781781
* Other special forms from `Core` like `Core.ReturnNode`, `Core.EnterNode`, etc.
782782
* `Core.SSAValue`, indexing any value generated from a statement in the `code`
783783
array.
@@ -857,7 +857,7 @@ Pros:
857857
- Replaces more Expr usage
858858
- Replaces a whole pile of C code with significantly less Julia code
859859
- Lowering output becomes more consistently imperative
860-
Cons:
860+
Cons:
861861
- Lots more code to write
862862
- May need to invent intermediate data structures to replace `Expr`
863863
- Bootstrap?
@@ -895,4 +895,3 @@ Some differences which makes Racket's macro expander different from Julia:
895895
expand macros; the "pass system". Julia just executes all top level
896896
statements in order when precompiling a package.
897897
* As a lisp, Racket's surface syntax is dramatically simpler and more uniform
898-

src/utils.jl

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,29 +39,31 @@ function _show_provtree(io::IO, prov, indent)
3939
printstyled(io, "@ $fn:$line\n", color=:light_black)
4040
end
4141

42-
function showprov(io::IO, exs::AbstractVector)
42+
function showprov(io::IO, exs::AbstractVector; note=nothing, highlight_kwargs...)
4343
for (i,ex) in enumerate(Iterators.reverse(exs))
4444
sr = sourceref(ex)
4545
if i > 1
4646
print(io, "\n\n")
4747
end
4848
k = kind(ex)
49-
note = i > 1 && k == K"macrocall" ? "in macro expansion" :
50-
i > 1 && k == K"$" ? "interpolated here" :
51-
"in source"
52-
highlight(io, sr, note=note)
49+
if isnothing(note)
50+
note = i > 1 && k == K"macrocall" ? "in macro expansion" :
51+
i > 1 && k == K"$" ? "interpolated here" :
52+
"in source"
53+
end
54+
highlight(io, sr; note=note, highlight_kwargs...)
5355

5456
line, _ = source_location(sr)
5557
locstr = "$(filename(sr)):$line"
5658
JuliaSyntax._printstyled(io, "\n# @ $locstr", fgcolor=:light_black)
5759
end
5860
end
5961

60-
function showprov(io::IO, ex::SyntaxTree; tree=false)
62+
function showprov(io::IO, ex::SyntaxTree; tree::Bool=false, showprov_kwargs...)
6163
if tree
6264
_show_provtree(io, ex, "")
6365
else
64-
showprov(io, flattened_provenance(ex))
66+
showprov(io, flattened_provenance(ex); showprov_kwargs...)
6567
end
6668
end
6769

@@ -165,4 +167,3 @@ function _print_ir(io::IO, ex, indent)
165167
end
166168
end
167169
end
168-

0 commit comments

Comments
 (0)