Skip to content

Commit bf27cba

Browse files
authored
Merge pull request #49 from JuliaDebug/teh/docexprs2
More thorough detection of atsign-doc expressions
2 parents 8038678 + 2ba2c53 commit bf27cba

File tree

2 files changed

+35
-4
lines changed

2 files changed

+35
-4
lines changed

src/JuliaInterpreter.jl

+24-1
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,12 @@ Base.nameof(frame) = isa(frame.code.scope, Method) ? frame.code.scope.name : nam
170170

171171
is_loc_meta(expr, kind) = isexpr(expr, :meta) && length(expr.args) >= 1 && expr.args[1] === kind
172172

173+
"""
174+
isglobalref(g, mod, name)
175+
176+
Tests whether `g` is equal to `GlobalRef(mod, name)`.
177+
"""
178+
isglobalref(g, mod, name) = isa(g, GlobalRef) && g.mod === mod && g.name == name
173179

174180
function to_function(x)
175181
if isa(x, GlobalRef)
@@ -434,7 +440,24 @@ function prepare_toplevel(mod::Module, expr::Expr; filename=nothing, kwargs...)
434440
return prepare_toplevel!(modexs, docexprs, mod, expr; filename=filename, kwargs...)
435441
end
436442

437-
isdocexpr(ex) = isexpr(ex, :macrocall) && (a = ex.args[1]; a isa GlobalRef && a.mod == Core && a.name == Symbol("@doc"))
443+
"""
444+
isdocexpr(ex)
445+
446+
Test whether expression `ex` is a `@doc` expression.
447+
"""
448+
function isdocexpr(ex)
449+
docsym = Symbol("@doc")
450+
if isexpr(ex, :macrocall)
451+
a = ex.args[1]
452+
isglobalref(a, Core, docsym) && return true
453+
isa(a, Symbol) && a == docsym && return true
454+
if isexpr(a, :.)
455+
mod, name = a.args[1], a.args[2]
456+
return mod === :Core && isa(name, QuoteNode) && name.value == docsym
457+
end
458+
end
459+
return false
460+
end
438461

439462
prepare_toplevel!(modexs, docexprs, mod::Module, ex::Expr; kwargs...) =
440463
prepare_toplevel!(modexs, docexprs, Expr(:block), mod, ex; kwargs...)

test/toplevel.jl

+11-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
11
using JuliaInterpreter, Test
22

3-
function read_and_parse(filename)
4-
src = read(filename, String)
5-
ex = Base.parse_input_line(src; filename=filename)
3+
@testset "Basics" begin
4+
@test JuliaInterpreter.isdocexpr(:(@doc "string" sum))
5+
@test JuliaInterpreter.isdocexpr(:(Core.@doc "string" sum))
6+
ex = quote
7+
"""
8+
a docstring
9+
"""
10+
sum
11+
end
12+
@test JuliaInterpreter.isdocexpr(ex.args[2])
13+
@test !JuliaInterpreter.isdocexpr(:(1+1))
614
end
715

816
module Toplevel end

0 commit comments

Comments
 (0)