106
|
1 module dmd.ddoc.DocComment;
|
|
2
|
|
3 import dmd.ddoc.Sections;
|
|
4 import dmd.Array;
|
|
5 import dmd.Macro;
|
|
6 import dmd.Escape;
|
|
7 import dmd.Scope;
|
|
8 import dmd.Dsymbol;
|
|
9 import dmd.OutBuffer;
|
|
10
|
|
11 //!
|
|
12 class DocComment
|
|
13 {
|
|
14 Sections sections; // Section*[]
|
|
15
|
|
16 Section summary;
|
|
17 Section copyright;
|
|
18 Section macros;
|
|
19 Macro** pmacrotable;
|
|
20 Escape** pescapetable;
|
|
21
|
|
22 this()
|
|
23 {
|
|
24 // TODO: memset(this, 0, sizeof(DocComment));
|
|
25 }
|
|
26
|
|
27 static DocComment parse(Scope sc, Dsymbol s, string comment)
|
|
28 {
|
|
29 unsigned idlen;
|
|
30
|
|
31 //printf("parse(%s): '%s'\n", s.toChars(), comment);
|
|
32 if (sc.lastdc && isDitto(comment))
|
|
33 return null;
|
|
34
|
|
35 DocComment dc = new DocComment();
|
|
36 if (!comment)
|
|
37 return dc;
|
|
38
|
|
39 dc.parseSections(comment);
|
|
40
|
|
41 foreach (Section s; dc.sections)
|
|
42 {
|
|
43 if (icmp("copyright", s.name, s.namelen) == 0)
|
|
44 {
|
|
45 dc.copyright = s;
|
|
46 }
|
|
47 if (icmp("macros", s.name, s.namelen) == 0)
|
|
48 {
|
|
49 dc.macros = s;
|
|
50 }
|
|
51 }
|
|
52
|
|
53 sc.lastdc = dc;
|
|
54 return dc;
|
|
55 }
|
|
56
|
|
57 static void parseMacros(Escape** pescapetable, Macro** pmacrotable, ubyte* m, uint mlen)
|
|
58 {
|
|
59 assert(false);
|
|
60 }
|
|
61
|
|
62 static void parseEscapes(Escape** pescapetable, ubyte* textstart, uint textlen)
|
|
63 {
|
|
64 assert(false);
|
|
65 }
|
|
66 /*****************************************
|
|
67 * Parse next paragraph out of *pcomment.
|
|
68 * Update *pcomment to point past paragraph.
|
|
69 * Returns null if no more paragraphs.
|
|
70 * If paragraph ends in 'identifier:',
|
|
71 * then (*pcomment)[0 .. idlen] is the identifier.
|
|
72 */
|
|
73 void parseSections(ubyte* comment)
|
|
74 {
|
|
75 ubyte*p;
|
|
76 ubyte*pstart;
|
|
77 ubyte*pend;
|
|
78 ubyte*idstart;
|
|
79 unsigned idlen;
|
|
80
|
|
81 ubyte*name = null;
|
|
82 unsigned namelen = 0;
|
|
83
|
|
84 //printf("parseSections('%s')\n", comment);
|
|
85 p = comment;
|
|
86 while (*p)
|
|
87 {
|
|
88 p = skipwhitespace(p);
|
|
89 pstart = p;
|
|
90
|
|
91 /* Find end of section, which is ended by one of:
|
|
92 * 'identifier:' (but not inside a code section)
|
|
93 * '\0'
|
|
94 */
|
|
95 idlen = 0;
|
|
96 int inCode = 0;
|
|
97 while (1)
|
|
98 {
|
|
99 // Check for start/end of a code section
|
|
100 if (*p == '-')
|
|
101 {
|
|
102 int numdash = 0;
|
|
103 while (*p == '-')
|
|
104 {
|
|
105 ++numdash;
|
|
106 p++;
|
|
107 }
|
|
108 // BUG: handle UTF PS and LS too
|
|
109 if (!*p || *p == '\r' || *p == '\n' && numdash >= 3)
|
|
110 inCode ^= 1;
|
|
111 }
|
|
112
|
|
113 if (!inCode && isIdStart(p))
|
|
114 {
|
|
115 ubyte*q = p + utfStride(p);
|
|
116 while (isIdTail(q))
|
|
117 q += utfStride(q);
|
|
118 if (*q == ':') // identifier: ends it
|
|
119 {
|
|
120 idlen = q - p;
|
|
121 idstart = p;
|
|
122 for (pend = p; pend > pstart; pend--)
|
|
123 {
|
|
124 if (pend[-1] == '\n')
|
|
125 break;
|
|
126 }
|
|
127 p = q + 1;
|
|
128 break;
|
|
129 }
|
|
130 }
|
|
131 while (1)
|
|
132 {
|
|
133 if (!*p)
|
|
134 { pend = p;
|
|
135 goto L1;
|
|
136 }
|
|
137 if (*p == '\n')
|
|
138 { p++;
|
|
139 if (*p == '\n' && !summary && !namelen)
|
|
140 {
|
|
141 pend = p;
|
|
142 p++;
|
|
143 goto L1;
|
|
144 }
|
|
145 break;
|
|
146 }
|
|
147 p++;
|
|
148 }
|
|
149 p = skipwhitespace(p);
|
|
150 }
|
|
151 L1:
|
|
152
|
|
153 if (namelen || pstart < pend)
|
|
154 {
|
|
155 Section *s;
|
|
156 if (icmp("Params", name, namelen) == 0)
|
|
157 s = new ParamSection();
|
|
158 else if (icmp("Macros", name, namelen) == 0)
|
|
159 s = new MacroSection();
|
|
160 else
|
|
161 s = new Section();
|
|
162 s.name = name;
|
|
163 s.namelen = namelen;
|
|
164 s.body_ = pstart;
|
|
165 s.bodylen = pend - pstart;
|
|
166 s.nooutput = 0;
|
|
167
|
|
168 //printf("Section: '%.*s' = '%.*s'\n", s.namelen, s.name, s.body_len, s.body);
|
|
169
|
|
170 sections.push(s);
|
|
171
|
|
172 if (!summary && !namelen)
|
|
173 summary = s;
|
|
174 }
|
|
175
|
|
176 if (idlen)
|
|
177 { name = idstart;
|
|
178 namelen = idlen;
|
|
179 }
|
|
180 else
|
|
181 { name = null;
|
|
182 namelen = 0;
|
|
183 if (!*p)
|
|
184 break;
|
|
185 }
|
|
186 }
|
|
187 }
|
|
188
|
|
189 void writeSections(Scope sc, Dsymbol s, OutBuffer buf)
|
|
190 {
|
|
191 //printf("DocComment.writeSections()\n");
|
|
192 if (sections.dim)
|
|
193 {
|
|
194 buf.writestring("$(DDOC_SECTIONS \n");
|
|
195 for (int i = 0; i < sections.dim; i++)
|
|
196 {
|
|
197 Section sec = cast(Section)sections.data[i];
|
|
198
|
|
199 if (sec.nooutput)
|
|
200 continue;
|
|
201 //printf("Section: '%.*s' = '%.*s'\n", sec.namelen, sec.name, sec.bodylen, sec.body);
|
|
202 if (sec.namelen || i)
|
|
203 sec.write(this, sc, s, buf);
|
|
204 else
|
|
205 {
|
|
206 buf.writestring("$(DDOC_SUMMARY ");
|
|
207 unsigned o = buf.offset;
|
|
208 buf.write(sec.body_, sec.bodylen);
|
|
209 highlightText(sc, s, buf, o);
|
|
210 buf.writestring(")\n");
|
|
211 }
|
|
212 }
|
|
213 buf.writestring(")\n");
|
|
214 }
|
|
215 else
|
|
216 {
|
|
217 buf.writestring("$(DDOC_BLANKLINE)\n");
|
|
218 }
|
|
219 }
|
|
220 } |