-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModuleInterfaceTools.jl
More file actions
403 lines (346 loc) · 12.1 KB
/
Copy pathModuleInterfaceTools.jl
File metadata and controls
403 lines (346 loc) · 12.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
__precompile__(true)
"""
API Tools package
Copyright 2018 Gandalf Software, Inc., Scott P. Jones
Licensed under MIT License, see LICENSE.md
(@def macro "stolen" from DiffEqBase.jl/src/util.jl :-) )
"""
module ModuleInterfaceTools
const debug = Ref(false)
const V6_COMPAT = VERSION < v"0.7-"
const BIG_ENDIAN = (ENDIAN_BOM == 0x01020304)
_stdout() = @static V6_COMPAT ? STDOUT : stdout
_stderr() = @static V6_COMPAT ? STDERR : stderr
Base.parse(::Type{Expr}, args...; kwargs...) =
Meta.parse(args...; kwargs...)
export @api, V6_COMPAT, BIG_ENDIAN
_api_def(name, definition) =
quote
macro $(esc(name))()
esc($(Expr(:quote, definition)))
end
end
const SymSet = Set{Symbol}
abstract type AbstractAPI end
struct TMP_API <: AbstractAPI
mod::Module
base::SymSet
public::SymSet
develop::SymSet
public!::SymSet
develop!::SymSet
modules::SymSet
TMP_API(mod::Module) = new(mod, SymSet(), SymSet(), SymSet(), SymSet(), SymSet(), SymSet())
end
const SymList = Tuple{Vararg{Symbol}}
struct API <: AbstractAPI
mod::Module
base::SymList
public::SymList
develop::SymList
public!::SymList
develop!::SymList
modules::SymList
end
API(api::TMP_API) =
API(api.mod, SymList(api.base), SymList(api.public), SymList(api.develop),
SymList(api.public!), SymList(api.develop!), SymList(api.modules))
function Base.show(io::IO, api::AbstractAPI)
println(io, "ModuleInterfaceTools.API: ", api.mod)
for fld in (:base, :public, :develop, :public!, :develop!, :modules)
syms = getfield(api, fld)
isempty(syms) && continue
print(fld, ":")
for s in syms
print(" ", s)
end
println()
println()
end
end
function m_eval(mod, expr)
try
Core.eval(mod, expr)
catch ex
println("m_eval($mod, $expr)");
println(sprint(showerror, ex, catch_backtrace()))
#rethrow(ex)
end
end
"""
@api <cmd> [<symbols>...]
* freeze # use at end of module to freeze API
* list <modules>... # list API(s) of given modules (or current if none given)
* use <modules>... # use, without importing (i.e. can't extend)
* use! <modules>... # use, without importing (i.e. can't extend), "export"
* test <modules>... # using public and develop APIs, for testing purposes
* extend <modules>... # for development, imports api & dev, use api & dev definitions
* extend! <modules>... # for development, imports api & dev, use api & dev definitions, "export"
* export <modules>... # export public definitions
* base <names...> # Add functions from Base that are part of the API (extendible)
* base! <names...> # Add functions from Base or define them if not in Base
* public <names...> # Add other symbols that are part of the public API (structs, consts)
* public! <names...> # Add functions that are part of the public API (extendible)
* develop <names...> # Add other symbols that are part of the development API
* develop! <names...> # Add functions that are part of the development API (extendible)
* define! <names...> # Define functions to be extended, public API
* defdev! <names...> # Define functions to be extended, develop API
* modules <names...> # Add submodule names that are part of the API
* path <paths...> # Add paths to LOAD_PATH
* def <name> <expr> # Same as the @def macro, creates a macro with the given name
"""
macro api(cmd::Symbol)
mod = @static V6_COMPAT ? current_module() : __module__
cmd == :list ? _api_list(mod) :
cmd == :freeze ? _api_freeze(mod) :
cmd == :test ? _api_test(mod) :
error("@api unrecognized command: $cmd")
end
function _api_display(mod, nam)
if isdefined(mod, nam) && (api = m_eval(mod, nam)) !== nothing
show(api);
else
println("Exported from $mod:")
syms = names(mod)
if !isempty(syms)
print(fld, ":")
for s in syms
print(" ", s)
end
end
end
println()
end
_api_list(mod::Module) = (_api_display(mod, :__api__) ; _api_display(mod, :__tmp_api__))
function _api_freeze(mod::Module)
ex = :( const __api__ = ModuleInterfaceTools.API(__tmp_api__) ; __tmp_api__ = nothing )
isdefined(mod, :__tmp_api__) && m_eval(mod, :( __tmp_api__ !== nothing ) ) && m_eval(mod, ex)
nothing
end
function _api_path(curmod, exprs)
for exp in exprs
if isa(exp, Expr) || isa(exp, Symbol)
str = m_eval(curmod, exp)
elseif isa(exp, String)
str = exp
else
error("@api path: syntax error $exp")
end
m_eval(curmod, :( push!(LOAD_PATH, $str) ))
end
nothing
end
const _cmduse = (:use, :use!, :test, :extend, :extend!, :export, :list)
const _cmdadd =
(:modules, :public, :develop, :public!, :develop!, :base, :base!, :define!, :defdev!)
@static V6_COMPAT ? (const _ff = findfirst) :
(_ff(lst, val) = (ret = findfirst(isequal(val), lst); ret === nothing ? 0 : ret))
function _add_def!(curmod, grp, exp)
debug[] && print("_add_def!($curmod, $grp, $exp::$(typeof(exp))")
if isa(exp, Symbol)
sym = exp
elseif isa(exp, AbstractString)
sym = Symbol(exp)
else
error("@api $grp: syntax error $exp")
end
if grp == :base! && isdefined(Base, sym)
m_eval(curmod, :(import Base.$sym ))
m_eval(curmod, :(push!(__tmp_api__.base, $(QuoteNode(sym)))))
return
end
m_eval(curmod, :(function $sym end))
m_eval(curmod, (grp == :defdev!
? :(push!(__tmp_api__.develop!, $(QuoteNode(sym))))
: :(push!(__tmp_api__.public!, $(QuoteNode(sym))))))
end
function push_args!(symbols, lst, grp)
for ex in lst
if isa(ex, Expr) && ex.head == :tuple
push_args!(symbols, ex.args)
elseif isa(ex, Symbol)
push!(symbols, ex)
elseif isa(ex, AbstractString)
push!(symbols, Symbol(ex))
else
error("@api $grp: syntax error $ex")
end
end
end
"""Initialize the temp api variable for this module"""
_init_api(curmod) =
isdefined(curmod, :__tmp_api__) ||
m_eval(curmod, :( global __tmp_api__ = ModuleInterfaceTools.TMP_API($curmod)))
"""Add symbols"""
function _add_symbols(curmod, grp, exprs)
if debug[]
print("_add_symbols($curmod, $grp, $exprs)")
isdefined(curmod, :__tmp_api__) && print(" => ", m_eval(curmod, :__tmp_api__))
println()
end
_init_api(curmod)
if grp == :base! || grp == :define! || grp == :defdev!
for ex in exprs
if isa(ex, Expr) && ex.head == :tuple
for sym in ex.args
_add_def!(curmod, grp, sym)
end
else
_add_def!(curmod, grp, ex)
end
end
else
symbols = SymSet()
for ex in exprs
if isa(ex, Expr) && ex.head == :tuple
push_args!(symbols, ex.args, grp)
elseif isa(ex, Symbol)
push!(symbols, ex)
elseif isa(ex, AbstractString)
push!(symbols, Symbol(ex))
else
error("@api $grp: syntax error $ex")
end
end
if grp == :base
for sym in symbols
m_eval(curmod, :( import Base.$sym ))
end
end
for sym in symbols
m_eval(curmod, :( push!(__tmp_api__.$grp, $(QuoteNode(sym)) )))
end
end
debug[] && println("after add symbols: ", m_eval(curmod, :__tmp_api__))
nothing
end
has_api(mod) = isdefined(mod, :__api__)
get_api(mod) = m_eval(mod, :__api__)
function _api_extend(curmod, modules, cpy::Bool)
for nam in modules
mod = m_eval(curmod, nam)
if has_api(mod)
api = get_api(mod)
_do_list(curmod, cpy, :import, Base, :Base, :base, api)
_do_list(curmod, cpy, :import, mod, nam, :public!, api)
_do_list(curmod, cpy, :import, mod, nam, :develop!, api)
_do_list(curmod, cpy, :using, mod, nam, :public, api)
_do_list(curmod, cpy, :using, mod, nam, :develop, api)
else
_do_list(curmod, cpy, :import, mod, nam, :public!, names(mod))
end
end
nothing
end
function _api_use(curmod, modules, cpy::Bool)
for nam in modules
mod = m_eval(curmod, nam)
if has_api(mod)
api = get_api(mod)
_do_list(curmod, cpy, :using, mod, nam, :public, api)
_do_list(curmod, cpy, :using, mod, nam, :public!, api)
else
_do_list(curmod, cpy, :using, mod, nam, :public!, names(mod))
end
end
nothing
end
function _api_export(curmod, modules)
for nam in modules
mod = m_eval(curmod, nam)
api = get_api(mod)
if has_api(mod)
api = get_api(mod)
m_eval(curmod, Expr( :export, getfield(api, :modules)...))
m_eval(curmod, Expr( :export, getfield(api, :public)...))
m_eval(curmod, Expr( :export, getfield(api, :public!)...))
end
end
nothing
end
function _api_list(curmod, modules)
for nam in modules
_api_list(m_eval(curmod, nam))
end
nothing
end
_api_test(mod) = m_eval(mod, V6_COMPAT ? :(using Base.Test) : :(using Test))
function _api(curmod::Module, cmd::Symbol, exprs)
cmd == :def && return _api_def(exprs...)
cmd == :path && return _api_path(curmod, exprs)
ind = _ff(_cmdadd, cmd)
ind == 0 || return _add_symbols(curmod, cmd, exprs)
_ff(_cmduse, cmd) == 0 && error("Syntax error: @api $cmd $exprs")
debug[] && print("_api($curmod, $cmd, $exprs)")
# Be nice and set up standard Test
cmd == :test && _api_test(curmod)
modules = SymSet()
for ex in exprs
if isa(ex, Expr) && ex.head == :tuple
# Some of these might not just be modules
# might have module(symbols, !syms, sym => other), need to add support for that
for sym in ex.args
if isa(sym, Symbol)
push!(modules, sym)
m_eval(curmod, :(import $sym))
else
println("Not a symbol: $sym");
dump(sym);
end
end
elseif isa(ex, Symbol)
push!(modules, ex)
m_eval(curmod, :(import $ex))
else
error("@api $cmd: syntax error $ex")
end
end
debug[] && println(" => $modules")
cmd == :export && return _api_export(curmod, modules)
cmd == :list && return _api_list(curmod, modules)
cpy = (cmd == :use!) || (cmd == :extend!)
cpy && _init_api(curmod)
for nam in modules
mod = m_eval(curmod, nam)
if has_api(mod)
for sym in getfield(get_api(mod), :modules)
if isdefined(mod, sym)
m_eval(curmod, :(using $nam.$sym))
cpy && m_eval(curmod, :( push!(__tmp_api__.modules, $(QuoteNode(sym)) )))
else
println(_stderr(), "Warning: Exported symbol $sym is not defined in $nam")
end
end
end
end
((cmd == :use || cmd == :use!)
? _api_use(curmod, modules, cpy)
: _api_extend(curmod, modules, cpy))
end
function makecmd(cmd, nam, sym)
@static if V6_COMPAT
Expr(cmd, nam, sym)
else
(cmd == :using
? Expr(cmd, Expr(:(:), Expr(:., nam), Expr(:., sym)))
: Expr(cmd, Expr(:., nam, sym)))
end
end
_do_list(curmod, cpy, cmd, mod, nam, grp, api::API) =
_do_list(curmod, cpy, cmd, mod, nam, grp, getfield(api, grp))
function _do_list(curmod, cpy, cmd, mod, nam, grp, lst)
debug[] && println("_do_list($curmod, $cpy, $cmd, $mod, $nam, $grp, $lst)")
for sym in lst
if isdefined(mod, sym)
m_eval(curmod, makecmd(cmd, nam, sym))
cpy && m_eval(curmod, :( push!(__tmp_api__.$grp, $(QuoteNode(sym)) )))
else
println(_stderr(), "Warning: Exported symbol $sym is not defined in $nam")
end
end
end
macro api(cmd::Symbol, exprs...)
mod = @static V6_COMPAT ? current_module() : __module__
_api(mod, cmd, exprs)
end
end # module ModuleInterfaceTools