annotate trunk/src/main.d @ 343:95f1b6e43214

- Removed TOK.Special and added an own entry for each special token. - Added method finalizeSpecialToken() which assigns a token a value according to its semantics. - Added VENDOR constant.
author aziz
date Thu, 23 Aug 2007 14:10:04 +0000
parents 63c6afb915f7
children 73f81f21f870
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
8ba2570de175 Initial import.
aziz
parents:
diff changeset
1 /++
8ba2570de175 Initial import.
aziz
parents:
diff changeset
2 Author: Aziz Köksal
249
32d354584b28 - Upgraded license notices to GPL3.
aziz
parents: 213
diff changeset
3 License: GPL3
0
8ba2570de175 Initial import.
aziz
parents:
diff changeset
4 +/
8ba2570de175 Initial import.
aziz
parents:
diff changeset
5 module dparser;
3
4bbce78bfb1e - Added TOK enum.
aziz
parents: 0
diff changeset
6 import std.stdio;
4
92df59b1ec4a - Started implementation of scan().
aziz
parents: 3
diff changeset
7 import std.file;
327
a48a987f7515 - Added package dil to import declarations.
aziz
parents: 323
diff changeset
8 import dil.Parser;
a48a987f7515 - Added package dil to import declarations.
aziz
parents: 323
diff changeset
9 import dil.Lexer;
a48a987f7515 - Added package dil to import declarations.
aziz
parents: 323
diff changeset
10 import dil.Token;
a48a987f7515 - Added package dil to import declarations.
aziz
parents: 323
diff changeset
11 import dil.Messages;
a48a987f7515 - Added package dil to import declarations.
aziz
parents: 323
diff changeset
12 import dil.Settings;
a48a987f7515 - Added package dil to import declarations.
aziz
parents: 323
diff changeset
13 import dil.Declarations, dil.Expressions, dil.SyntaxTree;
306
9c866aadcb5b - Moved code out of main() to separate functions.
aziz
parents: 305
diff changeset
14
0
8ba2570de175 Initial import.
aziz
parents:
diff changeset
15 void main(char[][] args)
8ba2570de175 Initial import.
aziz
parents:
diff changeset
16 {
309
b4d842b0d2c7 - Added new files Settings.d, config.d and lang_en.d
aziz
parents: 308
diff changeset
17 GlobalSettings.load();
b4d842b0d2c7 - Added new files Settings.d, config.d and lang_en.d
aziz
parents: 308
diff changeset
18
306
9c866aadcb5b - Moved code out of main() to separate functions.
aziz
parents: 305
diff changeset
19 if (args.length <= 1)
338
63c6afb915f7 - Made some corrections to the message catalogs.
aziz
parents: 334
diff changeset
20 return writefln(helpMain());
306
9c866aadcb5b - Moved code out of main() to separate functions.
aziz
parents: 305
diff changeset
21
9c866aadcb5b - Moved code out of main() to separate functions.
aziz
parents: 305
diff changeset
22 string command = args[1];
9c866aadcb5b - Moved code out of main() to separate functions.
aziz
parents: 305
diff changeset
23 switch (command)
9c866aadcb5b - Moved code out of main() to separate functions.
aziz
parents: 305
diff changeset
24 {
329
17f43b0d6106 - Renamed command highlight to generate.
aziz
parents: 328
diff changeset
25 case "gen", "generate":
322
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
26 char[] fileName;
329
17f43b0d6106 - Renamed command highlight to generate.
aziz
parents: 328
diff changeset
27 DocOption options = DocOption.Tokens;
322
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
28 foreach (arg; args[2..$])
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
29 {
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
30 switch (arg)
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
31 {
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
32 case "--syntax":
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
33 options |= DocOption.Syntax; break;
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
34 case "--xml":
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
35 options |= DocOption.XML; break;
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
36 case "--html":
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
37 options |= DocOption.HTML; break;
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
38 default:
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
39 fileName = arg;
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
40 }
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
41 }
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
42 if (!(options & (DocOption.XML | DocOption.HTML)))
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
43 options |= DocOption.XML; // Default to XML.
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
44 if (options & DocOption.Syntax)
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
45 syntaxToDoc(fileName, options);
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
46 else
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
47 tokensToDoc(fileName, options);
306
9c866aadcb5b - Moved code out of main() to separate functions.
aziz
parents: 305
diff changeset
48 break;
312
fa0b6f32c1ae - Added Special to enum TOK.
aziz
parents: 309
diff changeset
49 case "parse":
fa0b6f32c1ae - Added Special to enum TOK.
aziz
parents: 309
diff changeset
50 if (args.length == 3)
fa0b6f32c1ae - Added Special to enum TOK.
aziz
parents: 309
diff changeset
51 parse(args[2]);
fa0b6f32c1ae - Added Special to enum TOK.
aziz
parents: 309
diff changeset
52 break;
338
63c6afb915f7 - Made some corrections to the message catalogs.
aziz
parents: 334
diff changeset
53 case "?", "help":
63c6afb915f7 - Made some corrections to the message catalogs.
aziz
parents: 334
diff changeset
54 if (args.length == 3)
63c6afb915f7 - Made some corrections to the message catalogs.
aziz
parents: 334
diff changeset
55 printHelp(args[2]);
63c6afb915f7 - Made some corrections to the message catalogs.
aziz
parents: 334
diff changeset
56 else
63c6afb915f7 - Made some corrections to the message catalogs.
aziz
parents: 334
diff changeset
57 writefln(helpMain());
63c6afb915f7 - Made some corrections to the message catalogs.
aziz
parents: 334
diff changeset
58 break;
306
9c866aadcb5b - Moved code out of main() to separate functions.
aziz
parents: 305
diff changeset
59 default:
9c866aadcb5b - Moved code out of main() to separate functions.
aziz
parents: 305
diff changeset
60 }
9c866aadcb5b - Moved code out of main() to separate functions.
aziz
parents: 305
diff changeset
61 }
9c866aadcb5b - Moved code out of main() to separate functions.
aziz
parents: 305
diff changeset
62
338
63c6afb915f7 - Made some corrections to the message catalogs.
aziz
parents: 334
diff changeset
63 const char[] COMMANDS =
63c6afb915f7 - Made some corrections to the message catalogs.
aziz
parents: 334
diff changeset
64 " generate (gen)\n"
63c6afb915f7 - Made some corrections to the message catalogs.
aziz
parents: 334
diff changeset
65 " help (?)\n";
63c6afb915f7 - Made some corrections to the message catalogs.
aziz
parents: 334
diff changeset
66
63c6afb915f7 - Made some corrections to the message catalogs.
aziz
parents: 334
diff changeset
67 char[] helpMain()
63c6afb915f7 - Made some corrections to the message catalogs.
aziz
parents: 334
diff changeset
68 {
63c6afb915f7 - Made some corrections to the message catalogs.
aziz
parents: 334
diff changeset
69 return format(MID.HelpMain, VERSION, COMMANDS, COMPILED_WITH, COMPILED_VERSION, COMPILED_DATE);
63c6afb915f7 - Made some corrections to the message catalogs.
aziz
parents: 334
diff changeset
70 }
63c6afb915f7 - Made some corrections to the message catalogs.
aziz
parents: 334
diff changeset
71
63c6afb915f7 - Made some corrections to the message catalogs.
aziz
parents: 334
diff changeset
72 void printHelp(char[] command)
63c6afb915f7 - Made some corrections to the message catalogs.
aziz
parents: 334
diff changeset
73 {
63c6afb915f7 - Made some corrections to the message catalogs.
aziz
parents: 334
diff changeset
74 char[] msg;
63c6afb915f7 - Made some corrections to the message catalogs.
aziz
parents: 334
diff changeset
75 switch (command)
63c6afb915f7 - Made some corrections to the message catalogs.
aziz
parents: 334
diff changeset
76 {
63c6afb915f7 - Made some corrections to the message catalogs.
aziz
parents: 334
diff changeset
77 case "gen", "generate":
63c6afb915f7 - Made some corrections to the message catalogs.
aziz
parents: 334
diff changeset
78 msg = GetMsg(MID.HelpGenerate);
63c6afb915f7 - Made some corrections to the message catalogs.
aziz
parents: 334
diff changeset
79 break;
63c6afb915f7 - Made some corrections to the message catalogs.
aziz
parents: 334
diff changeset
80 default:
63c6afb915f7 - Made some corrections to the message catalogs.
aziz
parents: 334
diff changeset
81 msg = helpMain();
63c6afb915f7 - Made some corrections to the message catalogs.
aziz
parents: 334
diff changeset
82 }
63c6afb915f7 - Made some corrections to the message catalogs.
aziz
parents: 334
diff changeset
83 writefln(msg);
63c6afb915f7 - Made some corrections to the message catalogs.
aziz
parents: 334
diff changeset
84 }
63c6afb915f7 - Made some corrections to the message catalogs.
aziz
parents: 334
diff changeset
85
322
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
86 enum DocOption
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
87 {
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
88 Tokens,
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
89 Syntax = 1<<1,
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
90 HTML = 1<<2,
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
91 XML = 1<<3
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
92 }
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
93
306
9c866aadcb5b - Moved code out of main() to separate functions.
aziz
parents: 305
diff changeset
94 void parse(string fileName)
9c866aadcb5b - Moved code out of main() to separate functions.
aziz
parents: 305
diff changeset
95 {
9c866aadcb5b - Moved code out of main() to separate functions.
aziz
parents: 305
diff changeset
96 auto sourceText = cast(char[]) std.file.read(fileName);
9c866aadcb5b - Moved code out of main() to separate functions.
aziz
parents: 305
diff changeset
97 auto parser = new Parser(sourceText, fileName);
213
b0ebde88d2d4 - Added members trying, errorCount and prev (for debugging) to Parser.
aziz
parents: 112
diff changeset
98 parser.start();
305
df237b3b5f09 - Added class Declarations.
aziz
parents: 303
diff changeset
99 auto root = parser.parseModule();
299
559d5d62e0c1 - Added checks for null before adding member to Node.children.
aziz
parents: 249
diff changeset
100
559d5d62e0c1 - Added checks for null before adding member to Node.children.
aziz
parents: 249
diff changeset
101 void print(Node[] decls, char[] indent)
559d5d62e0c1 - Added checks for null before adding member to Node.children.
aziz
parents: 249
diff changeset
102 {
559d5d62e0c1 - Added checks for null before adding member to Node.children.
aziz
parents: 249
diff changeset
103 foreach(decl; decls)
559d5d62e0c1 - Added checks for null before adding member to Node.children.
aziz
parents: 249
diff changeset
104 {
559d5d62e0c1 - Added checks for null before adding member to Node.children.
aziz
parents: 249
diff changeset
105 assert(decl !is null);
303
bc1112f7933d - Added keyword debug to some statements.
aziz
parents: 299
diff changeset
106 writefln(indent, decl.classinfo.name, ": begin=%s end=%s", decl.begin ? decl.begin.srcText : "\33[31mnull\33[0m", decl.end ? decl.end.srcText : "\33[31mnull\33[0m");
299
559d5d62e0c1 - Added checks for null before adding member to Node.children.
aziz
parents: 249
diff changeset
107 print(decl.children, indent ~ " ");
559d5d62e0c1 - Added checks for null before adding member to Node.children.
aziz
parents: 249
diff changeset
108 }
559d5d62e0c1 - Added checks for null before adding member to Node.children.
aziz
parents: 249
diff changeset
109 }
305
df237b3b5f09 - Added class Declarations.
aziz
parents: 303
diff changeset
110 print(root.children, "");
213
b0ebde88d2d4 - Added members trying, errorCount and prev (for debugging) to Parser.
aziz
parents: 112
diff changeset
111 foreach (error; parser.errors)
b0ebde88d2d4 - Added members trying, errorCount and prev (for debugging) to Parser.
aziz
parents: 112
diff changeset
112 {
b0ebde88d2d4 - Added members trying, errorCount and prev (for debugging) to Parser.
aziz
parents: 112
diff changeset
113 writefln(`%s(%d)P: %s`, parser.lx.fileName, error.loc, error.getMsg);
b0ebde88d2d4 - Added members trying, errorCount and prev (for debugging) to Parser.
aziz
parents: 112
diff changeset
114 }
306
9c866aadcb5b - Moved code out of main() to separate functions.
aziz
parents: 305
diff changeset
115 }
213
b0ebde88d2d4 - Added members trying, errorCount and prev (for debugging) to Parser.
aziz
parents: 112
diff changeset
116
306
9c866aadcb5b - Moved code out of main() to separate functions.
aziz
parents: 305
diff changeset
117 char[] xml_escape(char[] text)
9c866aadcb5b - Moved code out of main() to separate functions.
aziz
parents: 305
diff changeset
118 {
9c866aadcb5b - Moved code out of main() to separate functions.
aziz
parents: 305
diff changeset
119 char[] result;
9c866aadcb5b - Moved code out of main() to separate functions.
aziz
parents: 305
diff changeset
120 foreach(c; text)
9c866aadcb5b - Moved code out of main() to separate functions.
aziz
parents: 305
diff changeset
121 switch(c)
9c866aadcb5b - Moved code out of main() to separate functions.
aziz
parents: 305
diff changeset
122 {
9c866aadcb5b - Moved code out of main() to separate functions.
aziz
parents: 305
diff changeset
123 case '<': result ~= "&lt;"; break;
9c866aadcb5b - Moved code out of main() to separate functions.
aziz
parents: 305
diff changeset
124 case '>': result ~= "&gt;"; break;
9c866aadcb5b - Moved code out of main() to separate functions.
aziz
parents: 305
diff changeset
125 case '&': result ~= "&amp;"; break;
9c866aadcb5b - Moved code out of main() to separate functions.
aziz
parents: 305
diff changeset
126 default: result ~= c;
9c866aadcb5b - Moved code out of main() to separate functions.
aziz
parents: 305
diff changeset
127 }
9c866aadcb5b - Moved code out of main() to separate functions.
aziz
parents: 305
diff changeset
128 return result;
9c866aadcb5b - Moved code out of main() to separate functions.
aziz
parents: 305
diff changeset
129 }
0
8ba2570de175 Initial import.
aziz
parents:
diff changeset
130
322
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
131 char[] getShortClassName(Node n)
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
132 {
334
ca8ad7c34d6a - Added a name table to getShortClassName().
aziz
parents: 330
diff changeset
133 static char[][] name_table;
ca8ad7c34d6a - Added a name table to getShortClassName().
aziz
parents: 330
diff changeset
134 if (name_table is null)
ca8ad7c34d6a - Added a name table to getShortClassName().
aziz
parents: 330
diff changeset
135 name_table = new char[][NodeKind.max+1];
ca8ad7c34d6a - Added a name table to getShortClassName().
aziz
parents: 330
diff changeset
136 char[] name = name_table[n.kind];
ca8ad7c34d6a - Added a name table to getShortClassName().
aziz
parents: 330
diff changeset
137 if (name !is null)
ca8ad7c34d6a - Added a name table to getShortClassName().
aziz
parents: 330
diff changeset
138 return name;
ca8ad7c34d6a - Added a name table to getShortClassName().
aziz
parents: 330
diff changeset
139
322
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
140 alias std.string.find find;
334
ca8ad7c34d6a - Added a name table to getShortClassName().
aziz
parents: 330
diff changeset
141 name = n.classinfo.name;
329
17f43b0d6106 - Renamed command highlight to generate.
aziz
parents: 328
diff changeset
142 name = name[find(name, ".")+1 .. $]; // Remove package name
322
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
143 name = name[find(name, ".")+1 .. $]; // Remove module name
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
144 char[] remove;
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
145 switch (n.category)
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
146 {
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
147 alias NodeCategory NC;
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
148 case NC.Declaration: remove = "Declaration"; break;
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
149 case NC.Statement:
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
150 if (n.kind == NodeKind.Statements)
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
151 return name;
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
152 remove = "Statement";
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
153 break;
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
154 case NC.Expression: remove = "Expression"; break;
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
155 case NC.Type: remove = "Type"; break;
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
156 case NC.Other: return name;
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
157 default:
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
158 }
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
159 // Remove common suffix.
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
160 auto idx = find(name, remove);
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
161 if (idx != -1)
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
162 name = name[0 .. idx];
334
ca8ad7c34d6a - Added a name table to getShortClassName().
aziz
parents: 330
diff changeset
163 // Store the name.
ca8ad7c34d6a - Added a name table to getShortClassName().
aziz
parents: 330
diff changeset
164 name_table[n.kind] = name;
322
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
165 return name;
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
166 }
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
167
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
168 enum DocPart
306
9c866aadcb5b - Moved code out of main() to separate functions.
aziz
parents: 305
diff changeset
169 {
322
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
170 Head,
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
171 CompBegin,
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
172 CompEnd,
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
173 Error,
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
174 SyntaxBegin,
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
175 SyntaxEnd,
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
176 SrcBegin,
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
177 SrcEnd,
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
178 Tail,
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
179 // Tokens:
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
180 Identifier,
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
181 Comment,
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
182 StringLiteral,
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
183 CharLiteral,
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
184 Operator,
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
185 LorG,
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
186 LessEqual,
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
187 GreaterEqual,
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
188 AndLogical,
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
189 OrLogical,
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
190 NotEqual,
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
191 Not,
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
192 Number,
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
193 Bracket,
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
194 SpecialToken,
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
195 Shebang,
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
196 Keyword,
328
39f93a4ec416 - Added code for printing #line and filespec tokens.
aziz
parents: 327
diff changeset
197 HLineBegin,
39f93a4ec416 - Added code for printing #line and filespec tokens.
aziz
parents: 327
diff changeset
198 HLineEnd,
39f93a4ec416 - Added code for printing #line and filespec tokens.
aziz
parents: 327
diff changeset
199 Filespec,
322
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
200 }
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
201
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
202 auto html_tags = [
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
203 // Head
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
204 `<html>`\n
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
205 `<head>`\n
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
206 `<meta http-equiv="Content-Type" content="text/html; charset=utf-8">`\n
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
207 `<link href="dil_html.css" rel="stylesheet" type="text/css">`\n
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
208 `</head>`\n
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
209 `<body>`[],
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
210 // CompBegin
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
211 `<div class="compilerinfo">`,
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
212 // CompEnd
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
213 `</div>`,
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
214 // Error
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
215 `<p class="error %s">%s(%d)%s: %s</p>`,
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
216 // SyntaxBegin
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
217 `<span class="%s %s">`,
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
218 // SyntaxEnd
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
219 `</span>`,
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
220 // SrcBegin
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
221 `<pre class="sourcecode">`,
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
222 // SrcEnd
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
223 `</pre>`,
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
224 // Tail
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
225 `</html>`,
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
226 // Identifier
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
227 `<span class="i">%s</span>`,
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
228 // Comment
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
229 `<span class="c%s">%s</span>`,
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
230 // StringLiteral
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
231 `<span class="sl">%s</span>`,
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
232 // CharLiteral
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
233 `<span class="cl">%s</span>`,
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
234 // Operator
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
235 `<span class="op">%s</span>`,
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
236 // LorG
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
237 `<span class="oplg">&lt;&gt;</span>`,
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
238 // LessEqual
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
239 `<span class="ople">&lt;=</span>`,
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
240 // GreaterEqual
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
241 `<span class="opge">&gt;=</span>`,
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
242 // AndLogical
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
243 `<span class="opaa">&amp;&amp;</span>`,
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
244 // OrLogical
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
245 `<span class="opoo">||</span>`,
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
246 // NotEqual
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
247 `<span class="opne">!=</span>`,
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
248 // Not
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
249 `<span class="opn">!</span>`,
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
250 // Number
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
251 `<span class="n">%s</span>`,
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
252 // Bracket
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
253 `<span class="br">%s</span>`,
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
254 // SpecialToken
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
255 `<span class="st">%s</span>`,
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
256 // Shebang
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
257 `<span class="shebang">%s</span>`,
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
258 // Keyword
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
259 `<span class="k">%s</span>`,
328
39f93a4ec416 - Added code for printing #line and filespec tokens.
aziz
parents: 327
diff changeset
260 // HLineBegin
39f93a4ec416 - Added code for printing #line and filespec tokens.
aziz
parents: 327
diff changeset
261 `<span class="hl">#line`,
39f93a4ec416 - Added code for printing #line and filespec tokens.
aziz
parents: 327
diff changeset
262 // HLineEnd
39f93a4ec416 - Added code for printing #line and filespec tokens.
aziz
parents: 327
diff changeset
263 "</span>",
39f93a4ec416 - Added code for printing #line and filespec tokens.
aziz
parents: 327
diff changeset
264 // Filespec
39f93a4ec416 - Added code for printing #line and filespec tokens.
aziz
parents: 327
diff changeset
265 `<span class="fs">%s</span>`,
322
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
266 ];
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
267
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
268 auto xml_tags = [
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
269 // Head
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
270 `<?xml version="1.0"?>`\n
330
44fc02d1eae6 - Renamed format.css to dil_xml.css.
aziz
parents: 329
diff changeset
271 `<?xml-stylesheet href="dil_xml.css" type="text/css"?>`\n
322
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
272 `<root>`[],
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
273 // CompBegin
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
274 `<compilerinfo>`,
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
275 // CompEnd
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
276 `</compilerinfo>`,
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
277 // Error
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
278 `<error t="%s">%s(%d)%s: %s</error>`,
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
279 // SyntaxBegin
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
280 `<%s t="%s">`,
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
281 // SyntaxEnd
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
282 `</%s>`,
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
283 // SrcBegin
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
284 `<sourcecode>`,
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
285 // SrcEnd
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
286 `</sourcecode>`,
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
287 // Tail
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
288 `</root>`,
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
289 // Identifier
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
290 "<i>%s</i>",
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
291 // Comment
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
292 `<c t="%s">%s</c>`,
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
293 // StringLiteral
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
294 "<sl>%s</sl>",
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
295 // CharLiteral
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
296 "<cl>%s</cl>",
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
297 // Operator
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
298 "<op>%s</op>",
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
299 // LorG
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
300 `<op t="lg">&lt;&gt;</op>`,
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
301 // LessEqual
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
302 `<op t="le">&lt;=</op>`,
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
303 // GreaterEqual
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
304 `<op t="ge">&gt;=</op>`,
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
305 // AndLogical
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
306 `<op t="aa">&amp;&amp;</op>`,
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
307 // OrLogical
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
308 `<op t="oo">||</op>`,
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
309 // NotEqual
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
310 `<op t="ne">!=</op>`,
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
311 // Not
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
312 `<op t="n">!</op>`,
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
313 // Number
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
314 "<n>%s</n>",
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
315 // Bracket
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
316 "<br>%s</br>",
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
317 // SpecialToken
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
318 "<st>%s</st>",
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
319 // Shebang
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
320 "<shebang>%s</shebang>",
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
321 // Keyword
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
322 "<k>%s</k>",
328
39f93a4ec416 - Added code for printing #line and filespec tokens.
aziz
parents: 327
diff changeset
323 // HLineBegin
39f93a4ec416 - Added code for printing #line and filespec tokens.
aziz
parents: 327
diff changeset
324 "<hl>#line",
39f93a4ec416 - Added code for printing #line and filespec tokens.
aziz
parents: 327
diff changeset
325 // HLineEnd
39f93a4ec416 - Added code for printing #line and filespec tokens.
aziz
parents: 327
diff changeset
326 "</hl>",
39f93a4ec416 - Added code for printing #line and filespec tokens.
aziz
parents: 327
diff changeset
327 // Filespec
39f93a4ec416 - Added code for printing #line and filespec tokens.
aziz
parents: 327
diff changeset
328 "<fs>%s</fs>",
322
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
329 ];
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
330
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
331 static assert(html_tags.length == DocPart.max+1);
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
332 static assert(xml_tags.length == DocPart.max+1);
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
333
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
334 void syntaxToDoc(string fileName, DocOption options)
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
335 {
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
336 auto tags = options & DocOption.HTML ? html_tags : xml_tags;
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
337 auto sourceText = cast(char[]) std.file.read(fileName);
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
338 auto parser = new Parser(sourceText, fileName);
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
339 parser.start();
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
340 auto root = parser.parseModule();
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
341 auto lx = parser.lx;
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
342
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
343 auto token = lx.head;
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
344 char* end = lx.text.ptr;
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
345
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
346 writefln(tags[DocPart.Head]);
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
347 // Output error messages.
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
348 if (lx.errors.length || parser.errors.length)
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
349 {
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
350 writefln(tags[DocPart.CompBegin]);
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
351 foreach (error; lx.errors)
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
352 {
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
353 writefln(tags[DocPart.Error], "L", lx.fileName, error.loc, "L", xml_escape(error.getMsg));
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
354 }
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
355 foreach (error; parser.errors)
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
356 {
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
357 writefln(tags[DocPart.Error], "P", lx.fileName, error.loc, "P", xml_escape(error.getMsg));
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
358 }
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
359 writefln(tags[DocPart.CompEnd]);
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
360 }
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
361 writef(tags[DocPart.SrcBegin]);
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
362
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
363 Node[][Token*] beginNodes, endNodes;
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
364
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
365 void populateAAs(Node[] nodes)
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
366 {
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
367 foreach (node; nodes)
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
368 {
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
369 auto begin = node.begin;
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
370 if (begin)
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
371 {
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
372 auto end = node.end;
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
373 assert(end);
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
374 beginNodes[begin] ~= node;
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
375 endNodes[end] ~= node;
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
376 }
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
377 if (node.children.length)
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
378 populateAAs(node.children);
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
379 }
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
380 }
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
381 populateAAs(root.children);
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
382
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
383 char[] getTag(NodeCategory nc)
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
384 {
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
385 char[] tag;
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
386 switch (nc)
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
387 {
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
388 alias NodeCategory NC;
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
389 case NC.Declaration: tag = "d"; break;
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
390 case NC.Statement: tag = "s"; break;
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
391 case NC.Expression: tag = "e"; break;
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
392 case NC.Type: tag = "t"; break;
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
393 case NC.Other: tag = "o"; break;
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
394 default:
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
395 }
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
396 return tag;
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
397 }
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
398
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
399 // Traverse linked list and print tokens.
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
400 while (token.type != TOK.EOF)
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
401 {
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
402 token = token.next;
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
403
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
404 // Print whitespace between previous and current token.
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
405 if (end != token.start)
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
406 writef("%s", end[0 .. token.start - end]);
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
407
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
408 Node[]* nodes = token in beginNodes;
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
409
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
410 if (nodes)
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
411 {
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
412 foreach (node; *nodes)
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
413 writef(tags[DocPart.SyntaxBegin], getTag(node.category), getShortClassName(node));
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
414 }
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
415
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
416 printToken(token, tags);
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
417
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
418 nodes = token in endNodes;
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
419
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
420 if (nodes)
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
421 {
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
422 foreach_reverse (node; *nodes)
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
423 if (options & DocOption.HTML)
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
424 writef(tags[DocPart.SyntaxEnd]);
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
425 else
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
426 writef(tags[DocPart.SyntaxEnd], getTag(node.category));
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
427 }
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
428
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
429 end = token.end;
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
430 }
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
431 writef(tags[DocPart.SrcEnd], tags[DocPart.Tail]);
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
432 }
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
433
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
434 void tokensToDoc(string fileName, DocOption options)
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
435 {
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
436 auto tags = options & DocOption.HTML ? html_tags : xml_tags;
306
9c866aadcb5b - Moved code out of main() to separate functions.
aziz
parents: 305
diff changeset
437 auto sourceText = cast(char[]) std.file.read(fileName);
9c866aadcb5b - Moved code out of main() to separate functions.
aziz
parents: 305
diff changeset
438 auto lx = new Lexer(sourceText, fileName);
9c866aadcb5b - Moved code out of main() to separate functions.
aziz
parents: 305
diff changeset
439
9c866aadcb5b - Moved code out of main() to separate functions.
aziz
parents: 305
diff changeset
440 auto token = lx.getTokens();
5
79b4e8848794 - Started writing XML generator.
aziz
parents: 4
diff changeset
441 char* end = lx.text.ptr;
79b4e8848794 - Started writing XML generator.
aziz
parents: 4
diff changeset
442
322
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
443 writefln(tags[DocPart.Head]);
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
444
306
9c866aadcb5b - Moved code out of main() to separate functions.
aziz
parents: 305
diff changeset
445 if (lx.errors.length)
51
cadd2bfe686c - Displaying error messages in XML.
aziz
parents: 44
diff changeset
446 {
322
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
447 writefln(tags[DocPart.CompBegin]);
306
9c866aadcb5b - Moved code out of main() to separate functions.
aziz
parents: 305
diff changeset
448 foreach (error; lx.errors)
9c866aadcb5b - Moved code out of main() to separate functions.
aziz
parents: 305
diff changeset
449 {
322
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
450 writefln(tags[DocPart.Error], "L", lx.fileName, error.loc, "L", xml_escape(error.getMsg));
306
9c866aadcb5b - Moved code out of main() to separate functions.
aziz
parents: 305
diff changeset
451 }
322
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
452 writefln(tags[DocPart.CompEnd]);
51
cadd2bfe686c - Displaying error messages in XML.
aziz
parents: 44
diff changeset
453 }
322
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
454 writef(tags[DocPart.SrcBegin]);
306
9c866aadcb5b - Moved code out of main() to separate functions.
aziz
parents: 305
diff changeset
455
9c866aadcb5b - Moved code out of main() to separate functions.
aziz
parents: 305
diff changeset
456 // Traverse linked list and print tokens.
9c866aadcb5b - Moved code out of main() to separate functions.
aziz
parents: 305
diff changeset
457 while (token.type != TOK.EOF)
4
92df59b1ec4a - Started implementation of scan().
aziz
parents: 3
diff changeset
458 {
306
9c866aadcb5b - Moved code out of main() to separate functions.
aziz
parents: 305
diff changeset
459 token = token.next;
9c866aadcb5b - Moved code out of main() to separate functions.
aziz
parents: 305
diff changeset
460
9c866aadcb5b - Moved code out of main() to separate functions.
aziz
parents: 305
diff changeset
461 // Print whitespace between previous and current token.
5
79b4e8848794 - Started writing XML generator.
aziz
parents: 4
diff changeset
462 if (end != token.start)
328
39f93a4ec416 - Added code for printing #line and filespec tokens.
aziz
parents: 327
diff changeset
463 writef("%s", end[0 .. token.start - end]);
322
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
464 printToken(token, tags);
5
79b4e8848794 - Started writing XML generator.
aziz
parents: 4
diff changeset
465 end = token.end;
4
92df59b1ec4a - Started implementation of scan().
aziz
parents: 3
diff changeset
466 }
322
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
467 writef(\n, tags[DocPart.SrcEnd], \n, tags[DocPart.Tail]);
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
468 }
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
469
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
470 void printToken(Token* token, string[] tags)
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
471 {
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
472 alias DocPart DP;
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
473 string srcText = xml_escape(token.srcText);
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
474
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
475 switch(token.type)
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
476 {
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
477 case TOK.Identifier:
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
478 writef(tags[DP.Identifier], srcText);
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
479 break;
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
480 case TOK.Comment:
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
481 string t;
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
482 switch (token.start[1])
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
483 {
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
484 case '/': t = "l"; break;
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
485 case '*': t = "b"; break;
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
486 case '+': t = "n"; break;
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
487 default:
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
488 assert(0);
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
489 }
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
490 writef(tags[DP.Comment], t, srcText);
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
491 break;
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
492 case TOK.String:
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
493 writef(tags[DP.StringLiteral], srcText);
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
494 break;
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
495 case TOK.CharLiteral, TOK.WCharLiteral, TOK.DCharLiteral:
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
496 writef(tags[DP.CharLiteral], srcText);
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
497 break;
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
498 case TOK.Assign, TOK.Equal,
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
499 TOK.Less, TOK.Greater,
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
500 TOK.LShiftAssign, TOK.LShift,
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
501 TOK.RShiftAssign, TOK.RShift,
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
502 TOK.URShiftAssign, TOK.URShift,
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
503 TOK.OrAssign, TOK.OrBinary,
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
504 TOK.AndAssign, TOK.AndBinary,
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
505 TOK.PlusAssign, TOK.PlusPlus, TOK.Plus,
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
506 TOK.MinusAssign, TOK.MinusMinus, TOK.Minus,
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
507 TOK.DivAssign, TOK.Div,
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
508 TOK.MulAssign, TOK.Mul,
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
509 TOK.ModAssign, TOK.Mod,
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
510 TOK.XorAssign, TOK.Xor,
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
511 TOK.CatAssign,
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
512 TOK.Tilde,
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
513 TOK.Unordered,
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
514 TOK.UorE,
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
515 TOK.UorG,
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
516 TOK.UorGorE,
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
517 TOK.UorL,
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
518 TOK.UorLorE,
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
519 TOK.LorEorG:
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
520 writef(tags[DP.Operator], srcText);
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
521 break;
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
522 case TOK.LorG:
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
523 writef(tags[DP.LorG]);
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
524 break;
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
525 case TOK.LessEqual:
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
526 writef(tags[DP.LessEqual]);
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
527 break;
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
528 case TOK.GreaterEqual:
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
529 writef(tags[DP.GreaterEqual]);
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
530 break;
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
531 case TOK.AndLogical:
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
532 writef(tags[DP.AndLogical]);
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
533 break;
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
534 case TOK.OrLogical:
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
535 writef(tags[DP.OrLogical]);
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
536 break;
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
537 case TOK.NotEqual:
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
538 writef(tags[DP.NotEqual]);
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
539 break;
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
540 case TOK.Not:
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
541 // Check if this is part of a template instantiation.
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
542 // TODO: comments aren't skipped.
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
543 if (token.prev.type == TOK.Identifier && token.next.type == TOK.LParen)
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
544 goto default;
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
545 writef(tags[DP.Not]);
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
546 break;
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
547 case TOK.Int32, TOK.Int64, TOK.Uint32, TOK.Uint64,
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
548 TOK.Float32, TOK.Float64, TOK.Float80,
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
549 TOK.Imaginary32, TOK.Imaginary64, TOK.Imaginary80:
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
550 writef(tags[DP.Number], srcText);
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
551 break;
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
552 case TOK.LParen, TOK.RParen, TOK.LBracket,
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
553 TOK.RBracket, TOK.LBrace, TOK.RBrace:
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
554 writef(tags[DP.Bracket], srcText);
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
555 break;
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
556 case TOK.Shebang:
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
557 writef(tags[DP.Shebang], srcText);
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
558 break;
323
6259fb93e3dd - Rewrote scanSpecialToken().
aziz
parents: 322
diff changeset
559 case TOK.HashLine:
328
39f93a4ec416 - Added code for printing #line and filespec tokens.
aziz
parents: 327
diff changeset
560 void printWS(char* start, char* end)
39f93a4ec416 - Added code for printing #line and filespec tokens.
aziz
parents: 327
diff changeset
561 {
39f93a4ec416 - Added code for printing #line and filespec tokens.
aziz
parents: 327
diff changeset
562 if (start != end)
39f93a4ec416 - Added code for printing #line and filespec tokens.
aziz
parents: 327
diff changeset
563 writef(start[0 .. end - start]);
39f93a4ec416 - Added code for printing #line and filespec tokens.
aziz
parents: 327
diff changeset
564 }
39f93a4ec416 - Added code for printing #line and filespec tokens.
aziz
parents: 327
diff changeset
565 writef(tags[DP.HLineBegin]);
39f93a4ec416 - Added code for printing #line and filespec tokens.
aziz
parents: 327
diff changeset
566 auto num = token.line_num;
39f93a4ec416 - Added code for printing #line and filespec tokens.
aziz
parents: 327
diff changeset
567 // Print whitespace between #line and number
39f93a4ec416 - Added code for printing #line and filespec tokens.
aziz
parents: 327
diff changeset
568 auto ptr = token.start + "#line".length;
39f93a4ec416 - Added code for printing #line and filespec tokens.
aziz
parents: 327
diff changeset
569 printWS(ptr, num.start);
39f93a4ec416 - Added code for printing #line and filespec tokens.
aziz
parents: 327
diff changeset
570 printToken(num, tags);
39f93a4ec416 - Added code for printing #line and filespec tokens.
aziz
parents: 327
diff changeset
571 if (token.line_filespec)
39f93a4ec416 - Added code for printing #line and filespec tokens.
aziz
parents: 327
diff changeset
572 {
39f93a4ec416 - Added code for printing #line and filespec tokens.
aziz
parents: 327
diff changeset
573 auto filespec = token.line_filespec;
39f93a4ec416 - Added code for printing #line and filespec tokens.
aziz
parents: 327
diff changeset
574 // Print whitespace between number and filespec
39f93a4ec416 - Added code for printing #line and filespec tokens.
aziz
parents: 327
diff changeset
575 printWS(num.end, filespec.start);
39f93a4ec416 - Added code for printing #line and filespec tokens.
aziz
parents: 327
diff changeset
576 writef(tags[DP.Filespec], xml_escape(filespec.srcText));
39f93a4ec416 - Added code for printing #line and filespec tokens.
aziz
parents: 327
diff changeset
577
39f93a4ec416 - Added code for printing #line and filespec tokens.
aziz
parents: 327
diff changeset
578 ptr = filespec.end;
39f93a4ec416 - Added code for printing #line and filespec tokens.
aziz
parents: 327
diff changeset
579 }
39f93a4ec416 - Added code for printing #line and filespec tokens.
aziz
parents: 327
diff changeset
580 else
39f93a4ec416 - Added code for printing #line and filespec tokens.
aziz
parents: 327
diff changeset
581 ptr = num.end;
39f93a4ec416 - Added code for printing #line and filespec tokens.
aziz
parents: 327
diff changeset
582 // Print remaining whitespace
39f93a4ec416 - Added code for printing #line and filespec tokens.
aziz
parents: 327
diff changeset
583 printWS(ptr, token.end);
39f93a4ec416 - Added code for printing #line and filespec tokens.
aziz
parents: 327
diff changeset
584 writef(tags[DP.HLineEnd]);
323
6259fb93e3dd - Rewrote scanSpecialToken().
aziz
parents: 322
diff changeset
585 break;
322
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
586 default:
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
587 if (token.isKeyword())
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
588 writef(tags[DP.Keyword], srcText);
343
95f1b6e43214 - Removed TOK.Special and added an own entry for each special token.
aziz
parents: 338
diff changeset
589 else if (token.isSpecialToken)
95f1b6e43214 - Removed TOK.Special and added an own entry for each special token.
aziz
parents: 338
diff changeset
590 writef(tags[DP.SpecialToken], srcText);
322
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
591 else
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
592 writef("%s", srcText);
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
593 }
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
594 }