summaryrefslogtreecommitdiff
path: root/utils/fpdoc/dw_dxml.pp
blob: 9f3ef98692e85759e6f24638d7e255f9e3875418 (plain)
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
unit dw_dXML;

{$mode objfpc}{$H+}

interface

uses
  PasTree, dwriter, SysUtils;
//uses DOM, PasTree, dwriter, xmlWrite, SysUtils;

type
  { TXMLWriter }

  TDXMLWriter = class(TFPDocWriter)
    procedure DoWriteDocumentation; override;
  end;

  { TDocumentation }

  TDocumentation = class(TPassTreeVisitor)
    f:   Text;
    lvl: integer;

    procedure GenerateDoc(OutputName: string; Module: TPasModule);

    procedure DocParameters(obj: TPasProcedureType);
    function DocProcFlags(obj: TPasProcedure): string;

    procedure Visit(obj: TPasElement); override;

    procedure DoVisit(obj: TPasSection); virtual;

    procedure DoVisit(obj: TPasRecordType); virtual;
    procedure DoVisit(obj: TPasEnumType); virtual;
    procedure DoVisit(obj: TPasProperty); virtual;
    procedure DoVisit(obj: TPasConst); virtual;
    procedure DoVisit(obj: TPasVariable); virtual;
    procedure DoVisit(obj: TPasProcedure); virtual;
    procedure DoVisit(obj: TPasDestructor); virtual;
    procedure DoVisit(obj: TPasConstructor); virtual;
    procedure DoVisit(obj: TPasFunction); virtual;
    procedure DoVisit(obj: TPasClassType); virtual;
    procedure DoVisit(obj: TPasElement); virtual;
    procedure DoVisit(obj: TPasOverloadedProc); virtual;
    procedure DoVisit(obj: TPasPointerType); virtual;
    procedure DoVisit(obj: TPasArrayType); virtual;
    procedure DoVisit(obj: TPasProcedureType); virtual;
    procedure DoVisit(obj: TPasFunctionType); virtual;
    procedure DoVisit(obj: TPasResString); virtual;
  end;

implementation

function EscapeXml(const s: string): string;
begin
  Result := StringReplace(s, '&', '&', [rfReplaceAll]);
  Result := StringReplace(Result, '<', '&lt;', [rfReplaceAll]);
  Result := StringReplace(Result, '>', '&gt;', [rfReplaceAll]);
end;

{ TDocumentation }

procedure TDocumentation.Visit(obj: TPasElement); 

begin
  If (Obj.ClassType=TPasSection) then
    DoVisit(TPasSection(Obj))
  else if (Obj.ClassType=TPasRecordType) then
    DoVisit(TPasRecordType(Obj))
  else if (Obj.ClassType=TPasEnumType) then
    DoVisit(TPasEnumType(Obj))
  else if (Obj.ClassType=TPasProperty) then
    DoVisit(TPasProperty(Obj))
  else if (Obj.ClassType=TPasConst) then
    DoVisit(TPasConst(Obj))
  else if (Obj.ClassType=TPasVariable) then
    DoVisit(TPasVariable(Obj))
  else if (Obj.ClassType=TPasProcedure) then
    DoVisit(TPasProcedure(Obj))
  else if (Obj.ClassType=TPasDestructor) then
    DoVisit(TPasDestructor(Obj))
  else if (Obj.ClassType=TPasConstructor) then
    DoVisit(TPasConstructor(Obj))
  else if (Obj.ClassType=TPasFunction) then
    DoVisit(TPasFunction(Obj))
  else if (Obj.ClassType=TPasClassType) then
    DoVisit(TPasClassType(Obj))
  else if (Obj.ClassType=TPasOverloadedProc) then
    DoVisit(TPasOverloadedProc(Obj))
  else if (Obj.ClassType=TPasPointerType) then
    DoVisit(TPasPointerType(Obj))
  else if (Obj.ClassType=TPasArrayType) then
    DoVisit(TPasArrayType(Obj))
  else if (Obj.ClassType=TPasProcedureType) then
    DoVisit(TPasProcedureType(Obj))
  else if (Obj.ClassType=TPasFunctionType) then
    DoVisit(TPasFunctionType(Obj))
  else if (Obj.ClassType=TPasResString) then
    DoVisit(TPasResString(Obj));
