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