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