comparison dmd2/doc.c @ 758:f04dde6e882c

Added initial D2 support, D2 frontend and changes to codegen to make things compile.
author Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
date Tue, 11 Nov 2008 01:38:48 +0100
parents
children 638d16625da2
comparison
equal deleted inserted replaced
757:2c730d530c98 758:f04dde6e882c
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 POSIX
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 const char *strings[256];
51
52 static const 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(const char *stringz, void *s, size_t slen);
100 int icmp(const 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/2.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 = /</&lt;/\n\
194 />/&gt;/\n\
195 /&/&amp;/\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, &macrotable, 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 = &macrotable;
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(&macrotable, (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(&macrotable, (unsigned char *)"DATETIME", 8, (unsigned char *)p, strlen(p));
261 Macro::define(&macrotable, (unsigned char *)"YEAR", 4, (unsigned char *)p + 20, 4);
262
263 char *docfilename = docfile->toChars();
264 Macro::define(&macrotable, (unsigned char *)"DOCFILENAME", 11, (unsigned char *)docfilename, strlen(docfilename));
265
266 if (dc->copyright)
267 {
268 dc->copyright->nooutput = 1;
269 Macro::define(&macrotable, (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(&macrotable, (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() %s\n", toChars());
404 OutBuffer *buf = sc->docbuf;
405
406 if (members)
407 { const 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 unsigned offset1 = buf->offset; // save starting offset
421 buf->writestring(m);
422 unsigned offset2 = buf->offset; // to see if we write anything
423 sc = sc->push(this);
424 for (int i = 0; i < members->dim; i++)
425 {
426 Dsymbol *s = (Dsymbol *)members->data[i];
427 //printf("\ts = '%s'\n", s->toChars());
428 s->emitComment(sc);
429 }
430 sc->pop();
431 if (buf->offset == offset2)
432 {
433 /* Didn't write out any members, so back out last write
434 */
435 buf->offset = offset1;
436 }
437 else
438 buf->writestring(")\n");
439 }
440 }
441
442 void emitProtection(OutBuffer *buf, PROT prot)
443 {
444 const char *p;
445
446 switch (prot)
447 {
448 case PROTpackage: p = "package"; break;
449 case PROTprotected: p = "protected"; break;
450 case PROTexport: p = "export"; break;
451 default: p = NULL; break;
452 }
453 if (p)
454 buf->printf("%s ", p);
455 }
456
457 void Dsymbol::emitComment(Scope *sc) { }
458 void InvariantDeclaration::emitComment(Scope *sc) { }
459 #if DMDV2
460 void PostBlitDeclaration::emitComment(Scope *sc) { }
461 #endif
462 void DtorDeclaration::emitComment(Scope *sc) { }
463 void StaticCtorDeclaration::emitComment(Scope *sc) { }
464 void StaticDtorDeclaration::emitComment(Scope *sc) { }
465 void ClassInfoDeclaration::emitComment(Scope *sc) { }
466 void ModuleInfoDeclaration::emitComment(Scope *sc) { }
467 void TypeInfoDeclaration::emitComment(Scope *sc) { }
468
469
470 void Declaration::emitComment(Scope *sc)
471 {
472 //printf("Declaration::emitComment(%p '%s'), comment = '%s'\n", this, toChars(), comment);
473 //printf("type = %p\n", type);
474
475 if (protection == PROTprivate || !ident ||
476 (!type && !isCtorDeclaration() && !isAliasDeclaration()))
477 return;
478 if (!comment)
479 return;
480
481 OutBuffer *buf = sc->docbuf;
482 DocComment *dc = DocComment::parse(sc, this, comment);
483 unsigned o;
484
485 if (!dc)
486 {
487 emitDitto(sc);
488 return;
489 }
490 dc->pmacrotable = &sc->module->macrotable;
491
492 buf->writestring(ddoc_decl_s);
493 o = buf->offset;
494 toDocBuffer(buf);
495 highlightCode(sc, this, buf, o);
496 sc->lastoffset = buf->offset;
497 buf->writestring(ddoc_decl_e);
498
499 buf->writestring(ddoc_decl_dd_s);
500 dc->writeSections(sc, this, buf);
501 buf->writestring(ddoc_decl_dd_e);
502 }
503
504 void AggregateDeclaration::emitComment(Scope *sc)
505 {
506 //printf("AggregateDeclaration::emitComment() '%s'\n", toChars());
507 if (prot() == PROTprivate)
508 return;
509 if (!comment)
510 return;
511
512 OutBuffer *buf = sc->docbuf;
513 DocComment *dc = DocComment::parse(sc, this, comment);
514
515 if (!dc)
516 {
517 emitDitto(sc);
518 return;
519 }
520 dc->pmacrotable = &sc->module->macrotable;
521
522 buf->writestring(ddoc_decl_s);
523 toDocBuffer(buf);
524 sc->lastoffset = buf->offset;
525 buf->writestring(ddoc_decl_e);
526
527 buf->writestring(ddoc_decl_dd_s);
528 dc->writeSections(sc, this, buf);
529 emitMemberComments(sc);
530 buf->writestring(ddoc_decl_dd_e);
531 }
532
533 void TemplateDeclaration::emitComment(Scope *sc)
534 {
535 //printf("TemplateDeclaration::emitComment() '%s', kind = %s\n", toChars(), kind());
536 if (prot() == PROTprivate)
537 return;
538
539 unsigned char *com = comment;
540 int hasmembers = 1;
541
542 Dsymbol *ss = this;
543
544 if (onemember)
545 {
546 ss = onemember->isAggregateDeclaration();
547 if (!ss)
548 {
549 ss = onemember->isFuncDeclaration();
550 if (ss)
551 { hasmembers = 0;
552 if (com != ss->comment)
553 com = Lexer::combineComments(com, ss->comment);
554 }
555 else
556 ss = this;
557 }
558 }
559
560 if (!com)
561 return;
562
563 OutBuffer *buf = sc->docbuf;
564 DocComment *dc = DocComment::parse(sc, this, com);
565 unsigned o;
566
567 if (!dc)
568 {
569 ss->emitDitto(sc);
570 return;
571 }
572 dc->pmacrotable = &sc->module->macrotable;
573
574 buf->writestring(ddoc_decl_s);
575 o = buf->offset;
576 ss->toDocBuffer(buf);
577 if (ss == this)
578 highlightCode(sc, this, buf, o);
579 sc->lastoffset = buf->offset;
580 buf->writestring(ddoc_decl_e);
581
582 buf->writestring(ddoc_decl_dd_s);
583 dc->writeSections(sc, this, buf);
584 if (hasmembers)
585 ((ScopeDsymbol *)ss)->emitMemberComments(sc);
586 buf->writestring(ddoc_decl_dd_e);
587 }
588
589 void EnumDeclaration::emitComment(Scope *sc)
590 {
591 if (prot() == PROTprivate)
592 return;
593 // if (!comment)
594 { if (isAnonymous() && members)
595 {
596 for (int i = 0; i < members->dim; i++)
597 {
598 Dsymbol *s = (Dsymbol *)members->data[i];
599 s->emitComment(sc);
600 }
601 return;
602 }
603 }
604 if (!comment)
605 return;
606 if (isAnonymous())
607 return;
608
609 OutBuffer *buf = sc->docbuf;
610 DocComment *dc = DocComment::parse(sc, this, comment);
611
612 if (!dc)
613 {
614 emitDitto(sc);
615 return;
616 }
617 dc->pmacrotable = &sc->module->macrotable;
618
619 buf->writestring(ddoc_decl_s);
620 toDocBuffer(buf);
621 sc->lastoffset = buf->offset;
622 buf->writestring(ddoc_decl_e);
623
624 buf->writestring(ddoc_decl_dd_s);
625 dc->writeSections(sc, this, buf);
626 emitMemberComments(sc);
627 buf->writestring(ddoc_decl_dd_e);
628 }
629
630 void EnumMember::emitComment(Scope *sc)
631 {
632 //printf("EnumMember::emitComment(%p '%s'), comment = '%s'\n", this, toChars(), comment);
633 if (prot() == PROTprivate)
634 return;
635 if (!comment)
636 return;
637
638 OutBuffer *buf = sc->docbuf;
639 DocComment *dc = DocComment::parse(sc, this, comment);
640 unsigned o;
641
642 if (!dc)
643 {
644 emitDitto(sc);
645 return;
646 }
647 dc->pmacrotable = &sc->module->macrotable;
648
649 buf->writestring(ddoc_decl_s);
650 o = buf->offset;
651 toDocBuffer(buf);
652 highlightCode(sc, this, buf, o);
653 sc->lastoffset = buf->offset;
654 buf->writestring(ddoc_decl_e);
655
656 buf->writestring(ddoc_decl_dd_s);
657 dc->writeSections(sc, this, buf);
658 buf->writestring(ddoc_decl_dd_e);
659 }
660
661 /******************************* toDocBuffer **********************************/
662
663 void Dsymbol::toDocBuffer(OutBuffer *buf)
664 {
665 //printf("Dsymbol::toDocbuffer() %s\n", toChars());
666 HdrGenState hgs;
667
668 hgs.ddoc = 1;
669 toCBuffer(buf, &hgs);
670 }
671
672 void prefix(OutBuffer *buf, Dsymbol *s)
673 {
674 if (s->isDeprecated())
675 buf->writestring("deprecated ");
676 Declaration *d = s->isDeclaration();
677 if (d)
678 {
679 emitProtection(buf, d->protection);
680 if (d->isAbstract())
681 buf->writestring("abstract ");
682 if (d->isStatic())
683 buf->writestring("static ");
684 if (d->isConst())
685 buf->writestring("const ");
686 #if DMDV2
687 if (d->isInvariant())
688 buf->writestring("invariant ");
689 #endif
690 if (d->isFinal())
691 buf->writestring("final ");
692 if (d->isSynchronized())
693 buf->writestring("synchronized ");
694 }
695 }
696
697 void Declaration::toDocBuffer(OutBuffer *buf)
698 {
699 //printf("Declaration::toDocbuffer() %s, originalType = %p\n", toChars(), originalType);
700 if (ident)
701 {
702 prefix(buf, this);
703
704 if (type)
705 { HdrGenState hgs;
706 hgs.ddoc = 1;
707 if (originalType)
708 { //originalType->print();
709 originalType->toCBuffer(buf, ident, &hgs);
710 }
711 else
712 type->toCBuffer(buf, ident, &hgs);
713 }
714 else
715 buf->writestring(ident->toChars());
716 buf->writestring(";\n");
717 }
718 }
719
720
721 void AliasDeclaration::toDocBuffer(OutBuffer *buf)
722 {
723 //printf("AliasDeclaration::toDocbuffer() %s\n", toChars());
724 if (ident)
725 {
726 if (isDeprecated())
727 buf->writestring("deprecated ");
728
729 emitProtection(buf, protection);
730 buf->writestring("alias ");
731 buf->writestring(toChars());
732 buf->writestring(";\n");
733 }
734 }
735
736
737 void TypedefDeclaration::toDocBuffer(OutBuffer *buf)
738 {
739 if (ident)
740 {
741 if (isDeprecated())
742 buf->writestring("deprecated ");
743
744 emitProtection(buf, protection);
745 buf->writestring("typedef ");
746 buf->writestring(toChars());
747 buf->writestring(";\n");
748 }
749 }
750
751
752 void FuncDeclaration::toDocBuffer(OutBuffer *buf)
753 {
754 //printf("FuncDeclaration::toDocbuffer() %s\n", toChars());
755 if (ident)
756 {
757 TemplateDeclaration *td;
758
759 if (parent &&
760 (td = parent->isTemplateDeclaration()) != NULL &&
761 td->onemember == this)
762 { /* It's a function template
763 */
764 HdrGenState hgs;
765 unsigned o = buf->offset;
766 TypeFunction *tf = (TypeFunction *)type;
767
768 hgs.ddoc = 1;
769 prefix(buf, td);
770 tf->next->toCBuffer(buf, NULL, &hgs);
771 buf->writeByte(' ');
772 buf->writestring(ident->toChars());
773 buf->writeByte('(');
774 for (int i = 0; i < td->origParameters->dim; i++)
775 {
776 TemplateParameter *tp = (TemplateParameter *)td->origParameters->data[i];
777 if (i)
778 buf->writestring(", ");
779 tp->toCBuffer(buf, &hgs);
780 }
781 buf->writeByte(')');
782 Argument::argsToCBuffer(buf, &hgs, tf->parameters, tf->varargs);
783 buf->writestring(";\n");
784
785 highlightCode(NULL, this, buf, o);
786 }
787 else
788 {
789 Declaration::toDocBuffer(buf);
790 }
791 }
792 }
793
794 void CtorDeclaration::toDocBuffer(OutBuffer *buf)
795 {
796 HdrGenState hgs;
797
798 buf->writestring("this");
799 Argument::argsToCBuffer(buf, &hgs, arguments, varargs);
800 buf->writestring(";\n");
801 }
802
803
804 void AggregateDeclaration::toDocBuffer(OutBuffer *buf)
805 {
806 if (ident)
807 {
808 #if 0
809 emitProtection(buf, protection);
810 #endif
811 buf->printf("%s $(DDOC_PSYMBOL %s)", kind(), toChars());
812 buf->writestring(";\n");
813 }
814 }
815
816 void StructDeclaration::toDocBuffer(OutBuffer *buf)
817 {
818 //printf("StructDeclaration::toDocbuffer() %s\n", toChars());
819 if (ident)
820 {
821 #if 0
822 emitProtection(buf, protection);
823 #endif
824 TemplateDeclaration *td;
825
826 if (parent &&
827 (td = parent->isTemplateDeclaration()) != NULL &&
828 td->onemember == this)
829 { unsigned o = buf->offset;
830 td->toDocBuffer(buf);
831 highlightCode(NULL, this, buf, o);
832 }
833 else
834 {
835 buf->printf("%s $(DDOC_PSYMBOL %s)", kind(), toChars());
836 }
837 buf->writestring(";\n");
838 }
839 }
840
841 void ClassDeclaration::toDocBuffer(OutBuffer *buf)
842 {
843 //printf("ClassDeclaration::toDocbuffer() %s\n", toChars());
844 if (ident)
845 {
846 #if 0
847 emitProtection(buf, protection);
848 #endif
849 TemplateDeclaration *td;
850
851 if (parent &&
852 (td = parent->isTemplateDeclaration()) != NULL &&
853 td->onemember == this)
854 { unsigned o = buf->offset;
855 td->toDocBuffer(buf);
856 highlightCode(NULL, this, buf, o);
857 }
858 else
859 {
860 buf->printf("%s $(DDOC_PSYMBOL %s)", kind(), toChars());
861 }
862 int any = 0;
863 for (int i = 0; i < baseclasses.dim; i++)
864 { BaseClass *bc = (BaseClass *)baseclasses.data[i];
865
866 if (bc->protection == PROTprivate)
867 continue;
868 if (bc->base && bc->base->ident == Id::Object)
869 continue;
870
871 if (any)
872 buf->writestring(", ");
873 else
874 { buf->writestring(": ");
875 any = 1;
876 }
877 emitProtection(buf, bc->protection);
878 if (bc->base)
879 {
880 buf->writestring(bc->base->toPrettyChars());
881 }
882 else
883 {
884 HdrGenState hgs;
885 bc->type->toCBuffer(buf, NULL, &hgs);
886 }
887 }
888 buf->writestring(";\n");
889 }
890 }
891
892
893 void EnumDeclaration::toDocBuffer(OutBuffer *buf)
894 {
895 if (ident)
896 {
897 buf->printf("%s $(DDOC_PSYMBOL %s)", kind(), toChars());
898 buf->writestring(";\n");
899 }
900 }
901
902 void EnumMember::toDocBuffer(OutBuffer *buf)
903 {
904 if (ident)
905 {
906 buf->writestring(toChars());
907 }
908 }
909
910
911 /********************************* DocComment *********************************/
912
913 DocComment::DocComment()
914 {
915 memset(this, 0, sizeof(DocComment));
916 }
917
918 DocComment *DocComment::parse(Scope *sc, Dsymbol *s, unsigned char *comment)
919 { unsigned idlen;
920
921 //printf("parse(%s): '%s'\n", s->toChars(), comment);
922 if (sc->lastdc && isDitto(comment))
923 return NULL;
924
925 DocComment *dc = new DocComment();
926 if (!comment)
927 return dc;
928
929 dc->parseSections(comment);
930
931 for (int i = 0; i < dc->sections.dim; i++)
932 { Section *s = (Section *)dc->sections.data[i];
933
934 if (icmp("copyright", s->name, s->namelen) == 0)
935 {
936 dc->copyright = s;
937 }
938 if (icmp("macros", s->name, s->namelen) == 0)
939 {
940 dc->macros = s;
941 }
942 }
943
944 sc->lastdc = dc;
945 return dc;
946 }
947
948 /*****************************************
949 * Parse next paragraph out of *pcomment.
950 * Update *pcomment to point past paragraph.
951 * Returns NULL if no more paragraphs.
952 * If paragraph ends in 'identifier:',
953 * then (*pcomment)[0 .. idlen] is the identifier.
954 */
955
956 void DocComment::parseSections(unsigned char *comment)
957 { unsigned char *p;
958 unsigned char *pstart;
959 unsigned char *pend;
960 unsigned char *q;
961 unsigned char *idstart;
962 unsigned idlen;
963
964 unsigned char *name = NULL;
965 unsigned namelen = 0;
966
967 //printf("parseSections('%s')\n", comment);
968 p = comment;
969 while (*p)
970 {
971 p = skipwhitespace(p);
972 pstart = p;
973
974 /* Find end of section, which is ended by one of:
975 * 'identifier:'
976 * '\0'
977 */
978 idlen = 0;
979 while (1)
980 {
981 if (isalpha(*p) || *p == '_')
982 {
983 q = p + 1;
984 while (isalnum(*q) || *q == '_')
985 q++;
986 if (*q == ':') // identifier: ends it
987 { idlen = q - p;
988 idstart = p;
989 for (pend = p; pend > pstart; pend--)
990 { if (pend[-1] == '\n')
991 break;
992 }
993 p = q + 1;
994 break;
995 }
996 }
997 while (1)
998 {
999 if (!*p)
1000 { pend = p;
1001 goto L1;
1002 }
1003 if (*p == '\n')
1004 { p++;
1005 if (*p == '\n' && !summary && !namelen)
1006 {
1007 pend = p;
1008 p++;
1009 goto L1;
1010 }
1011 break;
1012 }
1013 p++;
1014 }
1015 p = skipwhitespace(p);
1016 }
1017 L1:
1018
1019 if (namelen || pstart < pend)
1020 {
1021 Section *s;
1022 if (icmp("Params", name, namelen) == 0)
1023 s = new ParamSection();
1024 else if (icmp("Macros", name, namelen) == 0)
1025 s = new MacroSection();
1026 else
1027 s = new Section();
1028 s->name = name;
1029 s->namelen = namelen;
1030 s->body = pstart;
1031 s->bodylen = pend - pstart;
1032 s->nooutput = 0;
1033
1034 //printf("Section: '%.*s' = '%.*s'\n", s->namelen, s->name, s->bodylen, s->body);
1035
1036 sections.push(s);
1037
1038 if (!summary && !namelen)
1039 summary = s;
1040 }
1041
1042 if (idlen)
1043 { name = idstart;
1044 namelen = idlen;
1045 }
1046 else
1047 { name = NULL;
1048 namelen = 0;
1049 if (!*p)
1050 break;
1051 }
1052 }
1053 }
1054
1055 void DocComment::writeSections(Scope *sc, Dsymbol *s, OutBuffer *buf)
1056 {
1057 //printf("DocComment::writeSections()\n");
1058 if (sections.dim)
1059 {
1060 buf->writestring("$(DDOC_SECTIONS \n");
1061 for (int i = 0; i < sections.dim; i++)
1062 { Section *sec = (Section *)sections.data[i];
1063
1064 if (sec->nooutput)
1065 continue;
1066 //printf("Section: '%.*s' = '%.*s'\n", sec->namelen, sec->name, sec->bodylen, sec->body);
1067 if (sec->namelen || i)
1068 sec->write(this, sc, s, buf);
1069 else
1070 {
1071 buf->writestring("$(DDOC_SUMMARY ");
1072 unsigned o = buf->offset;
1073 buf->write(sec->body, sec->bodylen);
1074 highlightText(sc, s, buf, o);
1075 buf->writestring(")\n");
1076 }
1077 }
1078 buf->writestring(")\n");
1079 }
1080 else
1081 {
1082 buf->writestring("$(DDOC_BLANKLINE)\n");
1083 }
1084 }
1085
1086 /***************************************************
1087 */
1088
1089 void Section::write(DocComment *dc, Scope *sc, Dsymbol *s, OutBuffer *buf)
1090 {
1091 if (namelen)
1092 {
1093 static const char *table[] =
1094 { "AUTHORS", "BUGS", "COPYRIGHT", "DATE",
1095 "DEPRECATED", "EXAMPLES", "HISTORY", "LICENSE",
1096 "RETURNS", "SEE_ALSO", "STANDARDS", "THROWS",
1097 "VERSION" };
1098
1099 for (int i = 0; i < sizeof(table) / sizeof(table[0]); i++)
1100 {
1101 if (icmp(table[i], name, namelen) == 0)
1102 {
1103 buf->printf("$(DDOC_%s ", table[i]);
1104 goto L1;
1105 }
1106 }
1107
1108 buf->writestring("$(DDOC_SECTION ");
1109 // Replace _ characters with spaces
1110 buf->writestring("$(DDOC_SECTION_H ");
1111 for (unsigned u = 0; u < namelen; u++)
1112 { unsigned char c = name[u];
1113 buf->writeByte((c == '_') ? ' ' : c);
1114 }
1115 buf->writestring(":)\n");
1116 }
1117 else
1118 {
1119 buf->writestring("$(DDOC_DESCRIPTION ");
1120 }
1121 L1:
1122 unsigned o = buf->offset;
1123 buf->write(body, bodylen);
1124 highlightText(sc, s, buf, o);
1125 buf->writestring(")\n");
1126 }
1127
1128 /***************************************************
1129 */
1130
1131 void ParamSection::write(DocComment *dc, Scope *sc, Dsymbol *s, OutBuffer *buf)
1132 {
1133 unsigned char *p = body;
1134 unsigned len = bodylen;
1135 unsigned char *pend = p + len;
1136
1137 unsigned char *tempstart;
1138 unsigned templen;
1139
1140 unsigned char *namestart;
1141 unsigned namelen = 0; // !=0 if line continuation
1142
1143 unsigned char *textstart;
1144 unsigned textlen;
1145
1146 unsigned o;
1147 Argument *arg;
1148
1149 buf->writestring("$(DDOC_PARAMS \n");
1150 while (p < pend)
1151 {
1152 // Skip to start of macro
1153 for (; 1; p++)
1154 {
1155 switch (*p)
1156 {
1157 case ' ':
1158 case '\t':
1159 continue;
1160
1161 case '\n':
1162 p++;
1163 goto Lcont;
1164
1165 default:
1166 if (!(isalpha(*p) || *p == '_'))
1167 {
1168 if (namelen)
1169 goto Ltext; // continuation of prev macro
1170 goto Lskipline;
1171 }
1172 break;
1173 }
1174 break;
1175 }
1176 tempstart = p;
1177
1178 while (isalnum(*p) || *p == '_')
1179 p++;
1180 templen = p - tempstart;
1181
1182 while (*p == ' ' || *p == '\t')
1183 p++;
1184
1185 if (*p != '=')
1186 { if (namelen)
1187 goto Ltext; // continuation of prev macro
1188 goto Lskipline;
1189 }
1190 p++;
1191
1192 if (namelen)
1193 { // Output existing param
1194
1195 L1:
1196 //printf("param '%.*s' = '%.*s'\n", namelen, namestart, textlen, textstart);
1197 HdrGenState hgs;
1198 buf->writestring("$(DDOC_PARAM_ROW ");
1199 buf->writestring("$(DDOC_PARAM_ID ");
1200 o = buf->offset;
1201 arg = isFunctionParameter(s, namestart, namelen);
1202 if (arg && arg->type && arg->ident)
1203 arg->type->toCBuffer(buf, arg->ident, &hgs);
1204 else
1205 buf->write(namestart, namelen);
1206 highlightCode(sc, s, buf, o);
1207 buf->writestring(")\n");
1208
1209 buf->writestring("$(DDOC_PARAM_DESC ");
1210 o = buf->offset;
1211 buf->write(textstart, textlen);
1212 highlightText(sc, s, buf, o);
1213 buf->writestring(")");
1214 buf->writestring(")\n");
1215 namelen = 0;
1216 if (p >= pend)
1217 break;
1218 }
1219
1220 namestart = tempstart;
1221 namelen = templen;
1222
1223 while (*p == ' ' || *p == '\t')
1224 p++;
1225 textstart = p;
1226
1227 Ltext:
1228 while (*p != '\n')
1229 p++;
1230 textlen = p - textstart;
1231 p++;
1232
1233 Lcont:
1234 continue;
1235
1236 Lskipline:
1237 // Ignore this line
1238 while (*p++ != '\n')
1239 ;
1240 }
1241 if (namelen)
1242 goto L1; // write out last one
1243 buf->writestring(")\n");
1244 }
1245
1246 /***************************************************
1247 */
1248
1249 void MacroSection::write(DocComment *dc, Scope *sc, Dsymbol *s, OutBuffer *buf)
1250 {
1251 //printf("MacroSection::write()\n");
1252 DocComment::parseMacros(dc->pescapetable, dc->pmacrotable, body, bodylen);
1253 }
1254
1255 /************************************************
1256 * Parse macros out of Macros: section.
1257 * Macros are of the form:
1258 * name1 = value1
1259 *
1260 * name2 = value2
1261 */
1262
1263 void DocComment::parseMacros(Escape **pescapetable, Macro **pmacrotable, unsigned char *m, unsigned mlen)
1264 {
1265 unsigned char *p = m;
1266 unsigned len = mlen;
1267 unsigned char *pend = p + len;
1268
1269 unsigned char *tempstart;
1270 unsigned templen;
1271
1272 unsigned char *namestart;
1273 unsigned namelen = 0; // !=0 if line continuation
1274
1275 unsigned char *textstart;
1276 unsigned textlen;
1277
1278 while (p < pend)
1279 {
1280 // Skip to start of macro
1281 for (; 1; p++)
1282 {
1283 if (p >= pend)
1284 goto Ldone;
1285 switch (*p)
1286 {
1287 case ' ':
1288 case '\t':
1289 continue;
1290
1291 case '\n':
1292 p++;
1293 goto Lcont;
1294
1295 default:
1296 if (!(isalpha(*p) || *p == '_'))
1297 {
1298 if (namelen)
1299 goto Ltext; // continuation of prev macro
1300 goto Lskipline;
1301 }
1302 break;
1303 }
1304 break;
1305 }
1306 tempstart = p;
1307
1308 while (1)
1309 {
1310 if (p >= pend)
1311 goto Ldone;
1312 if (!(isalnum(*p) || *p == '_'))
1313 break;
1314 p++;
1315 }
1316 templen = p - tempstart;
1317
1318 while (1)
1319 {
1320 if (p >= pend)
1321 goto Ldone;
1322 if (!(*p == ' ' || *p == '\t'))
1323 break;
1324 p++;
1325 }
1326
1327 if (*p != '=')
1328 { if (namelen)
1329 goto Ltext; // continuation of prev macro
1330 goto Lskipline;
1331 }
1332 p++;
1333 if (p >= pend)
1334 goto Ldone;
1335
1336 if (namelen)
1337 { // Output existing macro
1338 L1:
1339 //printf("macro '%.*s' = '%.*s'\n", namelen, namestart, textlen, textstart);
1340 if (icmp("ESCAPES", namestart, namelen) == 0)
1341 parseEscapes(pescapetable, textstart, textlen);
1342 else
1343 Macro::define(pmacrotable, namestart, namelen, textstart, textlen);
1344 namelen = 0;
1345 if (p >= pend)
1346 break;
1347 }
1348
1349 namestart = tempstart;
1350 namelen = templen;
1351
1352 while (p < pend && (*p == ' ' || *p == '\t'))
1353 p++;
1354 textstart = p;
1355
1356 Ltext:
1357 while (p < pend && *p != '\n')
1358 p++;
1359 textlen = p - textstart;
1360
1361 // Remove trailing \r if there is one
1362 if (p > m && p[-1] == '\r')
1363 textlen--;
1364
1365 p++;
1366 //printf("p = %p, pend = %p\n", p, pend);
1367
1368 Lcont:
1369 continue;
1370
1371 Lskipline:
1372 // Ignore this line
1373 while (p < pend && *p++ != '\n')
1374 ;
1375 }
1376 Ldone:
1377 if (namelen)
1378 goto L1; // write out last one
1379 }
1380
1381 /**************************************
1382 * Parse escapes of the form:
1383 * /c/string/
1384 * where c is a single character.
1385 * Multiple escapes can be separated
1386 * by whitespace and/or commas.
1387 */
1388
1389 void DocComment::parseEscapes(Escape **pescapetable, unsigned char *textstart, unsigned textlen)
1390 { Escape *escapetable = *pescapetable;
1391
1392 if (!escapetable)
1393 { escapetable = new Escape;
1394 *pescapetable = escapetable;
1395 }
1396 unsigned char *p = textstart;
1397 unsigned char *pend = p + textlen;
1398
1399 while (1)
1400 {
1401 while (1)
1402 {
1403 if (p + 4 >= pend)
1404 return;
1405 if (!(*p == ' ' || *p == '\t' || *p == '\n' || *p == ','))
1406 break;
1407 p++;
1408 }
1409 if (p[0] != '/' || p[2] != '/')
1410 return;
1411 unsigned char c = p[1];
1412 p += 3;
1413 unsigned char *start = p;
1414 while (1)
1415 {
1416 if (p >= pend)
1417 return;
1418 if (*p == '/')
1419 break;
1420 p++;
1421 }
1422 size_t len = p - start;
1423 char *s = (char *)memcpy(mem.malloc(len + 1), start, len);
1424 s[len] = 0;
1425 escapetable->strings[c] = s;
1426 //printf("%c = '%s'\n", c, s);
1427 p++;
1428 }
1429 }
1430
1431
1432 /******************************************
1433 * Compare 0-terminated string with length terminated string.
1434 * Return < 0, ==0, > 0
1435 */
1436
1437 int cmp(const char *stringz, void *s, size_t slen)
1438 {
1439 size_t len1 = strlen(stringz);
1440
1441 if (len1 != slen)
1442 return len1 - slen;
1443 return memcmp(stringz, s, slen);
1444 }
1445
1446 int icmp(const char *stringz, void *s, size_t slen)
1447 {
1448 size_t len1 = strlen(stringz);
1449
1450 if (len1 != slen)
1451 return len1 - slen;
1452 return memicmp(stringz, (char *)s, slen);
1453 }
1454
1455 /*****************************************
1456 * Return !=0 if comment consists entirely of "ditto".
1457 */
1458
1459 int isDitto(unsigned char *comment)
1460 {
1461 if (comment)
1462 {
1463 unsigned char *p = skipwhitespace(comment);
1464
1465 if (memicmp((char *)p, "ditto", 5) == 0 && *skipwhitespace(p + 5) == 0)
1466 return 1;
1467 }
1468 return 0;
1469 }
1470
1471 /**********************************************
1472 * Skip white space.
1473 */
1474
1475 unsigned char *skipwhitespace(unsigned char *p)
1476 {
1477 for (; 1; p++)
1478 { switch (*p)
1479 {
1480 case ' ':
1481 case '\t':
1482 case '\n':
1483 continue;
1484 }
1485 break;
1486 }
1487 return p;
1488 }
1489
1490
1491 /************************************************
1492 * Scan forward to one of:
1493 * start of identifier
1494 * beginning of next line
1495 * end of buf
1496 */
1497
1498 unsigned skiptoident(OutBuffer *buf, unsigned i)
1499 {
1500 for (; i < buf->offset; i++)
1501 {
1502 // BUG: handle unicode alpha's
1503 unsigned char c = buf->data[i];
1504 if (isalpha(c) || c == '_')
1505 break;
1506 if (c == '\n')
1507 break;
1508 }
1509 return i;
1510 }
1511
1512 /************************************************
1513 * Scan forward past end of identifier.
1514 */
1515
1516 unsigned skippastident(OutBuffer *buf, unsigned i)
1517 {
1518 for (; i < buf->offset; i++)
1519 {
1520 // BUG: handle unicode alpha's
1521 unsigned char c = buf->data[i];
1522 if (!(isalnum(c) || c == '_'))
1523 break;
1524 }
1525 return i;
1526 }
1527
1528
1529 /************************************************
1530 * Scan forward past URL starting at i.
1531 * We don't want to highlight parts of a URL.
1532 * Returns:
1533 * i if not a URL
1534 * index just past it if it is a URL
1535 */
1536
1537 unsigned skippastURL(OutBuffer *buf, unsigned i)
1538 { unsigned length = buf->offset - i;
1539 unsigned char *p = &buf->data[i];
1540 unsigned j;
1541 unsigned sawdot = 0;
1542
1543 if (length > 7 && memicmp((char *)p, "http://", 7) == 0)
1544 {
1545 j = 7;
1546 }
1547 else if (length > 8 && memicmp((char *)p, "https://", 8) == 0)
1548 {
1549 j = 8;
1550 }
1551 else
1552 goto Lno;
1553
1554 for (; j < length; j++)
1555 { unsigned char c = p[j];
1556 if (isalnum(c))
1557 continue;
1558 if (c == '-' || c == '_' || c == '?' ||
1559 c == '=' || c == '%' || c == '&' ||
1560 c == '/' || c == '+' || c == '#' ||
1561 c == '~')
1562 continue;
1563 if (c == '.')
1564 {
1565 sawdot = 1;
1566 continue;
1567 }
1568 break;
1569 }
1570 if (sawdot)
1571 return i + j;
1572
1573 Lno:
1574 return i;
1575 }
1576
1577
1578 /****************************************************
1579 */
1580
1581 int isKeyword(unsigned char *p, unsigned len)
1582 {
1583 static const char *table[] = { "true", "false", "null" };
1584
1585 for (int i = 0; i < sizeof(table) / sizeof(table[0]); i++)
1586 {
1587 if (cmp(table[i], p, len) == 0)
1588 return 1;
1589 }
1590 return 0;
1591 }
1592
1593 /****************************************************
1594 */
1595
1596 Argument *isFunctionParameter(Dsymbol *s, unsigned char *p, unsigned len)
1597 {
1598 FuncDeclaration *f = s->isFuncDeclaration();
1599
1600 /* f->type may be NULL for template members.
1601 */
1602 if (f && f->type)
1603 {
1604 TypeFunction *tf;
1605 if (f->originalType)
1606 {
1607 tf = (TypeFunction *)f->originalType;
1608 }
1609 else
1610 tf = (TypeFunction *)f->type;
1611
1612 if (tf->parameters)
1613 {
1614 for (size_t k = 0; k < tf->parameters->dim; k++)
1615 { Argument *arg = (Argument *)tf->parameters->data[k];
1616
1617 if (arg->ident && cmp(arg->ident->toChars(), p, len) == 0)
1618 {
1619 return arg;
1620 }
1621 }
1622 }
1623 }
1624 return NULL;
1625 }
1626
1627 /**************************************************
1628 * Highlight text section.
1629 */
1630
1631 void highlightText(Scope *sc, Dsymbol *s, OutBuffer *buf, unsigned offset)
1632 {
1633 //printf("highlightText()\n");
1634 const char *sid = s->ident->toChars();
1635 FuncDeclaration *f = s->isFuncDeclaration();
1636 unsigned char *p;
1637 const char *se;
1638
1639 int leadingBlank = 1;
1640 int inCode = 0;
1641 int inComment = 0; // in <!-- ... --> comment
1642 unsigned iCodeStart; // start of code section
1643
1644 unsigned iLineStart = offset;
1645
1646 for (unsigned i = offset; i < buf->offset; i++)
1647 { unsigned char c = buf->data[i];
1648
1649 Lcont:
1650 switch (c)
1651 {
1652 case ' ':
1653 case '\t':
1654 break;
1655
1656 case '\n':
1657 if (sc && !inCode && i == iLineStart && i + 1 < buf->offset) // if "\n\n"
1658 {
1659 static char blankline[] = "$(DDOC_BLANKLINE)\n";
1660
1661 i = buf->insert(i, blankline, sizeof(blankline) - 1);
1662 }
1663 leadingBlank = 1;
1664 iLineStart = i + 1;
1665 break;
1666
1667 case '<':
1668 leadingBlank = 0;
1669 if (inCode)
1670 break;
1671 p = &buf->data[i];
1672
1673 // Skip over comments
1674 if (p[1] == '!' && p[2] == '-' && p[3] == '-')
1675 { unsigned j = i + 4;
1676 p += 4;
1677 while (1)
1678 {
1679 if (j == buf->offset)
1680 goto L1;
1681 if (p[0] == '-' && p[1] == '-' && p[2] == '>')
1682 {
1683 i = j + 2; // place on closing '>'
1684 break;
1685 }
1686 j++;
1687 p++;
1688 }
1689 break;
1690 }
1691
1692 // Skip over HTML tag
1693 if (isalpha(p[1]) || (p[1] == '/' && isalpha(p[2])))
1694 { unsigned j = i + 2;
1695 p += 2;
1696 while (1)
1697 {
1698 if (j == buf->offset)
1699 goto L1;
1700 if (p[0] == '>')
1701 {
1702 i = j; // place on closing '>'
1703 break;
1704 }
1705 j++;
1706 p++;
1707 }
1708 break;
1709 }
1710
1711 L1:
1712 // Replace '<' with '&lt;' character entity
1713 se = Escape::escapeChar('<');
1714 if (se)
1715 { size_t len = strlen(se);
1716 buf->remove(i, 1);
1717 i = buf->insert(i, se, len);
1718 i--; // point to ';'
1719 }
1720 break;
1721
1722 case '>':
1723 leadingBlank = 0;
1724 if (inCode)
1725 break;
1726 // Replace '>' with '&gt;' character entity
1727 se = Escape::escapeChar('>');
1728 if (se)
1729 { size_t len = strlen(se);
1730 buf->remove(i, 1);
1731 i = buf->insert(i, se, len);
1732 i--; // point to ';'
1733 }
1734 break;
1735
1736 case '&':
1737 leadingBlank = 0;
1738 if (inCode)
1739 break;
1740 p = &buf->data[i];
1741 if (p[1] == '#' || isalpha(p[1]))
1742 break; // already a character entity
1743 // Replace '&' with '&amp;' character entity
1744 se = Escape::escapeChar('&');
1745 if (se)
1746 { size_t len = strlen(se);
1747 buf->remove(i, 1);
1748 i = buf->insert(i, se, len);
1749 i--; // point to ';'
1750 }
1751 break;
1752
1753 case '-':
1754 /* A line beginning with --- delimits a code section.
1755 * inCode tells us if it is start or end of a code section.
1756 */
1757 if (leadingBlank)
1758 { int istart = i;
1759 int eollen = 0;
1760
1761 leadingBlank = 0;
1762 while (1)
1763 {
1764 ++i;
1765 if (i >= buf->offset)
1766 break;
1767 c = buf->data[i];
1768 if (c == '\n')
1769 { eollen = 1;
1770 break;
1771 }
1772 if (c == '\r')
1773 {
1774 eollen = 1;
1775 if (i + 1 >= buf->offset)
1776 break;
1777 if (buf->data[i + 1] == '\n')
1778 { eollen = 2;
1779 break;
1780 }
1781 }
1782 // BUG: handle UTF PS and LS too
1783 if (c != '-')
1784 goto Lcont;
1785 }
1786 if (i - istart < 3)
1787 goto Lcont;
1788
1789 // We have the start/end of a code section
1790
1791 // Remove the entire --- line, including blanks and \n
1792 buf->remove(iLineStart, i - iLineStart + eollen);
1793 i = iLineStart;
1794
1795 if (inCode)
1796 {
1797 inCode = 0;
1798 // The code section is from iCodeStart to i
1799 OutBuffer codebuf;
1800
1801 codebuf.write(buf->data + iCodeStart, i - iCodeStart);
1802 codebuf.writeByte(0);
1803 highlightCode2(sc, s, &codebuf, 0);
1804 buf->remove(iCodeStart, i - iCodeStart);
1805 i = buf->insert(iCodeStart, codebuf.data, codebuf.offset);
1806 i = buf->insert(i, ")\n", 2);
1807 i--;
1808 }
1809 else
1810 { static char pre[] = "$(D_CODE \n";
1811
1812 inCode = 1;
1813 i = buf->insert(i, pre, sizeof(pre) - 1);
1814 iCodeStart = i;
1815 i--; // place i on >
1816 }
1817 }
1818 break;
1819
1820 default:
1821 leadingBlank = 0;
1822 if (sc && !inCode && (isalpha(c) || c == '_'))
1823 { unsigned j;
1824
1825 j = skippastident(buf, i);
1826 if (j > i)
1827 {
1828 unsigned k = skippastURL(buf, i);
1829 if (k > i)
1830 { i = k - 1;
1831 break;
1832 }
1833
1834 if (buf->data[i] == '_') // leading '_' means no highlight
1835 {
1836 buf->remove(i, 1);
1837 i = j - 1;
1838 }
1839 else
1840 {
1841 if (cmp(sid, buf->data + i, j - i) == 0)
1842 {
1843 i = buf->bracket(i, "$(DDOC_PSYMBOL ", j, ")") - 1;
1844 break;
1845 }
1846 else if (isKeyword(buf->data + i, j - i))
1847 {
1848 i = buf->bracket(i, "$(DDOC_KEYWORD ", j, ")") - 1;
1849 break;
1850 }
1851 else
1852 {
1853 if (f && isFunctionParameter(f, buf->data + i, j - i))
1854 {
1855 //printf("highlighting arg '%s', i = %d, j = %d\n", arg->ident->toChars(), i, j);
1856 i = buf->bracket(i, "$(DDOC_PARAM ", j, ")") - 1;
1857 break;
1858 }
1859 }
1860 i = j - 1;
1861 }
1862 }
1863 }
1864 break;
1865 }
1866 }
1867 Ldone:
1868 ;
1869 }
1870
1871 /**************************************************
1872 * Highlight code for DDOC section.
1873 */
1874
1875 void highlightCode(Scope *sc, Dsymbol *s, OutBuffer *buf, unsigned offset)
1876 {
1877 char *sid = s->ident->toChars();
1878 FuncDeclaration *f = s->isFuncDeclaration();
1879
1880 //printf("highlightCode(s = '%s', kind = %s)\n", sid, s->kind());
1881 for (unsigned i = offset; i < buf->offset; i++)
1882 { unsigned char c = buf->data[i];
1883 const char *se;
1884
1885 se = Escape::escapeChar(c);
1886 if (se)
1887 {
1888 size_t len = strlen(se);
1889 buf->remove(i, 1);
1890 i = buf->insert(i, se, len);
1891 i--; // point to ';'
1892 }
1893 else if (isalpha(c) || c == '_')
1894 { unsigned j;
1895
1896 j = skippastident(buf, i);
1897 if (j > i)
1898 {
1899 if (cmp(sid, buf->data + i, j - i) == 0)
1900 {
1901 i = buf->bracket(i, "$(DDOC_PSYMBOL ", j, ")") - 1;
1902 continue;
1903 }
1904 else if (f)
1905 {
1906 if (isFunctionParameter(f, buf->data + i, j - i))
1907 {
1908 //printf("highlighting arg '%s', i = %d, j = %d\n", arg->ident->toChars(), i, j);
1909 i = buf->bracket(i, "$(DDOC_PARAM ", j, ")") - 1;
1910 continue;
1911 }
1912 }
1913 i = j - 1;
1914 }
1915 }
1916 }
1917 }
1918
1919 /****************************************
1920 */
1921
1922 void highlightCode3(OutBuffer *buf, unsigned char *p, unsigned char *pend)
1923 {
1924 for (; p < pend; p++)
1925 { const char *s = Escape::escapeChar(*p);
1926 if (s)
1927 buf->writestring(s);
1928 else
1929 buf->writeByte(*p);
1930 }
1931 }
1932
1933 /**************************************************
1934 * Highlight code for CODE section.
1935 */
1936
1937
1938 void highlightCode2(Scope *sc, Dsymbol *s, OutBuffer *buf, unsigned offset)
1939 {
1940 char *sid = s->ident->toChars();
1941 FuncDeclaration *f = s->isFuncDeclaration();
1942 unsigned errorsave = global.errors;
1943 Lexer lex(NULL, buf->data, 0, buf->offset - 1, 0, 1);
1944 Token tok;
1945 OutBuffer res;
1946 unsigned char *lastp = buf->data;
1947 const char *highlight;
1948
1949 //printf("highlightCode2('%.*s')\n", buf->offset - 1, buf->data);
1950 res.reserve(buf->offset);
1951 while (1)
1952 {
1953 lex.scan(&tok);
1954 highlightCode3(&res, lastp, tok.ptr);
1955 highlight = NULL;
1956 switch (tok.value)
1957 {
1958 case TOKidentifier:
1959 if (!sc)
1960 break;
1961 if (cmp(sid, tok.ptr, lex.p - tok.ptr) == 0)
1962 {
1963 highlight = "$(D_PSYMBOL ";
1964 break;
1965 }
1966 else if (f)
1967 {
1968 if (isFunctionParameter(f, tok.ptr, lex.p - tok.ptr))
1969 {
1970 //printf("highlighting arg '%s', i = %d, j = %d\n", arg->ident->toChars(), i, j);
1971 highlight = "$(D_PARAM ";
1972 break;
1973 }
1974 }
1975 break;
1976
1977 case TOKcomment:
1978 highlight = "$(D_COMMENT ";
1979 break;
1980
1981 case TOKstring:
1982 highlight = "$(D_STRING ";
1983 break;
1984
1985 default:
1986 if (tok.isKeyword())
1987 highlight = "$(D_KEYWORD ";
1988 break;
1989 }
1990 if (highlight)
1991 res.writestring(highlight);
1992 highlightCode3(&res, tok.ptr, lex.p);
1993 if (highlight)
1994 res.writeByte(')');
1995 if (tok.value == TOKeof)
1996 break;
1997 lastp = lex.p;
1998 }
1999 buf->setsize(offset);
2000 buf->write(&res);
2001 global.errors = errorsave;
2002 }
2003
2004 /***************************************
2005 * Find character string to replace c with.
2006 */
2007
2008 const char *Escape::escapeChar(unsigned c)
2009 { const char *s;
2010
2011 switch (c)
2012 {
2013 case '<':
2014 s = "&lt;";
2015 break;
2016 case '>':
2017 s = "&gt;";
2018 break;
2019 case '&':
2020 s = "&amp;";
2021 break;
2022 default:
2023 s = NULL;
2024 break;
2025 }
2026 return s;
2027 }
2028