Skip to content

Commit e15d232

Browse files
authored
make showprov() able to take optional keyword arguments for highlight() (#16)
* 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`. * add `include_location::Bool` option for `showprov`
1 parent b5de736 commit e15d232

File tree

2 files changed

+25
-22
lines changed

2 files changed

+25
-22
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: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,29 +39,34 @@ 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;
43+
note=nothing, include_location::Bool=true, highlight_kwargs...)
4344
for (i,ex) in enumerate(Iterators.reverse(exs))
4445
sr = sourceref(ex)
4546
if i > 1
4647
print(io, "\n\n")
4748
end
4849
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)
50+
if isnothing(note) # use provided `note` otherwise
51+
note = i > 1 && k == K"macrocall" ? "in macro expansion" :
52+
i > 1 && k == K"$" ? "interpolated here" :
53+
"in source"
54+
end
55+
highlight(io, sr; note=note, highlight_kwargs...)
5356

54-
line, _ = source_location(sr)
55-
locstr = "$(filename(sr)):$line"
56-
JuliaSyntax._printstyled(io, "\n# @ $locstr", fgcolor=:light_black)
57+
if include_location
58+
line, _ = source_location(sr)
59+
locstr = "$(filename(sr)):$line"
60+
JuliaSyntax._printstyled(io, "\n# @ $locstr", fgcolor=:light_black)
61+
end
5762
end
5863
end
5964

60-
function showprov(io::IO, ex::SyntaxTree; tree=false)
65+
function showprov(io::IO, ex::SyntaxTree; tree::Bool=false, showprov_kwargs...)
6166
if tree
6267
_show_provtree(io, ex, "")
6368
else
64-
showprov(io, flattened_provenance(ex))
69+
showprov(io, flattened_provenance(ex); showprov_kwargs...)
6570
end
6671
end
6772

@@ -165,4 +170,3 @@ function _print_ir(io::IO, ex, indent)
165170
end
166171
end
167172
end
168-

0 commit comments

Comments
 (0)