106
|
1 module dmd.ddoc.DocComment;
|
|
2
|
152
|
3 import dmd.ddoc.Escape;
|
|
4 import dmd.ddoc.Macro;
|
106
|
5 import dmd.ddoc.Sections;
|
|
6 import dmd.Array;
|
|
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
|
152
|
31 // writef("parse(%s): '%s'\n", s.toChars(), comment);
|
106
|
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
|
152
|
57 /************************************************
|
|
58 * Parse macros out of Macros: section.
|
|
59 * Macros are of the form:
|
|
60 * name1 = value1
|
|
61 *
|
|
62 * name2 = value2
|
|
63 */
|
106
|
64 static void parseMacros(Escape** pescapetable, Macro** pmacrotable, ubyte* m, uint mlen)
|
|
65 {
|
|
66 assert(false);
|
|
67 }
|
|
68
|
152
|
69 /**************************************
|
|
70 * Parse escapes of the form:
|
|
71 * /c/string/
|
|
72 * where c is a single character.
|
|
73 * Multiple escapes can be separated
|
|
74 * by whitespace and/or commas.
|
|
75 */
|
106
|
76 static void parseEscapes(Escape** pescapetable, ubyte* textstart, uint textlen)
|
|
77 {
|
|
78 assert(false);
|
|
79 }
|
|
80 /*****************************************
|
|
81 * Parse next paragraph out of *pcomment.
|
|
82 * Update *pcomment to point past paragraph.
|
|
83 * Returns null if no more paragraphs.
|
|
84 * If paragraph ends in 'identifier:',
|
|
85 * then (*pcomment)[0 .. idlen] is the identifier.
|
|
86 */
|
152
|
87 void parseSections(string comment)
|
106
|
88 {
|
|
89 ubyte*p;
|
|
90 ubyte*pstart;
|
|
91 ubyte*pend;
|
|
92 ubyte*idstart;
|
|
93 unsigned idlen;
|
|
94
|
|
95 ubyte*name = null;
|
|
96 unsigned namelen = 0;
|
|
97
|
|
98 //printf("parseSections('%s')\n", comment);
|
|
99 p = comment;
|
|
100 while (*p)
|
|
101 {
|
|
102 p = skipwhitespace(p);
|
|
103 pstart = p;
|
|
104
|
|
105 /* Find end of section, which is ended by one of:
|
|
106 * 'identifier:' (but not inside a code section)
|
|
107 * '\0'
|
|
108 */
|
|
109 idlen = 0;
|
|
110 int inCode = 0;
|
|
111 while (1)
|
|
112 {
|
|
113 // Check for start/end of a code section
|
|
114 if (*p == '-')
|
|
115 {
|
|
116 int numdash = 0;
|
|
117 while (*p == '-')
|
|
118 {
|
|
119 ++numdash;
|
|
120 p++;
|
|
121 }
|
|
122 // BUG: handle UTF PS and LS too
|
|
123 if (!*p || *p == '\r' || *p == '\n' && numdash >= 3)
|
|
124 inCode ^= 1;
|
|
125 }
|
|
126
|
|
127 if (!inCode && isIdStart(p))
|
|
128 {
|
|
129 ubyte*q = p + utfStride(p);
|
|
130 while (isIdTail(q))
|
|
131 q += utfStride(q);
|
|
132 if (*q == ':') // identifier: ends it
|
|
133 {
|
|
134 idlen = q - p;
|
|
135 idstart = p;
|
|
136 for (pend = p; pend > pstart; pend--)
|
|
137 {
|
|
138 if (pend[-1] == '\n')
|
|
139 break;
|
|
140 }
|
|
141 p = q + 1;
|
|
142 break;
|
|
143 }
|
|
144 }
|
|
145 while (1)
|
|
146 {
|
|
147 if (!*p)
|
|
148 { pend = p;
|
|
149 goto L1;
|
|
150 }
|
|
151 if (*p == '\n')
|
|
152 { p++;
|
|
153 if (*p == '\n' && !summary && !namelen)
|
|
154 {
|
|
155 pend = p;
|
|
156 p++;
|
|
157 goto L1;
|
|
158 }
|
|
159 break;
|
|
160 }
|
|
161 p++;
|
|
162 }
|
|
163 p = skipwhitespace(p);
|
|
164 }
|
|
165 L1:
|
|
166
|
|
167 if (namelen || pstart < pend)
|
|
168 {
|
|
169 Section *s;
|
|
170 if (icmp("Params", name, namelen) == 0)
|
|
171 s = new ParamSection();
|
|
172 else if (icmp("Macros", name, namelen) == 0)
|
|
173 s = new MacroSection();
|
|
174 else
|
|
175 s = new Section();
|
|
176 s.name = name;
|
|
177 s.namelen = namelen;
|
|
178 s.body_ = pstart;
|
|
179 s.bodylen = pend - pstart;
|
|
180 s.nooutput = 0;
|
|
181
|
|
182 //printf("Section: '%.*s' = '%.*s'\n", s.namelen, s.name, s.body_len, s.body);
|
|
183
|
|
184 sections.push(s);
|
|
185
|
|
186 if (!summary && !namelen)
|
|
187 summary = s;
|
|
188 }
|
|
189
|
|
190 if (idlen)
|
|
191 { name = idstart;
|
|
192 namelen = idlen;
|
|
193 }
|
|
194 else
|
|
195 { name = null;
|
|
196 namelen = 0;
|
|
197 if (!*p)
|
|
198 break;
|
|
199 }
|
|
200 }
|
|
201 }
|
|
202
|
|
203 void writeSections(Scope sc, Dsymbol s, OutBuffer buf)
|
|
204 {
|
|
205 //printf("DocComment.writeSections()\n");
|
|
206 if (sections.dim)
|
|
207 {
|
|
208 buf.writestring("$(DDOC_SECTIONS \n");
|
|
209 for (int i = 0; i < sections.dim; i++)
|
|
210 {
|
|
211 Section sec = cast(Section)sections.data[i];
|
|
212
|
|
213 if (sec.nooutput)
|
|
214 continue;
|
|
215 //printf("Section: '%.*s' = '%.*s'\n", sec.namelen, sec.name, sec.bodylen, sec.body);
|
|
216 if (sec.namelen || i)
|
|
217 sec.write(this, sc, s, buf);
|
|
218 else
|
|
219 {
|
|
220 buf.writestring("$(DDOC_SUMMARY ");
|
|
221 unsigned o = buf.offset;
|
|
222 buf.write(sec.body_, sec.bodylen);
|
|
223 highlightText(sc, s, buf, o);
|
|
224 buf.writestring(")\n");
|
|
225 }
|
|
226 }
|
|
227 buf.writestring(")\n");
|
|
228 }
|
|
229 else
|
|
230 {
|
|
231 buf.writestring("$(DDOC_BLANKLINE)\n");
|
|
232 }
|
|
233 }
|
|
234 } |