end;

procedure TDocumentation.GenerateDoc(OutputName: string; Module: TPasModule);
begin
  lvl := 0;
  Assign(f, OutputName);
  Rewrite(f);
  WriteLn(f, '<?xml version="1.0" encoding="utf-8"?>');
  WriteLn(f, '<namespace name="', Module.Name, '">');

  Module.InterfaceSection.Accept(Self);
  //Module.Accept(Self);

  WriteLn(f, '</namespace>');
  Close(f);
end;

procedure TDocumentation.DocParameters(obj: TPasProcedureType);
var
  I: integer;
begin
  for I := 0 to obj.Args.Count - 1 do
  begin
    Write(f, ' ': lvl * 2, '<parameter name="' + TPasArgument(obj.Args[i]).Name + '"');

    if TPasArgument(obj.Args[i]).ArgType <> nil then
      Write(f, ' type="' + TPasArgument(obj.Args[i]).ArgType.Name + '"');

    if TPasArgument(obj.Args[i]).Access <> argDefault then
      if (TPasArgument(obj.Args[i]).ArgType is TPasClassType) then
        Write(f, ' paramflags="' + 'var' + '"')
      else
        Write(f, ' paramflags="' +
          Trim(AccessNames[TPasArgument(obj.Args[i]).Access]) + '"');

    if TPasArgument(obj.Args[i]).Value <> '' then
    begin
      WriteLn(f, '>');
      WriteLn(f, ' ': lvl * 2 + 2, '<value>');
      WriteLn(f, ' ': lvl * 2 + 4, EscapeXml(TPasArgument(obj.Args[i]).Value));
      WriteLn(f, ' ': lvl * 2 + 2, '</value>');
      WriteLn(f, ' ': lvl * 2, '</parameter>');
    end
    else
      WriteLn(f, ' />');

  end;
end;

function TDocumentation.DocProcFlags(obj: TPasProcedure): string;

  procedure DoAdd(B: boolean; S: string);
  begin
    if B then
    begin
      if Result <> '' then
        Result := Result + ' ';
      Result   := Result + S;
    end;
  end;

begin
  Result := '';
  DoAdd(obj.IsAbstract, 'abstract');
  Doadd(obj.IsVirtual, 'virtual');
  DoAdd(obj.IsDynamic, 'dynamic');
  DoAdd(obj.IsOverride, 'override');
  DoAdd(obj.IsOverload, 'overload');
  DoAdd(obj.IsReintroduced, 'reintroduce');
  DoAdd(obj.IsStatic, 'static');
  DoAdd(obj.IsMessage, 'message');
end;

procedure TDocumentation.DoVisit(obj: TPasSection);
var
  i: integer;
begin
  Inc(lvl);
  for i := 0 to obj.Declarations.Count - 1 do
    TPasElement(obj.Declarations[i]).Accept(Self);
  Dec(lvl);
end;

procedure TDocumentation.DoVisit(obj: TPasRecordType);
var
  I: integer;
begin
  Write(f, StringOfChar(' ', lvl * 2) + '<struct');
  if obj.Name <> '' then
    Write(f, ' name="' + obj.Name + '"');
  if obj.IsPacked then
    Write(f, ' packed="true"');
  WriteLn(f, '>');
  Inc(lvl);
  for I := 0 to obj.Members.Count - 1 do
    TPasVariable(obj.Members[i]).Accept(Self);
  Dec(lvl);
  WriteLn(f, StringOfChar(' ', lvl * 2) + '</struct>');
end;

procedure TDocumentation.DoVisit(obj: TPasEnumType);
var
  I: integer;
