Repository navigation
Expand file tree
/
Copy pathdoc.lua
More file actions
1219 lines (1117 loc) · 38.4 KB
/
Copy pathdoc.lua
File metadata and controls
1219 lines (1117 loc) · 38.4 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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
------
-- Defining the ldoc document model.
local class = require 'pl.class'
local utils = require 'pl.utils'
local List = require 'pl.List'
local Map = require 'pl.Map'
local doc = {}
local global = require 'ldoc.builtin.globals'
local tools = require 'ldoc.tools'
local split_dotted_name = tools.split_dotted_name
local TAG_MULTI,TAG_ID,TAG_SINGLE,TAG_TYPE,TAG_FLAG,TAG_MULTI_LINE = 'M','id','S','T','N','ML'
-- these are the basic tags known to ldoc. They come in several varieties:
-- - 'M' tags with multiple values like 'param' (TAG_MULTI)
-- - 'id' tags which are identifiers, like 'name' (TAG_ID)
-- - 'S' tags with a single value, like 'release' (TAG_SINGLE)
-- - 'N' tags which have no associated value, like 'local` (TAG_FLAG)
-- - 'T' tags which represent a type, like 'function' (TAG_TYPE)
local known_tags = {
param = 'M', see = 'M', comment = 'M', usage = 'ML', ['return'] = 'M', field = 'M', author='M',set='M';
class = 'id', name = 'id', pragma = 'id', alias = 'id', within = 'id',
copyright = 'S', summary = 'S', description = 'S', release = 'S', license = 'S',
fixme = 'S', todo = 'S', warning = 'S', raise = 'S', charset = 'S',
['local'] = 'N', export = 'N', private = 'N', constructor = 'N', static = 'N';
-- project-level
module = 'T', script = 'T', example = 'T', topic = 'T', submodule='T', classmod='T',
-- module-level
['function'] = 'T', lfunction = 'T', table = 'T', section = 'T', type = 'T',
annotation = 'T', factory = 'T';
}
known_tags._alias = {}
known_tags._project_level = {
module = true,
script = true,
example = true,
topic = true,
submodule = true,
classmod = true,
}
known_tags._code_types = {
module = true,
script = true,
classmod = true,
}
known_tags._presentation_names = {
classmod = 'Class',
}
known_tags._module_info = {
'copyright','release','license','author'
}
local see_reference_handlers = {}
doc.TAG_MULTI,doc.TAG_ID,doc.TAG_SINGLE,doc.TAG_TYPE,doc.TAG_FLAG =
TAG_MULTI,TAG_ID,TAG_SINGLE,TAG_TYPE,TAG_FLAG
-- add a new tag.
function doc.add_tag(tag,type,project_level)
if not known_tags[tag] then
known_tags[tag] = type
known_tags._project_level[tag] = project_level
end
end
function doc.add_custom_see_handler(pat,action)
see_reference_handlers[pat] = action
end
-- add an alias to an existing tag (exposed through ldoc API)
function doc.add_alias (a,tag)
known_tags._alias[a] = tag
end
-- get the tag alias value, if it exists.
function doc.get_alias(tag)
return known_tags._alias[tag]
end
-- is it a'project level' tag, such as 'module' or 'script'?
function doc.project_level(tag)
return known_tags._project_level[tag]
end
-- is it a project level tag containing code?
function doc.code_tag (tag)
return known_tags._code_types[tag]
end
-- is it a section tag?
function doc.section_tag (tag)
return tag == 'section' or doc.class_tag(tag)
end
-- is it a class tag, like 'type' or 'factory'?
function doc.class_tag (tag)
return tag == 'type' or tag == 'factory'
end
-- how the type wants to be formally presented; e.g. 'module' becomes 'Module'
-- but 'classmod' will become 'Class'
function doc.presentation_name (tag)
local name = known_tags._presentation_names[tag]
if not name then
name = tag:gsub('(%a)(%a*)',function(f,r)
return f:upper()..r
end)
end
return name
end
function doc.module_info_tags ()
return List.iter(known_tags._module_info)
end
-- annotation tags can appear anywhere in the code and may contain any of these tags:
known_tags._annotation_tags = {
fixme = true, todo = true, warning = true
}
local acount = 1
function doc.expand_annotation_item (tags, last_item)
if tags.summary ~= '' then return false end
local item_name = last_item and last_item.tags.name or '?'
for tag, value in pairs(tags) do
if known_tags._annotation_tags[tag] then
tags:add('class','annotation')
tags:add('summary',value)
tags:add('name',item_name..'-'..tag..acount)
acount = acount + 1
return true
elseif tag == 'return' then
last_item:set_tag(tag,value)
end
end
return false
end
-- we process each file, resulting in a File object, which has a list of Item objects.
-- Items can be modules, scripts ('project level') or functions, tables, etc.
-- (In the code 'module' refers to any project level tag.)
-- When the File object is finalized, we specialize some items as modules which
-- are 'container' types containing functions and tables, etc.
local File = class()
local Item = class()
local Module = class(Item) -- a specialized kind of Item
doc.File = File
doc.Item = Item
doc.Module = Module
function File:_init(filename)
self.filename = filename
self.items = List()
self.modules = List()
self.sections = List()
end
function File:new_item(tags,line)
local item = Item(tags,self,line or 1)
self.items:append(item)
return item
end
function File:export_item (name)
for item in self.items:iter() do
local tags = item.tags
if tags.name == name then
if tags['local'] then
tags['local'] = nil
end
return
end
end
-- warn if any of these guys are not found, indicating no
-- documentation was given.
self:warning('no docs '..tools.quote(name))
end
local function has_prefix (name,prefix)
local i1,i2 = name:find(prefix)
return i1 == 1 and i2 == #prefix
end
local function mod_section_type (this_mod)
return this_mod and this_mod.section and this_mod.section.type
end
function File:find_module_in_files (name)
for f in File.list:iter() do
for m in f.modules:iter() do
if m.name == name then
return m,f.filename
end
end
end
end
function File:finish()
local this_mod
local items = self.items
local tagged_inside
local function add_section (item, display_name)
display_name = display_name or item.display_name
this_mod.section = item
this_mod.kinds:add_kind(display_name,display_name..' ',nil,item)
this_mod.sections:append(item)
this_mod.sections.by_name[display_name:gsub('%A','_')] = item
end
for item in items:iter() do
if mod_section_type(this_mod) == 'factory' and item.tags then
local klass = '@{'..this_mod.section.name..'}'
-- Factory constructors return the object type, and methods all have implicit self argument
if item.tags.constructor and not item.tags['return'] then
item.tags['return'] = List{klass}
elseif item.tags.param then
item.tags.param:put('self '..klass)
end
end
item:finish()
if doc.project_level(item.type) then
this_mod = item
local package,mname,submodule
if item.type == 'module' then
-- if name is 'package.mod', then mod_name is 'mod'
package,mname = split_dotted_name(this_mod.name)
if self.args.merge then
local mod,mf = self:find_module_in_files(item.name)
if mod then
print('found master module',mf)
this_mod = mod
if this_mod.section then
print '***closing section from master module***'
this_mod.section = nil
end
submodule = true
end
end
elseif item.type == 'submodule' then
local mf
submodule = true
this_mod,mf = self:find_module_in_files(item.name)
if this_mod == nil then
self:error("'"..item.name.."' not found for submodule")
end
tagged_inside = tools.this_module_name(self.base,self.filename)..' Functions'
this_mod.kinds:add_kind(tagged_inside, tagged_inside)
end
if not package then
mname = this_mod.name
package = ''
end
if not submodule then
this_mod.package = package
this_mod.mod_name = mname
this_mod.kinds = ModuleMap() -- the iterator over the module contents
self.modules:append(this_mod)
end
elseif doc.section_tag(item.type) then
local display_name = item.name
if display_name == 'end' then
this_mod.section = nil
else
local summary = item.summary:gsub('%.$','')
if doc.class_tag(item.type) then
display_name = 'Class '..item.name
item.module = this_mod
this_mod.items.by_name[item.name] = item
else
display_name = summary
end
item.display_name = display_name
add_section(item)
end
else
local to_be_removed
-- add the item to the module's item list
if this_mod then
-- new-style modules will have qualified names like 'mod.foo'
local mod,fname = split_dotted_name(item.name)
-- warning for inferred unqualified names in new style modules
-- (retired until we handle methods like Set:unset() properly)
if not mod and not this_mod.old_style and item.inferred then
--item:warning(item.name .. ' is declared in global scope')
end
-- the function may be qualified with a module alias...
local alias = this_mod.tags.alias
if (alias and mod == alias) or mod == 'M' or mod == '_M' then
mod = this_mod.mod_name
end
-- if that's the mod_name, then we want to only use 'foo'
if mod == this_mod.mod_name and this_mod.tags.pragma ~= 'nostrip' then
item.name = fname
end
local enclosing_section
if tagged_inside then
item.tags.within = tagged_inside
end
if item.tags.within then
local name = item.tags.within
this_mod.kinds:add_kind(name, name)
enclosing_section = this_mod.section
this_mod.section = nil
end
-- right, this item was within a section or a 'class'
local section_description
local classmod = this_mod.type == 'classmod'
if this_mod.section or classmod then
local stype
local this_section = this_mod.section
if this_section then
item.section = this_section.display_name
stype = this_section.type
end
-- if it was a class, then if the name is unqualified then it becomes
-- 'Class:foo' (unless flagged as being a constructor, static or not a function)
if doc.class_tag(stype) or classmod then
if not item.name:match '[:%.]' then -- not qualified
local class = classmod and this_mod.name or this_section.name
local lang = this_mod.file.lang
local static = item.tags.constructor or item.tags.static or item.type ~= 'function'
item.name = class..(not static and lang.method_call or '.')..item.name
end
if stype == 'factory' then
if item.tags.private then to_be_removed = true
elseif item.type == 'lfunction' then
item.type = 'function'
end
if item.tags.constructor then
item.section = item.type
end
end
end
if this_section then
section_description = this_section.summary..' '..(this_section.description or '')
this_section.summary = ''
elseif item.tags.within then
section_description = item.tags.within
item.section = section_description
else
section_description = "Methods"
item.section = item.type
end
elseif item.tags.within then
section_description = item.tags.within
item.section = section_description
else -- otherwise, just goes into the default sections (Functions,Tables,etc)
item.section = item.type
end
item.module = this_mod
if not to_be_removed then
local these_items = this_mod.items
these_items.by_name[item.name] = item
these_items:append(item)
this_mod.kinds:add(item,these_items,section_description)
end
-- restore current section after a 'within'
if enclosing_section then this_mod.section = enclosing_section end
else
-- must be a free-standing function (sometimes a problem...)
end
end
end
end
-- some serious hackery. We force sections into this 'module',
-- and ensure that there is a dummy item so that the section
-- is not empty.
function File:add_document_section(title)
local section = title:gsub('%W','_')
self:new_item {
name = section,
class = 'section',
summary = title
}
self:new_item {
name = 'dumbo',
class = 'function',
}
return section
end
function Item:_init(tags,file,line)
self.file = file
self.lineno = line
self.summary = tags.summary
self.description = tags.description
tags.summary = nil
tags.description = nil
self.tags = {}
self.formal_args = tags.formal_args
tags.formal_args = nil
local iter = tags.iter or Map.iter
for tag in iter(tags) do
self:set_tag(tag,tags[tag])
end
end
function Item:add_to_description (rest)
if type(rest) == 'string' then
self.description = (self.description or '') .. rest
end
end
function Item:trailing_warning (kind,tag,rest)
if type(rest)=='string' and #rest > 0 then
Item.warning(self,kind.." tag: '"..tag..'" has trailing text; use not_luadoc=true if you want description to continue between tags\n'..rest)
end
end
local function is_list (l)
return getmetatable(l) == List
end
function Item:set_tag (tag,value)
local ttype = known_tags[tag]
local args = self.file.args
if ttype == TAG_MULTI or ttype == TAG_MULTI_LINE then -- value is always a List!
local ovalue = self.tags[tag]
if ovalue then -- already defined, must be a list
--print(tag,ovalue,value)
if is_list(value) then
ovalue:extend(value)
else
ovalue:append(value)
end
value = ovalue
end
-- these multiple values are always represented as lists
if not is_list(value) then
value = List{value}
end
if ttype ~= TAG_MULTI_LINE and args and args.not_luadoc then
local last = value[#value]
if type(last) == 'string' and last:match '\n' then
local line,rest = last:match('([^\n]+)(.*)')
value[#value] = line
self:add_to_description(rest)
end
end
self.tags[tag] = value
elseif ttype == TAG_ID then
local modifiers
if type(value) == 'table' then
if value.append then -- it was a List!
-- such tags are _not_ multiple, e.g. name
self:error("'"..tag.."' cannot have multiple values")
end
value = value[1]
modifiers = value.modifiers
end
if value == nil then self:error("Tag without value: "..tag) end
local id, rest = tools.extract_identifier(value)
self.tags[tag] = id
if args and args.not_luadoc then
self:add_to_description(rest)
else
self:trailing_warning('id',tag,rest)
end
elseif ttype == TAG_SINGLE then
self.tags[tag] = value
elseif ttype == TAG_FLAG then
self.tags[tag] = true
if args.not_luadoc then
self:add_to_description(value)
else
self:trailing_warning('flag',tag,value)
end
else
Item.warning(self,"unknown tag: '"..tag.."
10000
39; "..tostring(ttype))
end
end
-- preliminary processing of tags. We check for any aliases, and for tags
-- which represent types. This implements the shortcut notation.
function Item.check_tag(tags,tag, value, modifiers)
local alias = doc.get_alias(tag)
if alias then
if type(alias) == 'string' then
tag = alias
elseif type(alias) == 'table' then --{ tag, value=, modifiers = }
local avalue,amod
tag, avalue, amod = alias[1],alias.value,alias.modifiers
if avalue then value = avalue..' '..value end
if amod then
modifiers = modifiers or {}
for m,v in pairs(amod) do
local idx = v:match('^%$(%d+)')
if idx then
v, value = value:match('(%S+)(.*)')
end
modifiers[m] = v
end
end
else -- has to be a function that at least returns tag, value
return alias(tags,value,modifiers)
end
end
local ttype = known_tags[tag]
if ttype == TAG_TYPE then
tags:add('class',tag)
tag = 'name'
end
return tag, value, modifiers
end
-- any tag (except name and classs) may have associated modifiers,
-- in the form @tag[m1,...] where m1 is either name1=value1 or name1.
-- At this stage, these are encoded
-- in the tag value table and need to be extracted.
local function extract_value_modifier (p)
if type(p)~='table' then
return p, { }
else
return p[1], p.modifiers or { }
end
end
local function extract_tag_modifiers (tags)
local modifiers, mods = {}
for tag, value in pairs(tags) do
if type(value)=='table' and value.append then -- i.e. it is a List!
local tmods = {}
for i, v in ipairs(value) do
v, mods = extract_value_modifier(v)
tmods[i] = mods
value[i] = v
end
modifiers[tag] = tmods
else
value, mods = extract_value_modifier(value)
modifiers[tag] = mods
tags[tag] = value
end
end
return modifiers
end
local function read_del (tags,name)
local ret = tags[name]
tags[name] = nil
return ret
end
local build_arg_list, split_iden -- forward declaration
function Item:split_param (line)
local name, comment = line:match('%s*([%w_%.:]+)(.*)')
if not name then
self:error("bad param name format '"..line.."'. Are you missing a parameter name?")
end
return name, comment
end
function Item:finish()
local tags = self.tags
local quote = tools.quote
self.name = read_del(tags,'name')
self.type = read_del(tags,'class')
self.modifiers = extract_tag_modifiers(tags)
self.usage = read_del(tags,'usage')
tags.see = read_del(tags,'see')
if tags.see then
tags.see = tools.identifier_list(tags.see)
end
if doc.project_level(self.type) then
-- we are a module, so become one!
self.items = List()
self.sections = List()
self.items.by_name = {}
self.sections.by_name = {}
setmetatable(self,Module)
elseif not doc.section_tag(self.type) then
-- params are either a function's arguments, or a table's fields, etc.
if self.type == 'function' then
self.parameter = 'param'
self.ret = read_del(tags,'return')
self.raise = read_del(tags,'raise')
if tags['local'] then
self.type = 'lfunction'
end
else
self.parameter = 'field'
end
local field = self.parameter
local params = read_del(tags,field)
-- use of macros like @string (which is short for '@tparam string')
-- can lead to param tags associated with a table.
if self.parameter == 'field' and tags.param then
local tparams = read_del(tags,'param')
if params then
params:extend(tparams)
List(self.modifiers.field):extend(self.modifiers.param)
else
params = tparams
self.modifiers.field = self.modifiers.param
end
end
local param_names, comments = List(), List()
if params then
for line in params:iter() do
local name, comment = self:split_param(line)
param_names:append(name)
comments:append(comment)
end
end
self.modifiers['return'] = self.modifiers['return'] or List()
self.modifiers[field] = self.modifiers[field] or List()
-- we use the formal arguments (if available) as the authoritative list.
-- If there are both params and formal args, then they must match;
-- (A formal argument of ... may match any number of params at the end, however.)
-- If there are formal args and no params, we see if the args have any suitable comments.
-- Params may have subfields.
local fargs, formal = self.formal_args
if fargs then
if #param_names == 0 then
--docs may be embedded in argument comments; in either case, use formal arg names
local ret
formal,comments,ret = self:parse_formal_arguments(fargs)
if ret and not self.ret then self.ret = ret end
elseif #fargs > 0 then -- consistency check!
local varargs = fargs[#fargs] == '...'
if varargs then table.remove(fargs) end
local k = 0
for _,pname in ipairs(param_names) do
local _,field = split_iden(pname)
if not field then
k = k + 1
if k > #fargs then
if not varargs then
self:warning("extra param with no formal argument: "..quote(pname))
end
elseif pname ~= fargs[k] then
self:warning("param and formal argument name mismatch: "..quote(pname).." "..quote(fargs[k]))
end
end
end
if k < #fargs then
for i = k+1,#fargs do if fargs[i] ~= '...' then
self:warning("undocumented formal argument: "..quote(fargs[i]))
end end
end
-- formal arguments may come with types, inferred by the
-- appropriate code in ldoc.lang
if fargs.types then
self.modifiers[field] = List()
for t in fargs.types:iter() do
self:add_type(field,t)
end
if fargs.return_type then
if not self.ret then -- type, but no comment; no worries
self.ret = List{''}
end
self.modifiers['return'] = List()
self:add_type('return',fargs.return_type)
end
end
end -- #fargs > 0
end -- fargs
-- the comments are associated with each parameter by
-- adding name-value pairs to the params list (this is
-- also done for any associated modifiers)
-- (At this point we patch up any subparameter references)
local pmods = self.modifiers[field]
local params, fields = List()
local original_names = formal and formal or param_names
local names = List()
self.subparams = {}
for i,name in ipairs(original_names) do
if type(name) ~= 'string' then
self:error("declared table cannot have array entries")
end
local pname,field = split_iden(name)
if field then
if not fields then
fields = List()
self.subparams[pname] = fields
end
fields:append(name)
else
names:append(name)
params:append(name)
fields = nil
end
params[name] = comments[i]
if pmods then
pmods[name] = pmods[i]
end
end
self.params = params
self.args = build_arg_list (names,pmods)
end
if self.ret then
self:build_return_groups()
end
end
function Item:add_type(field,type)
self.modifiers[field]:append {type = type}
end
-- ldoc allows comments in the formal arg list to be used, if they aren't specified with @param
-- Further, these comments may start with a type followed by a colon, and are then equivalent
-- to a @tparam
function Item:parse_argument_comment (comment,field)
if comment then
comment = comment:gsub('^%-+%s*','')
local type,rest = comment:match '([^:]+):(.*)'
if type then
self:add_type(field,type)
comment = rest
end
end
return comment or ''
end
function Item:parse_formal_arguments (fargs)
local formal, comments, ret = List(), List()
if fargs.return_comment then
local retc = self:parse_argument_comment(fargs.return_comment,'return')
ret = List{retc}
end
for i, name in ipairs(fargs) do
formal:append(name)
comments:append(self:parse_argument_comment(fargs.comments[name],self.parameter))
end
return formal, comments, ret
end
function split_iden (name)
if name == '...' then return name end
local pname,field = name:match('(.-)%.(.+)')
if not pname then
return name
else
return pname,field
end
end
function build_arg_list (names,pmods)
-- build up the string representation of the argument list,
-- using any opt and optchain modifiers if present.
-- For instance, '(a [, b])' if b is marked as optional
-- with @param[opt] b
local buffer, npending = { }, 0
local function acc(x) table.insert(buffer, x) end
-- a number of trailing [opt]s can be safely converted to [opt],[optchain],...
if pmods then
local m = pmods[#names]
if m and m.opt then
m.optchain = m.opt
for i = #names-1,1,-1 do
m = pmods[i]
if not m or not m.opt then break end
m.optchain = m.opt
end
end
end
for i = 1, #names do
local m = pmods and pmods[i]
local opt
if m then
if not m.optchain then
acc ((']'):rep(npending))
npending=0
end
opt = m.optchain or m.opt
if opt then
acc('[')
npending=npending+1
end
end
if i>1 then acc (', ') end
acc(names[i])
if opt and opt ~= true then acc('='..opt) end
end
acc ((']'):rep(npending))
return '('..table.concat(buffer)..')'
end
------ retrieving information about parameters -----
-- The template leans on these guys heavily....
function Item:param_modifiers (p)
local mods = self.modifiers[self.parameter]
if not mods then return '' end
return rawget(mods,p)
end
function Item:type_of_param(p)
local mparam = self:param_modifiers(p)
return mparam and mparam.type or ''
end
function Item:default_of_param(p)
local m = self:param_modifiers(p)
if not m then return nil end
local opt = m.optchain or m.opt
if opt == true then return nil end
return opt
end
function Item:subparam(p)
local subp = rawget(self.subparams,p)
if subp then
return subp,p
else
return {p},nil
end
end
function Item:display_name_of(p)
local pname,field = split_iden(p)
if field then
return field
else
return pname
end
end
-------- return values and types -------
function Item:type_of_ret(idx)
local rparam = self.modifiers['return'][idx]
return rparam and rparam.type or ''
end
local function integer_keys(t)
if type(t) ~= 'table' then return 0 end
for k in pairs(t) do
local num = tonumber(k)
if num then return num end
end
return 0
end
function Item:return_type(r)
if not r.type then return '' end
return r.type, r.ctypes
end
local struct_return_type = '*'
function Item:build_return_groups()
local modifiers = self.modifiers
local retmod = modifiers['return']
local groups = List()
local lastg, group
for i,ret in ipairs(self.ret) do
local mods = retmod[i]
local g = integer_keys(mods)
if g ~= lastg then
group = List()
group.g = g
groups:append(group)
lastg = g
end
--require 'pl.pretty'.dump(ret)
group:append({text=ret, type = mods.type or '',mods = mods})
end
-- order by groups to force error groups to the end
table.sort(groups,function(g1,g2) return g1.g < g2.g end)
self.retgroups = groups
--require 'pl.pretty'.dump(groups)
-- cool, now see if there are any treturns that have tfields to associate with
local fields = self.tags.field
if fields then
local fcomments = List()
for i,f in ipairs(fields) do
local name, comment = self:split_param(f)
fields[i] = name
fcomments[i] = comment
end
local fmods = modifiers.field
for group in groups:iter() do for r in group:iter() do
if r.mods and r.mods.type then
local ctypes, T = List(), r.mods.type
for i,f in ipairs(fields) do if fmods[i][T] then
ctypes:append {name=f,type=fmods[i].type,comment=fcomments[i]}
end end
r.ctypes = ctypes
--require 'pl.pretty'.dump(ctypes)
end
end end
end
end
local ecount = 0
-- this alias macro implements @error.
-- Alias macros need to return the same results as Item:check_tags...
function doc.error_macro(tags,value,modifiers)
local merge_groups = doc.ldoc.merge_error_groups
local g = '2' -- our default group id
-- Were we given an explicit group modifier?
local key = integer_keys(modifiers)
if key > 0 then
g = tostring(key)
else
local l = tags:get 'return'
if l then -- there were returns already......
-- maximum group of _existing_ error return
local grp, lastr = 0
for r in l:iter() do if type(r) == 'table' then
local rg = r.modifiers._err
if rg then
lastr = r
grp = math.max(grp,rg)
end
end end
if grp > 0 then -- cool, create new group
if not merge_groups then
g = tostring(grp+1)
else
local mods, text, T = lastr.modifiers
local new = function(text)
return mods._collected..' '..text,{type='string',[T]=true}
end
if not mods._collected then
text = lastr[1]
lastr[1] = merge_groups
T = '@'..ecount
mods.type = T
mods._collected = 1
ecount = ecount + 1
tags:add('field',new(text))
else
T = mods.type
end
mods._collected = mods._collected + 1
return 'field',new(value)
end
end
end
end
tags:add('return','',{[g]=true,type='nil'})
-- note that this 'return' is tagged with _err!
return 'return', value, {[g]=true,_err=tonumber(g),type='string'}
end
---------- bothering the user --------------------
function Item:warning(msg)
local file = self.file and self.file.filename
if type(file) == 'table' then require 'pl.pretty'.dump(file); file = '?' end
file = file or '?'
io.stderr:write(file,':',self.lineno or '1',': ',self.name or '?',': ',msg,'\n')
return nil
end
function Item:error(msg)
self:warning(msg)
os.exit(1)
end
Module.warning, Module.error = Item.warning, Item.error
-------- Resolving References -----------------
function Module:hunt_for_reference (packmod, modules)
local mod_ref
local package = self.package or ''
repeat -- same package?
local nmod = package..'.'..packmod
mod_ref = modules.by_name[nmod]
if mod_ref then break end -- cool
package = split_dotted_name(package)
until not package
return mod_ref
end
local err = io.stderr
local function custom_see_references (s)
for pat, action in pairs(see_reference_handlers) do
if s:match(pat) then
local label, href = action(s:match(pat))
if not label then print('custom rule failed',s,pat,href) end
return {href = href, label = label}