159
|
1
|
|
2 // Compiler implementation of the D programming language
|
|
3 // Copyright (c) 1999-2008 by Digital Mars
|
|
4 // All Rights Reserved
|
|
5 // written by Walter Bright
|
|
6 // http://www.digitalmars.com
|
|
7 // License for redistribution is by either the Artistic License
|
|
8 // in artistic.txt, or the GNU General Public License in gnu.txt.
|
|
9 // See the included readme.txt for details.
|
|
10
|
|
11 // This implements the Ddoc capability.
|
|
12
|
|
13 #include <stdio.h>
|
|
14 #include <string.h>
|
|
15 #include <time.h>
|
|
16 #include <ctype.h>
|
|
17 #include <assert.h>
|
|
18
|
|
19 #if IN_GCC || IN_LLVM
|
|
20 #include "mem.h"
|
|
21 #else
|
|
22 #if _WIN32
|
|
23 #include "..\root\mem.h"
|
|
24 #elif linux
|
|
25 #include "../root/mem.h"
|
|
26 #else
|
|
27 #error "fix this"
|
|
28 #endif
|
|
29 #endif
|
|
30
|
|
31 #include "root.h"
|
|
32
|
|
33 #include "mars.h"
|
|
34 #include "dsymbol.h"
|
|
35 #include "macro.h"
|
|
36 #include "template.h"
|
|
37 #include "lexer.h"
|
|
38 #include "aggregate.h"
|
|
39 #include "declaration.h"
|
|
40 #include "enum.h"
|
|
41 #include "id.h"
|
|
42 #include "module.h"
|
|
43 #include "scope.h"
|
|
44 #include "hdrgen.h"
|
|
45 #include "doc.h"
|
|
46 #include "mtype.h"
|
|
47
|
|
48 struct Escape
|
|
49 {
|
|
50 char *strings[256];
|
|
51
|
|
52 static char *escapeChar(unsigned c);
|
|
53 };
|
|
54
|
|
55 struct Section
|
|
56 {
|
|
57 unsigned char *name;
|
|
58 unsigned namelen;
|
|
59
|
|
60 unsigned char *body;
|
|
61 unsigned bodylen;
|
|
62
|
|
63 int nooutput;
|
|
64
|
|
65 virtual void write(DocComment *dc, Scope *sc, Dsymbol *s, OutBuffer *buf);
|
|
66 };
|
|
67
|
|
68 struct ParamSection : Section
|
|
69 {
|
|
70 void write(DocComment *dc, Scope *sc, Dsymbol *s, OutBuffer *buf);
|
|
71 };
|
|
72
|
|
73 struct MacroSection : Section
|
|
74 {
|
|
75 void write(DocComment *dc, Scope *sc, Dsymbol *s, OutBuffer *buf);
|
|
76 };
|
|
77
|
|
78 struct DocComment
|
|
79 {
|
|
80 Array sections; // Section*[]
|
|
81
|
|
82 Section *summary;
|
|
83 Section *copyright;
|
|
84 Section *macros;
|
|
85 Macro **pmacrotable;
|
|
86 Escape **pescapetable;
|
|
87
|
|
88 DocComment();
|
|
89
|
|
90 static DocComment *parse(Scope *sc, Dsymbol *s, unsigned char *comment);
|
|
91 static void parseMacros(Escape **pescapetable, Macro **pmacrotable, unsigned char *m, unsigned mlen);
|
|
92 static void parseEscapes(Escape **pescapetable, unsigned char *textstart, unsigned textlen);
|
|
93
|
|
94 void parseSections(unsigned char *comment);
|
|
95 void writeSections(Scope *sc, Dsymbol *s, OutBuffer *buf);
|
|
96 };
|
|
97
|
|
98
|
|
99 int cmp(char *stringz, void *s, size_t slen);
|
|
100 int icmp(char *stringz, void *s, size_t slen);
|
|
101 int isDitto(unsigned char *comment);
|
|
102 unsigned char *skipwhitespace(unsigned char *p);
|
|
103 unsigned skiptoident(OutBuffer *buf, unsigned i);
|
|
104 unsigned skippastident(OutBuffer *buf, unsigned i);
|
|
105 unsigned skippastURL(OutBuffer *buf, unsigned i);
|
|
106 void highlightText(Scope *sc, Dsymbol *s, OutBuffer *buf, unsigned offset);
|
|
107 void highlightCode(Scope *sc, Dsymbol *s, OutBuffer *buf, unsigned offset);
|
|
108 void highlightCode2(Scope *sc, Dsymbol *s, OutBuffer *buf, unsigned offset);
|
|
109 Argument *isFunctionParameter(Dsymbol *s, unsigned char *p, unsigned len);
|
|
110
|
|
111 static unsigned char ddoc_default[] = "\
|
|
112 DDOC = <html><head>\n\
|
|
113 <META http-equiv=\"content-type\" content=\"text/html; charset=utf-8\">\n\
|
|
114 <title>$(TITLE)</title>\n\
|
|
115 </head><body>\n\
|
|
116 <h1>$(TITLE)</h1>\n\
|
|
117 $(BODY)\n\
|
|
118 <hr>$(SMALL Page generated by $(LINK2 http://www.digitalmars.com/d/1.0/ddoc.html, Ddoc). $(COPYRIGHT))\n\
|
|
119 </body></html>\n\
|
|
120 \n\
|
|
121 B = <b>$0</b>\n\
|
|
122 I = <i>$0</i>\n\
|
|
123 U = <u>$0</u>\n\
|
|
124 P = <p>$0</p>\n\
|
|
125 DL = <dl>$0</dl>\n\
|
|
126 DT = <dt>$0</dt>\n\
|
|
127 DD = <dd>$0</dd>\n\
|
|
128 TABLE = <table>$0</table>\n\
|
|
129 TR = <tr>$0</tr>\n\
|
|
130 TH = <th>$0</th>\n\
|
|
131 TD = <td>$0</td>\n\
|
|
132 OL = <ol>$0</ol>\n\
|
|
133 UL = <ul>$0</ul>\n\
|
|
134 LI = <li>$0</li>\n\
|
|
135 BIG = <big>$0</big>\n\
|
|
136 SMALL = <small>$0</small>\n\
|
|
137 BR = <br>\n\
|
|
138 LINK = <a href=\"$0\">$0</a>\n\
|
|
139 LINK2 = <a href=\"$1\">$+</a>\n\
|
|
140 \n\
|
|
141 RED = <font color=red>$0</font>\n\
|
|
142 BLUE = <font color=blue>$0</font>\n\
|
|
143 GREEN = <font color=green>$0</font>\n\
|
|
144 YELLOW =<font color=yellow>$0</font>\n\
|
|
145 BLACK = <font color=black>$0</font>\n\
|
|
146 WHITE = <font color=white>$0</font>\n\
|
|
147 \n\
|
|
148 D_CODE = <pre class=\"d_code\">$0</pre>\n\
|
|
149 D_COMMENT = $(GREEN $0)\n\
|
|
150 D_STRING = $(RED $0)\n\
|
|
151 D_KEYWORD = $(BLUE $0)\n\
|
|
152 D_PSYMBOL = $(U $0)\n\
|
|
153 D_PARAM = $(I $0)\n\
|
|
154 \n\
|
|
155 DDOC_COMMENT = <!-- $0 -->\n\
|
|
156 DDOC_DECL = $(DT $(BIG $0))\n\
|
|
157 DDOC_DECL_DD = $(DD $0)\n\
|
|
158 DDOC_DITTO = $(BR)$0\n\
|
|
159 DDOC_SECTIONS = $0\n\
|
|
160 DDOC_SUMMARY = $0$(BR)$(BR)\n\
|
|
161 DDOC_DESCRIPTION = $0$(BR)$(BR)\n\
|
|
162 DDOC_AUTHORS = $(B Authors:)$(BR)\n$0$(BR)$(BR)\n\
|
|
163 DDOC_BUGS = $(RED BUGS:)$(BR)\n$0$(BR)$(BR)\n\
|
|
164 DDOC_COPYRIGHT = $(B Copyright:)$(BR)\n$0$(BR)$(BR)\n\
|
|
165 DDOC_DATE = $(B Date:)$(BR)\n$0$(BR)$(BR)\n\
|
|
166 DDOC_DEPRECATED = $(RED Deprecated:)$(BR)\n$0$(BR)$(BR)\n\
|
|
167 DDOC_EXAMPLES = $(B Examples:)$(BR)\n$0$(BR)$(BR)\n\
|
|
168 DDOC_HISTORY = $(B History:)$(BR)\n$0$(BR)$(BR)\n\
|
|
169 DDOC_LICENSE = $(B License:)$(BR)\n$0$(BR)$(BR)\n\
|
|
170 DDOC_RETURNS = $(B Returns:)$(BR)\n$0$(BR)$(BR)\n\
|
|
171 DDOC_SEE_ALSO = $(B See Also:)$(BR)\n$0$(BR)$(BR)\n\
|
|
172 DDOC_STANDARDS = $(B Standards:)$(BR)\n$0$(BR)$(BR)\n\
|
|
173 DDOC_THROWS = $(B Throws:)$(BR)\n$0$(BR)$(BR)\n\
|
|
174 DDOC_VERSION = $(B Version:)$(BR)\n$0$(BR)$(BR)\n\
|
|
175 DDOC_SECTION_H = $(B $0)$(BR)\n\
|
|
176 DDOC_SECTION = $0$(BR)$(BR)\n\
|
|
177 DDOC_MEMBERS = $(DL $0)\n\
|
|
178 DDOC_MODULE_MEMBERS = $(DDOC_MEMBERS $0)\n\
|
|
179 DDOC_CLASS_MEMBERS = $(DDOC_MEMBERS $0)\n\
|
|
180 DDOC_STRUCT_MEMBERS = $(DDOC_MEMBERS $0)\n\
|
|
181 DDOC_ENUM_MEMBERS = $(DDOC_MEMBERS $0)\n\
|
|
182 DDOC_TEMPLATE_MEMBERS = $(DDOC_MEMBERS $0)\n\
|
|
183 DDOC_PARAMS = $(B Params:)$(BR)\n$(TABLE $0)$(BR)\n\
|
|
184 DDOC_PARAM_ROW = $(TR $0)\n\
|
|
185 DDOC_PARAM_ID = $(TD $0)\n\
|
|
186 DDOC_PARAM_DESC = $(TD $0)\n\
|
|
187 DDOC_BLANKLINE = $(BR)$(BR)\n\
|
|
188 \n\
|
|
189 DDOC_PSYMBOL = $(U $0)\n\
|
|
190 DDOC_KEYWORD = $(B $0)\n\
|
|
191 DDOC_PARAM = $(I $0)\n\
|
|
192 \n\
|
|
193 ESCAPES = /</</\n\
|
|
194 />/>/\n\
|
|
195 /&/&/\n\
|
|
196 ";
|
|
197
|
|
198 static char ddoc_decl_s[] = "$(DDOC_DECL ";
|
|
199 static char ddoc_decl_e[] = ")\n";
|
|
200
|
|
201 static char ddoc_decl_dd_s[] = "$(DDOC_DECL_DD ";
|
|
202 static char ddoc_decl_dd_e[] = ")\n";
|
|
203
|
|
204
|
|
205 /****************************************************
|
|
206 */
|
|
207
|
|
208 void Module::gendocfile()
|
|
209 {
|
|
210 static OutBuffer mbuf;
|
|
211 static int mbuf_done;
|
|
212
|
|
213 OutBuffer buf;
|
|
214
|
|
215 //printf("Module::gendocfile()\n");
|
|
216
|
|
217 if (!mbuf_done) // if not already read the ddoc files
|
|
218 { mbuf_done = 1;
|
|
219
|
|
220 // Use our internal default
|
|
221 mbuf.write(ddoc_default, sizeof(ddoc_default) - 1);
|
|
222
|
|
223 // Override with DDOCFILE specified in the sc.ini file
|
|
224 char *p = getenv("DDOCFILE");
|
|
225 if (p)
|
|
226 global.params.ddocfiles->shift(p);
|
|
227
|
|
228 // Override with the ddoc macro files from the command line
|
|
229 for (int i = 0; i < global.params.ddocfiles->dim; i++)
|
|
230 {
|
|
231 FileName f((char *)global.params.ddocfiles->data[i], 0);
|
|
232 File file(&f);
|
|
233 file.readv();
|
|
234 // BUG: convert file contents to UTF-8 before use
|
|
235
|
|
236 //printf("file: '%.*s'\n", file.len, file.buffer);
|
|
237 mbuf.write(file.buffer, file.len);
|
|
238 }
|
|
239 }
|
|
240 DocComment::parseMacros(&escapetable, ¯otable, mbuf.data, mbuf.offset);
|
|
241
|
|
242 Scope *sc = Scope::createGlobal(this); // create root scope
|
|
243 sc->docbuf = &buf;
|
|
244
|
|
245 DocComment *dc = DocComment::parse(sc, this, comment);
|
|
246 dc->pmacrotable = ¯otable;
|
|
247 dc->pescapetable = &escapetable;
|
|
248
|
|
249 // Generate predefined macros
|
|
250
|
|
251 // Set the title to be the name of the module
|
|
252 { char *p = toPrettyChars();
|
|
253 Macro::define(¯otable, (unsigned char *)"TITLE", 5, (unsigned char *)p, strlen(p));
|
|
254 }
|
|
255
|
|
256 time_t t;
|
|
257 time(&t);
|
|
258 char *p = ctime(&t);
|
|
259 p = mem.strdup(p);
|
|
260 Macro::define(¯otable, (unsigned char *)"DATETIME", 8, (unsigned char *)p, strlen(p));
|
|
261 Macro::define(¯otable, (unsigned char *)"YEAR", 4, (unsigned char *)p + 20, 4);
|
|
262
|
|
263 char *docfilename = docfile->toChars();
|
|
264 Macro::define(¯otable, (unsigned char *)"DOCFILENAME", 11, (unsigned char *)docfilename, strlen(docfilename));
|
|
265
|
|
266 if (dc->copyright)
|
|
267 {
|
|
268 dc->copyright->nooutput = 1;
|
|
269 Macro::define(¯otable, (unsigned char *)"COPYRIGHT", 9, dc->copyright->body, dc->copyright->bodylen);
|
|
270 }
|
|
271
|
|
272 buf.printf("$(DDOC_COMMENT Generated by Ddoc from %s)\n", srcfile->toChars());
|
|
273 if (isDocFile)
|
|
274 {
|
|
275 size_t commentlen = strlen((char *)comment);
|
|
276 if (dc->macros)
|
|
277 {
|
|
278 commentlen = dc->macros->name - comment;
|
|
279 dc->macros->write(dc, sc, this, sc->docbuf);
|
|
280 }
|
|
281 sc->docbuf->write(comment, commentlen);
|
|
282 highlightText(NULL, this, sc->docbuf, 0);
|
|
283 }
|
|
284 else
|
|
285 {
|
|
286 dc->writeSections(sc, this, sc->docbuf);
|
|
287 emitMemberComments(sc);
|
|
288 }
|
|
289
|
|
290 //printf("BODY= '%.*s'\n", buf.offset, buf.data);
|
|
291 Macro::define(¯otable, (unsigned char *)"BODY", 4, buf.data, buf.offset);
|
|
292
|
|
293 OutBuffer buf2;
|
|
294 buf2.writestring("$(DDOC)\n");
|
|
295 unsigned end = buf2.offset;
|
|
296 macrotable->expand(&buf2, 0, &end, NULL, 0);
|
|
297
|
|
298 #if 1
|
|
299 /* Remove all the escape sequences from buf2,
|
|
300 * and make CR-LF the newline.
|
|
301 */
|
|
302 {
|
|
303 buf.setsize(0);
|
|
304 buf.reserve(buf2.offset);
|
|
305 unsigned char *p = buf2.data;
|
|
306 for (unsigned j = 0; j < buf2.offset; j++)
|
|
307 {
|
|
308 unsigned char c = p[j];
|
|
309 if (c == 0xFF && j + 1 < buf2.offset)
|
|
310 {
|
|
311 j++;
|
|
312 continue;
|
|
313 }
|
|
314 if (c == '\n')
|
|
315 buf.writeByte('\r');
|
|
316 else if (c == '\r')
|
|
317 {
|
|
318 buf.writestring("\r\n");
|
|
319 if (j + 1 < buf2.offset && p[j + 1] == '\n')
|
|
320 {
|
|
321 j++;
|
|
322 }
|
|
323 continue;
|
|
324 }
|
|
325 buf.writeByte(c);
|
|
326 }
|
|
327 }
|
|
328
|
|
329 // Transfer image to file
|
|
330 assert(docfile);
|
|
331 docfile->setbuffer(buf.data, buf.offset);
|
|
332 docfile->ref = 1;
|
|
333 char *pt = FileName::path(docfile->toChars());
|
|
334 if (*pt)
|
|
335 FileName::ensurePathExists(pt);
|
|
336 mem.free(pt);
|
|
337 docfile->writev();
|
|
338 #else
|
|
339 /* Remove all the escape sequences from buf2
|
|
340 */
|
|
341 { unsigned i = 0;
|
|
342 unsigned char *p = buf2.data;
|
|
343 for (unsigned j = 0; j < buf2.offset; j++)
|
|
344 {
|
|
345 if (p[j] == 0xFF && j + 1 < buf2.offset)
|
|
346 {
|
|
347 j++;
|
|
348 continue;
|
|
349 }
|
|
350 p[i] = p[j];
|
|
351 i++;
|
|
352 }
|
|
353 buf2.setsize(i);
|
|
354 }
|
|
355
|
|
356 // Transfer image to file
|
|
357 docfile->setbuffer(buf2.data, buf2.offset);
|
|
358 docfile->ref = 1;
|
|
359 char *pt = FileName::path(docfile->toChars());
|
|
360 if (*pt)
|
|
361 FileName::ensurePathExists(pt);
|
|
362 mem.free(pt);
|
|
363 docfile->writev();
|
|
364 #endif
|
|
365 }
|
|
366
|
|
367 /******************************* emitComment **********************************/
|
|
368
|
|
369 /*
|
|
370 * Emit doc comment to documentation file
|
|
371 */
|
|
372
|
|
373 void Dsymbol::emitDitto(Scope *sc)
|
|
374 {
|
|
375 //printf("Dsymbol::emitDitto() %s %s\n", kind(), toChars());
|
|
376 OutBuffer *buf = sc->docbuf;
|
|
377 unsigned o;
|
|
378 OutBuffer b;
|
|
379
|
|
380 b.writestring("$(DDOC_DITTO ");
|
|
381 o = b.offset;
|
|
382 toDocBuffer(&b);
|
|
383 //printf("b: '%.*s'\n", b.offset, b.data);
|
|
384 /* If 'this' is a function template, then highlightCode() was
|
|
385 * already run by FuncDeclaration::toDocbuffer().
|
|
386 */
|
|
387 TemplateDeclaration *td;
|
|
388 if (parent &&
|
|
389 (td = parent->isTemplateDeclaration()) != NULL &&
|
|
390 td->onemember == this)
|
|
391 {
|
|
392 }
|
|
393 else
|
|
394 highlightCode(sc, this, &b, o);
|
|
395 b.writeByte(')');
|
|
396 buf->spread(sc->lastoffset, b.offset);
|
|
397 memcpy(buf->data + sc->lastoffset, b.data, b.offset);
|
|
398 sc->lastoffset += b.offset;
|
|
399 }
|
|
400
|
|
401 void ScopeDsymbol::emitMemberComments(Scope *sc)
|
|
402 {
|
|
403 //printf("ScopeDsymbol::emitMemberComments()\n");
|
|
404 OutBuffer *buf = sc->docbuf;
|
|
405
|
|
406 if (members)
|
|
407 { char *m = "$(DDOC_MEMBERS \n";
|
|
408
|
|
409 if (isModule())
|
|
410 m = "$(DDOC_MODULE_MEMBERS \n";
|
|
411 else if (isClassDeclaration())
|
|
412 m = "$(DDOC_CLASS_MEMBERS \n";
|
|
413 else if (isStructDeclaration())
|
|
414 m = "$(DDOC_STRUCT_MEMBERS \n";
|
|
415 else if (isEnumDeclaration())
|
|
416 m = "$(DDOC_ENUM_MEMBERS \n";
|
|
417 else if (isTemplateDeclaration())
|
|
418 m = "$(DDOC_TEMPLATE_MEMBERS \n";
|
|
419
|
|
420 // BUG: if no members are actually printed, we should not emit DDOC_MEMBERS
|
|
421 buf->writestring(m);
|
|
422 sc = sc->push(this);
|
|
423 for (int i = 0; i < members->dim; i++)
|
|
424 {
|
|
425 Dsymbol *s = (Dsymbol *)members->data[i];
|
|
426 //printf("\ts = '%s'\n", s->toChars());
|
|
427 s->emitComment(sc);
|
|
428 }
|
|
429 sc->pop();
|
|
430 buf->writestring(")\n");
|
|
431 }
|
|
432 }
|
|
433
|
|
434 void emitProtection(OutBuffer *buf, PROT prot)
|
|
435 {
|
|
436 char *p;
|
|
437
|
|
438 switch (prot)
|
|
439 {
|
|
440 case PROTpackage: p = "package"; break;
|
|
441 case PROTprotected: p = "protected"; break;
|
|
442 case PROTexport: p = "export"; break;
|
|
443 default: p = NULL; break;
|
|
444 }
|
|
445 if (p)
|
|
446 buf->printf("%s ", p);
|
|
447 }
|
|
448
|
|
449 void Dsymbol::emitComment(Scope *sc) { }
|
|
450 void InvariantDeclaration::emitComment(Scope *sc) { }
|
|
451 //void PostBlitDeclaration::emitComment(Scope *sc) { }
|
|
452 void DtorDeclaration::emitComment(Scope *sc) { }
|
|
453 void StaticCtorDeclaration::emitComment(Scope *sc) { }
|
|
454 void StaticDtorDeclaration::emitComment(Scope *sc) { }
|
|
455 void ClassInfoDeclaration::emitComment(Scope *sc) { }
|
|
456 void ModuleInfoDeclaration::emitComment(Scope *sc) { }
|
|
457 void TypeInfoDeclaration::emitComment(Scope *sc) { }
|
|
458
|
|
459
|
|
460 void Declaration::emitComment(Scope *sc)
|
|
461 {
|
|
462 //printf("Declaration::emitComment(%p '%s'), comment = '%s'\n", this, toChars(), comment);
|
|
463 //printf("type = %p\n", type);
|
|
464
|
|
465 if (protection == PROTprivate || !ident ||
|
|
466 (!type && !isCtorDeclaration() && !isAliasDeclaration()))
|
|
467 return;
|
|
468 if (!comment)
|
|
469 return;
|
|
470
|
|
471 OutBuffer *buf = sc->docbuf;
|
|
472 DocComment *dc = DocComment::parse(sc, this, comment);
|
|
473 unsigned o;
|
|
474
|
|
475 if (!dc)
|
|
476 {
|
|
477 emitDitto(sc);
|
|
478 return;
|
|
479 }
|
|
480 dc->pmacrotable = &sc->module->macrotable;
|
|
481
|
|
482 buf->writestring(ddoc_decl_s);
|
|
483 o = buf->offset;
|
|
484 toDocBuffer(buf);
|
|
485 highlightCode(sc, this, buf, o);
|
|
486 sc->lastoffset = buf->offset;
|
|
487 buf->writestring(ddoc_decl_e);
|
|
488
|
|
489 buf->writestring(ddoc_decl_dd_s);
|
|
490 dc->writeSections(sc, this, buf);
|
|
491 buf->writestring(ddoc_decl_dd_e);
|
|
492 }
|
|
493
|
|
494 void AggregateDeclaration::emitComment(Scope *sc)
|
|
495 {
|
|
496 //printf("AggregateDeclaration::emitComment() '%s'\n", toChars());
|
|
497 if (prot() == PROTprivate)
|
|
498 return;
|
|
499 if (!comment)
|
|
500 return;
|
|
501
|
|
502 OutBuffer *buf = sc->docbuf;
|
|
503 DocComment *dc = DocComment::parse(sc, this, comment);
|
|
504
|
|
505 if (!dc)
|
|
506 {
|
|
507 emitDitto(sc);
|
|
508 return;
|
|
509 }
|
|
510 dc->pmacrotable = &sc->module->macrotable;
|
|
511
|
|
512 buf->writestring(ddoc_decl_s);
|
|
513 toDocBuffer(buf);
|
|
514 sc->lastoffset = buf->offset;
|
|
515 buf->writestring(ddoc_decl_e);
|
|
516
|
|
517 buf->writestring(ddoc_decl_dd_s);
|
|
518 dc->writeSections(sc, this, buf);
|
|
519 emitMemberComments(sc);
|
|
520 buf->writestring(ddoc_decl_dd_e);
|
|
521 }
|
|
522
|
|
523 void TemplateDeclaration::emitComment(Scope *sc)
|
|
524 {
|
|
525 //printf("TemplateDeclaration::emitComment() '%s', kind = %s\n", toChars(), kind());
|
|
526 if (prot() == PROTprivate)
|
|
527 return;
|
|
528 if (!comment)
|
|
529 return;
|
|
530
|
|
531 OutBuffer *buf = sc->docbuf;
|
|
532 DocComment *dc = DocComment::parse(sc, this, comment);
|
|
533 unsigned o;
|
|
534 int hasmembers = 1;
|
|
535
|
|
536 Dsymbol *ss = this;
|
|
537
|
|
538 if (onemember)
|
|
539 {
|
|
540 ss = onemember->isAggregateDeclaration();
|
|
541 if (!ss)
|
|
542 {
|
|
543 ss = onemember->isFuncDeclaration();
|
|
544 if (ss)
|
|
545 hasmembers = 0;
|
|
546 else
|
|
547 ss = this;
|
|
548 }
|
|
549 }
|
|
550
|
|
551 if (!dc)
|
|
552 {
|
|
553 ss->emitDitto(sc);
|
|
554 return;
|
|
555 }
|
|
556 dc->pmacrotable = &sc->module->macrotable;
|
|
557
|
|
558 buf->writestring(ddoc_decl_s);
|
|
559 o = buf->offset;
|
|
560 ss->toDocBuffer(buf);
|
|
561 if (ss == this)
|
|
562 highlightCode(sc, this, buf, o);
|
|
563 sc->lastoffset = buf->offset;
|
|
564 buf->writestring(ddoc_decl_e);
|
|
565
|
|
566 buf->writestring(ddoc_decl_dd_s);
|
|
567 dc->writeSections(sc, this, buf);
|
|
568 if (hasmembers)
|
|
569 ((ScopeDsymbol *)ss)->emitMemberComments(sc);
|
|
570 buf->writestring(ddoc_decl_dd_e);
|
|
571 }
|
|
572
|
|
573 void EnumDeclaration::emitComment(Scope *sc)
|
|
574 {
|
|
575 if (prot() == PROTprivate)
|
|
576 return;
|
|
577 // if (!comment)
|
|
578 { if (isAnonymous() && members)
|
|
579 {
|
|
580 for (int i = 0; i < members->dim; i++)
|
|
581 {
|
|
582 Dsymbol *s = (Dsymbol *)members->data[i];
|
|
583 s->emitComment(sc);
|
|
584 }
|
|
585 return;
|
|
586 }
|
|
587 }
|
|
588 if (!comment)
|
|
589 return;
|
|
590 if (isAnonymous())
|
|
591 return;
|
|
592
|
|
593 OutBuffer *buf = sc->docbuf;
|
|
594 DocComment *dc = DocComment::parse(sc, this, comment);
|
|
595
|
|
596 if (!dc)
|
|
597 {
|
|
598 emitDitto(sc);
|
|
599 return;
|
|
600 }
|
|
601 dc->pmacrotable = &sc->module->macrotable;
|
|
602
|
|
603 buf->writestring(ddoc_decl_s);
|
|
604 toDocBuffer(buf);
|
|
605 sc->lastoffset = buf->offset;
|
|
606 buf->writestring(ddoc_decl_e);
|
|
607
|
|
608 buf->writestring(ddoc_decl_dd_s);
|
|
609 dc->writeSections(sc, this, buf);
|
|
610 emitMemberComments(sc);
|
|
611 buf->writestring(ddoc_decl_dd_e);
|
|
612 }
|
|
613
|
|
614 void EnumMember::emitComment(Scope *sc)
|
|
615 {
|
|
616 //printf("EnumMember::emitComment(%p '%s'), comment = '%s'\n", this, toChars(), comment);
|
|
617 if (prot() == PROTprivate)
|
|
618 return;
|
|
619 if (!comment)
|
|
620 return;
|
|
621
|
|
622 OutBuffer *buf = sc->docbuf;
|
|
623 DocComment *dc = DocComment::parse(sc, this, comment);
|
|
624 unsigned o;
|
|
625
|
|
626 if (!dc)
|
|
627 {
|
|
628 emitDitto(sc);
|
|
629 return;
|
|
630 }
|
|
631 dc->pmacrotable = &sc->module->macrotable;
|
|
632
|
|
633 buf->writestring(ddoc_decl_s);
|
|
634 o = buf->offset;
|
|
635 toDocBuffer(buf);
|
|
636 highlightCode(sc, this, buf, o);
|
|
637 sc->lastoffset = buf->offset;
|
|
638 buf->writestring(ddoc_decl_e);
|
|
639
|
|
640 buf->writestring(ddoc_decl_dd_s);
|
|
641 dc->writeSections(sc, this, buf);
|
|
642 buf->writestring(ddoc_decl_dd_e);
|
|
643 }
|
|
644
|
|
645 /******************************* toDocBuffer **********************************/
|
|
646
|
|
647 void Dsymbol::toDocBuffer(OutBuffer *buf)
|
|
648 {
|
|
649 //printf("Dsymbol::toDocbuffer() %s\n", toChars());
|
|
650 HdrGenState hgs;
|
|
651
|
|
652 hgs.ddoc = 1;
|
|
653 toCBuffer(buf, &hgs);
|
|
654 }
|
|
655
|
|
656 void prefix(OutBuffer *buf, Dsymbol *s)
|
|
657 {
|
|
658 if (s->isDeprecated())
|
|
659 buf->writestring("deprecated ");
|
|
660 Declaration *d = s->isDeclaration();
|
|
661 if (d)
|
|
662 {
|
|
663 emitProtection(buf, d->protection);
|
|
664 if (d->isAbstract())
|
|
665 buf->writestring("abstract ");
|
|
666 if (d->isStatic())
|
|
667 buf->writestring("static ");
|
|
668 if (d->isConst())
|
|
669 buf->writestring("const ");
|
|
670 #if V2
|
|
671 if (d->isInvariant())
|
|
672 buf->writestring("invariant ");
|
|
673 #endif
|
|
674 if (d->isFinal())
|
|
675 buf->writestring("final ");
|
|
676 if (d->isSynchronized())
|
|
677 buf->writestring("synchronized ");
|
|
678 }
|
|
679 }
|
|
680
|
|
681 void Declaration::toDocBuffer(OutBuffer *buf)
|
|
682 {
|
|
683 //printf("Declaration::toDocbuffer() %s, originalType = %p\n", toChars(), originalType);
|
|
684 if (ident)
|
|
685 {
|
|
686 prefix(buf, this);
|
|
687
|
|
688 if (type)
|
|
689 { HdrGenState hgs;
|
|
690 hgs.ddoc = 1;
|
|
691 if (originalType)
|
|
692 { //originalType->print();
|
|
693 originalType->toCBuffer(buf, ident, &hgs);
|
|
694 }
|
|
695 else
|
|
696 type->toCBuffer(buf, ident, &hgs);
|
|
697 }
|
|
698 else
|
|
699 buf->writestring(ident->toChars());
|
|
700 buf->writestring(";\n");
|
|
701 }
|
|
702 }
|
|
703
|
|
704
|
|
705 void AliasDeclaration::toDocBuffer(OutBuffer *buf)
|
|
706 {
|
|
707 //printf("AliasDeclaration::toDocbuffer() %s\n", toChars());
|
|
708 if (ident)
|
|
709 {
|
|
710 if (isDeprecated())
|
|
711 buf->writestring("deprecated ");
|
|
712
|
|
713 emitProtection(buf, protection);
|
|
714 buf->writestring("alias ");
|
|
715 buf->writestring(toChars());
|
|
716 buf->writestring(";\n");
|
|
717 }
|
|
718 }
|
|
719
|
|
720
|
|
721 void TypedefDeclaration::toDocBuffer(OutBuffer *buf)
|
|
722 {
|
|
723 if (ident)
|
|
724 {
|
|
725 if (isDeprecated())
|
|
726 buf->writestring("deprecated ");
|
|
727
|
|
728 emitProtection(buf, protection);
|
|
729 buf->writestring("typedef ");
|
|
730 buf->writestring(toChars());
|
|
731 buf->writestring(";\n");
|
|
732 }
|
|
733 }
|
|
734
|
|
735
|
|
736 void FuncDeclaration::toDocBuffer(OutBuffer *buf)
|
|
737 {
|
|
738 //printf("FuncDeclaration::toDocbuffer() %s\n", toChars());
|
|
739 if (ident)
|
|
740 {
|
|
741 TemplateDeclaration *td;
|
|
742
|
|
743 if (parent &&
|
|
744 (td = parent->isTemplateDeclaration()) != NULL &&
|
|
745 td->onemember == this)
|
|
746 { HdrGenState hgs;
|
|
747 unsigned o = buf->offset;
|
|
748 TypeFunction *tf = (TypeFunction *)type;
|
|
749
|
|
750 hgs.ddoc = 1;
|
|
751 prefix(buf, td);
|
|
752 tf->next->toCBuffer(buf, NULL, &hgs);
|
|
753 buf->writeByte(' ');
|
|
754 buf->writestring(ident->toChars());
|
|
755 buf->writeByte('(');
|
|
756 for (int i = 0; i < td->origParameters->dim; i++)
|
|
757 {
|
|
758 TemplateParameter *tp = (TemplateParameter *)td->origParameters->data[i];
|
|
759 if (i)
|
|
760 buf->writeByte(',');
|
|
761 tp->toCBuffer(buf, &hgs);
|
|
762 }
|
|
763 buf->writeByte(')');
|
|
764 Argument::argsToCBuffer(buf, &hgs, tf->parameters, tf->varargs);
|
|
765 buf->writestring(";\n");
|
|
766
|
|
767 highlightCode(NULL, this, buf, o);
|
|
768 }
|
|
769 else
|
|
770 {
|
|
771 Declaration::toDocBuffer(buf);
|
|
772 }
|
|
773 }
|
|
774 }
|
|
775
|
|
776 void CtorDeclaration::toDocBuffer(OutBuffer *buf)
|
|
777 {
|
|
778 HdrGenState hgs;
|
|
779
|
|
780 buf->writestring("this");
|
|
781 Argument::argsToCBuffer(buf, &hgs, arguments, varargs);
|
|
782 buf->writestring(";\n");
|
|
783 }
|
|
784
|
|
785
|
|
786 void AggregateDeclaration::toDocBuffer(OutBuffer *buf)
|
|
787 {
|
|
788 if (ident)
|
|
789 {
|
|
790 #if 0
|
|
791 emitProtection(buf, protection);
|
|
792 #endif
|
|
793 buf->printf("%s $(DDOC_PSYMBOL %s)", kind(), toChars());
|
|
794 buf->writestring(";\n");
|
|
795 }
|
|
796 }
|
|
797
|
|
798 void StructDeclaration::toDocBuffer(OutBuffer *buf)
|
|
799 {
|
|
800 //printf("StructDeclaration::toDocbuffer() %s\n", toChars());
|
|
801 if (ident)
|
|
802 {
|
|
803 #if 0
|
|
804 emitProtection(buf, protection);
|
|
805 #endif
|
|
806 TemplateDeclaration *td;
|
|
807
|
|
808 if (parent &&
|
|
809 (td = parent->isTemplateDeclaration()) != NULL &&
|
|
810 td->onemember == this)
|
|
811 { unsigned o = buf->offset;
|
|
812 td->toDocBuffer(buf);
|
|
813 highlightCode(NULL, this, buf, o);
|
|
814 }
|
|
815 else
|
|
816 {
|
|
817 buf->printf("%s $(DDOC_PSYMBOL %s)", kind(), toChars());
|
|
818 }
|
|
819 buf->writestring(";\n");
|
|
820 }
|
|
821 }
|
|
822
|
|
823 void ClassDeclaration::toDocBuffer(OutBuffer *buf)
|
|
824 {
|
|
825 //printf("ClassDeclaration::toDocbuffer() %s\n", toChars());
|
|
826 if (ident)
|
|
827 {
|
|
828 #if 0
|
|
829 emitProtection(buf, protection);
|
|
830 #endif
|
|
831 TemplateDeclaration *td;
|
|
832
|
|
833 if (parent &&
|
|
834 (td = parent->isTemplateDeclaration()) != NULL &&
|
|
835 td->onemember == this)
|
|
836 { unsigned o = buf->offset;
|
|
837 td->toDocBuffer(buf);
|
|
838 highlightCode(NULL, this, buf, o);
|
|
839 }
|
|
840 else
|
|
841 {
|
|
842 buf->printf("%s $(DDOC_PSYMBOL %s)", kind(), toChars());
|
|
843 }
|
|
844 int any = 0;
|
|
845 for (int i = 0; i < baseclasses.dim; i++)
|
|
846 { BaseClass *bc = (BaseClass *)baseclasses.data[i];
|
|
847
|
|
848 if (bc->protection == PROTprivate)
|
|
849 continue;
|
|
850 if (bc->base && bc->base->ident == Id::Object)
|
|
851 continue;
|
|
852
|
|
853 if (any)
|
|
854 buf->writestring(", ");
|
|
855 else
|
|
856 { buf->writestring(": ");
|
|
857 any = 1;
|
|
858 }
|
|
859 emitProtection(buf, bc->protection);
|
|
860 if (bc->base)
|
|
861 {
|
|
862 buf->writestring(bc->base->toPrettyChars());
|
|
863 }
|
|
864 else
|
|
865 {
|
|
866 HdrGenState hgs;
|
|
867 bc->type->toCBuffer(buf, NULL, &hgs);
|
|
868 }
|
|
869 }
|
|
870 buf->writestring(";\n");
|
|
871 }
|
|
872 }
|
|
873
|
|
874
|
|
875 void EnumDeclaration::toDocBuffer(OutBuffer *buf)
|
|
876 {
|
|
877 if (ident)
|
|
878 {
|
|
879 buf->printf("%s $(DDOC_PSYMBOL %s)", kind(), toChars());
|
|
880 buf->writestring(";\n");
|
|
881 }
|
|
882 }
|
|
883
|
|
884 void EnumMember::toDocBuffer(OutBuffer *buf)
|
|
885 {
|
|
886 if (ident)
|
|
887 {
|
|
888 buf->writestring(toChars());
|
|
889 }
|
|
890 }
|
|
891
|
|
892
|
|
893 /********************************* DocComment *********************************/
|
|
894
|
|
895 DocComment::DocComment()
|
|
896 {
|
|
897 memset(this, 0, sizeof(DocComment));
|
|
898 }
|
|
899
|
|
900 DocComment *DocComment::parse(Scope *sc, Dsymbol *s, unsigned char *comment)
|
|
901 { unsigned idlen;
|
|
902
|
|
903 if (sc->lastdc && isDitto(comment))
|
|
904 return NULL;
|
|
905
|
|
906 DocComment *dc = new DocComment();
|
|
907 if (!comment)
|
|
908 return dc;
|
|
909
|
|
910 dc->parseSections(comment);
|
|
911
|
|
912 for (int i = 0; i < dc->sections.dim; i++)
|
|
913 { Section *s = (Section *)dc->sections.data[i];
|
|
914
|
|
915 if (icmp("copyright", s->name, s->namelen) == 0)
|
|
916 {
|
|
917 dc->copyright = s;
|
|
918 }
|
|
919 if (icmp("macros", s->name, s->namelen) == 0)
|
|
920 {
|
|
921 dc->macros = s;
|
|
922 }
|
|
923 }
|
|
924
|
|
925 sc->lastdc = dc;
|
|
926 return dc;
|
|
927 }
|
|
928
|
|
929 /*****************************************
|
|
930 * Parse next paragraph out of *pcomment.
|
|
931 * Update *pcomment to point past paragraph.
|
|
932 * Returns NULL if no more paragraphs.
|
|
933 * If paragraph ends in 'identifier:',
|
|
934 * then (*pcomment)[0 .. idlen] is the identifier.
|
|
935 */
|
|
936
|
|
937 void DocComment::parseSections(unsigned char *comment)
|
|
938 { unsigned char *p;
|
|
939 unsigned char *pstart;
|
|
940 unsigned char *pend;
|
|
941 unsigned char *q;
|
|
942 unsigned char *idstart;
|
|
943 unsigned idlen;
|
|
944
|
|
945 unsigned char *name = NULL;
|
|
946 unsigned namelen = 0;
|
|
947
|
|
948 p = comment;
|
|
949 while (*p)
|
|
950 {
|
|
951 p = skipwhitespace(p);
|
|
952 pstart = p;
|
|
953
|
|
954 /* Find end of section, which is ended by one of:
|
|
955 * 'identifier:'
|
|
956 * '\0'
|
|
957 */
|
|
958 idlen = 0;
|
|
959 while (1)
|
|
960 {
|
|
961 if (isalpha(*p) || *p == '_')
|
|
962 {
|
|
963 q = p + 1;
|
|
964 while (isalnum(*q) || *q == '_')
|
|
965 q++;
|
|
966 if (*q == ':') // identifier: ends it
|
|
967 { idlen = q - p;
|
|
968 idstart = p;
|
|
969 for (pend = p; pend > pstart; pend--)
|
|
970 { if (pend[-1] == '\n')
|
|
971 break;
|
|
972 }
|
|
973 p = q + 1;
|
|
974 break;
|
|
975 }
|
|
976 }
|
|
977 while (1)
|
|
978 {
|
|
979 if (!*p)
|
|
980 { pend = p;
|
|
981 goto L1;
|
|
982 }
|
|
983 if (*p == '\n')
|
|
984 { p++;
|
|
985 if (*p == '\n' && !summary && !namelen)
|
|
986 {
|
|
987 pend = p;
|
|
988 p++;
|
|
989 goto L1;
|
|
990 }
|
|
991 break;
|
|
992 }
|
|
993 p++;
|
|
994 }
|
|
995 p = skipwhitespace(p);
|
|
996 }
|
|
997 L1:
|
|
998
|
|
999 if (namelen || pstart < pend)
|
|
1000 {
|
|
1001 Section *s;
|
|
1002 if (icmp("Params", name, namelen) == 0)
|
|
1003 s = new ParamSection();
|
|
1004 else if (icmp("Macros", name, namelen) == 0)
|
|
1005 s = new MacroSection();
|
|
1006 else
|
|
1007 s = new Section();
|
|
1008 s->name = name;
|
|
1009 s->namelen = namelen;
|
|
1010 s->body = pstart;
|
|
1011 s->bodylen = pend - pstart;
|
|
1012 s->nooutput = 0;
|
|
1013
|
|
1014 //printf("Section: '%.*s' = '%.*s'\n", s->namelen, s->name, s->bodylen, s->body);
|
|
1015
|
|
1016 sections.push(s);
|
|
1017
|
|
1018 if (!summary && !namelen)
|
|
1019 summary = s;
|
|
1020 }
|
|
1021
|
|
1022 if (idlen)
|
|
1023 { name = idstart;
|
|
1024 namelen = idlen;
|
|
1025 }
|
|
1026 else
|
|
1027 { name = NULL;
|
|
1028 namelen = 0;
|
|
1029 if (!*p)
|
|
1030 break;
|
|
1031 }
|
|
1032 }
|
|
1033 }
|
|
1034
|
|
1035 void DocComment::writeSections(Scope *sc, Dsymbol *s, OutBuffer *buf)
|
|
1036 {
|
|
1037 //printf("DocComment::writeSections()\n");
|
|
1038 if (sections.dim)
|
|
1039 {
|
|
1040 buf->writestring("$(DDOC_SECTIONS \n");
|
|
1041 for (int i = 0; i < sections.dim; i++)
|
|
1042 { Section *sec = (Section *)sections.data[i];
|
|
1043
|
|
1044 if (sec->nooutput)
|
|
1045 continue;
|
|
1046 //printf("Section: '%.*s' = '%.*s'\n", sec->namelen, sec->name, sec->bodylen, sec->body);
|
|
1047 if (sec->namelen || i)
|
|
1048 sec->write(this, sc, s, buf);
|
|
1049 else
|
|
1050 {
|
|
1051 buf->writestring("$(DDOC_SUMMARY ");
|
|
1052 unsigned o = buf->offset;
|
|
1053 buf->write(sec->body, sec->bodylen);
|
|
1054 highlightText(sc, s, buf, o);
|
|
1055 buf->writestring(")\n");
|
|
1056 }
|
|
1057 }
|
|
1058 buf->writestring(")\n");
|
|
1059 }
|
|
1060 else
|
|
1061 {
|
|
1062 buf->writestring("$(DDOC_BLANKLINE)\n");
|
|
1063 }
|
|
1064 }
|
|
1065
|
|
1066 /***************************************************
|
|
1067 */
|
|
1068
|
|
1069 void Section::write(DocComment *dc, Scope *sc, Dsymbol *s, OutBuffer *buf)
|
|
1070 {
|
|
1071 if (namelen)
|
|
1072 {
|
|
1073 static char *table[] =
|
|
1074 { "AUTHORS", "BUGS", "COPYRIGHT", "DATE",
|
|
1075 "DEPRECATED", "EXAMPLES", "HISTORY", "LICENSE",
|
|
1076 "RETURNS", "SEE_ALSO", "STANDARDS", "THROWS",
|
|
1077 "VERSION" };
|
|
1078
|
|
1079 for (int i = 0; i < sizeof(table) / sizeof(table[0]); i++)
|
|
1080 {
|
|
1081 if (icmp(table[i], name, namelen) == 0)
|
|
1082 {
|
|
1083 buf->printf("$(DDOC_%s ", table[i]);
|
|
1084 goto L1;
|
|
1085 }
|
|
1086 }
|
|
1087
|
|
1088 buf->writestring("$(DDOC_SECTION ");
|
|
1089 // Replace _ characters with spaces
|
|
1090 buf->writestring("$(DDOC_SECTION_H ");
|
|
1091 for (unsigned u = 0; u < namelen; u++)
|
|
1092 { unsigned char c = name[u];
|
|
1093 buf->writeByte((c == '_') ? ' ' : c);
|
|
1094 }
|
|
1095 buf->writestring(":)\n");
|
|
1096 }
|
|
1097 else
|
|
1098 {
|
|
1099 buf->writestring("$(DDOC_DESCRIPTION ");
|
|
1100 }
|
|
1101 L1:
|
|
1102 unsigned o = buf->offset;
|
|
1103 buf->write(body, bodylen);
|
|
1104 highlightText(sc, s, buf, o);
|
|
1105 buf->writestring(")\n");
|
|
1106 }
|
|
1107
|
|
1108 /***************************************************
|
|
1109 */
|
|
1110
|
|
1111 void ParamSection::write(DocComment *dc, Scope *sc, Dsymbol *s, OutBuffer *buf)
|
|
1112 {
|
|
1113 unsigned char *p = body;
|
|
1114 unsigned len = bodylen;
|
|
1115 unsigned char *pend = p + len;
|
|
1116
|
|
1117 unsigned char *tempstart;
|
|
1118 unsigned templen;
|
|
1119
|
|
1120 unsigned char *namestart;
|
|
1121 unsigned namelen = 0; // !=0 if line continuation
|
|
1122
|
|
1123 unsigned char *textstart;
|
|
1124 unsigned textlen;
|
|
1125
|
|
1126 unsigned o;
|
|
1127 Argument *arg;
|
|
1128
|
|
1129 buf->writestring("$(DDOC_PARAMS \n");
|
|
1130 while (p < pend)
|
|
1131 {
|
|
1132 // Skip to start of macro
|
|
1133 for (; 1; p++)
|
|
1134 {
|
|
1135 switch (*p)
|
|
1136 {
|
|
1137 case ' ':
|
|
1138 case '\t':
|
|
1139 continue;
|
|
1140
|
|
1141 case '\n':
|
|
1142 p++;
|
|
1143 goto Lcont;
|
|
1144
|
|
1145 default:
|
|
1146 if (!(isalpha(*p) || *p == '_'))
|
|
1147 {
|
|
1148 if (namelen)
|
|
1149 goto Ltext; // continuation of prev macro
|
|
1150 goto Lskipline;
|
|
1151 }
|
|
1152 break;
|
|
1153 }
|
|
1154 break;
|
|
1155 }
|
|
1156 tempstart = p;
|
|
1157
|
|
1158 while (isalnum(*p) || *p == '_')
|
|
1159 p++;
|
|
1160 templen = p - tempstart;
|
|
1161
|
|
1162 while (*p == ' ' || *p == '\t')
|
|
1163 p++;
|
|
1164
|
|
1165 if (*p != '=')
|
|
1166 { if (namelen)
|
|
1167 goto Ltext; // continuation of prev macro
|
|
1168 goto Lskipline;
|
|
1169 }
|
|
1170 p++;
|
|
1171
|
|
1172 if (namelen)
|
|
1173 { // Output existing param
|
|
1174
|
|
1175 L1:
|
|
1176 //printf("param '%.*s' = '%.*s'\n", namelen, namestart, textlen, textstart);
|
|
1177 HdrGenState hgs;
|
|
1178 buf->writestring("$(DDOC_PARAM_ROW ");
|
|
1179 buf->writestring("$(DDOC_PARAM_ID ");
|
|
1180 o = buf->offset;
|
|
1181 arg = isFunctionParameter(s, namestart, namelen);
|
|
1182 if (arg && arg->type && arg->ident)
|
|
1183 arg->type->toCBuffer(buf, arg->ident, &hgs);
|
|
1184 else
|
|
1185 buf->write(namestart, namelen);
|
|
1186 highlightCode(sc, s, buf, o);
|
|
1187 buf->writestring(")\n");
|
|
1188
|
|
1189 buf->writestring("$(DDOC_PARAM_DESC ");
|
|
1190 o = buf->offset;
|
|
1191 buf->write(textstart, textlen);
|
|
1192 highlightText(sc, s, buf, o);
|
|
1193 buf->writestring(")");
|
|
1194 buf->writestring(")\n");
|
|
1195 namelen = 0;
|
|
1196 if (p >= pend)
|
|
1197 break;
|
|
1198 }
|
|
1199
|
|
1200 namestart = tempstart;
|
|
1201 namelen = templen;
|
|
1202
|
|
1203 while (*p == ' ' || *p == '\t')
|
|
1204 p++;
|
|
1205 textstart = p;
|
|
1206
|
|
1207 Ltext:
|
|
1208 while (*p != '\n')
|
|
1209 p++;
|
|
1210 textlen = p - textstart;
|
|
1211 p++;
|
|
1212
|
|
1213 Lcont:
|
|
1214 continue;
|
|
1215
|
|
1216 Lskipline:
|
|
1217 // Ignore this line
|
|
1218 while (*p++ != '\n')
|
|
1219 ;
|
|
1220 }
|
|
1221 if (namelen)
|
|
1222 goto L1; // write out last one
|
|
1223 buf->writestring(")\n");
|
|
1224 }
|
|
1225
|
|
1226 /***************************************************
|
|
1227 */
|
|
1228
|
|
1229 void MacroSection::write(DocComment *dc, Scope *sc, Dsymbol *s, OutBuffer *buf)
|
|
1230 {
|
|
1231 //printf("MacroSection::write()\n");
|
|
1232 DocComment::parseMacros(dc->pescapetable, dc->pmacrotable, body, bodylen);
|
|
1233 }
|
|
1234
|
|
1235 /************************************************
|
|
1236 * Parse macros out of Macros: section.
|
|
1237 * Macros are of the form:
|
|
1238 * name1 = value1
|
|
1239 *
|
|
1240 * name2 = value2
|
|
1241 */
|
|
1242
|
|
1243 void DocComment::parseMacros(Escape **pescapetable, Macro **pmacrotable, unsigned char *m, unsigned mlen)
|
|
1244 {
|
|
1245 unsigned char *p = m;
|
|
1246 unsigned len = mlen;
|
|
1247 unsigned char *pend = p + len;
|
|
1248
|
|
1249 unsigned char *tempstart;
|
|
1250 unsigned templen;
|
|
1251
|
|
1252 unsigned char *namestart;
|
|
1253 unsigned namelen = 0; // !=0 if line continuation
|
|
1254
|
|
1255 unsigned char *textstart;
|
|
1256 unsigned textlen;
|
|
1257
|
|
1258 while (p < pend)
|
|
1259 {
|
|
1260 // Skip to start of macro
|
|
1261 for (; 1; p++)
|
|
1262 {
|
|
1263 if (p >= pend)
|
|
1264 goto Ldone;
|
|
1265 switch (*p)
|
|
1266 {
|
|
1267 case ' ':
|
|
1268 case '\t':
|
|
1269 continue;
|
|
1270
|
|
1271 case '\n':
|
|
1272 p++;
|
|
1273 goto Lcont;
|
|
1274
|
|
1275 default:
|
|
1276 if (!(isalpha(*p) || *p == '_'))
|
|
1277 {
|
|
1278 if (namelen)
|
|
1279 goto Ltext; // continuation of prev macro
|
|
1280 goto Lskipline;
|
|
1281 }
|
|
1282 break;
|
|
1283 }
|
|
1284 break;
|
|
1285 }
|
|
1286 tempstart = p;
|
|
1287
|
|
1288 while (1)
|
|
1289 {
|
|
1290 if (p >= pend)
|
|
1291 goto Ldone;
|
|
1292 if (!(isalnum(*p) || *p == '_'))
|
|
1293 break;
|
|
1294 p++;
|
|
1295 }
|
|
1296 templen = p - tempstart;
|
|
1297
|
|
1298 while (1)
|
|
1299 {
|
|
1300 if (p >= pend)
|
|
1301 goto Ldone;
|
|
1302 if (!(*p == ' ' || *p == '\t'))
|
|
1303 break;
|
|
1304 p++;
|
|
1305 }
|
|
1306
|
|
1307 if (*p != '=')
|
|
1308 { if (namelen)
|
|
1309 goto Ltext; // continuation of prev macro
|
|
1310 goto Lskipline;
|
|
1311 }
|
|
1312 p++;
|
|
1313 if (p >= pend)
|
|
1314 goto Ldone;
|
|
1315
|
|
1316 if (namelen)
|
|
1317 { // Output existing macro
|
|
1318 L1:
|
|
1319 //printf("macro '%.*s' = '%.*s'\n", namelen, namestart, textlen, textstart);
|
|
1320 if (icmp("ESCAPES", namestart, namelen) == 0)
|
|
1321 parseEscapes(pescapetable, textstart, textlen);
|
|
1322 else
|
|
1323 Macro::define(pmacrotable, namestart, namelen, textstart, textlen);
|
|
1324 namelen = 0;
|
|
1325 if (p >= pend)
|
|
1326 break;
|
|
1327 }
|
|
1328
|
|
1329 namestart = tempstart;
|
|
1330 namelen = templen;
|
|
1331
|
|
1332 while (p < pend && (*p == ' ' || *p == '\t'))
|
|
1333 p++;
|
|
1334 textstart = p;
|
|
1335
|
|
1336 Ltext:
|
|
1337 while (p < pend && *p != '\n')
|
|
1338 p++;
|
|
1339 textlen = p - textstart;
|
|
1340
|
|
1341 // Remove trailing \r if there is one
|
|
1342 if (p > m && p[-1] == '\r')
|
|
1343 textlen--;
|
|
1344
|
|
1345 p++;
|
|
1346 //printf("p = %p, pend = %p\n", p, pend);
|
|
1347
|
|
1348 Lcont:
|
|
1349 continue;
|
|
1350
|
|
1351 Lskipline:
|
|
1352 // Ignore this line
|
|
1353 while (p < pend && *p++ != '\n')
|
|
1354 ;
|
|
1355 }
|
|
1356 Ldone:
|
|
1357 if (namelen)
|
|
1358 goto L1; // write out last one
|
|
1359 }
|
|
1360
|
|
1361 /**************************************
|
|
1362 * Parse escapes of the form:
|
|
1363 * /c/string/
|
|
1364 * where c is a single character.
|
|
1365 * Multiple escapes can be separated
|
|
1366 * by whitespace and/or commas.
|
|
1367 */
|
|
1368
|
|
1369 void DocComment::parseEscapes(Escape **pescapetable, unsigned char *textstart, unsigned textlen)
|
|
1370 { Escape *escapetable = *pescapetable;
|
|
1371
|
|
1372 if (!escapetable)
|
|
1373 { escapetable = new Escape;
|
|
1374 *pescapetable = escapetable;
|
|
1375 }
|
|
1376 unsigned char *p = textstart;
|
|
1377 unsigned char *pend = p + textlen;
|
|
1378
|
|
1379 while (1)
|
|
1380 {
|
|
1381 while (1)
|
|
1382 {
|
|
1383 if (p + 4 >= pend)
|
|
1384 return;
|
|
1385 if (!(*p == ' ' || *p == '\t' || *p == '\n' || *p == ','))
|
|
1386 break;
|
|
1387 p++;
|
|
1388 }
|
|
1389 if (p[0] != '/' || p[2] != '/')
|
|
1390 return;
|
|
1391 unsigned char c = p[1];
|
|
1392 p += 3;
|
|
1393 unsigned char *start = p;
|
|
1394 while (1)
|
|
1395 {
|
|
1396 if (p >= pend)
|
|
1397 return;
|
|
1398 if (*p == '/')
|
|
1399 break;
|
|
1400 p++;
|
|
1401 }
|
|
1402 size_t len = p - start;
|
|
1403 char *s = (char *)memcpy(mem.malloc(len + 1), start, len);
|
|
1404 s[len] = 0;
|
|
1405 escapetable->strings[c] = s;
|
|
1406 //printf("%c = '%s'\n", c, s);
|
|
1407 p++;
|
|
1408 }
|
|
1409 }
|
|
1410
|
|
1411
|
|
1412 /******************************************
|
|
1413 * Compare 0-terminated string with length terminated string.
|
|
1414 * Return < 0, ==0, > 0
|
|
1415 */
|
|
1416
|
|
1417 int cmp(char *stringz, void *s, size_t slen)
|
|
1418 {
|
|
1419 size_t len1 = strlen(stringz);
|
|
1420
|
|
1421 if (len1 != slen)
|
|
1422 return len1 - slen;
|
|
1423 return memcmp(stringz, s, slen);
|
|
1424 }
|
|
1425
|
|
1426 int icmp(char *stringz, void *s, size_t slen)
|
|
1427 {
|
|
1428 size_t len1 = strlen(stringz);
|
|
1429
|
|
1430 if (len1 != slen)
|
|
1431 return len1 - slen;
|
|
1432 return memicmp(stringz, (char *)s, slen);
|
|
1433 }
|
|
1434
|
|
1435 /*****************************************
|
|
1436 * Return !=0 if comment consists entirely of "ditto".
|
|
1437 */
|
|
1438
|
|
1439 int isDitto(unsigned char *comment)
|
|
1440 {
|
|
1441 if (comment)
|
|
1442 {
|
|
1443 unsigned char *p = skipwhitespace(comment);
|
|
1444
|
|
1445 if (memicmp((char *)p, "ditto", 5) == 0 && *skipwhitespace(p + 5) == 0)
|
|
1446 return 1;
|
|
1447 }
|
|
1448 return 0;
|
|
1449 }
|
|
1450
|
|
1451 /**********************************************
|
|
1452 * Skip white space.
|
|
1453 */
|
|
1454
|
|
1455 unsigned char *skipwhitespace(unsigned char *p)
|
|
1456 {
|
|
1457 for (; 1; p++)
|
|
1458 { switch (*p)
|
|
1459 {
|
|
1460 case ' ':
|
|
1461 case '\t':
|
|
1462 case '\n':
|
|
1463 continue;
|
|
1464 }
|
|
1465 break;
|
|
1466 }
|
|
1467 return p;
|
|
1468 }
|
|
1469
|
|
1470
|
|
1471 /************************************************
|
|
1472 * Scan forward to one of:
|
|
1473 * start of identifier
|
|
1474 * beginning of next line
|
|
1475 * end of buf
|
|
1476 */
|
|
1477
|
|
1478 unsigned skiptoident(OutBuffer *buf, unsigned i)
|
|
1479 {
|
|
1480 for (; i < buf->offset; i++)
|
|
1481 {
|
|
1482 // BUG: handle unicode alpha's
|
|
1483 unsigned char c = buf->data[i];
|
|
1484 if (isalpha(c) || c == '_')
|
|
1485 break;
|
|
1486 if (c == '\n')
|
|
1487 break;
|
|
1488 }
|
|
1489 return i;
|
|
1490 }
|
|
1491
|
|
1492 /************************************************
|
|
1493 * Scan forward past end of identifier.
|
|
1494 */
|
|
1495
|
|
1496 unsigned skippastident(OutBuffer *buf, unsigned i)
|
|
1497 {
|
|
1498 for (; i < buf->offset; i++)
|
|
1499 {
|
|
1500 // BUG: handle unicode alpha's
|
|
1501 unsigned char c = buf->data[i];
|
|
1502 if (!(isalnum(c) || c == '_'))
|
|
1503 break;
|
|
1504 }
|
|
1505 return i;
|
|
1506 }
|
|
1507
|
|
1508
|
|
1509 /************************************************
|
|
1510 * Scan forward past URL starting at i.
|
|
1511 * We don't want to highlight parts of a URL.
|
|
1512 * Returns:
|
|
1513 * i if not a URL
|
|
1514 * index just past it if it is a URL
|
|
1515 */
|
|
1516
|
|
1517 unsigned skippastURL(OutBuffer *buf, unsigned i)
|
|
1518 { unsigned length = buf->offset - i;
|
|
1519 unsigned char *p = &buf->data[i];
|
|
1520 unsigned j;
|
|
1521 unsigned sawdot = 0;
|
|
1522
|
|
1523 if (length > 7 && memicmp((char *)p, "http://", 7) == 0)
|
|
1524 {
|
|
1525 j = 7;
|
|
1526 }
|
|
1527 else if (length > 8 && memicmp((char *)p, "https://", 8) == 0)
|
|
1528 {
|
|
1529 j = 8;
|
|
1530 }
|
|
1531 else
|
|
1532 goto Lno;
|
|
1533
|
|
1534 for (; j < length; j++)
|
|
1535 { unsigned char c = p[j];
|
|
1536 if (isalnum(c))
|
|
1537 continue;
|
|
1538 if (c == '-' || c == '_' || c == '?' ||
|
|
1539 c == '=' || c == '%' || c == '&' ||
|
|
1540 c == '/' || c == '+' || c == '#' ||
|
|
1541 c == '~')
|
|
1542 continue;
|
|
1543 if (c == '.')
|
|
1544 {
|
|
1545 sawdot = 1;
|
|
1546 continue;
|
|
1547 }
|
|
1548 break;
|
|
1549 }
|
|
1550 if (sawdot)
|
|
1551 return i + j;
|
|
1552
|
|
1553 Lno:
|
|
1554 return i;
|
|
1555 }
|
|
1556
|
|
1557
|
|
1558 /****************************************************
|
|
1559 */
|
|
1560
|
|
1561 int isKeyword(unsigned char *p, unsigned len)
|
|
1562 {
|
|
1563 static char *table[] = { "true", "false", "null" };
|
|
1564
|
|
1565 for (int i = 0; i < sizeof(table) / sizeof(table[0]); i++)
|
|
1566 {
|
|
1567 if (cmp(table[i], p, len) == 0)
|
|
1568 return 1;
|
|
1569 }
|
|
1570 return 0;
|
|
1571 }
|
|
1572
|
|
1573 /****************************************************
|
|
1574 */
|
|
1575
|
|
1576 Argument *isFunctionParameter(Dsymbol *s, unsigned char *p, unsigned len)
|
|
1577 {
|
|
1578 FuncDeclaration *f = s->isFuncDeclaration();
|
|
1579
|
|
1580 /* f->type may be NULL for template members.
|
|
1581 */
|
|
1582 if (f && f->type)
|
|
1583 {
|
|
1584 TypeFunction *tf = (TypeFunction *)f->type;
|
|
1585
|
|
1586 if (tf->parameters)
|
|
1587 {
|
|
1588 for (size_t k = 0; k < tf->parameters->dim; k++)
|
|
1589 { Argument *arg = (Argument *)tf->parameters->data[k];
|
|
1590
|
|
1591 if (arg->ident && cmp(arg->ident->toChars(), p, len) == 0)
|
|
1592 {
|
|
1593 return arg;
|
|
1594 }
|
|
1595 }
|
|
1596 }
|
|
1597 }
|
|
1598 return NULL;
|
|
1599 }
|
|
1600
|
|
1601 /**************************************************
|
|
1602 * Highlight text section.
|
|
1603 */
|
|
1604
|
|
1605 void highlightText(Scope *sc, Dsymbol *s, OutBuffer *buf, unsigned offset)
|
|
1606 {
|
|
1607 //printf("highlightText()\n");
|
|
1608 char *sid = s->ident->toChars();
|
|
1609 FuncDeclaration *f = s->isFuncDeclaration();
|
|
1610 unsigned char *p;
|
|
1611 char *se;
|
|
1612
|
|
1613 int leadingBlank = 1;
|
|
1614 int inCode = 0;
|
|
1615 int inComment = 0; // in <!-- ... --> comment
|
|
1616 unsigned iCodeStart; // start of code section
|
|
1617
|
|
1618 unsigned iLineStart = offset;
|
|
1619
|
|
1620 for (unsigned i = offset; i < buf->offset; i++)
|
|
1621 { unsigned char c = buf->data[i];
|
|
1622
|
|
1623 Lcont:
|
|
1624 switch (c)
|
|
1625 {
|
|
1626 case ' ':
|
|
1627 case '\t':
|
|
1628 break;
|
|
1629
|
|
1630 case '\n':
|
|
1631 if (sc && !inCode && i == iLineStart && i + 1 < buf->offset) // if "\n\n"
|
|
1632 {
|
|
1633 static char blankline[] = "$(DDOC_BLANKLINE)\n";
|
|
1634
|
|
1635 i = buf->insert(i, blankline, sizeof(blankline) - 1);
|
|
1636 }
|
|
1637 leadingBlank = 1;
|
|
1638 iLineStart = i + 1;
|
|
1639 break;
|
|
1640
|
|
1641 case '<':
|
|
1642 leadingBlank = 0;
|
|
1643 if (inCode)
|
|
1644 break;
|
|
1645 p = &buf->data[i];
|
|
1646
|
|
1647 // Skip over comments
|
|
1648 if (p[1] == '!' && p[2] == '-' && p[3] == '-')
|
|
1649 { unsigned j = i + 4;
|
|
1650 p += 4;
|
|
1651 while (1)
|
|
1652 {
|
|
1653 if (j == buf->offset)
|
|
1654 goto L1;
|
|
1655 if (p[0] == '-' && p[1] == '-' && p[2] == '>')
|
|
1656 {
|
|
1657 i = j + 2; // place on closing '>'
|
|
1658 break;
|
|
1659 }
|
|
1660 j++;
|
|
1661 p++;
|
|
1662 }
|
|
1663 break;
|
|
1664 }
|
|
1665
|
|
1666 // Skip over HTML tag
|
|
1667 if (isalpha(p[1]) || (p[1] == '/' && isalpha(p[2])))
|
|
1668 { unsigned j = i + 2;
|
|
1669 p += 2;
|
|
1670 while (1)
|
|
1671 {
|
|
1672 if (j == buf->offset)
|
|
1673 goto L1;
|
|
1674 if (p[0] == '>')
|
|
1675 {
|
|
1676 i = j; // place on closing '>'
|
|
1677 break;
|
|
1678 }
|
|
1679 j++;
|
|
1680 p++;
|
|
1681 }
|
|
1682 break;
|
|
1683 }
|
|
1684
|
|
1685 L1:
|
|
1686 // Replace '<' with '<' character entity
|
|
1687 se = Escape::escapeChar('<');
|
|
1688 if (se)
|
|
1689 { size_t len = strlen(se);
|
|
1690 buf->remove(i, 1);
|
|
1691 i = buf->insert(i, se, len);
|
|
1692 i--; // point to ';'
|
|
1693 }
|
|
1694 break;
|
|
1695
|
|
1696 case '>':
|
|
1697 leadingBlank = 0;
|
|
1698 if (inCode)
|
|
1699 break;
|
|
1700 // Replace '>' with '>' character entity
|
|
1701 se = Escape::escapeChar('>');
|
|
1702 if (se)
|
|
1703 { size_t len = strlen(se);
|
|
1704 buf->remove(i, 1);
|
|
1705 i = buf->insert(i, se, len);
|
|
1706 i--; // point to ';'
|
|
1707 }
|
|
1708 break;
|
|
1709
|
|
1710 case '&':
|
|
1711 leadingBlank = 0;
|
|
1712 if (inCode)
|
|
1713 break;
|
|
1714 p = &buf->data[i];
|
|
1715 if (p[1] == '#' || isalpha(p[1]))
|
|
1716 break; // already a character entity
|
|
1717 // Replace '&' with '&' character entity
|
|
1718 se = Escape::escapeChar('&');
|
|
1719 if (se)
|
|
1720 { size_t len = strlen(se);
|
|
1721 buf->remove(i, 1);
|
|
1722 i = buf->insert(i, se, len);
|
|
1723 i--; // point to ';'
|
|
1724 }
|
|
1725 break;
|
|
1726
|
|
1727 case '-':
|
|
1728 /* A line beginning with --- delimits a code section.
|
|
1729 * inCode tells us if it is start or end of a code section.
|
|
1730 */
|
|
1731 if (leadingBlank)
|
|
1732 { int istart = i;
|
|
1733 int eollen = 0;
|
|
1734
|
|
1735 leadingBlank = 0;
|
|
1736 while (1)
|
|
1737 {
|
|
1738 ++i;
|
|
1739 if (i >= buf->offset)
|
|
1740 break;
|
|
1741 c = buf->data[i];
|
|
1742 if (c == '\n')
|
|
1743 { eollen = 1;
|
|
1744 break;
|
|
1745 }
|
|
1746 if (c == '\r')
|
|
1747 {
|
|
1748 eollen = 1;
|
|
1749 if (i + 1 >= buf->offset)
|
|
1750 break;
|
|
1751 if (buf->data[i + 1] == '\n')
|
|
1752 { eollen = 2;
|
|
1753 break;
|
|
1754 }
|
|
1755 }
|
|
1756 // BUG: handle UTF PS and LS too
|
|
1757 if (c != '-')
|
|
1758 goto Lcont;
|
|
1759 }
|
|
1760 if (i - istart < 3)
|
|
1761 goto Lcont;
|
|
1762
|
|
1763 // We have the start/end of a code section
|
|
1764
|
|
1765 // Remove the entire --- line, including blanks and \n
|
|
1766 buf->remove(iLineStart, i - iLineStart + eollen);
|
|
1767 i = iLineStart;
|
|
1768
|
|
1769 if (inCode)
|
|
1770 {
|
|
1771 inCode = 0;
|
|
1772 // The code section is from iCodeStart to i
|
|
1773 OutBuffer codebuf;
|
|
1774
|
|
1775 codebuf.write(buf->data + iCodeStart, i - iCodeStart);
|
|
1776 codebuf.writeByte(0);
|
|
1777 highlightCode2(sc, s, &codebuf, 0);
|
|
1778 buf->remove(iCodeStart, i - iCodeStart);
|
|
1779 i = buf->insert(iCodeStart, codebuf.data, codebuf.offset);
|
|
1780 i = buf->insert(i, ")\n", 2);
|
|
1781 i--;
|
|
1782 }
|
|
1783 else
|
|
1784 { static char pre[] = "$(D_CODE \n";
|
|
1785
|
|
1786 inCode = 1;
|
|
1787 i = buf->insert(i, pre, sizeof(pre) - 1);
|
|
1788 iCodeStart = i;
|
|
1789 i--; // place i on >
|
|
1790 }
|
|
1791 }
|
|
1792 break;
|
|
1793
|
|
1794 default:
|
|
1795 leadingBlank = 0;
|
|
1796 if (sc && !inCode && (isalpha(c) || c == '_'))
|
|
1797 { unsigned j;
|
|
1798
|
|
1799 j = skippastident(buf, i);
|
|
1800 if (j > i)
|
|
1801 {
|
|
1802 unsigned k = skippastURL(buf, i);
|
|
1803 if (k > i)
|
|
1804 { i = k - 1;
|
|
1805 break;
|
|
1806 }
|
|
1807
|
|
1808 if (buf->data[i] == '_') // leading '_' means no highlight
|
|
1809 {
|
|
1810 buf->remove(i, 1);
|
|
1811 i = j - 1;
|
|
1812 }
|
|
1813 else
|
|
1814 {
|
|
1815 if (cmp(sid, buf->data + i, j - i) == 0)
|
|
1816 {
|
|
1817 i = buf->bracket(i, "$(DDOC_PSYMBOL ", j, ")") - 1;
|
|
1818 break;
|
|
1819 }
|
|
1820 else if (isKeyword(buf->data + i, j - i))
|
|
1821 {
|
|
1822 i = buf->bracket(i, "$(DDOC_KEYWORD ", j, ")") - 1;
|
|
1823 break;
|
|
1824 }
|
|
1825 else
|
|
1826 {
|
|
1827 if (f && isFunctionParameter(f, buf->data + i, j - i))
|
|
1828 {
|
|
1829 //printf("highlighting arg '%s', i = %d, j = %d\n", arg->ident->toChars(), i, j);
|
|
1830 i = buf->bracket(i, "$(DDOC_PARAM ", j, ")") - 1;
|
|
1831 break;
|
|
1832 }
|
|
1833 }
|
|
1834 i = j - 1;
|
|
1835 }
|
|
1836 }
|
|
1837 }
|
|
1838 break;
|
|
1839 }
|
|
1840 }
|
|
1841 Ldone:
|
|
1842 ;
|
|
1843 }
|
|
1844
|
|
1845 /**************************************************
|
|
1846 * Highlight code for DDOC section.
|
|
1847 */
|
|
1848
|
|
1849 void highlightCode(Scope *sc, Dsymbol *s, OutBuffer *buf, unsigned offset)
|
|
1850 {
|
|
1851 char *sid = s->ident->toChars();
|
|
1852 FuncDeclaration *f = s->isFuncDeclaration();
|
|
1853
|
|
1854 //printf("highlightCode(s = '%s', kind = %s)\n", sid, s->kind());
|
|
1855 for (unsigned i = offset; i < buf->offset; i++)
|
|
1856 { unsigned char c = buf->data[i];
|
|
1857 char *se;
|
|
1858
|
|
1859 se = Escape::escapeChar(c);
|
|
1860 if (se)
|
|
1861 {
|
|
1862 size_t len = strlen(se);
|
|
1863 buf->remove(i, 1);
|
|
1864 i = buf->insert(i, se, len);
|
|
1865 i--; // point to ';'
|
|
1866 }
|
|
1867 else if (isalpha(c) || c == '_')
|
|
1868 { unsigned j;
|
|
1869
|
|
1870 j = skippastident(buf, i);
|
|
1871 if (j > i)
|
|
1872 {
|
|
1873 if (cmp(sid, buf->data + i, j - i) == 0)
|
|
1874 {
|
|
1875 i = buf->bracket(i, "$(DDOC_PSYMBOL ", j, ")") - 1;
|
|
1876 continue;
|
|
1877 }
|
|
1878 else if (f)
|
|
1879 {
|
|
1880 if (isFunctionParameter(f, buf->data + i, j - i))
|
|
1881 {
|
|
1882 //printf("highlighting arg '%s', i = %d, j = %d\n", arg->ident->toChars(), i, j);
|
|
1883 i = buf->bracket(i, "$(DDOC_PARAM ", j, ")") - 1;
|
|
1884 continue;
|
|
1885 }
|
|
1886 }
|
|
1887 i = j - 1;
|
|
1888 }
|
|
1889 }
|
|
1890 }
|
|
1891 }
|
|
1892
|
|
1893 /****************************************
|
|
1894 */
|
|
1895
|
|
1896 void highlightCode3(OutBuffer *buf, unsigned char *p, unsigned char *pend)
|
|
1897 {
|
|
1898 for (; p < pend; p++)
|
|
1899 { char *s = Escape::escapeChar(*p);
|
|
1900 if (s)
|
|
1901 buf->writestring(s);
|
|
1902 else
|
|
1903 buf->writeByte(*p);
|
|
1904 }
|
|
1905 }
|
|
1906
|
|
1907 /**************************************************
|
|
1908 * Highlight code for CODE section.
|
|
1909 */
|
|
1910
|
|
1911
|
|
1912 void highlightCode2(Scope *sc, Dsymbol *s, OutBuffer *buf, unsigned offset)
|
|
1913 {
|
|
1914 char *sid = s->ident->toChars();
|
|
1915 FuncDeclaration *f = s->isFuncDeclaration();
|
|
1916 unsigned errorsave = global.errors;
|
|
1917 Lexer lex(NULL, buf->data, 0, buf->offset - 1, 0, 1);
|
|
1918 Token tok;
|
|
1919 OutBuffer res;
|
|
1920 unsigned char *lastp = buf->data;
|
|
1921 char *highlight;
|
|
1922
|
|
1923 //printf("highlightCode2('%.*s')\n", buf->offset - 1, buf->data);
|
|
1924 res.reserve(buf->offset);
|
|
1925 while (1)
|
|
1926 {
|
|
1927 lex.scan(&tok);
|
|
1928 highlightCode3(&res, lastp, tok.ptr);
|
|
1929 highlight = NULL;
|
|
1930 switch (tok.value)
|
|
1931 {
|
|
1932 case TOKidentifier:
|
|
1933 if (!sc)
|
|
1934 break;
|
|
1935 if (cmp(sid, tok.ptr, lex.p - tok.ptr) == 0)
|
|
1936 {
|
|
1937 highlight = "$(D_PSYMBOL ";
|
|
1938 break;
|
|
1939 }
|
|
1940 else if (f)
|
|
1941 {
|
|
1942 if (isFunctionParameter(f, tok.ptr, lex.p - tok.ptr))
|
|
1943 {
|
|
1944 //printf("highlighting arg '%s', i = %d, j = %d\n", arg->ident->toChars(), i, j);
|
|
1945 highlight = "$(D_PARAM ";
|
|
1946 break;
|
|
1947 }
|
|
1948 }
|
|
1949 break;
|
|
1950
|
|
1951 case TOKcomment:
|
|
1952 highlight = "$(D_COMMENT ";
|
|
1953 break;
|
|
1954
|
|
1955 case TOKstring:
|
|
1956 highlight = "$(D_STRING ";
|
|
1957 break;
|
|
1958
|
|
1959 default:
|
|
1960 if (tok.isKeyword())
|
|
1961 highlight = "$(D_KEYWORD ";
|
|
1962 break;
|
|
1963 }
|
|
1964 if (highlight)
|
|
1965 res.writestring(highlight);
|
|
1966 highlightCode3(&res, tok.ptr, lex.p);
|
|
1967 if (highlight)
|
|
1968 res.writeByte(')');
|
|
1969 if (tok.value == TOKeof)
|
|
1970 break;
|
|
1971 lastp = lex.p;
|
|
1972 }
|
|
1973 buf->setsize(offset);
|
|
1974 buf->write(&res);
|
|
1975 global.errors = errorsave;
|
|
1976 }
|
|
1977
|
|
1978 /***************************************
|
|
1979 * Find character string to replace c with.
|
|
1980 */
|
|
1981
|
|
1982 char *Escape::escapeChar(unsigned c)
|
|
1983 { char *s;
|
|
1984
|
|
1985 switch (c)
|
|
1986 {
|
|
1987 case '<':
|
|
1988 s = "<";
|
|
1989 break;
|
|
1990 case '>':
|
|
1991 s = ">";
|
|
1992 break;
|
|
1993 case '&':
|
|
1994 s = "&";
|
|
1995 break;
|
|
1996 default:
|
|
1997 s = NULL;
|
|
1998 break;
|
|
1999 }
|
|
2000 return s;
|
|
2001 }
|
|
2002
|