begin
  for I := 0 to obj.Values.Count - 1 do
  begin
    WriteLn(f, ' ': lvl * 2, '<const name="' + TPasEnumValue(obj.Values[i]).Name + '" type="' +
      obj.Name + '">');
    WriteLn(f, ' ': lvl * 2 + 2, '<value>');
    WriteLn(f, ' ': lvl * 2 + 4, TPasEnumValue(obj.Values[i]).Name);
    WriteLn(f, ' ': lvl * 2 + 2, '</value>');
    WriteLn(f, ' ': lvl * 2, '</const>');
  end;

  WriteLn(f, ' ': lvl * 2, '<enum name="' + obj.Name + '">');
  for I := 0 to obj.Values.Count - 1 do
    WriteLn(f, ' ': lvl * 2 + 2, '<element name="' + TPasEnumValue(obj.Values[i]).Name + '" />');
  WriteLn(f, ' ': lvl * 2, '</enum>');
end;

procedure TDocumentation.DoVisit(obj: TPasProperty);
begin
  if (obj.VarType <> nil) and (obj.VarType is TPasProcedureType) and
    (TPasProcedureType(obj.VarType).IsOfObject) then
    Write(f, ' ': lvl * 2, '<event name="' + obj.Name + '" visibility="' +
      VisibilityNames[obj.Visibility] + '"')
  else
    Write(f, ' ': lvl * 2, '<property name="' + obj.Name + '" visibility="' +
      VisibilityNames[obj.Visibility] + '"');
  if obj.ReadAccessorName <> '' then
    Write(f, ' read="' + obj.ReadAccessorName + '"');
  if obj.WriteAccessorName <> '' then
    Write(f, ' write="' + obj.WriteAccessorName + '"');
  if obj.VarType <> nil then
    Write(f, ' type="' + obj.VarType.Name + '"');
  if obj.DefaultValue <> '' then
    Write(f, ' default="' + obj.DefaultValue + '"');
  WriteLn(f, ' />');
end;

procedure TDocumentation.DoVisit(obj: TPasConst);
begin
  Write(f, ' ': lvl * 2, '<const name="' + obj.Name + '"');
  if (obj.VarType <> nil) and (obj.VarType.Name <> '') then
    Write(f, ' type="' + obj.VarType.Name + '"');
  WriteLn(f, '>');
  WriteLn(f, ' ': lvl * 2 + 2, '<value>');
  WriteLn(f, ' ': lvl * 2 + 4, EscapeXml(obj.Value));
  WriteLn(f, ' ': lvl * 2 + 2, '</value>');
  WriteLn(f, ' ': lvl * 2, '</const>');
end;

procedure TDocumentation.DoVisit(obj: TPasVariable);
begin
  Write(f, ' ': lvl * 2, '<field name="' + obj.Name + '"');
  if (obj.VarType <> nil) and (obj.VarType.Name <> '') then
    Write(f, ' type="' + obj.VarType.Name {.GetDeclaration(True)} + '"');
  if obj.Visibility <> visDefault then
    Write(f, ' visibility="' + VisibilityNames[obj.Visibility] + '"');

  if (obj.VarType <> nil) and (obj.VarType.Name = '')
  {(VarType.ElementTypeName <> SPasTreeType) and (VarType.ElementTypeName <> SPasTreeUnresolvedTypeRef)}
  then
  begin
    WriteLn(f, '>');
    Inc(lvl);
    obj.VarType.Accept(Self);
    Dec(lvl);
    WriteLn(f, ' ': lvl * 2, '</field>');
  end
  else
    WriteLn(f, ' />');
end;

procedure TDocumentation.DoVisit(obj: TPasProcedure);
var
  t: string;
begin
  Write(f, ' ': lvl * 2, '<procedure name="' + obj.Name + '"');
  if obj.Visibility <> visDefault then
    Write(f, ' visibility="' + VisibilityNames[obj.Visibility] + '"');
  t := DocProcFlags(obj);
  if t <> '' then
    Write(f, ' procflags="' + t + '"');
  WriteLn(f, '>');
  Inc(lvl);

  if obj.ProcType.Args.Count > 0 then
  begin
    WriteLn(f, ' ': lvl * 2, '<parameters>');
    Inc(lvl);
    DocParameters(obj.ProcType);
    Dec(lvl);
    WriteLn(f, ' ': lvl * 2, '</parameters>');
  end;

  Dec(lvl);
  WriteLn(f, ' ': lvl * 2, '</procedure>');
