Mercurial > projects > dil
annotate trunk/src/cmd/Generate.d @ 581:61f9784aaf2a
Improved function getShortClassName().
author | Aziz K?ksal <aziz.koeksal@gmail.com> |
---|---|
date | Sat, 05 Jan 2008 18:40:10 +0100 |
parents | fa6d3c52757d |
children | 724603deed37 |
rev | line source |
---|---|
363 | 1 /++ |
2 Author: Aziz Köksal | |
3 License: GPL3 | |
4 +/ | |
5 module cmd.Generate; | |
578
c769bc239006
Moved Parser.d to new package 'parser'.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
576
diff
changeset
|
6 |
580
fa6d3c52757d
Moved SyntaxTree.d to new package 'ast'.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
578
diff
changeset
|
7 import dil.ast.Node; |
363 | 8 import dil.Token; |
578
c769bc239006
Moved Parser.d to new package 'parser'.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
576
diff
changeset
|
9 import dil.parser.Parser; |
c769bc239006
Moved Parser.d to new package 'parser'.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
576
diff
changeset
|
10 import dil.lexer.Lexer; |
363 | 11 import dil.File; |
391
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
390
diff
changeset
|
12 import tango.io.Print; |
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
390
diff
changeset
|
13 import common; |
363 | 14 |
15 enum DocOption | |
16 { | |
392
bb935c6f9b7a
Applied fixes and improvements to the Lexer class.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
391
diff
changeset
|
17 Empty, |
bb935c6f9b7a
Applied fixes and improvements to the Lexer class.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
391
diff
changeset
|
18 Tokens = 1, |
363 | 19 Syntax = 1<<1, |
392
bb935c6f9b7a
Applied fixes and improvements to the Lexer class.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
391
diff
changeset
|
20 HTML = 1<<2, |
bb935c6f9b7a
Applied fixes and improvements to the Lexer class.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
391
diff
changeset
|
21 XML = 1<<3 |
363 | 22 } |
23 | |
364 | 24 void execute(string fileName, DocOption options) |
25 { | |
392
bb935c6f9b7a
Applied fixes and improvements to the Lexer class.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
391
diff
changeset
|
26 assert(options != DocOption.Empty); |
364 | 27 if (options & DocOption.Syntax) |
391
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
390
diff
changeset
|
28 syntaxToDoc(fileName, Stdout, options); |
364 | 29 else |
391
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
390
diff
changeset
|
30 tokensToDoc(fileName, Stdout, options); |
364 | 31 } |
32 | |
363 | 33 char[] xml_escape(char[] text) |
34 { | |
35 char[] result; | |
36 foreach(c; text) | |
37 switch(c) | |
38 { | |
39 case '<': result ~= "<"; break; | |
40 case '>': result ~= ">"; break; | |
41 case '&': result ~= "&"; break; | |
42 default: result ~= c; | |
43 } | |
44 return result; | |
45 } | |
46 | |
391
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
390
diff
changeset
|
47 |
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
390
diff
changeset
|
48 /// Find object in subject and return position. |
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
390
diff
changeset
|
49 /// Returns -1 if no match was found. |
581
61f9784aaf2a
Improved function getShortClassName().
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
580
diff
changeset
|
50 /+int find(char[] subject, char[] object) |
391
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
390
diff
changeset
|
51 { |
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
390
diff
changeset
|
52 if (object.length > subject.length) |
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
390
diff
changeset
|
53 return -1; |
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
390
diff
changeset
|
54 foreach (i, c; subject) |
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
390
diff
changeset
|
55 { |
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
390
diff
changeset
|
56 if (c == object[0]) |
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
390
diff
changeset
|
57 { |
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
390
diff
changeset
|
58 if (object.length > (subject.length - i)) |
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
390
diff
changeset
|
59 return -1; |
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
390
diff
changeset
|
60 if (object == subject[i..i+object.length]) |
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
390
diff
changeset
|
61 return i; |
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
390
diff
changeset
|
62 } |
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
390
diff
changeset
|
63 } |
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
390
diff
changeset
|
64 return -1; |
581
61f9784aaf2a
Improved function getShortClassName().
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
580
diff
changeset
|
65 }+/ |
61f9784aaf2a
Improved function getShortClassName().
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
580
diff
changeset
|
66 |
61f9784aaf2a
Improved function getShortClassName().
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
580
diff
changeset
|
67 int rfind(char[] subject, char object) |
61f9784aaf2a
Improved function getShortClassName().
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
580
diff
changeset
|
68 { |
61f9784aaf2a
Improved function getShortClassName().
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
580
diff
changeset
|
69 foreach_reverse(i, c; subject) |
61f9784aaf2a
Improved function getShortClassName().
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
580
diff
changeset
|
70 if (c == object) |
61f9784aaf2a
Improved function getShortClassName().
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
580
diff
changeset
|
71 return i; |
61f9784aaf2a
Improved function getShortClassName().
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
580
diff
changeset
|
72 return -1; |
391
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
390
diff
changeset
|
73 } |
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
390
diff
changeset
|
74 |
581
61f9784aaf2a
Improved function getShortClassName().
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
580
diff
changeset
|
75 char[] getShortClassName(Node node) |
363 | 76 { |
77 static char[][] name_table; | |
78 if (name_table is null) | |
581
61f9784aaf2a
Improved function getShortClassName().
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
580
diff
changeset
|
79 name_table = new char[][NodeKind.max+1]; // Create a new table. |
61f9784aaf2a
Improved function getShortClassName().
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
580
diff
changeset
|
80 // Look up in table. |
61f9784aaf2a
Improved function getShortClassName().
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
580
diff
changeset
|
81 char[] name = name_table[node.kind]; |
363 | 82 if (name !is null) |
581
61f9784aaf2a
Improved function getShortClassName().
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
580
diff
changeset
|
83 return name; // Return cached name. |
363 | 84 |
581
61f9784aaf2a
Improved function getShortClassName().
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
580
diff
changeset
|
85 name = node.classinfo.name; |
61f9784aaf2a
Improved function getShortClassName().
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
580
diff
changeset
|
86 name = name[rfind(name, '.')+1 .. $]; // Remove package and module name |
61f9784aaf2a
Improved function getShortClassName().
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
580
diff
changeset
|
87 |
61f9784aaf2a
Improved function getShortClassName().
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
580
diff
changeset
|
88 uint suffixLength; |
61f9784aaf2a
Improved function getShortClassName().
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
580
diff
changeset
|
89 switch (node.category) |
363 | 90 { |
91 alias NodeCategory NC; | |
485
ea8c7459f1c4
Changed a lot of things in the Lexer.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
422
diff
changeset
|
92 case NC.Declaration: |
581
61f9784aaf2a
Improved function getShortClassName().
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
580
diff
changeset
|
93 if (node.kind == NodeKind.Declarations) |
61f9784aaf2a
Improved function getShortClassName().
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
580
diff
changeset
|
94 break; |
61f9784aaf2a
Improved function getShortClassName().
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
580
diff
changeset
|
95 suffixLength = "Declaration".length; |
485
ea8c7459f1c4
Changed a lot of things in the Lexer.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
422
diff
changeset
|
96 break; |
363 | 97 case NC.Statement: |
581
61f9784aaf2a
Improved function getShortClassName().
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
580
diff
changeset
|
98 if (node.kind == NodeKind.Statements) |
61f9784aaf2a
Improved function getShortClassName().
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
580
diff
changeset
|
99 break; |
61f9784aaf2a
Improved function getShortClassName().
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
580
diff
changeset
|
100 suffixLength = "Statement".length; |
363 | 101 break; |
581
61f9784aaf2a
Improved function getShortClassName().
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
580
diff
changeset
|
102 case NC.Expression: |
61f9784aaf2a
Improved function getShortClassName().
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
580
diff
changeset
|
103 suffixLength = "Expression".length; |
61f9784aaf2a
Improved function getShortClassName().
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
580
diff
changeset
|
104 break; |
61f9784aaf2a
Improved function getShortClassName().
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
580
diff
changeset
|
105 case NC.Type: |
61f9784aaf2a
Improved function getShortClassName().
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
580
diff
changeset
|
106 suffixLength = "Type".length; |
61f9784aaf2a
Improved function getShortClassName().
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
580
diff
changeset
|
107 break; |
61f9784aaf2a
Improved function getShortClassName().
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
580
diff
changeset
|
108 case NC.Other: |
363 | 109 default: |
110 } | |
111 // Remove common suffix. | |
581
61f9784aaf2a
Improved function getShortClassName().
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
580
diff
changeset
|
112 name = name[0 .. $ - suffixLength]; |
61f9784aaf2a
Improved function getShortClassName().
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
580
diff
changeset
|
113 // Store the name in the table. |
61f9784aaf2a
Improved function getShortClassName().
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
580
diff
changeset
|
114 name_table[node.kind] = name; |
363 | 115 return name; |
116 } | |
117 | |
118 enum DocPart | |
119 { | |
120 Head, | |
121 CompBegin, | |
122 CompEnd, | |
123 Error, | |
124 SyntaxBegin, | |
125 SyntaxEnd, | |
126 SrcBegin, | |
127 SrcEnd, | |
128 Tail, | |
129 // Tokens: | |
130 Identifier, | |
131 Comment, | |
132 StringLiteral, | |
133 CharLiteral, | |
134 Operator, | |
135 LorG, | |
136 LessEqual, | |
137 GreaterEqual, | |
138 AndLogical, | |
139 OrLogical, | |
140 NotEqual, | |
141 Not, | |
142 Number, | |
143 Bracket, | |
144 SpecialToken, | |
145 Shebang, | |
146 Keyword, | |
147 HLineBegin, | |
148 HLineEnd, | |
149 Filespec, | |
150 } | |
151 | |
152 auto html_tags = [ | |
153 // Head | |
154 `<html>`\n | |
155 `<head>`\n | |
156 `<meta http-equiv="Content-Type" content="text/html; charset=utf-8">`\n | |
157 `<link href="dil_html.css" rel="stylesheet" type="text/css">`\n | |
158 `</head>`\n | |
159 `<body>`[], | |
160 // CompBegin | |
161 `<div class="compilerinfo">`, | |
162 // CompEnd | |
163 `</div>`, | |
164 // Error | |
391
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
390
diff
changeset
|
165 `<p class="error {0}">{1}({2}){3}: {4}</p>`, |
363 | 166 // SyntaxBegin |
391
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
390
diff
changeset
|
167 `<span class="{0} {1}">`, |
363 | 168 // SyntaxEnd |
169 `</span>`, | |
170 // SrcBegin | |
171 `<pre class="sourcecode">`, | |
172 // SrcEnd | |
173 `</pre>`, | |
174 // Tail | |
175 `</html>`, | |
176 // Identifier | |
391
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
390
diff
changeset
|
177 `<span class="i">{0}</span>`, |
363 | 178 // Comment |
391
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
390
diff
changeset
|
179 `<span class="c{0}">{1}</span>`, |
363 | 180 // StringLiteral |
391
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
390
diff
changeset
|
181 `<span class="sl">{0}</span>`, |
363 | 182 // CharLiteral |
391
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
390
diff
changeset
|
183 `<span class="cl">{0}</span>`, |
363 | 184 // Operator |
391
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
390
diff
changeset
|
185 `<span class="op">{0}</span>`, |
363 | 186 // LorG |
187 `<span class="oplg"><></span>`, | |
188 // LessEqual | |
189 `<span class="ople"><=</span>`, | |
190 // GreaterEqual | |
191 `<span class="opge">>=</span>`, | |
192 // AndLogical | |
193 `<span class="opaa">&&</span>`, | |
194 // OrLogical | |
195 `<span class="opoo">||</span>`, | |
196 // NotEqual | |
197 `<span class="opne">!=</span>`, | |
198 // Not | |
199 `<span class="opn">!</span>`, | |
200 // Number | |
391
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
390
diff
changeset
|
201 `<span class="n">{0}</span>`, |
363 | 202 // Bracket |
391
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
390
diff
changeset
|
203 `<span class="br">{0}</span>`, |
363 | 204 // SpecialToken |
391
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
390
diff
changeset
|
205 `<span class="st">{0}</span>`, |
363 | 206 // Shebang |
391
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
390
diff
changeset
|
207 `<span class="shebang">{0}</span>`, |
363 | 208 // Keyword |
391
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
390
diff
changeset
|
209 `<span class="k">{0}</span>`, |
363 | 210 // HLineBegin |
388
ae154eceba65
Applied some fixes to scanning and printing #line tokens.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
379
diff
changeset
|
211 `<span class="hl">`, |
363 | 212 // HLineEnd |
213 "</span>", | |
214 // Filespec | |
391
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
390
diff
changeset
|
215 `<span class="fs">{0}</span>`, |
363 | 216 ]; |
217 | |
218 auto xml_tags = [ | |
219 // Head | |
220 `<?xml version="1.0"?>`\n | |
221 `<?xml-stylesheet href="dil_xml.css" type="text/css"?>`\n | |
222 `<root>`[], | |
223 // CompBegin | |
224 `<compilerinfo>`, | |
225 // CompEnd | |
226 `</compilerinfo>`, | |
227 // Error | |
391
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
390
diff
changeset
|
228 `<error t="{0}">{1}({2}){3}: {4}</error>`, |
363 | 229 // SyntaxBegin |
391
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
390
diff
changeset
|
230 `<{0} t="{1}">`, |
363 | 231 // SyntaxEnd |
391
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
390
diff
changeset
|
232 `</{0}>`, |
363 | 233 // SrcBegin |
234 `<sourcecode>`, | |
235 // SrcEnd | |
236 `</sourcecode>`, | |
237 // Tail | |
238 `</root>`, | |
239 // Identifier | |
391
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
390
diff
changeset
|
240 "<i>{0}</i>", |
363 | 241 // Comment |
391
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
390
diff
changeset
|
242 `<c t="{0}">{1}</c>`, |
363 | 243 // StringLiteral |
391
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
390
diff
changeset
|
244 "<sl>{0}</sl>", |
363 | 245 // CharLiteral |
391
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
390
diff
changeset
|
246 "<cl>{0}</cl>", |
363 | 247 // Operator |
391
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
390
diff
changeset
|
248 "<op>{0}</op>", |
363 | 249 // LorG |
250 `<op t="lg"><></op>`, | |
251 // LessEqual | |
252 `<op t="le"><=</op>`, | |
253 // GreaterEqual | |
254 `<op t="ge">>=</op>`, | |
255 // AndLogical | |
256 `<op t="aa">&&</op>`, | |
257 // OrLogical | |
258 `<op t="oo">||</op>`, | |
259 // NotEqual | |
260 `<op t="ne">!=</op>`, | |
261 // Not | |
262 `<op t="n">!</op>`, | |
263 // Number | |
391
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
390
diff
changeset
|
264 "<n>{0}</n>", |
363 | 265 // Bracket |
391
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
390
diff
changeset
|
266 "<br>{0}</br>", |
363 | 267 // SpecialToken |
391
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
390
diff
changeset
|
268 "<st>{0}</st>", |
363 | 269 // Shebang |
391
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
390
diff
changeset
|
270 "<shebang>{0}</shebang>", |
363 | 271 // Keyword |
391
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
390
diff
changeset
|
272 "<k>{0}</k>", |
363 | 273 // HLineBegin |
388
ae154eceba65
Applied some fixes to scanning and printing #line tokens.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
379
diff
changeset
|
274 "<hl>", |
363 | 275 // HLineEnd |
276 "</hl>", | |
277 // Filespec | |
391
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
390
diff
changeset
|
278 "<fs>{0}</fs>", |
363 | 279 ]; |
280 | |
281 static assert(html_tags.length == DocPart.max+1); | |
282 static assert(xml_tags.length == DocPart.max+1); | |
283 | |
391
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
390
diff
changeset
|
284 void syntaxToDoc(string fileName, Print!(char) print, DocOption options) |
363 | 285 { |
286 auto tags = options & DocOption.HTML ? html_tags : xml_tags; | |
287 auto sourceText = loadFile(fileName); | |
288 auto parser = new Parser(sourceText, fileName); | |
368 | 289 auto root = parser.start(); |
363 | 290 auto lx = parser.lx; |
291 | |
292 auto token = lx.head; | |
293 | |
391
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
390
diff
changeset
|
294 print(tags[DocPart.Head]~\n); |
363 | 295 // Output error messages. |
296 if (lx.errors.length || parser.errors.length) | |
297 { | |
391
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
390
diff
changeset
|
298 print(tags[DocPart.CompBegin]~\n); |
363 | 299 foreach (error; lx.errors) |
300 { | |
422
ad7977fe315a
Added support for column numbers in error messages.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
396
diff
changeset
|
301 print.formatln(tags[DocPart.Error], "L", error.filePath, Format("{0},{1}", error.loc, error.col), "L", xml_escape(error.getMsg)); |
363 | 302 } |
303 foreach (error; parser.errors) | |
304 { | |
422
ad7977fe315a
Added support for column numbers in error messages.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
396
diff
changeset
|
305 print.formatln(tags[DocPart.Error], "P", error.filePath, Format("{0},{1}", error.loc, error.col), "P", xml_escape(error.getMsg)); |
363 | 306 } |
391
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
390
diff
changeset
|
307 print(tags[DocPart.CompEnd]~\n); |
363 | 308 } |
391
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
390
diff
changeset
|
309 print(tags[DocPart.SrcBegin]); |
363 | 310 |
311 Node[][Token*] beginNodes, endNodes; | |
312 | |
313 void populateAAs(Node[] nodes) | |
314 { | |
315 foreach (node; nodes) | |
316 { | |
396
0a4619735ce9
Applied fixes to Lexer, Parser and other classes.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
394
diff
changeset
|
317 assert(delegate bool(){ |
0a4619735ce9
Applied fixes to Lexer, Parser and other classes.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
394
diff
changeset
|
318 foreach (child; node.children) |
0a4619735ce9
Applied fixes to Lexer, Parser and other classes.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
394
diff
changeset
|
319 if (child is null) |
0a4619735ce9
Applied fixes to Lexer, Parser and other classes.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
394
diff
changeset
|
320 return false; |
0a4619735ce9
Applied fixes to Lexer, Parser and other classes.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
394
diff
changeset
|
321 return true; |
0a4619735ce9
Applied fixes to Lexer, Parser and other classes.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
394
diff
changeset
|
322 }() == true, Format("Node '{0}' has a null child", node.classinfo.name) |
0a4619735ce9
Applied fixes to Lexer, Parser and other classes.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
394
diff
changeset
|
323 ); |
363 | 324 auto begin = node.begin; |
325 if (begin) | |
326 { | |
327 auto end = node.end; | |
328 assert(end); | |
329 beginNodes[begin] ~= node; | |
330 endNodes[end] ~= node; | |
331 } | |
396
0a4619735ce9
Applied fixes to Lexer, Parser and other classes.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
394
diff
changeset
|
332 |
363 | 333 if (node.children.length) |
334 populateAAs(node.children); | |
335 } | |
336 } | |
396
0a4619735ce9
Applied fixes to Lexer, Parser and other classes.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
394
diff
changeset
|
337 assert(delegate bool(){ |
0a4619735ce9
Applied fixes to Lexer, Parser and other classes.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
394
diff
changeset
|
338 foreach (child; root.children) |
0a4619735ce9
Applied fixes to Lexer, Parser and other classes.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
394
diff
changeset
|
339 if (child is null) |
0a4619735ce9
Applied fixes to Lexer, Parser and other classes.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
394
diff
changeset
|
340 return false; |
0a4619735ce9
Applied fixes to Lexer, Parser and other classes.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
394
diff
changeset
|
341 return true; |
0a4619735ce9
Applied fixes to Lexer, Parser and other classes.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
394
diff
changeset
|
342 }() == true, Format("Root node has a null child") |
0a4619735ce9
Applied fixes to Lexer, Parser and other classes.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
394
diff
changeset
|
343 ); |
363 | 344 populateAAs(root.children); |
345 | |
346 char[] getTag(NodeCategory nc) | |
347 { | |
348 char[] tag; | |
349 switch (nc) | |
350 { | |
351 alias NodeCategory NC; | |
352 case NC.Declaration: tag = "d"; break; | |
353 case NC.Statement: tag = "s"; break; | |
354 case NC.Expression: tag = "e"; break; | |
355 case NC.Type: tag = "t"; break; | |
356 case NC.Other: tag = "o"; break; | |
357 default: | |
358 } | |
359 return tag; | |
360 } | |
361 | |
362 // Traverse linked list and print tokens. | |
363 while (token.type != TOK.EOF) | |
364 { | |
365 token = token.next; | |
366 | |
394
6440da4adb07
Fixed forward references of enum MID complaints by compiler.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
392
diff
changeset
|
367 // Print whitespace. |
6440da4adb07
Fixed forward references of enum MID complaints by compiler.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
392
diff
changeset
|
368 if (token.ws) |
6440da4adb07
Fixed forward references of enum MID complaints by compiler.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
392
diff
changeset
|
369 print(token.ws[0..token.start - token.ws]); |
6440da4adb07
Fixed forward references of enum MID complaints by compiler.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
392
diff
changeset
|
370 |
363 | 371 Node[]* nodes = token in beginNodes; |
372 | |
373 if (nodes) | |
374 { | |
375 foreach (node; *nodes) | |
391
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
390
diff
changeset
|
376 print.format(tags[DocPart.SyntaxBegin], getTag(node.category), getShortClassName(node)); |
363 | 377 } |
378 | |
391
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
390
diff
changeset
|
379 printToken(token, tags, print); |
363 | 380 |
381 nodes = token in endNodes; | |
382 | |
383 if (nodes) | |
384 { | |
385 foreach_reverse (node; *nodes) | |
386 if (options & DocOption.HTML) | |
391
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
390
diff
changeset
|
387 print(tags[DocPart.SyntaxEnd]); |
363 | 388 else |
391
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
390
diff
changeset
|
389 print.format(tags[DocPart.SyntaxEnd], getTag(node.category)); |
363 | 390 } |
391 } | |
391
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
390
diff
changeset
|
392 print(\n~tags[DocPart.SrcEnd])(\n~tags[DocPart.Tail]); |
363 | 393 } |
394 | |
391
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
390
diff
changeset
|
395 void tokensToDoc(string fileName, Print!(char) print, DocOption options) |
363 | 396 { |
397 auto tags = options & DocOption.HTML ? html_tags : xml_tags; | |
398 auto sourceText = loadFile(fileName); | |
399 auto lx = new Lexer(sourceText, fileName); | |
400 | |
401 auto token = lx.getTokens(); | |
402 | |
391
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
390
diff
changeset
|
403 print(tags[DocPart.Head]~\n); |
363 | 404 |
405 if (lx.errors.length) | |
406 { | |
391
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
390
diff
changeset
|
407 print(tags[DocPart.CompBegin]~\n); |
363 | 408 foreach (error; lx.errors) |
409 { | |
422
ad7977fe315a
Added support for column numbers in error messages.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
396
diff
changeset
|
410 print.formatln(tags[DocPart.Error], "L", error.filePath, Format("{0},{1}", error.loc, error.col), "L", xml_escape(error.getMsg)); |
363 | 411 } |
391
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
390
diff
changeset
|
412 print(tags[DocPart.CompEnd]~\n); |
363 | 413 } |
391
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
390
diff
changeset
|
414 print(tags[DocPart.SrcBegin]); |
363 | 415 |
416 // Traverse linked list and print tokens. | |
417 while (token.type != TOK.EOF) | |
418 { | |
419 token = token.next; | |
394
6440da4adb07
Fixed forward references of enum MID complaints by compiler.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
392
diff
changeset
|
420 // Print whitespace. |
6440da4adb07
Fixed forward references of enum MID complaints by compiler.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
392
diff
changeset
|
421 if (token.ws) |
6440da4adb07
Fixed forward references of enum MID complaints by compiler.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
392
diff
changeset
|
422 print(token.ws[0..token.start - token.ws]); |
391
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
390
diff
changeset
|
423 printToken(token, tags, print); |
363 | 424 } |
391
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
390
diff
changeset
|
425 print(\n~tags[DocPart.SrcEnd])(\n~tags[DocPart.Tail]); |
363 | 426 } |
427 | |
391
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
390
diff
changeset
|
428 void printToken(Token* token, string[] tags, Print!(char) print) |
363 | 429 { |
430 alias DocPart DP; | |
431 string srcText = xml_escape(token.srcText); | |
432 | |
433 switch(token.type) | |
434 { | |
435 case TOK.Identifier: | |
391
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
390
diff
changeset
|
436 print.format(tags[DP.Identifier], srcText); |
363 | 437 break; |
438 case TOK.Comment: | |
439 string t; | |
440 switch (token.start[1]) | |
441 { | |
442 case '/': t = "l"; break; | |
443 case '*': t = "b"; break; | |
444 case '+': t = "n"; break; | |
445 default: | |
446 assert(0); | |
447 } | |
391
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
390
diff
changeset
|
448 print.format(tags[DP.Comment], t, srcText); |
363 | 449 break; |
450 case TOK.String: | |
391
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
390
diff
changeset
|
451 print.format(tags[DP.StringLiteral], srcText); |
363 | 452 break; |
507
996041463028
Removed TOK.WCharLiteral and TOK.DCharLiteral.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
485
diff
changeset
|
453 case TOK.CharLiteral: |
391
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
390
diff
changeset
|
454 print.format(tags[DP.CharLiteral], srcText); |
363 | 455 break; |
456 case TOK.Assign, TOK.Equal, | |
457 TOK.Less, TOK.Greater, | |
458 TOK.LShiftAssign, TOK.LShift, | |
459 TOK.RShiftAssign, TOK.RShift, | |
460 TOK.URShiftAssign, TOK.URShift, | |
461 TOK.OrAssign, TOK.OrBinary, | |
462 TOK.AndAssign, TOK.AndBinary, | |
463 TOK.PlusAssign, TOK.PlusPlus, TOK.Plus, | |
464 TOK.MinusAssign, TOK.MinusMinus, TOK.Minus, | |
465 TOK.DivAssign, TOK.Div, | |
466 TOK.MulAssign, TOK.Mul, | |
467 TOK.ModAssign, TOK.Mod, | |
468 TOK.XorAssign, TOK.Xor, | |
469 TOK.CatAssign, | |
470 TOK.Tilde, | |
471 TOK.Unordered, | |
472 TOK.UorE, | |
473 TOK.UorG, | |
474 TOK.UorGorE, | |
475 TOK.UorL, | |
476 TOK.UorLorE, | |
477 TOK.LorEorG: | |
391
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
390
diff
changeset
|
478 print.format(tags[DP.Operator], srcText); |
363 | 479 break; |
480 case TOK.LorG: | |
391
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
390
diff
changeset
|
481 print(tags[DP.LorG]); |
363 | 482 break; |
483 case TOK.LessEqual: | |
391
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
390
diff
changeset
|
484 print(tags[DP.LessEqual]); |
363 | 485 break; |
486 case TOK.GreaterEqual: | |
391
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
390
diff
changeset
|
487 print(tags[DP.GreaterEqual]); |
363 | 488 break; |
489 case TOK.AndLogical: | |
391
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
390
diff
changeset
|
490 print(tags[DP.AndLogical]); |
363 | 491 break; |
492 case TOK.OrLogical: | |
391
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
390
diff
changeset
|
493 print(tags[DP.OrLogical]); |
363 | 494 break; |
495 case TOK.NotEqual: | |
391
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
390
diff
changeset
|
496 print(tags[DP.NotEqual]); |
363 | 497 break; |
498 case TOK.Not: | |
499 // Check if this is part of a template instantiation. | |
391
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
390
diff
changeset
|
500 // TODO: comments aren't skipped. Use Token.nextNWS and Token.prevNWS |
363 | 501 if (token.prev.type == TOK.Identifier && token.next.type == TOK.LParen) |
502 goto default; | |
391
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
390
diff
changeset
|
503 print(tags[DP.Not]); |
363 | 504 break; |
505 case TOK.Int32, TOK.Int64, TOK.Uint32, TOK.Uint64, | |
506 TOK.Float32, TOK.Float64, TOK.Float80, | |
507 TOK.Imaginary32, TOK.Imaginary64, TOK.Imaginary80: | |
391
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
390
diff
changeset
|
508 print.format(tags[DP.Number], srcText); |
363 | 509 break; |
510 case TOK.LParen, TOK.RParen, TOK.LBracket, | |
511 TOK.RBracket, TOK.LBrace, TOK.RBrace: | |
391
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
390
diff
changeset
|
512 print.format(tags[DP.Bracket], srcText); |
363 | 513 break; |
514 case TOK.Shebang: | |
391
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
390
diff
changeset
|
515 print.format(tags[DP.Shebang], srcText); |
363 | 516 break; |
517 case TOK.HashLine: | |
518 void printWS(char* start, char* end) | |
519 { | |
520 if (start != end) | |
391
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
390
diff
changeset
|
521 print(start[0 .. end - start]); |
363 | 522 } |
391
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
390
diff
changeset
|
523 print(tags[DP.HLineBegin]); |
485
ea8c7459f1c4
Changed a lot of things in the Lexer.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
422
diff
changeset
|
524 auto num = token.tokLineNum; |
388
ae154eceba65
Applied some fixes to scanning and printing #line tokens.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
379
diff
changeset
|
525 if (num is null) |
ae154eceba65
Applied some fixes to scanning and printing #line tokens.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
379
diff
changeset
|
526 { |
391
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
390
diff
changeset
|
527 print(token.srcText); |
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
390
diff
changeset
|
528 print(tags[DP.HLineEnd]); |
388
ae154eceba65
Applied some fixes to scanning and printing #line tokens.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
379
diff
changeset
|
529 break; |
ae154eceba65
Applied some fixes to scanning and printing #line tokens.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
379
diff
changeset
|
530 } |
363 | 531 // Print whitespace between #line and number |
388
ae154eceba65
Applied some fixes to scanning and printing #line tokens.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
379
diff
changeset
|
532 auto ptr = token.start; |
ae154eceba65
Applied some fixes to scanning and printing #line tokens.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
379
diff
changeset
|
533 printWS(ptr, num.start); // prints "#line" as well |
391
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
390
diff
changeset
|
534 printToken(num, tags, print); |
485
ea8c7459f1c4
Changed a lot of things in the Lexer.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
422
diff
changeset
|
535 if (token.tokLineFilespec) |
363 | 536 { |
485
ea8c7459f1c4
Changed a lot of things in the Lexer.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
422
diff
changeset
|
537 auto filespec = token.tokLineFilespec; |
363 | 538 // Print whitespace between number and filespec |
539 printWS(num.end, filespec.start); | |
391
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
390
diff
changeset
|
540 print.format(tags[DP.Filespec], xml_escape(filespec.srcText)); |
363 | 541 |
542 ptr = filespec.end; | |
543 } | |
544 else | |
545 ptr = num.end; | |
546 // Print remaining whitespace | |
547 printWS(ptr, token.end); | |
391
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
390
diff
changeset
|
548 print(tags[DP.HLineEnd]); |
363 | 549 break; |
550 default: | |
551 if (token.isKeyword()) | |
391
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
390
diff
changeset
|
552 print.format(tags[DP.Keyword], srcText); |
363 | 553 else if (token.isSpecialToken) |
391
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
390
diff
changeset
|
554 print.format(tags[DP.SpecialToken], srcText); |
363 | 555 else |
391
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
390
diff
changeset
|
556 print(srcText); |
363 | 557 } |
558 } |