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