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