Warn about "assert X,Y"
[delight/core.git] / dmd2 / doc.c
blob5f130b97c5c0b89d424f6deeb92d90589a4755ce
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.
11 // This implements the Ddoc capability.
13 #include <stdio.h>
14 #include <string.h>
15 #include <time.h>
16 #include <ctype.h>
17 #include <assert.h>
19 #ifdef IN_GCC
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
31 #include "root.h"
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"
48 struct Escape
50 char *strings[256];
52 static char *escapeChar(unsigned c);
55 struct Section
57 unsigned char *name;
58 unsigned namelen;
60 unsigned char *body;
61 unsigned bodylen;
63 int nooutput;
65 virtual void write(DocComment *dc, Scope *sc, Dsymbol *s, OutBuffer *buf);
68 struct ParamSection : Section
70 void write(DocComment *dc, Scope *sc, Dsymbol *s, OutBuffer *buf);
73 struct MacroSection : Section
75 void write(DocComment *dc, Scope *sc, Dsymbol *s, OutBuffer *buf);
78 struct DocComment
80 Array sections; // Section*[]
82 Section *summary;
83 Section *copyright;
84 Section *macros;
85 Macro **pmacrotable;
86 Escape **pescapetable;
88 DocComment();
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);
94 void parseSections(unsigned char *comment);
95 void writeSections(Scope *sc, Dsymbol *s, OutBuffer *buf);
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);
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\
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\
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\
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\
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\
189 DDOC_PSYMBOL = $(U $0)\n\
190 DDOC_KEYWORD = $(B $0)\n\
191 DDOC_PARAM = $(I $0)\n\
193 ESCAPES = /</&lt;/\n\
194 />/&gt;/\n\
195 /&/&amp;/\n\
198 static char ddoc_decl_s[] = "$(DDOC_DECL ";
199 static char ddoc_decl_e[] = ")\n";
201 static char ddoc_decl_dd_s[] = "$(DDOC_DECL_DD ";
202 static char ddoc_decl_dd_e[] = ")\n";
205 /****************************************************
208 void Module::gendocfile()
210 static OutBuffer mbuf;
211 static int mbuf_done;
213 OutBuffer buf;
215 //printf("Module::gendocfile()\n");
217 if (!mbuf_done) // if not already read the ddoc files
218 { mbuf_done = 1;
220 // Use our internal default
221 mbuf.write(ddoc_default, sizeof(ddoc_default) - 1);
223 // Override with DDOCFILE specified in the sc.ini file
224 char *p = getenv("DDOCFILE");
225 if (p)
226 global.params.ddocfiles->shift(p);
228 // Override with the ddoc macro files from the command line
229 for (int i = 0; i < global.params.ddocfiles->dim; i++)
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
236 //printf("file: '%.*s'\n", file.len, file.buffer);
237 mbuf.write(file.buffer, file.len);
240 DocComment::parseMacros(&escapetable, &macrotable, mbuf.data, mbuf.offset);
242 Scope *sc = Scope::createGlobal(this); // create root scope
243 sc->docbuf = &buf;
245 DocComment *dc = DocComment::parse(sc, this, comment);
246 dc->pmacrotable = &macrotable;
247 dc->pescapetable = &escapetable;
249 // Generate predefined macros
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));
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);
263 char *docfilename = docfile->toChars();
264 Macro::define(&macrotable, (unsigned char *)"DOCFILENAME", 11, (unsigned char *)docfilename, strlen(docfilename));
266 if (dc->copyright)
268 dc->copyright->nooutput = 1;
269 Macro::define(&macrotable, (unsigned char *)"COPYRIGHT", 9, dc->copyright->body, dc->copyright->bodylen);
272 buf.printf("$(DDOC_COMMENT Generated by Ddoc from %s)\n", srcfile->toChars());
273 if (isDocFile)
275 size_t commentlen = strlen((char *)comment);
276 if (dc->macros)
278 commentlen = dc->macros->name - comment;
279 dc->macros->write(dc, sc, this, sc->docbuf);
281 sc->docbuf->write(comment, commentlen);
282 highlightText(NULL, this, sc->docbuf, 0);
284 else
286 dc->writeSections(sc, this, sc->docbuf);
287 emitMemberComments(sc);
290 //printf("BODY= '%.*s'\n", buf.offset, buf.data);
291 Macro::define(&macrotable, (unsigned char *)"BODY", 4, buf.data, buf.offset);
293 OutBuffer buf2;
294 buf2.writestring("$(DDOC)\n");
295 unsigned end = buf2.offset;
296 macrotable->expand(&buf2, 0, &end, NULL, 0);
298 #if 1
299 /* Remove all the escape sequences from buf2,
300 * and make CR-LF the newline.
303 buf.setsize(0);
304 buf.reserve(buf2.offset);
305 unsigned char *p = buf2.data;
306 for (unsigned j = 0; j < buf2.offset; j++)
308 unsigned char c = p[j];
309 if (c == 0xFF && j + 1 < buf2.offset)
311 j++;
312 continue;
314 if (c == '\n')
315 buf.writeByte('\r');
316 else if (c == '\r')
318 buf.writestring("\r\n");
319 if (j + 1 < buf2.offset && p[j + 1] == '\n')
321 j++;
323 continue;
325 buf.writeByte(c);
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
341 { unsigned i = 0;
342 unsigned char *p = buf2.data;
343 for (unsigned j = 0; j < buf2.offset; j++)
345 if (p[j] == 0xFF && j + 1 < buf2.offset)
347 j++;
348 continue;
350 p[i] = p[j];
351 i++;
353 buf2.setsize(i);
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
367 /******************************* emitComment **********************************/
370 * Emit doc comment to documentation file
373 void Dsymbol::emitDitto(Scope *sc)
375 //printf("Dsymbol::emitDitto() %s %s\n", kind(), toChars());
376 OutBuffer *buf = sc->docbuf;
377 unsigned o;
378 OutBuffer b;
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().
387 TemplateDeclaration *td;
388 if (parent &&
389 (td = parent->isTemplateDeclaration()) != NULL &&
390 td->onemember == this)
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;
401 void ScopeDsymbol::emitMemberComments(Scope *sc)
403 //printf("ScopeDsymbol::emitMemberComments()\n");
404 OutBuffer *buf = sc->docbuf;
406 if (members)
407 { char *m = "$(DDOC_MEMBERS \n";
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";
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++)
425 Dsymbol *s = (Dsymbol *)members->data[i];
426 //printf("\ts = '%s'\n", s->toChars());
427 s->emitComment(sc);
429 sc->pop();
430 buf->writestring(")\n");
434 void emitProtection(OutBuffer *buf, PROT prot)
436 char *p;
438 switch (prot)
440 case PROTpackage: p = "package"; break;
441 case PROTprotected: p = "protected"; break;
442 case PROTexport: p = "export"; break;
443 default: p = NULL; break;
445 if (p)
446 buf->printf("%s ", p);
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) { }
460 void Declaration::emitComment(Scope *sc)
462 //printf("Declaration::emitComment(%p '%s'), comment = '%s'\n", this, toChars(), comment);
463 //printf("type = %p\n", type);
465 if (protection == PROTprivate || !ident ||
466 (!type && !isCtorDeclaration() && !isAliasDeclaration()))
467 return;
468 if (!comment)
469 return;
471 OutBuffer *buf = sc->docbuf;
472 DocComment *dc = DocComment::parse(sc, this, comment);
473 unsigned o;
475 if (!dc)
477 emitDitto(sc);
478 return;
480 dc->pmacrotable = &sc->module->macrotable;
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);
489 buf->writestring(ddoc_decl_dd_s);
490 dc->writeSections(sc, this, buf);
491 buf->writestring(ddoc_decl_dd_e);
494 void AggregateDeclaration::emitComment(Scope *sc)
496 //printf("AggregateDeclaration::emitComment() '%s'\n", toChars());
497 if (prot() == PROTprivate)
498 return;
499 if (!comment)
500 return;
502 OutBuffer *buf = sc->docbuf;
503 DocComment *dc = DocComment::parse(sc, this, comment);
505 if (!dc)
507 emitDitto(sc);
508 return;
510 dc->pmacrotable = &sc->module->macrotable;
512 buf->writestring(ddoc_decl_s);
513 toDocBuffer(buf);
514 sc->lastoffset = buf->offset;
515 buf->writestring(ddoc_decl_e);
517 buf->writestring(ddoc_decl_dd_s);
518 dc->writeSections(sc, this, buf);
519 emitMemberComments(sc);
520 buf->writestring(ddoc_decl_dd_e);
523 void TemplateDeclaration::emitComment(Scope *sc)
525 //printf("TemplateDeclaration::emitComment() '%s', kind = %s\n", toChars(), kind());
526 if (prot() == PROTprivate)
527 return;
528 if (!comment)
529 return;
531 OutBuffer *buf = sc->docbuf;
532 DocComment *dc = DocComment::parse(sc, this, comment);
533 unsigned o;
534 int hasmembers = 1;
536 Dsymbol *ss = this;
538 if (onemember)
540 ss = onemember->isAggregateDeclaration();
541 if (!ss)
543 ss = onemember->isFuncDeclaration();
544 if (ss)
545 hasmembers = 0;
546 else
547 ss = this;
551 if (!dc)
553 ss->emitDitto(sc);
554 return;
556 dc->pmacrotable = &sc->module->macrotable;
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);
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);
573 void EnumDeclaration::emitComment(Scope *sc)
575 if (prot() == PROTprivate)
576 return;
577 // if (!comment)
578 { if (isAnonymous() && members)
580 for (int i = 0; i < members->dim; i++)
582 Dsymbol *s = (Dsymbol *)members->data[i];
583 s->emitComment(sc);
585 return;
588 if (!comment)
589 return;
590 if (isAnonymous())
591 return;
593 OutBuffer *buf = sc->docbuf;
594 DocComment *dc = DocComment::parse(sc, this, comment);
596 if (!dc)
598 emitDitto(sc);
599 return;
601 dc->pmacrotable = &sc->module->macrotable;
603 buf->writestring(ddoc_decl_s);
604 toDocBuffer(buf);
605 sc->lastoffset = buf->offset;
606 buf->writestring(ddoc_decl_e);
608 buf->writestring(ddoc_decl_dd_s);
609 dc->writeSections(sc, this, buf);
610 emitMemberComments(sc);
611 buf->writestring(ddoc_decl_dd_e);
614 void EnumMember::emitComment(Scope *sc)
616 //printf("EnumMember::emitComment(%p '%s'), comment = '%s'\n", this, toChars(), comment);
617 if (prot() == PROTprivate)
618 return;
619 if (!comment)
620 return;
622 OutBuffer *buf = sc->docbuf;
623 DocComment *dc = DocComment::parse(sc, this, comment);
624 unsigned o;
626 if (!dc)
628 emitDitto(sc);
629 return;
631 dc->pmacrotable = &sc->module->macrotable;
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);
640 buf->writestring(ddoc_decl_dd_s);
641 dc->writeSections(sc, this, buf);
642 buf->writestring(ddoc_decl_dd_e);
645 /******************************* toDocBuffer **********************************/
647 void Dsymbol::toDocBuffer(OutBuffer *buf)
649 //printf("Dsymbol::toDocbuffer() %s\n", toChars());
650 HdrGenState hgs;
652 hgs.ddoc = 1;
653 toCBuffer(buf, &hgs);
656 void prefix(OutBuffer *buf, Dsymbol *s)
658 if (s->isDeprecated())
659 buf->writestring("deprecated ");
660 Declaration *d = s->isDeclaration();
661 if (d)
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 ");
681 void Declaration::toDocBuffer(OutBuffer *buf)
683 //printf("Declaration::toDocbuffer() %s, originalType = %p\n", toChars(), originalType);
684 if (ident)
686 prefix(buf, this);
688 if (type)
689 { HdrGenState hgs;
690 hgs.ddoc = 1;
691 if (originalType)
692 { //originalType->print();
693 originalType->toCBuffer(buf, ident, &hgs);
695 else
696 type->toCBuffer(buf, ident, &hgs);
698 else
699 buf->writestring(ident->toChars());
700 buf->writestring(";\n");
705 void AliasDeclaration::toDocBuffer(OutBuffer *buf)
707 //printf("AliasDeclaration::toDocbuffer() %s\n", toChars());
708 if (ident)
710 if (isDeprecated())
711 buf->writestring("deprecated ");
713 emitProtection(buf, protection);
714 buf->writestring("alias ");
715 buf->writestring(toChars());
716 buf->writestring(";\n");
721 void TypedefDeclaration::toDocBuffer(OutBuffer *buf)
723 if (ident)
725 if (isDeprecated())
726 buf->writestring("deprecated ");
728 emitProtection(buf, protection);
729 buf->writestring("typedef ");
730 buf->writestring(toChars());
731 buf->writestring(";\n");
736 void FuncDeclaration::toDocBuffer(OutBuffer *buf)
738 //printf("FuncDeclaration::toDocbuffer() %s\n", toChars());
739 if (ident)
741 TemplateDeclaration *td;
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;
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++)
758 TemplateParameter *tp = (TemplateParameter *)td->origParameters->data[i];
759 if (i)
760 buf->writeByte(',');
761 tp->toCBuffer(buf, &hgs);
763 buf->writeByte(')');
764 Argument::argsToCBuffer(buf, &hgs, tf->parameters, tf->varargs);
765 buf->writestring(";\n");
767 highlightCode(NULL, this, buf, o);
769 else
771 Declaration::toDocBuffer(buf);
776 void CtorDeclaration::toDocBuffer(OutBuffer *buf)
778 HdrGenState hgs;
780 buf->writestring("this");
781 Argument::argsToCBuffer(buf, &hgs, arguments, varargs);
782 buf->writestring(";\n");
786 void AggregateDeclaration::toDocBuffer(OutBuffer *buf)
788 if (ident)
790 #if 0
791 emitProtection(buf, protection);
792 #endif
793 buf->printf("%s $(DDOC_PSYMBOL %s)", kind(), toChars());
794 buf->writestring(";\n");
798 void StructDeclaration::toDocBuffer(OutBuffer *buf)
800 //printf("StructDeclaration::toDocbuffer() %s\n", toChars());
801 if (ident)
803 #if 0
804 emitProtection(buf, protection);
805 #endif
806 TemplateDeclaration *td;
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);
815 else
817 buf->printf("%s $(DDOC_PSYMBOL %s)", kind(), toChars());
819 buf->writestring(";\n");
823 void ClassDeclaration::toDocBuffer(OutBuffer *buf)
825 //printf("ClassDeclaration::toDocbuffer() %s\n", toChars());
826 if (ident)
828 #if 0
829 emitProtection(buf, protection);
830 #endif
831 TemplateDeclaration *td;
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);
840 else
842 buf->printf("%s $(DDOC_PSYMBOL %s)", kind(), toChars());
844 int any = 0;
845 for (int i = 0; i < baseclasses.dim; i++)
846 { BaseClass *bc = (BaseClass *)baseclasses.data[i];
848 if (bc->protection == PROTprivate)
849 continue;
850 if (bc->base && bc->base->ident == Id::Object)
851 continue;
853 if (any)
854 buf->writestring(", ");
855 else
856 { buf->writestring(": ");
857 any = 1;
859 emitProtection(buf, bc->protection);
860 if (bc->base)
862 buf->writestring(bc->base->toPrettyChars());
864 else
866 HdrGenState hgs;
867 bc->type->toCBuffer(buf, NULL, &hgs);
870 buf->writestring(";\n");
875 void EnumDeclaration::toDocBuffer(OutBuffer *buf)
877 if (ident)
879 buf->printf("%s $(DDOC_PSYMBOL %s)", kind(), toChars());
880 buf->writestring(";\n");
884 void EnumMember::toDocBuffer(OutBuffer *buf)
886 if (ident)
888 buf->writestring(toChars());
893 /********************************* DocComment *********************************/
895 DocComment::DocComment()
897 memset(this, 0, sizeof(DocComment));
900 DocComment *DocComment::parse(Scope *sc, Dsymbol *s, unsigned char *comment)
901 { unsigned idlen;
903 if (sc->lastdc && isDitto(comment))
904 return NULL;
906 DocComment *dc = new DocComment();
907 if (!comment)
908 return dc;
910 dc->parseSections(comment);
912 for (int i = 0; i < dc->sections.dim; i++)
913 { Section *s = (Section *)dc->sections.data[i];
915 if (icmp("copyright", s->name, s->namelen) == 0)
917 dc->copyright = s;
919 if (icmp("macros", s->name, s->namelen) == 0)
921 dc->macros = s;
925 sc->lastdc = dc;
926 return dc;
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.
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;
945 unsigned char *name = NULL;
946 unsigned namelen = 0;
948 p = comment;
949 while (*p)
951 p = skipwhitespace(p);
952 pstart = p;
954 /* Find end of section, which is ended by one of:
955 * 'identifier:'
956 * '\0'
958 idlen = 0;
959 while (1)
961 if (isalpha(*p) || *p == '_')
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;
973 p = q + 1;
974 break;
977 while (1)
979 if (!*p)
980 { pend = p;
981 goto L1;
983 if (*p == '\n')
984 { p++;
985 if (*p == '\n' && !summary && !namelen)
987 pend = p;
988 p++;
989 goto L1;
991 break;
993 p++;
995 p = skipwhitespace(p);
999 if (namelen || pstart < pend)
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;
1014 //printf("Section: '%.*s' = '%.*s'\n", s->namelen, s->name, s->bodylen, s->body);
1016 sections.push(s);
1018 if (!summary && !namelen)
1019 summary = s;
1022 if (idlen)
1023 { name = idstart;
1024 namelen = idlen;
1026 else
1027 { name = NULL;
1028 namelen = 0;
1029 if (!*p)
1030 break;
1035 void DocComment::writeSections(Scope *sc, Dsymbol *s, OutBuffer *buf)
1037 //printf("DocComment::writeSections()\n");
1038 if (sections.dim)
1040 buf->writestring("$(DDOC_SECTIONS \n");
1041 for (int i = 0; i < sections.dim; i++)
1042 { Section *sec = (Section *)sections.data[i];
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
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");
1058 buf->writestring(")\n");
1060 else
1062 buf->writestring("$(DDOC_BLANKLINE)\n");
1066 /***************************************************
1069 void Section::write(DocComment *dc, Scope *sc, Dsymbol *s, OutBuffer *buf)
1071 if (namelen)
1073 static char *table[] =
1074 { "AUTHORS", "BUGS", "COPYRIGHT", "DATE",
1075 "DEPRECATED", "EXAMPLES", "HISTORY", "LICENSE",
1076 "RETURNS", "SEE_ALSO", "STANDARDS", "THROWS",
1077 "VERSION" };
1079 for (int i = 0; i < sizeof(table) / sizeof(table[0]); i++)
1081 if (icmp(table[i], name, namelen) == 0)
1083 buf->printf("$(DDOC_%s ", table[i]);
1084 goto L1;
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);
1095 buf->writestring(":)\n");
1097 else
1099 buf->writestring("$(DDOC_DESCRIPTION ");
1102 unsigned o = buf->offset;
1103 buf->write(body, bodylen);
1104 highlightText(sc, s, buf, o);
1105 buf->writestring(")\n");
1108 /***************************************************
1111 void ParamSection::write(DocComment *dc, Scope *sc, Dsymbol *s, OutBuffer *buf)
1113 unsigned char *p = body;
1114 unsigned len = bodylen;
1115 unsigned char *pend = p + len;
1117 unsigned char *tempstart;
1118 unsigned templen;
1120 unsigned char *namestart;
1121 unsigned namelen = 0; // !=0 if line continuation
1123 unsigned char *textstart;
1124 unsigned textlen;
1126 unsigned o;
1127 Argument *arg;
1129 buf->writestring("$(DDOC_PARAMS \n");
1130 while (p < pend)
1132 // Skip to start of macro
1133 for (; 1; p++)
1135 switch (*p)
1137 case ' ':
1138 case '\t':
1139 continue;
1141 case '\n':
1142 p++;
1143 goto Lcont;
1145 default:
1146 if (!(isalpha(*p) || *p == '_'))
1148 if (namelen)
1149 goto Ltext; // continuation of prev macro
1150 goto Lskipline;
1152 break;
1154 break;
1156 tempstart = p;
1158 while (isalnum(*p) || *p == '_')
1159 p++;
1160 templen = p - tempstart;
1162 while (*p == ' ' || *p == '\t')
1163 p++;
1165 if (*p != '=')
1166 { if (namelen)
1167 goto Ltext; // continuation of prev macro
1168 goto Lskipline;
1170 p++;
1172 if (namelen)
1173 { // Output existing param
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");
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;
1200 namestart = tempstart;
1201 namelen = templen;
1203 while (*p == ' ' || *p == '\t')
1204 p++;
1205 textstart = p;
1207 Ltext:
1208 while (*p != '\n')
1209 p++;
1210 textlen = p - textstart;
1211 p++;
1213 Lcont:
1214 continue;
1216 Lskipline:
1217 // Ignore this line
1218 while (*p++ != '\n')
1221 if (namelen)
1222 goto L1; // write out last one
1223 buf->writestring(")\n");
1226 /***************************************************
1229 void MacroSection::write(DocComment *dc, Scope *sc, Dsymbol *s, OutBuffer *buf)
1231 //printf("MacroSection::write()\n");
1232 DocComment::parseMacros(dc->pescapetable, dc->pmacrotable, body, bodylen);
1235 /************************************************
1236 * Parse macros out of Macros: section.
1237 * Macros are of the form:
1238 * name1 = value1
1240 * name2 = value2
1243 void DocComment::parseMacros(Escape **pescapetable, Macro **pmacrotable, unsigned char *m, unsigned mlen)
1245 unsigned char *p = m;
1246 unsigned len = mlen;
1247 unsigned char *pend = p + len;
1249 unsigned char *tempstart;
1250 unsigned templen;
1252 unsigned char *namestart;
1253 unsigned namelen = 0; // !=0 if line continuation
1255 unsigned char *textstart;
1256 unsigned textlen;
1258 while (p < pend)
1260 // Skip to start of macro
1261 for (; 1; p++)
1263 if (p >= pend)
1264 goto Ldone;
1265 switch (*p)
1267 case ' ':
1268 case '\t':
1269 continue;
1271 case '\n':
1272 p++;
1273 goto Lcont;
1275 default:
1276 if (!(isalpha(*p) || *p == '_'))
1278 if (namelen)
1279 goto Ltext; // continuation of prev macro
1280 goto Lskipline;
1282 break;
1284 break;
1286 tempstart = p;
1288 while (1)
1290 if (p >= pend)
1291 goto Ldone;
1292 if (!(isalnum(*p) || *p == '_'))
1293 break;
1294 p++;
1296 templen = p - tempstart;
1298 while (1)
1300 if (p >= pend)
1301 goto Ldone;
1302 if (!(*p == ' ' || *p == '\t'))
1303 break;
1304 p++;
1307 if (*p != '=')
1308 { if (namelen)
1309 goto Ltext; // continuation of prev macro
1310 goto Lskipline;
1312 p++;
1313 if (p >= pend)
1314 goto Ldone;
1316 if (namelen)
1317 { // Output existing macro
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;
1329 namestart = tempstart;
1330 namelen = templen;
1332 while (p < pend && (*p == ' ' || *p == '\t'))
1333 p++;
1334 textstart = p;
1336 Ltext:
1337 while (p < pend && *p != '\n')
1338 p++;
1339 textlen = p - textstart;
1341 // Remove trailing \r if there is one
1342 if (p > m && p[-1] == '\r')
1343 textlen--;
1345 p++;
1346 //printf("p = %p, pend = %p\n", p, pend);
1348 Lcont:
1349 continue;
1351 Lskipline:
1352 // Ignore this line
1353 while (p < pend && *p++ != '\n')
1356 Ldone:
1357 if (namelen)
1358 goto L1; // write out last one
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.
1369 void DocComment::parseEscapes(Escape **pescapetable, unsigned char *textstart, unsigned textlen)
1370 { Escape *escapetable = *pescapetable;
1372 if (!escapetable)
1373 { escapetable = new Escape;
1374 *pescapetable = escapetable;
1376 unsigned char *p = textstart;
1377 unsigned char *pend = p + textlen;
1379 while (1)
1381 while (1)
1383 if (p + 4 >= pend)
1384 return;
1385 if (!(*p == ' ' || *p == '\t' || *p == '\n' || *p == ','))
1386 break;
1387 p++;
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)
1396 if (p >= pend)
1397 return;
1398 if (*p == '/')
1399 break;
1400 p++;
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++;
1412 /******************************************
1413 * Compare 0-terminated string with length terminated string.
1414 * Return < 0, ==0, > 0
1417 int cmp(char *stringz, void *s, size_t slen)
1419 size_t len1 = strlen(stringz);
1421 if (len1 != slen)
1422 return len1 - slen;
1423 return memcmp(stringz, s, slen);
1426 int icmp(char *stringz, void *s, size_t slen)
1428 size_t len1 = strlen(stringz);
1430 if (len1 != slen)
1431 return len1 - slen;
1432 return memicmp(stringz, (char *)s, slen);
1435 /*****************************************
1436 * Return !=0 if comment consists entirely of "ditto".
1439 int isDitto(unsigned char *comment)
1441 if (comment)
1443 unsigned char *p = skipwhitespace(comment);
1445 if (memicmp((char *)p, "ditto", 5) == 0 && *skipwhitespace(p + 5) == 0)
1446 return 1;
1448 return 0;
1451 /**********************************************
1452 * Skip white space.
1455 unsigned char *skipwhitespace(unsigned char *p)
1457 for (; 1; p++)
1458 { switch (*p)
1460 case ' ':
1461 case '\t':
1462 case '\n':
1463 continue;
1465 break;
1467 return p;
1471 /************************************************
1472 * Scan forward to one of:
1473 * start of identifier
1474 * beginning of next line
1475 * end of buf
1478 unsigned skiptoident(OutBuffer *buf, unsigned i)
1480 for (; i < buf->offset; i++)
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;
1489 return i;
1492 /************************************************
1493 * Scan forward past end of identifier.
1496 unsigned skippastident(OutBuffer *buf, unsigned i)
1498 for (; i < buf->offset; i++)
1500 // BUG: handle unicode alpha's
1501 unsigned char c = buf->data[i];
1502 if (!(isalnum(c) || c == '_'))
1503 break;
1505 return i;
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
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;
1523 if (length > 7 && memicmp((char *)p, "http://", 7) == 0)
1525 j = 7;
1527 else if (length > 8 && memicmp((char *)p, "https://", 8) == 0)
1529 j = 8;
1531 else
1532 goto Lno;
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 == '.')
1545 sawdot = 1;
1546 continue;
1548 break;
1550 if (sawdot)
1551 return i + j;
1553 Lno:
1554 return i;
1558 /****************************************************
1561 int isKeyword(unsigned char *p, unsigned len)
1563 static char *table[] = { "true", "false", "null" };
1565 for (int i = 0; i < sizeof(table) / sizeof(table[0]); i++)
1567 if (cmp(table[i], p, len) == 0)
1568 return 1;
1570 return 0;
1573 /****************************************************
1576 Argument *isFunctionParameter(Dsymbol *s, unsigned char *p, unsigned len)
1578 FuncDeclaration *f = s->isFuncDeclaration();
1580 /* f->type may be NULL for template members.
1582 if (f && f->type)
1584 TypeFunction *tf = (TypeFunction *)f->type;
1586 if (tf->parameters)
1588 for (size_t k = 0; k < tf->parameters->dim; k++)
1589 { Argument *arg = (Argument *)tf->parameters->data[k];
1591 if (arg->ident && cmp(arg->ident->toChars(), p, len) == 0)
1593 return arg;
1598 return NULL;
1601 /**************************************************
1602 * Highlight text section.
1605 void highlightText(Scope *sc, Dsymbol *s, OutBuffer *buf, unsigned offset)
1607 //printf("highlightText()\n");
1608 char *sid = s->ident->toChars();
1609 FuncDeclaration *f = s->isFuncDeclaration();
1610 unsigned char *p;
1611 char *se;
1613 int leadingBlank = 1;
1614 int inCode = 0;
1615 int inComment = 0; // in <!-- ... --> comment
1616 unsigned iCodeStart; // start of code section
1618 unsigned iLineStart = offset;
1620 for (unsigned i = offset; i < buf->offset; i++)
1621 { unsigned char c = buf->data[i];
1623 Lcont:
1624 switch (c)
1626 case ' ':
1627 case '\t':
1628 break;
1630 case '\n':
1631 if (sc && !inCode && i == iLineStart && i + 1 < buf->offset) // if "\n\n"
1633 static char blankline[] = "$(DDOC_BLANKLINE)\n";
1635 i = buf->insert(i, blankline, sizeof(blankline) - 1);
1637 leadingBlank = 1;
1638 iLineStart = i + 1;
1639 break;
1641 case '<':
1642 leadingBlank = 0;
1643 if (inCode)
1644 break;
1645 p = &buf->data[i];
1647 // Skip over comments
1648 if (p[1] == '!' && p[2] == '-' && p[3] == '-')
1649 { unsigned j = i + 4;
1650 p += 4;
1651 while (1)
1653 if (j == buf->offset)
1654 goto L1;
1655 if (p[0] == '-' && p[1] == '-' && p[2] == '>')
1657 i = j + 2; // place on closing '>'
1658 break;
1660 j++;
1661 p++;
1663 break;
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)
1672 if (j == buf->offset)
1673 goto L1;
1674 if (p[0] == '>')
1676 i = j; // place on closing '>'
1677 break;
1679 j++;
1680 p++;
1682 break;
1686 // Replace '<' with '&lt;' 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 ';'
1694 break;
1696 case '>':
1697 leadingBlank = 0;
1698 if (inCode)
1699 break;
1700 // Replace '>' with '&gt;' 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 ';'
1708 break;
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 '&amp;' 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 ';'
1725 break;
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.
1731 if (leadingBlank)
1732 { int istart = i;
1733 int eollen = 0;
1735 leadingBlank = 0;
1736 while (1)
1738 ++i;
1739 if (i >= buf->offset)
1740 break;
1741 c = buf->data[i];
1742 if (c == '\n')
1743 { eollen = 1;
1744 break;
1746 if (c == '\r')
1748 eollen = 1;
1749 if (i + 1 >= buf->offset)
1750 break;
1751 if (buf->data[i + 1] == '\n')
1752 { eollen = 2;
1753 break;
1756 // BUG: handle UTF PS and LS too
1757 if (c != '-')
1758 goto Lcont;
1760 if (i - istart < 3)
1761 goto Lcont;
1763 // We have the start/end of a code section
1765 // Remove the entire --- line, including blanks and \n
1766 buf->remove(iLineStart, i - iLineStart + eollen);
1767 i = iLineStart;
1769 if (inCode)
1771 inCode = 0;
1772 // The code section is from iCodeStart to i
1773 OutBuffer codebuf;
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--;
1783 else
1784 { static char pre[] = "$(D_CODE \n";
1786 inCode = 1;
1787 i = buf->insert(i, pre, sizeof(pre) - 1);
1788 iCodeStart = i;
1789 i--; // place i on >
1792 break;
1794 default:
1795 leadingBlank = 0;
1796 if (sc && !inCode && (isalpha(c) || c == '_'))
1797 { unsigned j;
1799 j = skippastident(buf, i);
1800 if (j > i)
1802 unsigned k = skippastURL(buf, i);
1803 if (k > i)
1804 { i = k - 1;
1805 break;
1808 if (buf->data[i] == '_') // leading '_' means no highlight
1810 buf->remove(i, 1);
1811 i = j - 1;
1813 else
1815 if (cmp(sid, buf->data + i, j - i) == 0)
1817 i = buf->bracket(i, "$(DDOC_PSYMBOL ", j, ")") - 1;
1818 break;
1820 else if (isKeyword(buf->data + i, j - i))
1822 i = buf->bracket(i, "$(DDOC_KEYWORD ", j, ")") - 1;
1823 break;
1825 else
1827 if (f && isFunctionParameter(f, buf->data + i, j - i))
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;
1834 i = j - 1;
1838 break;
1841 Ldone:
1845 /**************************************************
1846 * Highlight code for DDOC section.
1849 void highlightCode(Scope *sc, Dsymbol *s, OutBuffer *buf, unsigned offset)
1851 char *sid = s->ident->toChars();
1852 FuncDeclaration *f = s->isFuncDeclaration();
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;
1859 se = Escape::escapeChar(c);
1860 if (se)
1862 size_t len = strlen(se);
1863 buf->remove(i, 1);
1864 i = buf->insert(i, se, len);
1865 i--; // point to ';'
1867 else if (isalpha(c) || c == '_')
1868 { unsigned j;
1870 j = skippastident(buf, i);
1871 if (j > i)
1873 if (cmp(sid, buf->data + i, j - i) == 0)
1875 i = buf->bracket(i, "$(DDOC_PSYMBOL ", j, ")") - 1;
1876 continue;
1878 else if (f)
1880 if (isFunctionParameter(f, buf->data + i, j - i))
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;
1887 i = j - 1;
1893 /****************************************
1896 void highlightCode3(OutBuffer *buf, unsigned char *p, unsigned char *pend)
1898 for (; p < pend; p++)
1899 { char *s = Escape::escapeChar(*p);
1900 if (s)
1901 buf->writestring(s);
1902 else
1903 buf->writeByte(*p);
1907 /**************************************************
1908 * Highlight code for CODE section.
1912 void highlightCode2(Scope *sc, Dsymbol *s, OutBuffer *buf, unsigned offset)
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;
1923 //printf("highlightCode2('%.*s')\n", buf->offset - 1, buf->data);
1924 res.reserve(buf->offset);
1925 while (1)
1927 lex.scan(&tok);
1928 highlightCode3(&res, lastp, tok.ptr);
1929 highlight = NULL;
1930 switch (tok.value)
1932 case TOKidentifier:
1933 if (!sc)
1934 break;
1935 if (cmp(sid, tok.ptr, lex.p - tok.ptr) == 0)
1937 highlight = "$(D_PSYMBOL ";
1938 break;
1940 else if (f)
1942 if (isFunctionParameter(f, tok.ptr, lex.p - tok.ptr))
1944 //printf("highlighting arg '%s', i = %d, j = %d\n", arg->ident->toChars(), i, j);
1945 highlight = "$(D_PARAM ";
1946 break;
1949 break;
1951 case TOKcomment:
1952 highlight = "$(D_COMMENT ";
1953 break;
1955 case TOKstring:
1956 highlight = "$(D_STRING ";
1957 break;
1959 default:
1960 if (tok.isKeyword())
1961 highlight = "$(D_KEYWORD ";
1962 break;
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;
1973 buf->setsize(offset);
1974 buf->write(&res);
1975 global.errors = errorsave;
1978 /***************************************
1979 * Find character string to replace c with.
1982 char *Escape::escapeChar(unsigned c)
1983 { char *s;
1985 switch (c)
1987 case '<':
1988 s = "&lt;";
1989 break;
1990 case '>':
1991 s = "&gt;";
1992 break;
1993 case '&':
1994 s = "&amp;";
1995 break;
1996 default:
1997 s = NULL;
1998 break;
2000 return s;