annotate trunk/src/dil/doc/Doc.d @ 716:08e6174a2e1c

Added class DDocComment.
author Aziz K?ksal <aziz.koeksal@gmail.com>
date Thu, 31 Jan 2008 21:31:47 +0100
parents 140469ecb90e
children be887ada3e3e
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
c4e3a34e40f1 Added new module dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
7 import dil.ast.Node;
712
f8875ef9a66d Added function sanitize() to dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 680
diff changeset
8 import dil.lexer.Funcs;
716
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
9 import dil.Unicode;
712
f8875ef9a66d Added function sanitize() to dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 680
diff changeset
10 import common;
676
c4e3a34e40f1 Added new module dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
11
716
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
12 class DDocComment
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
13 {
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
14 string text;
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
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
19 this(string text)
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
20 {
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
21 assert(text.length && text[$-1] == '\0');
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
22 this.text = text;
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
23 }
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
24
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
25 /// Parses the DDoc text into sections.
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
26 void parseSections()
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
27 {
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
28 char* p = text.ptr;
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
29 char* textEnd = p + text.length;
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
30 char* summaryBegin;
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
31 char* idBegin, idEnd;
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
32 char* nextIdBegin, nextIdEnd;
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
33
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
34 skipWhitespace(p);
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
35 summaryBegin = p;
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
36
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
37 if (findNextIdColon(p, idBegin, idEnd))
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
38 { // Check that this is not an explicit section.
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
39 if (summaryBegin != idBegin)
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
40 scanSummaryAndDescription(summaryBegin, idBegin);
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
41 }
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
42 else // There are no explicit sections.
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
43 return scanSummaryAndDescription(summaryBegin, textEnd);
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
44
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
45 assert(idBegin && idEnd);
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
46 while (findNextIdColon(p, nextIdBegin, nextIdEnd))
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
47 {
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
48 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
49 idBegin = nextIdBegin;
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
50 idEnd = nextIdEnd;
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
51 }
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
52 // Add last section.
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
53 sections ~= new Section(makeString(idBegin, idEnd), makeString(idEnd+1, textEnd));
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
54 }
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
55
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
56 void scanSummaryAndDescription(char* p, char* end)
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
57 {
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
58 assert(p != end && p < end);
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
59 char* sectionBegin = p;
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
60 // Search for the end of the first paragraph.
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
61 while (p != end && !(*p == '\n' && p[1] == '\n'))
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
62 p++;
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
63 // The first paragraph is the summary.
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
64 summary = new Section("", makeString(sectionBegin, p));
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
65 sections ~= summary;
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
66 // The rest is the description section.
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
67 if (p != end)
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
68 {
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
69 sectionBegin = p;
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
70 skipWhitespace(p);
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
71 if (p < end)
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
72 {
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
73 description = new Section("", makeString(sectionBegin, end));
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
74 sections ~= description;
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
75 }
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
76 }
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
77 }
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
78
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
79 void skipWhitespace(ref char* p)
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
80 {
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
81 while (isspace(*p) || *p == '\n')
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
82 p++;
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
83 }
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
84
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
85 /// Find next "Identifier:".
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
86 /// Params:
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
87 /// p = current character pointer
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
88 /// idBegin = set to the first character of the Identifier
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
89 /// idEnd = set to the colon following the Identifier
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
90 /// Returns: true if found
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
91 bool findNextIdColon(ref char* ref_p, ref char* ref_idBegin, ref char* ref_idEnd)
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
92 {
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
93 auto p = ref_p;
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
94 while (*p != '\0')
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 auto idBegin = p;
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
97 assert(isascii(*p) || isLeadByte(*p));
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
98 if (isidbeg(*p) || isUnicodeAlpha(p)) // IdStart
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 do // IdChar*
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 while (isident(*p) || isUnicodeAlpha(p))
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
103 if (*p == ':') // :
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
104 {
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
105 ref_idBegin = idBegin;
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
106 ref_idEnd = p;
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
107 ref_p = p;
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
108 return true;
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
109 }
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
110 }
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
111 else if (!isascii(*p))
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
112 { // Skip UTF-8 sequences.
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
113 while (!isascii(*++p))
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 continue;
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
116 }
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
117 p++;
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 return false;
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
120 }
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
121
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
122 /// This function assumes that there are no invalid
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
123 /// UTF-8 sequences in the string.
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
124 bool isUnicodeAlpha(ref char* ref_p)
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
125 {
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
126 char* p = ref_p; // Copy.
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
127 if (isascii(*p))
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
128 return false;
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 dchar d = *p;
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
131 p++; // Move to second byte.
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
132 // Error if second byte is not a trail byte.
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
133 assert(isTrailByte(*p), p[0..5]);
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
134 // Check for overlong sequences.
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
135 assert(delegate () {
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
136 switch (d)
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 case 0xE0, 0xF0, 0xF8, 0xFC:
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
139 if ((*p & d) == 0x80)
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
140 return false;
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
141 default:
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
142 if ((d & 0xFE) == 0xC0) // 1100000x
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
143 return false;
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
144 return true;
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
145 }
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
146 }() == true
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 const char[] checkNextByte = "p++;"
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
149 "assert(isTrailByte(*p));";
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
150 const char[] appendSixBits = "d = (d << 6) | *p & 0b0011_1111;";
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
151 // Decode
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
152 if ((d & 0b1110_0000) == 0b1100_0000)
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
153 {
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
154 d &= 0b0001_1111;
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
155 mixin(appendSixBits);
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
156 }
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
157 else if ((d & 0b1111_0000) == 0b1110_0000)
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
158 {
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
159 d &= 0b0000_1111;
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
160 mixin(appendSixBits ~
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
161 checkNextByte ~ appendSixBits);
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
162 }
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
163 else if ((d & 0b1111_1000) == 0b1111_0000)
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
164 {
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
165 d &= 0b0000_0111;
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
166 mixin(appendSixBits ~
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
167 checkNextByte ~ appendSixBits ~
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
168 checkNextByte ~ appendSixBits);
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
169 }
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
170 else
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
171 return false;
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
172
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
173 assert(isTrailByte(*p) && isValidChar(d));
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
174 if (!isUniAlpha(d))
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
175 return false;
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
176 // Only advance pointer if this is a Unicode alpha character.
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
177 ref_p = p;
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
178 return true;
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
179 }
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
180 }
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
181
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
182 class Section
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
183 {
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
184 string name;
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
185 string text;
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
186 this(string name, string text)
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
187 {
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
188 this.name = name;
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
189 this.text = text;
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
190 }
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
191 }
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
192
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
193 char[] makeString(char* begin, char* end)
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
194 {
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
195 return begin[0 .. end - begin];
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
196 }
08e6174a2e1c Added class DDocComment.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 714
diff changeset
197
676
c4e3a34e40f1 Added new module dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
198 bool isDoxygenComment(Token* token)
c4e3a34e40f1 Added new module dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
199 { // Doxygen: '/+!' '/*!' '//!'
679
ff6971637f88 Renamed Token member type to kind.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 676
diff changeset
200 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
201 }
c4e3a34e40f1 Added new module dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
202
c4e3a34e40f1 Added new module dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
203 bool isDDocComment(Token* token)
c4e3a34e40f1 Added new module dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
204 { // DDOC: '/++' '/**' '///'
679
ff6971637f88 Renamed Token member type to kind.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 676
diff changeset
205 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
206 }
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 /++
c4e3a34e40f1 Added new module dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
209 Returns the surrounding documentation comment tokens.
c4e3a34e40f1 Added new module dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
210 Note: this function works correctly only if
c4e3a34e40f1 Added new module dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
211 the source text is syntactically correct.
c4e3a34e40f1 Added new module dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
212 +/
714
140469ecb90e Added code and applied fixes.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 712
diff changeset
213 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
214 {
c4e3a34e40f1 Added new module dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
215 Token*[] comments;
714
140469ecb90e Added code and applied fixes.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 712
diff changeset
216 auto isEnumMember = node.kind == NodeKind.EnumMemberDeclaration;
676
c4e3a34e40f1 Added new module dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
217 // Get preceding comments.
c4e3a34e40f1 Added new module dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
218 auto token = node.begin;
c4e3a34e40f1 Added new module dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
219 // 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
220 Loop:
676
c4e3a34e40f1 Added new module dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
221 while (1)
c4e3a34e40f1 Added new module dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
222 {
c4e3a34e40f1 Added new module dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
223 token = token.prev;
679
ff6971637f88 Renamed Token member type to kind.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 676
diff changeset
224 if (token.kind == TOK.LBrace ||
ff6971637f88 Renamed Token member type to kind.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 676
diff changeset
225 token.kind == TOK.RBrace ||
ff6971637f88 Renamed Token member type to kind.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 676
diff changeset
226 token.kind == TOK.Semicolon ||
ff6971637f88 Renamed Token member type to kind.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 676
diff changeset
227 token.kind == TOK.HEAD ||
714
140469ecb90e Added code and applied fixes.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 712
diff changeset
228 (isEnumMember && token.kind == TOK.Comma))
676
c4e3a34e40f1 Added new module dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
229 break;
c4e3a34e40f1 Added new module dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
230
679
ff6971637f88 Renamed Token member type to kind.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 676
diff changeset
231 if (token.kind == TOK.Comment)
714
140469ecb90e Added code and applied fixes.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 712
diff changeset
232 { // 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
233 switch (token.prev.kind)
676
c4e3a34e40f1 Added new module dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
234 {
714
140469ecb90e Added code and applied fixes.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 712
diff changeset
235 case TOK.Semicolon, TOK.RBrace, TOK.Comma:
140469ecb90e Added code and applied fixes.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 712
diff changeset
236 break Loop;
676
c4e3a34e40f1 Added new module dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
237 default:
c4e3a34e40f1 Added new module dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
238 if (isDocComment(token))
714
140469ecb90e Added code and applied fixes.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 712
diff changeset
239 comments = [token] ~ comments;
676
c4e3a34e40f1 Added new module dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
240 }
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 }
c4e3a34e40f1 Added new module dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
243 // Get single comment to the right.
c4e3a34e40f1 Added new module dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
244 token = node.end.next;
679
ff6971637f88 Renamed Token member type to kind.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 676
diff changeset
245 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
246 comments ~= token;
714
140469ecb90e Added code and applied fixes.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 712
diff changeset
247 else if (isEnumMember)
676
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 token = node.end.nextNWS;
679
ff6971637f88 Renamed Token member type to kind.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 676
diff changeset
250 if (token.kind == TOK.Comma)
676
c4e3a34e40f1 Added new module dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
251 {
c4e3a34e40f1 Added new module dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
252 token = token.next;
679
ff6971637f88 Renamed Token member type to kind.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 676
diff changeset
253 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
254 comments ~= token;
c4e3a34e40f1 Added new module dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
255 }
c4e3a34e40f1 Added new module dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
256 }
c4e3a34e40f1 Added new module dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
257 return comments;
c4e3a34e40f1 Added new module dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
258 }
712
f8875ef9a66d Added function sanitize() to dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 680
diff changeset
259
714
140469ecb90e Added code and applied fixes.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 712
diff changeset
260 bool isLineComment(Token* t)
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 assert(t.kind == TOK.Comment);
140469ecb90e Added code and applied fixes.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 712
diff changeset
263 return t.start[1] == '/';
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
140469ecb90e Added code and applied fixes.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 712
diff changeset
266 /// 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
267 string getDDocText(Token*[] tokens)
140469ecb90e Added code and applied fixes.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 712
diff changeset
268 {
140469ecb90e Added code and applied fixes.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 712
diff changeset
269 string result;
140469ecb90e Added code and applied fixes.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 712
diff changeset
270 foreach (token; tokens)
140469ecb90e Added code and applied fixes.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 712
diff changeset
271 {
140469ecb90e Added code and applied fixes.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 712
diff changeset
272 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
273 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
274 }
140469ecb90e Added code and applied fixes.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 712
diff changeset
275 return result;
140469ecb90e Added code and applied fixes.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 712
diff changeset
276 }
140469ecb90e Added code and applied fixes.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 712
diff changeset
277
712
f8875ef9a66d Added function sanitize() to dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 680
diff changeset
278 /// Sanitizes a DDoc comment string.
714
140469ecb90e Added code and applied fixes.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 712
diff changeset
279 /// 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
280 /// 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
281 /// Params:
f8875ef9a66d Added function sanitize() to dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 680
diff changeset
282 /// 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
283 /// commentChar = '/', '+', or '*'
f8875ef9a66d Added function sanitize() to dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 680
diff changeset
284 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
285 {
f8875ef9a66d Added function sanitize() to dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 680
diff changeset
286 string result = comment.dup ~ '\0';
f8875ef9a66d Added function sanitize() to dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 680
diff changeset
287
f8875ef9a66d Added function sanitize() to dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 680
diff changeset
288 assert(result[$-1] == '\0');
f8875ef9a66d Added function sanitize() to dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 680
diff changeset
289 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
290 uint i, j;
f8875ef9a66d Added function sanitize() to dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 680
diff changeset
291 for (; i < result.length; i++)
f8875ef9a66d Added function sanitize() to dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 680
diff changeset
292 {
f8875ef9a66d Added function sanitize() to dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 680
diff changeset
293 if (newline)
f8875ef9a66d Added function sanitize() to dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 680
diff changeset
294 { // 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
295 newline = false;
f8875ef9a66d Added function sanitize() to dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 680
diff changeset
296 while (isspace(result[i]))
f8875ef9a66d Added function sanitize() to dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 680
diff changeset
297 { i++; }
f8875ef9a66d Added function sanitize() to dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 680
diff changeset
298 while (result[i] == commentChar)
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 }
f8875ef9a66d Added function sanitize() to dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 680
diff changeset
301 // Check for Newline.
f8875ef9a66d Added function sanitize() to dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 680
diff changeset
302 switch (result[i])
f8875ef9a66d Added function sanitize() to dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 680
diff changeset
303 {
f8875ef9a66d Added function sanitize() to dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 680
diff changeset
304 case '\r':
f8875ef9a66d Added function sanitize() to dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 680
diff changeset
305 if (result[i+1] == '\n')
f8875ef9a66d Added function sanitize() to dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 680
diff changeset
306 i++;
f8875ef9a66d Added function sanitize() to dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 680
diff changeset
307 case '\n':
f8875ef9a66d Added function sanitize() to dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 680
diff changeset
308 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
309 newline = true;
f8875ef9a66d Added function sanitize() to dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 680
diff changeset
310 continue;
f8875ef9a66d Added function sanitize() to dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 680
diff changeset
311 default:
f8875ef9a66d Added function sanitize() to dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 680
diff changeset
312 if (isUnicodeNewline(result.ptr + i))
f8875ef9a66d Added function sanitize() to dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 680
diff changeset
313 {
f8875ef9a66d Added function sanitize() to dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 680
diff changeset
314 i++; i++;
f8875ef9a66d Added function sanitize() to dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 680
diff changeset
315 goto case '\n';
f8875ef9a66d Added function sanitize() to dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 680
diff changeset
316 }
f8875ef9a66d Added function sanitize() to dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 680
diff changeset
317 }
f8875ef9a66d Added function sanitize() to dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 680
diff changeset
318 // Copy character.
f8875ef9a66d Added function sanitize() to dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 680
diff changeset
319 result[j++] = result[i];
f8875ef9a66d Added function sanitize() to dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 680
diff changeset
320 }
f8875ef9a66d Added function sanitize() to dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 680
diff changeset
321 result.length = j; // Adjust length.
f8875ef9a66d Added function sanitize() to dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 680
diff changeset
322 // Lastly, strip trailing commentChars.
f8875ef9a66d Added function sanitize() to dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 680
diff changeset
323 i = result.length - (1 + 1);
f8875ef9a66d Added function sanitize() to dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 680
diff changeset
324 while (i && result[i] == commentChar)
f8875ef9a66d Added function sanitize() to dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 680
diff changeset
325 { i--; }
f8875ef9a66d Added function sanitize() to dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 680
diff changeset
326 return result;
f8875ef9a66d Added function sanitize() to dil.doc.Doc.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 680
diff changeset
327 }