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