Codebase list lua-ldoc / f55838e
keep a table of globals; used for ldoc -m and for resolving @see references to Lua standard library functions and tables steve donovan 12 years ago
4 changed file(s) with 125 addition(s) and 14 deletion(s). Raw diff Collapse all Expand all
0 -------
1 -- global functions and tables
2 local tools = require 'ldoc.tools'
3
4
5
6 local functions = {
7 assert = true,
8 collectgarbage = true,
9 dofile = true,
10 setfenv = true,
11 getfenv = true,
12 getmetatable = true,
13 setmetatable = true,
14 pairs = true,
15 ipairs = true,
16 load = true,
17 loadfile = true,
18 loadstring = true,
19 next = true,
20 pcall = true,
21 print = true,
22 rawequal = true,
23 rawget = true,
24 rawset = true,
25 select = true,
26 tonumber = true,
27 tostring = true,
28 type = true,
29 unpack = true,
30 xpcall = true,
31 module = true,
32 require = true,
33 }
34
35 local tables = {
36 io = '5.7',
37 package = '5.3',
38 math = '5.6',
39 os = '5.8',
40 string = '5.4',
41 table = '5.5',
42 coroutine = '5.2',
43 debug = '5.9'
44 }
45
46 local manual = 'http://www.lua.org/manual/5.1/manual.html#'
47 local fun_ref = manual..'pdf-'
48
49 local function function_ref (name)
50 return {href = fun_ref..name}
51 end
52
53 local function module_ref (name)
54 return {href = manual..tables[name]}
55 end
56
57
58 local function lua_manual_ref (name)
59 local tbl,fname = tools.split_dotted_name(name)
60 if not tbl then -- plain symbol
61 if functions[name] then
62 return function_ref(name)
63 end
64 if tables[name] then
65 return module_ref(name)
66 end
67 else
68 if tables[tbl] then
69 return function_ref(name)
70 end
71 end
72 return nil
73 end
74
75 return {
76 functions = functions,
77 tables = tables,
78 lua_manual_ref = lua_manual_ref
79 }
141141 </ol>
142142 # end -- if returns
143143
144 # local function href(see)
145 # if see.href then return see.href
146 # else return see.mod..'.html#'..see.name
147 # end
148 # end
144149 # if item.see then
145150 # local li,il = use_li(item.see)
146151 <h3>see also:</h3>
147152 <ul>
148153 # for see in iter(item.see) do
149 $(li)<a href="$(see.mod).html#$(see.name)">$(see.label)</a>$(il)
154 $(li)<a href="$(href(see))">$(see.label)</a>$(il)
150155 # end -- for
151156 </ul>
152157 # end -- if see
44 require 'pl'
55
66 local doc = {}
7
7 local global = require 'builtin.globals'
88 local tools = require 'ldoc.tools'
99 local split_dotted_name = tools.split_dotted_name
1010
9999 mname = this_mod.name
100100 package = ''
101101 else
102 package = package .. '.'
102 package = package
103103 end
104104 self.modules:append(this_mod)
105105 this_mod.package = package
276276 if type(name) == 'table' then pretty.dump(name); name = '?' end
277277 name = name or '?'
278278 io.stderr:write(name,':',self.lineno or '?',' ',msg,'\n')
279 return nil
279280 end
280281
281282 function Item:error(msg)
294295 function Module:resolve_references(modules)
295296 local found = List()
296297
298 local function hunt_for_reference (packmod)
299 local mod_ref
300 local package = self.package
301 repeat -- same package?
302 local nmod = package..'.'..packmod
303 mod_ref = modules.by_name[nmod]
304 if mod_ref then break end -- cool
305 package = split_dotted_name(package)
306 until not package
307 return mod_ref
308 end
309
297310 local function process_see_reference (item,see,s)
298311 local mod_ref,fun_ref,name,packmod
299312 -- is this a fully qualified module name?
300313 local mod_ref = modules.by_name[s]
301314 if mod_ref then return mod_ref,nil end
315 -- module reference?
316 mod_ref = hunt_for_reference(s)
317 if mod_ref then return mod_ref end
302318 local packmod,name = split_dotted_name(s) -- e.g. 'pl.utils','split'
303319 if packmod then -- qualified name
304320 mod_ref = modules.by_name[packmod] -- fully qualified mod name?
305321 if not mod_ref then
306 mod_ref = modules.by_name[self.package..packmod]
307 end
308 if not mod_ref then
309 item:warning("module not found: "..packmod)
310 return nil
322 mod_ref = hunt_for_reference(packmod)
323 if not mod_ref then
324 local ref = global.lua_manual_ref(s)
325 if ref then return ref end
326 return item:warning("module not found: "..packmod)
327 end
311328 end
312329 fun_ref = mod_ref.items.by_name[name]
313330 if fun_ref then
316333 item:warning("function not found: "..s.." in "..mod_ref.name)
317334 end
318335 else -- plain jane name; module in this package, function in this module
319 mod_ref = modules.by_name[self.package..s]
336 mod_ref = modules.by_name[self.package..'.'..s]
320337 if mod_ref then return mod_ref,nil end
321338 fun_ref = self.items.by_name[s]
322339 if fun_ref then return self,fun_ref
323340 else
341 local ref = global.lua_manual_ref (s)
342 if ref then return ref end
324343 item:warning("function not found: "..s.." in this module")
325344 end
326345 end
333352 for s in see:iter() do
334353 local mod_ref, item_ref = process_see_reference(item,see,s)
335354 if mod_ref then
336 local name = item_ref and item_ref.name or ''
337 -- this is deeply hacky; classes have 'Class ' prepended.
338 if item_ref and item_ref.type == 'type' then
339 name = 'Class_'..name
355 local href
356 if mod_ref.name then
357 local name = item_ref and item_ref.name or ''
358 -- this is deeply hacky; classes have 'Class ' prepended.
359 if item_ref and item_ref.type == 'type' then
360 name = 'Class_'..name
361 end
362 href = {mod=mod_ref.name,name=name,label=s}
363 else
364 href = {href = mod_ref.href,label=s}
340365 end
341 item.see:append {mod=mod_ref.name,name=name,label=s}
366 item.see:append (href)
342367 found:append{item,s}
343368 end
344369 end
194194 basename = basename..path.sep
195195 end
196196 local lpath,cnt = fname:gsub('^'..utils.escape(basename),'')
197 --print('deduce',lpath,cnt,basename)
197198 if cnt ~= 1 then quit("module(...) name deduction failed: base "..basename.." "..fname) end
198199 lpath = lpath:gsub(path.sep,'.')
199200 return M.name_of(lpath):gsub('%.init$','')