end;

procedure TDocumentation.DoVisit(obj: TPasDestructor);
begin
  Write(f, ' ': lvl * 2, '<destructor name="' + obj.Name + '"');
  if obj.Visibility <> visDefault then
    Write(f, ' visibility="' + VisibilityNames[obj.Visibility] + '"');
  WriteLn(f, '>');
  Inc(lvl);
  WriteLn(f, ' ': lvl * 2, '<parameters>');
  Inc(lvl);
  DocParameters(obj.ProcType);
  Dec(lvl);
  WriteLn(f, ' ': lvl * 2, '</parameters>');
  Dec(lvl);
  WriteLn(f, ' ': lvl * 2, '</destructor>');
end;

procedure TDocumentation.DoVisit(obj: TPasConstructor);
begin
  Write(f, ' ': lvl * 2, '<constructor name="' + obj.Name + '"');
  if obj.Visibility <> visDefault then
    Write(f, ' visibility="' + VisibilityNames[obj.Visibility] + '"');
  WriteLn(f, '>');
  Inc(lvl);
  WriteLn(f, ' ': lvl * 2, '<parameters>');
  Inc(lvl);
  DocParameters(obj.ProcType);
  Dec(lvl);
  WriteLn(f, ' ': lvl * 2, '</parameters>');
  Dec(lvl);
  WriteLn(f, ' ': lvl * 2, '</constructor>');
end;

procedure TDocumentation.DoVisit(obj: TPasFunction);
var
  t: string;
begin
  Write(f, ' ': lvl * 2, '<function name="' + obj.Name + '"');
  if obj.Visibility <> visDefault then
    Write(f, ' visibility="' + VisibilityNames[obj.Visibility] + '"');
  t := DocProcFlags(obj);
  if t <> '' then
    Write(f, ' procflags="' + t + '"');
  WriteLn(f, '>');
  Inc(lvl);
  WriteLn(f, ' ': lvl * 2, '<parameters>');
  Inc(lvl);
  DocParameters(obj.ProcType);
  WriteLn(f, ' ': lvl * 2, '<retval type="' +
    TPasFunctionType(obj.ProcType).ResultEl.ResultType.Name + '" />');
  Dec(lvl);
  WriteLn(f, ' ': lvl * 2, '</parameters>');
  Dec(lvl);
  WriteLn(f, ' ': lvl * 2, '</function>');
end;

procedure TDocumentation.DoVisit(obj: TPasClassType);
var
  i: integer;
begin
  case obj.ObjKind of
    okObject: WriteLn(f, ' ': lvl * 2, '<object name="' + obj.Name + '">');
    okClass: WriteLn(f, ' ': lvl * 2, '<class name="' + obj.Name + '">');
    okInterface: WriteLn(f, ' ': lvl * 2, '<interface name="' + obj.Name + '">');
  end;

  Inc(lvl);

  if obj.AncestorType <> nil then
    WriteLn(f, ' ': lvl * 2, '<ancestor name="' + obj.AncestorType.GetDeclaration(True) +
      '" namespace="StdCtrls2">')
  else
    WriteLn(f, ' ': lvl * 2, '<ancestor name="TObject" namespace="System">');
  WriteLn(f, ' ': lvl * 2, '</ancestor>');

  if obj.Members.Count > 0 then
  begin
    WriteLn(f, ' ': lvl * 2, '<members>');
    Inc(lvl);
    for i := 0 to obj.Members.Count - 1 do
      TPasProperty(obj.Members[i]).Accept(Self);
    Dec(lvl);
    WriteLn(f, ' ': lvl * 2, '</members>');
  end;

  Dec(lvl);

  case obj.ObjKind of
    okObject: WriteLn(f, ' ': lvl * 2, '</object>');
    okClass: WriteLn(f, ' ': lvl * 2, '</class>');
    okInterface: WriteLn(f, ' ': lvl * 2, '</interface>');
  end;
end;

procedure TDocumentation.DoVisit(obj: TPasElement);
begin
  WriteLn('Warning: NOT supported: ' + obj.ClassName + ' (' + obj.Name + ')');
