Mercurial > projects > dil
annotate trunk/src/cmd/Generate.d @ 392:bb935c6f9b7a
Applied fixes and improvements to the Lexer class.
In scanDelimitedStringLiteral:
- leading newlines are skipped now.
- passed c instead of closing_delim to isUniAlpha().
- the matching string delimiter must follow a newline.
- corrected if-statements and an assert().
Added inTokenString member:
- in scanTokenStringLiteral() this member is increment at the start
and decremented at the end.
- in scanSpecialTokenSequence() this member is checked and #line is not
evaluated if it is not zero.
Added missing break to case 'L' in scanNumber().
In scanReal():
- changed debug switch to a delegate inside an assert().
- removing underscores from buffer more efficiently.
Made some other trivial changes and fixes to other modules.
author | Aziz K?ksal <aziz.koeksal@gmail.com> |
---|---|
date | Mon, 17 Sep 2007 20:35:08 +0200 |
parents | 33b566df6af4 |
children | 6440da4adb07 |
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 { | |
391
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
390
diff
changeset
|
284 print.formatln(tags[DocPart.Error], "L", lx.fileName, error.loc, "L", xml_escape(error.getMsg)); |
363 | 285 } |
286 foreach (error; parser.errors) | |
287 { | |
391
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
390
diff
changeset
|
288 print.formatln(tags[DocPart.Error], "P", lx.fileName, error.loc, "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 { | |
379
a92f7826a4fa
- Fix in parseTemplateArguments_(): calling nested parseType_() instead of parseType() in try_().
aziz
parents:
368
diff
changeset
|
300 assert(node !is null); |
363 | 301 auto begin = node.begin; |
302 if (begin) | |
303 { | |
304 auto end = node.end; | |
305 assert(end); | |
306 beginNodes[begin] ~= node; | |
307 endNodes[end] ~= node; | |
308 } | |
309 if (node.children.length) | |
310 populateAAs(node.children); | |
311 } | |
312 } | |
313 populateAAs(root.children); | |
314 | |
315 char[] getTag(NodeCategory nc) | |
316 { | |
317 char[] tag; | |
318 switch (nc) | |
319 { | |
320 alias NodeCategory NC; | |
321 case NC.Declaration: tag = "d"; break; | |
322 case NC.Statement: tag = "s"; break; | |
323 case NC.Expression: tag = "e"; break; | |
324 case NC.Type: tag = "t"; break; | |
325 case NC.Other: tag = "o"; break; | |
326 default: | |
327 } | |
328 return tag; | |
329 } | |
330 | |
331 // Traverse linked list and print tokens. | |
332 while (token.type != TOK.EOF) | |
333 { | |
334 token = token.next; | |
335 | |
336 Node[]* nodes = token in beginNodes; | |
337 | |
338 if (nodes) | |
339 { | |
340 foreach (node; *nodes) | |
391
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
390
diff
changeset
|
341 print.format(tags[DocPart.SyntaxBegin], getTag(node.category), getShortClassName(node)); |
363 | 342 } |
343 | |
391
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
390
diff
changeset
|
344 printToken(token, tags, print); |
363 | 345 |
346 nodes = token in endNodes; | |
347 | |
348 if (nodes) | |
349 { | |
350 foreach_reverse (node; *nodes) | |
351 if (options & DocOption.HTML) | |
391
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
390
diff
changeset
|
352 print(tags[DocPart.SyntaxEnd]); |
363 | 353 else |
391
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
390
diff
changeset
|
354 print.format(tags[DocPart.SyntaxEnd], getTag(node.category)); |
363 | 355 } |
356 } | |
391
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
390
diff
changeset
|
357 print(\n~tags[DocPart.SrcEnd])(\n~tags[DocPart.Tail]); |
363 | 358 } |
359 | |
391
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
390
diff
changeset
|
360 void tokensToDoc(string fileName, Print!(char) print, DocOption options) |
363 | 361 { |
362 auto tags = options & DocOption.HTML ? html_tags : xml_tags; | |
363 auto sourceText = loadFile(fileName); | |
364 auto lx = new Lexer(sourceText, fileName); | |
365 | |
366 auto token = lx.getTokens(); | |
367 | |
391
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
390
diff
changeset
|
368 print(tags[DocPart.Head]~\n); |
363 | 369 |
370 if (lx.errors.length) | |
371 { | |
391
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
390
diff
changeset
|
372 print(tags[DocPart.CompBegin]~\n); |
363 | 373 foreach (error; lx.errors) |
374 { | |
391
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
390
diff
changeset
|
375 print.formatln(tags[DocPart.Error], "L", lx.fileName, error.loc, "L", xml_escape(error.getMsg)); |
363 | 376 } |
391
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
390
diff
changeset
|
377 print(tags[DocPart.CompEnd]~\n); |
363 | 378 } |
391
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
390
diff
changeset
|
379 print(tags[DocPart.SrcBegin]); |
363 | 380 |
381 // Traverse linked list and print tokens. | |
382 while (token.type != TOK.EOF) | |
383 { | |
384 token = token.next; | |
391
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
390
diff
changeset
|
385 printToken(token, tags, print); |
363 | 386 } |
391
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
390
diff
changeset
|
387 print(\n~tags[DocPart.SrcEnd])(\n~tags[DocPart.Tail]); |
363 | 388 } |
389 | |
391
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
390
diff
changeset
|
390 void printToken(Token* token, string[] tags, Print!(char) print) |
363 | 391 { |
392 alias DocPart DP; | |
393 string srcText = xml_escape(token.srcText); | |
394 | |
390
4d36eea1bbc9
Refactored Lexer.scan().
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
389
diff
changeset
|
395 // Print whitespace. |
4d36eea1bbc9
Refactored Lexer.scan().
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
389
diff
changeset
|
396 if (token.ws) |
391
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
390
diff
changeset
|
397 print(token.ws[0..token.start - token.ws]); |
390
4d36eea1bbc9
Refactored Lexer.scan().
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
389
diff
changeset
|
398 |
363 | 399 switch(token.type) |
400 { | |
401 case TOK.Identifier: | |
391
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
390
diff
changeset
|
402 print.format(tags[DP.Identifier], srcText); |
363 | 403 break; |
404 case TOK.Comment: | |
405 string t; | |
406 switch (token.start[1]) | |
407 { | |
408 case '/': t = "l"; break; | |
409 case '*': t = "b"; break; | |
410 case '+': t = "n"; break; | |
411 default: | |
412 assert(0); | |
413 } | |
391
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
390
diff
changeset
|
414 print.format(tags[DP.Comment], t, srcText); |
363 | 415 break; |
416 case TOK.String: | |
391
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
390
diff
changeset
|
417 print.format(tags[DP.StringLiteral], srcText); |
363 | 418 break; |
419 case TOK.CharLiteral, TOK.WCharLiteral, TOK.DCharLiteral: | |
391
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
390
diff
changeset
|
420 print.format(tags[DP.CharLiteral], srcText); |
363 | 421 break; |
422 case TOK.Assign, TOK.Equal, | |
423 TOK.Less, TOK.Greater, | |
424 TOK.LShiftAssign, TOK.LShift, | |
425 TOK.RShiftAssign, TOK.RShift, | |
426 TOK.URShiftAssign, TOK.URShift, | |
427 TOK.OrAssign, TOK.OrBinary, | |
428 TOK.AndAssign, TOK.AndBinary, | |
429 TOK.PlusAssign, TOK.PlusPlus, TOK.Plus, | |
430 TOK.MinusAssign, TOK.MinusMinus, TOK.Minus, | |
431 TOK.DivAssign, TOK.Div, | |
432 TOK.MulAssign, TOK.Mul, | |
433 TOK.ModAssign, TOK.Mod, | |
434 TOK.XorAssign, TOK.Xor, | |
435 TOK.CatAssign, | |
436 TOK.Tilde, | |
437 TOK.Unordered, | |
438 TOK.UorE, | |
439 TOK.UorG, | |
440 TOK.UorGorE, | |
441 TOK.UorL, | |
442 TOK.UorLorE, | |
443 TOK.LorEorG: | |
391
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
390
diff
changeset
|
444 print.format(tags[DP.Operator], srcText); |
363 | 445 break; |
446 case TOK.LorG: | |
391
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
390
diff
changeset
|
447 print(tags[DP.LorG]); |
363 | 448 break; |
449 case TOK.LessEqual: | |
391
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
390
diff
changeset
|
450 print(tags[DP.LessEqual]); |
363 | 451 break; |
452 case TOK.GreaterEqual: | |
391
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
390
diff
changeset
|
453 print(tags[DP.GreaterEqual]); |
363 | 454 break; |
455 case TOK.AndLogical: | |
391
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
390
diff
changeset
|
456 print(tags[DP.AndLogical]); |
363 | 457 break; |
458 case TOK.OrLogical: | |
391
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
390
diff
changeset
|
459 print(tags[DP.OrLogical]); |
363 | 460 break; |
461 case TOK.NotEqual: | |
391
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
390
diff
changeset
|
462 print(tags[DP.NotEqual]); |
363 | 463 break; |
464 case TOK.Not: | |
465 // 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
|
466 // TODO: comments aren't skipped. Use Token.nextNWS and Token.prevNWS |
363 | 467 if (token.prev.type == TOK.Identifier && token.next.type == TOK.LParen) |
468 goto default; | |
391
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
390
diff
changeset
|
469 print(tags[DP.Not]); |
363 | 470 break; |
471 case TOK.Int32, TOK.Int64, TOK.Uint32, TOK.Uint64, | |
472 TOK.Float32, TOK.Float64, TOK.Float80, | |
473 TOK.Imaginary32, TOK.Imaginary64, TOK.Imaginary80: | |
391
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
390
diff
changeset
|
474 print.format(tags[DP.Number], srcText); |
363 | 475 break; |
476 case TOK.LParen, TOK.RParen, TOK.LBracket, | |
477 TOK.RBracket, TOK.LBrace, TOK.RBrace: | |
391
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
390
diff
changeset
|
478 print.format(tags[DP.Bracket], srcText); |
363 | 479 break; |
480 case TOK.Shebang: | |
391
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
390
diff
changeset
|
481 print.format(tags[DP.Shebang], srcText); |
363 | 482 break; |
483 case TOK.HashLine: | |
484 void printWS(char* start, char* end) | |
485 { | |
486 if (start != end) | |
391
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
390
diff
changeset
|
487 print(start[0 .. end - start]); |
363 | 488 } |
391
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
390
diff
changeset
|
489 print(tags[DP.HLineBegin]); |
363 | 490 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
|
491 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
|
492 { |
391
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
390
diff
changeset
|
493 print(token.srcText); |
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
390
diff
changeset
|
494 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
|
495 break; |
ae154eceba65
Applied some fixes to scanning and printing #line tokens.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
379
diff
changeset
|
496 } |
363 | 497 // 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
|
498 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
|
499 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
|
500 printToken(num, tags, print); |
363 | 501 if (token.line_filespec) |
502 { | |
503 auto filespec = token.line_filespec; | |
504 // Print whitespace between number and filespec | |
505 printWS(num.end, filespec.start); | |
391
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
390
diff
changeset
|
506 print.format(tags[DP.Filespec], xml_escape(filespec.srcText)); |
363 | 507 |
508 ptr = filespec.end; | |
509 } | |
510 else | |
511 ptr = num.end; | |
512 // Print remaining whitespace | |
513 printWS(ptr, token.end); | |
391
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
390
diff
changeset
|
514 print(tags[DP.HLineEnd]); |
363 | 515 break; |
516 default: | |
517 if (token.isKeyword()) | |
391
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
390
diff
changeset
|
518 print.format(tags[DP.Keyword], srcText); |
363 | 519 else if (token.isSpecialToken) |
391
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
390
diff
changeset
|
520 print.format(tags[DP.SpecialToken], srcText); |
363 | 521 else |
391
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
390
diff
changeset
|
522 print(srcText); |
363 | 523 } |
524 } |