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