annotate trunk/src/dil/doc/Doc.d @ 728:41cad5ca4863

Added ParamsSection and MacrosSection.
author Aziz K?ksal <aziz.koeksal@gmail.com>
date Sat, 02 Feb 2008 23:17:14 +0100
parents ceaac6a24258
children ca7607226caa
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
676
c4e3a34e40f1 Added new module dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
1 /++
c4e3a34e40f1 Added new module dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
2 Author: Aziz Köksal
c4e3a34e40f1 Added new module dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
3 License: GPL3
c4e3a34e40f1 Added new module dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
4 +/
c4e3a34e40f1 Added new module dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
5 module dil.doc.Doc;
c4e3a34e40f1 Added new module dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
6
728
41cad5ca4863 Added ParamsSection and MacrosSection.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 722
diff changeset
7 import dil.doc.Parser;
676
c4e3a34e40f1 Added new module dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
8 import dil.ast.Node;
712
f8875ef9a66d Added function sanitize() to dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 680
diff changeset
9 import dil.lexer.Funcs;
716
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
10 import dil.Unicode;
712
f8875ef9a66d Added function sanitize() to dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 680
diff changeset
11 import common;
676
c4e3a34e40f1 Added new module dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
12
716
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
13 class DDocComment
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
14 {
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
15 Section[] sections;
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
16 Section summary; /// Optional summary section.
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
17 Section description; /// Optional description section.
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
18
722
ceaac6a24258 Added isUnicodeAlpha() for DDocParser and MacroParser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 718
diff changeset
19 this(Section[] sections, Section summary, Section description)
716
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
20 {
722
ceaac6a24258 Added isUnicodeAlpha() for DDocParser and MacroParser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 718
diff changeset
21 this.sections = sections;
ceaac6a24258 Added isUnicodeAlpha() for DDocParser and MacroParser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 718
diff changeset
22 this.summary = summary;
ceaac6a24258 Added isUnicodeAlpha() for DDocParser and MacroParser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 718
diff changeset
23 this.description = description;
716
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
24 }
722
ceaac6a24258 Added isUnicodeAlpha() for DDocParser and MacroParser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 718
diff changeset
25 }
ceaac6a24258 Added isUnicodeAlpha() for DDocParser and MacroParser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 718
diff changeset
26
ceaac6a24258 Added isUnicodeAlpha() for DDocParser and MacroParser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 718
diff changeset
27 struct DDocParser
ceaac6a24258 Added isUnicodeAlpha() for DDocParser and MacroParser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 718
diff changeset
28 {
ceaac6a24258 Added isUnicodeAlpha() for DDocParser and MacroParser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 718
diff changeset
29 char* p;
ceaac6a24258 Added isUnicodeAlpha() for DDocParser and MacroParser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 718
diff changeset
30 char* textEnd;
ceaac6a24258 Added isUnicodeAlpha() for DDocParser and MacroParser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 718
diff changeset
31 Section[] sections;
ceaac6a24258 Added isUnicodeAlpha() for DDocParser and MacroParser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 718
diff changeset
32 Section summary; /// Optional summary section.
ceaac6a24258 Added isUnicodeAlpha() for DDocParser and MacroParser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 718
diff changeset
33 Section description; /// Optional description section.
716
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
34
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
35 /// Parses the DDoc text into sections.
722
ceaac6a24258 Added isUnicodeAlpha() for DDocParser and MacroParser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 718
diff changeset
36 Section[] parse(string text)
716
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
37 {
722
ceaac6a24258 Added isUnicodeAlpha() for DDocParser and MacroParser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 718
diff changeset
38 if (!text.length)
ceaac6a24258 Added isUnicodeAlpha() for DDocParser and MacroParser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 718
diff changeset
39 return null;
ceaac6a24258 Added isUnicodeAlpha() for DDocParser and MacroParser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 718
diff changeset
40 if (text[$-1] != '\0')
ceaac6a24258 Added isUnicodeAlpha() for DDocParser and MacroParser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 718
diff changeset
41 text ~= '\0';
ceaac6a24258 Added isUnicodeAlpha() for DDocParser and MacroParser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 718
diff changeset
42 p = text.ptr;
ceaac6a24258 Added isUnicodeAlpha() for DDocParser and MacroParser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 718
diff changeset
43 textEnd = p + text.length;
ceaac6a24258 Added isUnicodeAlpha() for DDocParser and MacroParser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 718
diff changeset
44
716
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
45 char* summaryBegin;
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
46 char* idBegin, idEnd;
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
47 char* nextIdBegin, nextIdEnd;
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
48
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
49 skipWhitespace(p);
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
50 summaryBegin = p;
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
51
722
ceaac6a24258 Added isUnicodeAlpha() for DDocParser and MacroParser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 718
diff changeset
52 if (findNextIdColon(idBegin, idEnd))
716
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
53 { // Check that this is not an explicit section.
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
54 if (summaryBegin != idBegin)
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
55 scanSummaryAndDescription(summaryBegin, idBegin);
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
56 }
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
57 else // There are no explicit sections.
722
ceaac6a24258 Added isUnicodeAlpha() for DDocParser and MacroParser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 718
diff changeset
58 {
ceaac6a24258 Added isUnicodeAlpha() for DDocParser and MacroParser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 718
diff changeset
59 scanSummaryAndDescription(summaryBegin, textEnd);
ceaac6a24258 Added isUnicodeAlpha() for DDocParser and MacroParser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 718
diff changeset
60 return null;
ceaac6a24258 Added isUnicodeAlpha() for DDocParser and MacroParser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 718
diff changeset
61 }
716
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
62
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
63 assert(idBegin && idEnd);
722
ceaac6a24258 Added isUnicodeAlpha() for DDocParser and MacroParser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 718
diff changeset
64 while (findNextIdColon(nextIdBegin, nextIdEnd))
716
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
65 {
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
66 sections ~= new Section(makeString(idBegin, idEnd), makeString(idEnd+1, nextIdBegin));
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
67 idBegin = nextIdBegin;
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
68 idEnd = nextIdEnd;
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
69 }
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
70 // Add last section.
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
71 sections ~= new Section(makeString(idBegin, idEnd), makeString(idEnd+1, textEnd));
722
ceaac6a24258 Added isUnicodeAlpha() for DDocParser and MacroParser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 718
diff changeset
72 return sections;
716
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
73 }
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
74
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
75 void scanSummaryAndDescription(char* p, char* end)
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
76 {
722
ceaac6a24258 Added isUnicodeAlpha() for DDocParser and MacroParser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 718
diff changeset
77 assert(p < end);
716
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
78 char* sectionBegin = p;
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
79 // Search for the end of the first paragraph.
722
ceaac6a24258 Added isUnicodeAlpha() for DDocParser and MacroParser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 718
diff changeset
80 while (p < end && !(*p == '\n' && p[1] == '\n'))
716
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
81 p++;
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
82 // The first paragraph is the summary.
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
83 summary = new Section("", makeString(sectionBegin, p));
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
84 sections ~= summary;
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
85 // The rest is the description section.
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
86 if (p != end)
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
87 {
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
88 sectionBegin = p;
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
89 skipWhitespace(p);
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
90 if (p < end)
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
91 {
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
92 description = new Section("", makeString(sectionBegin, end));
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
93 sections ~= description;
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
94 }
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
95 }
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
96 }
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
97
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
98 void skipWhitespace(ref char* p)
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
99 {
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
100 while (isspace(*p) || *p == '\n')
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
101 p++;
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
102 }
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
103
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
104 /// Find next "Identifier:".
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
105 /// Params:
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
106 /// p = current character pointer
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
107 /// idBegin = set to the first character of the Identifier
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
108 /// idEnd = set to the colon following the Identifier
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
109 /// Returns: true if found
722
ceaac6a24258 Added isUnicodeAlpha() for DDocParser and MacroParser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 718
diff changeset
110 bool findNextIdColon(ref char* ref_idBegin, ref char* ref_idEnd)
716
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
111 {
722
ceaac6a24258 Added isUnicodeAlpha() for DDocParser and MacroParser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 718
diff changeset
112 auto p = this.p;
716
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
113 while (*p != '\0')
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
114 {
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
115 auto idBegin = p;
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
116 assert(isascii(*p) || isLeadByte(*p));
722
ceaac6a24258 Added isUnicodeAlpha() for DDocParser and MacroParser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 718
diff changeset
117 if (isidbeg(*p) || isUnicodeAlpha(p, textEnd)) // IdStart
716
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
118 {
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
119 do // IdChar*
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
120 p++;
722
ceaac6a24258 Added isUnicodeAlpha() for DDocParser and MacroParser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 718
diff changeset
121 while (isident(*p) || isUnicodeAlpha(p, textEnd))
716
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
122 if (*p == ':') // :
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
123 {
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
124 ref_idBegin = idBegin;
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
125 ref_idEnd = p;
722
ceaac6a24258 Added isUnicodeAlpha() for DDocParser and MacroParser.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 718
diff changeset
126 this.p = p;
716
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
127 return true;
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
128 }
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
129 }
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
130 else if (!isascii(*p))
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
131 { // Skip UTF-8 sequences.
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
132 while (!isascii(*++p))
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
133 {}
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
134 continue;
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
135 }
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
136 p++;
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
137 }
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
138 return false;
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
139 }
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
140 }
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
141
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
142 class Section
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
143 {
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
144 string name;
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
145 string text;
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
146 this(string name, string text)
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
147 {
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
148 this.name = name;
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
149 this.text = text;
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
150 }
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
151 }
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
152
728
41cad5ca4863 Added ParamsSection and MacrosSection.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 722
diff changeset
153 class ParamsSection : Section
716
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
154 {
728
41cad5ca4863 Added ParamsSection and MacrosSection.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 722
diff changeset
155 string[] paramNames; /// Parameter names.
41cad5ca4863 Added ParamsSection and MacrosSection.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 722
diff changeset
156 string[] paramDescs; /// Parameter descriptions.
41cad5ca4863 Added ParamsSection and MacrosSection.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 722
diff changeset
157 this(string name, string text)
41cad5ca4863 Added ParamsSection and MacrosSection.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 722
diff changeset
158 {
41cad5ca4863 Added ParamsSection and MacrosSection.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 722
diff changeset
159 super(name, text);
41cad5ca4863 Added ParamsSection and MacrosSection.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 722
diff changeset
160 IdentValueParser parser;
41cad5ca4863 Added ParamsSection and MacrosSection.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 722
diff changeset
161 auto idvalues = parser.parse(text);
41cad5ca4863 Added ParamsSection and MacrosSection.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 722
diff changeset
162 this.paramNames = new string[idvalues.length];
41cad5ca4863 Added ParamsSection and MacrosSection.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 722
diff changeset
163 this.paramDescs = new string[idvalues.length];
41cad5ca4863 Added ParamsSection and MacrosSection.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 722
diff changeset
164 foreach (i, idvalue; idvalues)
41cad5ca4863 Added ParamsSection and MacrosSection.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 722
diff changeset
165 {
41cad5ca4863 Added ParamsSection and MacrosSection.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 722
diff changeset
166 this.paramNames[i] = idvalue.ident;
41cad5ca4863 Added ParamsSection and MacrosSection.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 722
diff changeset
167 this.paramDescs[i] = idvalue.value;
41cad5ca4863 Added ParamsSection and MacrosSection.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 722
diff changeset
168 }
41cad5ca4863 Added ParamsSection and MacrosSection.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 722
diff changeset
169 }
41cad5ca4863 Added ParamsSection and MacrosSection.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 722
diff changeset
170 }
41cad5ca4863 Added ParamsSection and MacrosSection.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 722
diff changeset
171
41cad5ca4863 Added ParamsSection and MacrosSection.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 722
diff changeset
172 class MacrosSection : Section
41cad5ca4863 Added ParamsSection and MacrosSection.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 722
diff changeset
173 {
41cad5ca4863 Added ParamsSection and MacrosSection.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 722
diff changeset
174 string[] macroNames; /// Parameter names.
41cad5ca4863 Added ParamsSection and MacrosSection.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 722
diff changeset
175 string[] macroTexts; /// Parameter descriptions.
41cad5ca4863 Added ParamsSection and MacrosSection.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 722
diff changeset
176 this(string name, string text)
41cad5ca4863 Added ParamsSection and MacrosSection.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 722
diff changeset
177 {
41cad5ca4863 Added ParamsSection and MacrosSection.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 722
diff changeset
178 super(name, text);
41cad5ca4863 Added ParamsSection and MacrosSection.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 722
diff changeset
179 IdentValueParser parser;
41cad5ca4863 Added ParamsSection and MacrosSection.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 722
diff changeset
180 auto idvalues = parser.parse(text);
41cad5ca4863 Added ParamsSection and MacrosSection.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 722
diff changeset
181 this.macroNames = new string[idvalues.length];
41cad5ca4863 Added ParamsSection and MacrosSection.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 722
diff changeset
182 this.macroTexts = new string[idvalues.length];
41cad5ca4863 Added ParamsSection and MacrosSection.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 722
diff changeset
183 foreach (i, idvalue; idvalues)
41cad5ca4863 Added ParamsSection and MacrosSection.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 722
diff changeset
184 {
41cad5ca4863 Added ParamsSection and MacrosSection.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 722
diff changeset
185 this.macroNames[i] = idvalue.ident;
41cad5ca4863 Added ParamsSection and MacrosSection.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 722
diff changeset
186 this.macroTexts[i] = idvalue.value;
41cad5ca4863 Added ParamsSection and MacrosSection.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 722
diff changeset
187 }
41cad5ca4863 Added ParamsSection and MacrosSection.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 722
diff changeset
188 }
716
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
189 }
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
190
676
c4e3a34e40f1 Added new module dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
191 bool isDoxygenComment(Token* token)
c4e3a34e40f1 Added new module dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
192 { // Doxygen: '/+!' '/*!' '//!'
679
ff6971637f88 Renamed Token member type to kind.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 676
diff changeset
193 return token.kind == TOK.Comment && token.start[2] == '!';
676
c4e3a34e40f1 Added new module dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
194 }
c4e3a34e40f1 Added new module dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
195
c4e3a34e40f1 Added new module dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
196 bool isDDocComment(Token* token)
c4e3a34e40f1 Added new module dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
197 { // DDOC: '/++' '/**' '///'
679
ff6971637f88 Renamed Token member type to kind.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 676
diff changeset
198 return token.kind == TOK.Comment && token.start[1] == token.start[2];
676
c4e3a34e40f1 Added new module dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
199 }
c4e3a34e40f1 Added new module dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
200
c4e3a34e40f1 Added new module dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
201 /++
c4e3a34e40f1 Added new module dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
202 Returns the surrounding documentation comment tokens.
c4e3a34e40f1 Added new module dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
203 Note: this function works correctly only if
c4e3a34e40f1 Added new module dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
204 the source text is syntactically correct.
c4e3a34e40f1 Added new module dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
205 +/
714
140469ecb90e Added code and applied fixes.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 712
diff changeset
206 Token*[] getDocTokens(Node node, bool function(Token*) isDocComment = &isDDocComment)
676
c4e3a34e40f1 Added new module dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
207 {
c4e3a34e40f1 Added new module dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
208 Token*[] comments;
714
140469ecb90e Added code and applied fixes.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 712
diff changeset
209 auto isEnumMember = node.kind == NodeKind.EnumMemberDeclaration;
676
c4e3a34e40f1 Added new module dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
210 // Get preceding comments.
c4e3a34e40f1 Added new module dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
211 auto token = node.begin;
c4e3a34e40f1 Added new module dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
212 // Scan backwards until we hit another declaration.
714
140469ecb90e Added code and applied fixes.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 712
diff changeset
213 Loop:
676
c4e3a34e40f1 Added new module dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
214 while (1)
c4e3a34e40f1 Added new module dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
215 {
c4e3a34e40f1 Added new module dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
216 token = token.prev;
679
ff6971637f88 Renamed Token member type to kind.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 676
diff changeset
217 if (token.kind == TOK.LBrace ||
ff6971637f88 Renamed Token member type to kind.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 676
diff changeset
218 token.kind == TOK.RBrace ||
ff6971637f88 Renamed Token member type to kind.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 676
diff changeset
219 token.kind == TOK.Semicolon ||
ff6971637f88 Renamed Token member type to kind.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 676
diff changeset
220 token.kind == TOK.HEAD ||
714
140469ecb90e Added code and applied fixes.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 712
diff changeset
221 (isEnumMember && token.kind == TOK.Comma))
676
c4e3a34e40f1 Added new module dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
222 break;
c4e3a34e40f1 Added new module dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
223
679
ff6971637f88 Renamed Token member type to kind.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 676
diff changeset
224 if (token.kind == TOK.Comment)
714
140469ecb90e Added code and applied fixes.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 712
diff changeset
225 { // Check that this comment doesn't belong to the previous declaration.
679
ff6971637f88 Renamed Token member type to kind.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 676
diff changeset
226 switch (token.prev.kind)
676
c4e3a34e40f1 Added new module dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
227 {
714
140469ecb90e Added code and applied fixes.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 712
diff changeset
228 case TOK.Semicolon, TOK.RBrace, TOK.Comma:
140469ecb90e Added code and applied fixes.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 712
diff changeset
229 break Loop;
676
c4e3a34e40f1 Added new module dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
230 default:
c4e3a34e40f1 Added new module dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
231 if (isDocComment(token))
714
140469ecb90e Added code and applied fixes.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 712
diff changeset
232 comments = [token] ~ comments;
676
c4e3a34e40f1 Added new module dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
233 }
c4e3a34e40f1 Added new module dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
234 }
c4e3a34e40f1 Added new module dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
235 }
c4e3a34e40f1 Added new module dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
236 // Get single comment to the right.
c4e3a34e40f1 Added new module dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
237 token = node.end.next;
679
ff6971637f88 Renamed Token member type to kind.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 676
diff changeset
238 if (token.kind == TOK.Comment && isDocComment(token))
676
c4e3a34e40f1 Added new module dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
239 comments ~= token;
714
140469ecb90e Added code and applied fixes.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 712
diff changeset
240 else if (isEnumMember)
676
c4e3a34e40f1 Added new module dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
241 {
c4e3a34e40f1 Added new module dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
242 token = node.end.nextNWS;
679
ff6971637f88 Renamed Token member type to kind.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 676
diff changeset
243 if (token.kind == TOK.Comma)
676
c4e3a34e40f1 Added new module dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
244 {
c4e3a34e40f1 Added new module dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
245 token = token.next;
679
ff6971637f88 Renamed Token member type to kind.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 676
diff changeset
246 if (token.kind == TOK.Comment && isDocComment(token))
676
c4e3a34e40f1 Added new module dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
247 comments ~= token;
c4e3a34e40f1 Added new module dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
248 }
c4e3a34e40f1 Added new module dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
249 }
c4e3a34e40f1 Added new module dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
250 return comments;
c4e3a34e40f1 Added new module dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
251 }
712
f8875ef9a66d Added function sanitize() to dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 680
diff changeset
252
714
140469ecb90e Added code and applied fixes.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 712
diff changeset
253 bool isLineComment(Token* t)
140469ecb90e Added code and applied fixes.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 712
diff changeset
254 {
140469ecb90e Added code and applied fixes.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 712
diff changeset
255 assert(t.kind == TOK.Comment);
140469ecb90e Added code and applied fixes.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 712
diff changeset
256 return t.start[1] == '/';
140469ecb90e Added code and applied fixes.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 712
diff changeset
257 }
140469ecb90e Added code and applied fixes.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 712
diff changeset
258
140469ecb90e Added code and applied fixes.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 712
diff changeset
259 /// Extracts the text body of the comment tokens.
140469ecb90e Added code and applied fixes.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 712
diff changeset
260 string getDDocText(Token*[] tokens)
140469ecb90e Added code and applied fixes.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 712
diff changeset
261 {
140469ecb90e Added code and applied fixes.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 712
diff changeset
262 string result;
140469ecb90e Added code and applied fixes.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 712
diff changeset
263 foreach (token; tokens)
140469ecb90e Added code and applied fixes.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 712
diff changeset
264 {
140469ecb90e Added code and applied fixes.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 712
diff changeset
265 auto n = isLineComment(token) ? 0 : 2; // 0 for "//", 2 for "+/" and "*/".
140469ecb90e Added code and applied fixes.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 712
diff changeset
266 result ~= sanitize(token.srcText[3 .. $-n], token.start[1]);
140469ecb90e Added code and applied fixes.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 712
diff changeset
267 }
140469ecb90e Added code and applied fixes.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 712
diff changeset
268 return result;
140469ecb90e Added code and applied fixes.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 712
diff changeset
269 }
140469ecb90e Added code and applied fixes.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 712
diff changeset
270
712
f8875ef9a66d Added function sanitize() to dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 680
diff changeset
271 /// Sanitizes a DDoc comment string.
714
140469ecb90e Added code and applied fixes.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 712
diff changeset
272 /// Leading "commentChar"s are removed from the lines.
712
f8875ef9a66d Added function sanitize() to dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 680
diff changeset
273 /// The various newline types are converted to '\n'.
f8875ef9a66d Added function sanitize() to dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 680
diff changeset
274 /// Params:
f8875ef9a66d Added function sanitize() to dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 680
diff changeset
275 /// comment = the string to be sanitized.
f8875ef9a66d Added function sanitize() to dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 680
diff changeset
276 /// commentChar = '/', '+', or '*'
f8875ef9a66d Added function sanitize() to dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 680
diff changeset
277 string sanitize(string comment, char commentChar)
f8875ef9a66d Added function sanitize() to dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 680
diff changeset
278 {
f8875ef9a66d Added function sanitize() to dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 680
diff changeset
279 string result = comment.dup ~ '\0';
f8875ef9a66d Added function sanitize() to dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 680
diff changeset
280
f8875ef9a66d Added function sanitize() to dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 680
diff changeset
281 assert(result[$-1] == '\0');
f8875ef9a66d Added function sanitize() to dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 680
diff changeset
282 bool newline = true; // Indicates whether a newline has been encountered.
f8875ef9a66d Added function sanitize() to dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 680
diff changeset
283 uint i, j;
f8875ef9a66d Added function sanitize() to dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 680
diff changeset
284 for (; i < result.length; i++)
f8875ef9a66d Added function sanitize() to dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 680
diff changeset
285 {
f8875ef9a66d Added function sanitize() to dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 680
diff changeset
286 if (newline)
f8875ef9a66d Added function sanitize() to dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 680
diff changeset
287 { // Ignore commentChars at the beginning of each new line.
f8875ef9a66d Added function sanitize() to dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 680
diff changeset
288 newline = false;
f8875ef9a66d Added function sanitize() to dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 680
diff changeset
289 while (isspace(result[i]))
f8875ef9a66d Added function sanitize() to dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 680
diff changeset
290 { i++; }
f8875ef9a66d Added function sanitize() to dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 680
diff changeset
291 while (result[i] == commentChar)
f8875ef9a66d Added function sanitize() to dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 680
diff changeset
292 { i++; }
f8875ef9a66d Added function sanitize() to dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 680
diff changeset
293 }
f8875ef9a66d Added function sanitize() to dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 680
diff changeset
294 // Check for Newline.
f8875ef9a66d Added function sanitize() to dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 680
diff changeset
295 switch (result[i])
f8875ef9a66d Added function sanitize() to dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 680
diff changeset
296 {
f8875ef9a66d Added function sanitize() to dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 680
diff changeset
297 case '\r':
f8875ef9a66d Added function sanitize() to dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 680
diff changeset
298 if (result[i+1] == '\n')
f8875ef9a66d Added function sanitize() to dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 680
diff changeset
299 i++;
f8875ef9a66d Added function sanitize() to dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 680
diff changeset
300 case '\n':
f8875ef9a66d Added function sanitize() to dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 680
diff changeset
301 result[j++] = '\n'; // Copy Newline as '\n'.
f8875ef9a66d Added function sanitize() to dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 680
diff changeset
302 newline = true;
f8875ef9a66d Added function sanitize() to dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 680
diff changeset
303 continue;
f8875ef9a66d Added function sanitize() to dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 680
diff changeset
304 default:
f8875ef9a66d Added function sanitize() to dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 680
diff changeset
305 if (isUnicodeNewline(result.ptr + i))
f8875ef9a66d Added function sanitize() to dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 680
diff changeset
306 {
f8875ef9a66d Added function sanitize() to dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 680
diff changeset
307 i++; i++;
f8875ef9a66d Added function sanitize() to dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 680
diff changeset
308 goto case '\n';
f8875ef9a66d Added function sanitize() to dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 680
diff changeset
309 }
f8875ef9a66d Added function sanitize() to dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 680
diff changeset
310 }
f8875ef9a66d Added function sanitize() to dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 680
diff changeset
311 // Copy character.
f8875ef9a66d Added function sanitize() to dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 680
diff changeset
312 result[j++] = result[i];
f8875ef9a66d Added function sanitize() to dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 680
diff changeset
313 }
728
41cad5ca4863 Added ParamsSection and MacrosSection.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 722
diff changeset
314 result.length = j - 1; // Adjust length. -1 removes '\0'.
712
f8875ef9a66d Added function sanitize() to dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 680
diff changeset
315 // Lastly, strip trailing commentChars.
728
41cad5ca4863 Added ParamsSection and MacrosSection.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 722
diff changeset
316 i = result.length;
41cad5ca4863 Added ParamsSection and MacrosSection.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 722
diff changeset
317 while (--i && result[i] == commentChar)
41cad5ca4863 Added ParamsSection and MacrosSection.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 722
diff changeset
318 {}
41cad5ca4863 Added ParamsSection and MacrosSection.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 722
diff changeset
319 result.length = i + 1;
712
f8875ef9a66d Added function sanitize() to dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 680
diff changeset
320 return result;
f8875ef9a66d Added function sanitize() to dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 680
diff changeset
321 }