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