end;

procedure TDocumentation.DoVisit(obj: TPasOverloadedProc);
var
  i: integer;
begin
  for i := 0 to obj.Overloads.Count - 1 do
    TPasProcedure(obj.Overloads[i]).Accept(Self);
end;

procedure TDocumentation.DoVisit(obj: TPasPointerType);
begin
  Write(f, ' ': lvl * 2, '<pointer name="' + obj.Name + '"');
  if obj.DestType <> nil then
    Write(f, ' type="' + obj.DestType.Name + '"');
  WriteLn(f, ' indircnt="1" />');
end;

procedure TDocumentation.DoVisit(obj: TPasArrayType);
begin
  Write(f, ' ': lvl * 2, '<array name="' + obj.Name + '"');
  if obj.IndexRange <> '' then
  begin
    if Pos('..', obj.IndexRange) <> 0 then
    begin
      Write(f, ' low="' + Copy(obj.IndexRange, 1, Pos('..', obj.IndexRange) - 1) + '"');
      Write(f, ' high="' + Copy(obj.IndexRange, Pos('..', obj.IndexRange) + 2,
        MaxInt) + '"');
    end
    else
      Write(f, ' high="' + obj.IndexRange + '"');
  end;
  WriteLn(f, '>');

  WriteLn(f, '    <element type="' + obj.ElType.Name + '" />');
  WriteLn(f, '  </array>');
end;

procedure TDocumentation.DoVisit(obj: TPasProcedureType);
begin
  Write(f, ' ': lvl * 2, '<procedureDef name="' + obj.Name + '"');
  if obj.Visibility <> visDefault then
    Write(f, ' visibility="' + VisibilityNames[obj.Visibility] + '"');
  WriteLn(f, '>');

  if obj.Args.Count > 0 then
  begin
    WriteLn(f, ' ': lvl * 2 + 2, '<parameters>');
    DocParameters(obj);
    WriteLn(f, ' ': lvl * 2 + 2, '</parameters>');
  end;

  WriteLn(f, ' ': lvl * 2, '</procedureDef>');
end;

procedure TDocumentation.DoVisit(obj: TPasFunctionType);
begin
  Write(f, ' ': lvl * 2, '<functionDef name="' + obj.Name + '"');
  if obj.Visibility <> visDefault then
    Write(f, ' visibility="' + VisibilityNames[obj.Visibility] + '"');
  WriteLn(f, '>');
  WriteLn(f, ' ': lvl * 2 + 2, '<parameters>');
  DocParameters(obj);
  WriteLn(f, ' ': lvl * 2 + 4, '<retval type="' + obj.ResultEl.ResultType.Name + '" />');
  WriteLn(f, ' ': lvl * 2 + 2, '</parameters>');
  WriteLn(f, ' ': lvl * 2, '</functionDef>');
end;

procedure TDocumentation.DoVisit(obj: TPasResString);
begin
  WriteLn(f, ' ': lvl * 2, '<resourceString name="' + obj.Name + '">');
  WriteLn(f, ' ': lvl * 2 + 2, '<value>');
  WriteLn(f, ' ': lvl * 2 + 4, EscapeXml(obj.GetDeclaration(false)));
  WriteLn(f, ' ': lvl * 2 + 2, '</value>');
  WriteLn(f, ' ': lvl * 2, '</resourceString>');
end;

{ TXMLWriter }

procedure TDXMLWriter.DoWriteDocumentation;
var
  i: integer;
begin
  if Engine.Output <> '' then
    Engine.Output := IncludeTrailingBackSlash(Engine.Output);

  for i := 0 to Package.Modules.Count - 1 do
  begin
    with TDocumentation.Create do
    begin
      GenerateDoc(Engine.Output + TPasModule(Package.Modules[i]).Name +
        '.xml', TPasModule(Package.Modules[i]));
      Free;
    end;
  end;
end;

initialization
  // Do not localize.
  RegisterWriter(TDXMLWriter, 'dxml', 'fpdoc Delphi XML output.');

finalization
  UnRegisterWriter('dxml');
end.