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