annotate trunk/src/main.d @ 392:bb935c6f9b7a

Applied fixes and improvements to the Lexer class. In scanDelimitedStringLiteral: - leading newlines are skipped now. - passed c instead of closing_delim to isUniAlpha(). - the matching string delimiter must follow a newline. - corrected if-statements and an assert(). Added inTokenString member: - in scanTokenStringLiteral() this member is increment at the start and decremented at the end. - in scanSpecialTokenSequence() this member is checked and #line is not evaluated if it is not zero. Added missing break to case 'L' in scanNumber(). In scanReal(): - changed debug switch to a delegate inside an assert(). - removing underscores from buffer more efficiently. Made some other trivial changes and fixes to other modules.
author Aziz K?ksal <aziz.koeksal@gmail.com>
date Mon, 17 Sep 2007 20:35:08 +0200
parents 33b566df6af4
children fcdf7ac5ad27
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 +/
347
73f81f21f870 - Changed module name of main.d from dparser to main.
aziz
parents: 343
diff changeset
5 module main;
327
a48a987f7515 - Added package dil to import declarations.
aziz
parents: 323
diff changeset
6 import dil.Parser;
a48a987f7515 - Added package dil to import declarations.
aziz
parents: 323
diff changeset
7 import dil.Lexer;
a48a987f7515 - Added package dil to import declarations.
aziz
parents: 323
diff changeset
8 import dil.Token;
a48a987f7515 - Added package dil to import declarations.
aziz
parents: 323
diff changeset
9 import dil.Messages;
a48a987f7515 - Added package dil to import declarations.
aziz
parents: 323
diff changeset
10 import dil.Settings;
a48a987f7515 - Added package dil to import declarations.
aziz
parents: 323
diff changeset
11 import dil.Declarations, dil.Expressions, dil.SyntaxTree;
351
97a9a2d7d46d - Added module File.
aziz
parents: 347
diff changeset
12 import dil.File;
363
2b387a3c6b58 - Added package cmd.
aziz
parents: 358
diff changeset
13 import cmd.Generate;
2b387a3c6b58 - Added package cmd.
aziz
parents: 358
diff changeset
14 import cmd.Statistics;
364
1059295c2727 - Every command module has an execute method now.
aziz
parents: 363
diff changeset
15 import cmd.ImportGraph;
391
33b566df6af4 Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 375
diff changeset
16 import common;
33b566df6af4 Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 375
diff changeset
17
33b566df6af4 Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 375
diff changeset
18 import Integer = tango.text.convert.Integer;
306
9c866aadcb5b - Moved code out of main() to separate functions.
aziz
parents: 305
diff changeset
19
0
8ba2570de175 Initial import.
aziz
parents:
diff changeset
20 void main(char[][] args)
8ba2570de175 Initial import.
aziz
parents:
diff changeset
21 {
309
b4d842b0d2c7 - Added new files Settings.d, config.d and lang_en.d
aziz
parents: 308
diff changeset
22 GlobalSettings.load();
b4d842b0d2c7 - Added new files Settings.d, config.d and lang_en.d
aziz
parents: 308
diff changeset
23
306
9c866aadcb5b - Moved code out of main() to separate functions.
aziz
parents: 305
diff changeset
24 if (args.length <= 1)
391
33b566df6af4 Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 375
diff changeset
25 return Stdout(helpMain()).newline;
306
9c866aadcb5b - Moved code out of main() to separate functions.
aziz
parents: 305
diff changeset
26
9c866aadcb5b - Moved code out of main() to separate functions.
aziz
parents: 305
diff changeset
27 string command = args[1];
9c866aadcb5b - Moved code out of main() to separate functions.
aziz
parents: 305
diff changeset
28 switch (command)
9c866aadcb5b - Moved code out of main() to separate functions.
aziz
parents: 305
diff changeset
29 {
329
17f43b0d6106 - Renamed command highlight to generate.
aziz
parents: 328
diff changeset
30 case "gen", "generate":
322
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
31 char[] fileName;
329
17f43b0d6106 - Renamed command highlight to generate.
aziz
parents: 328
diff changeset
32 DocOption options = DocOption.Tokens;
322
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
33 foreach (arg; args[2..$])
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
34 {
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
35 switch (arg)
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
36 {
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
37 case "--syntax":
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
38 options |= DocOption.Syntax; break;
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
39 case "--xml":
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
40 options |= DocOption.XML; break;
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
41 case "--html":
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
42 options |= DocOption.HTML; break;
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
43 default:
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
44 fileName = arg;
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
45 }
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
46 }
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
47 if (!(options & (DocOption.XML | DocOption.HTML)))
ed4ef0173793 - Moved out large TOK switch case to function printToken().
aziz
parents: 315
diff changeset
48 options |= DocOption.XML; // Default to XML.
364
1059295c2727 - Every command module has an execute method now.
aziz
parents: 363
diff changeset
49 cmd.Generate.execute(fileName, options);
306
9c866aadcb5b - Moved code out of main() to separate functions.
aziz
parents: 305
diff changeset
50 break;
366
dcbd3bf9bf74 - Added command importgraph/igraph to main.d.
aziz
parents: 364
diff changeset
51 case "importgraph", "igraph":
375
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 368
diff changeset
52 string filePath;
366
dcbd3bf9bf74 - Added command importgraph/igraph to main.d.
aziz
parents: 364
diff changeset
53 string[] includePaths;
375
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 368
diff changeset
54 string[] regexps;
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 368
diff changeset
55 uint levels;
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 368
diff changeset
56 IGraphOption options;
366
dcbd3bf9bf74 - Added command importgraph/igraph to main.d.
aziz
parents: 364
diff changeset
57 foreach (arg; args[2..$])
dcbd3bf9bf74 - Added command importgraph/igraph to main.d.
aziz
parents: 364
diff changeset
58 {
375
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 368
diff changeset
59 if (strbeg(arg, "-I"))
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 368
diff changeset
60 includePaths ~= arg[2..$];
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 368
diff changeset
61 else if(strbeg(arg, "-r"))
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 368
diff changeset
62 regexps ~= arg[2..$];
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 368
diff changeset
63 else if(strbeg(arg, "-l"))
392
bb935c6f9b7a Applied fixes and improvements to the Lexer class.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 391
diff changeset
64 levels = Integer.toInt(arg[2..$]);
366
dcbd3bf9bf74 - Added command importgraph/igraph to main.d.
aziz
parents: 364
diff changeset
65 else
375
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 368
diff changeset
66 switch (arg)
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 368
diff changeset
67 {
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 368
diff changeset
68 case "--dot":
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 368
diff changeset
69 options |= IGraphOption.PrintDot; break;
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 368
diff changeset
70 case "--paths":
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 368
diff changeset
71 options |= IGraphOption.PrintPaths; break;
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 368
diff changeset
72 case "--list":
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 368
diff changeset
73 options |= IGraphOption.PrintList; break;
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 368
diff changeset
74 case "-i":
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 368
diff changeset
75 options |= IGraphOption.IncludeUnlocatableModules; break;
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 368
diff changeset
76 case "-hle":
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 368
diff changeset
77 options |= IGraphOption.HighlightCyclicEdges; break;
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 368
diff changeset
78 case "-hlv":
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 368
diff changeset
79 options |= IGraphOption.HighlightCyclicVertices; break;
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 368
diff changeset
80 case "-gbp":
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 368
diff changeset
81 options |= IGraphOption.GroupByPackageNames; break;
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 368
diff changeset
82 case "-gbf":
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 368
diff changeset
83 options |= IGraphOption.GroupByFullPackageName; break;
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 368
diff changeset
84 default:
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 368
diff changeset
85 filePath = arg;
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 368
diff changeset
86 }
366
dcbd3bf9bf74 - Added command importgraph/igraph to main.d.
aziz
parents: 364
diff changeset
87 }
375
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 368
diff changeset
88 cmd.ImportGraph.execute(filePath, includePaths, regexps, levels, options);
366
dcbd3bf9bf74 - Added command importgraph/igraph to main.d.
aziz
parents: 364
diff changeset
89 break;
363
2b387a3c6b58 - Added package cmd.
aziz
parents: 358
diff changeset
90 case "stats", "statistics":
364
1059295c2727 - Every command module has an execute method now.
aziz
parents: 363
diff changeset
91 cmd.Statistics.execute(args[2]);
363
2b387a3c6b58 - Added package cmd.
aziz
parents: 358
diff changeset
92 break;
312
fa0b6f32c1ae - Added Special to enum TOK.
aziz
parents: 309
diff changeset
93 case "parse":
fa0b6f32c1ae - Added Special to enum TOK.
aziz
parents: 309
diff changeset
94 if (args.length == 3)
fa0b6f32c1ae - Added Special to enum TOK.
aziz
parents: 309
diff changeset
95 parse(args[2]);
fa0b6f32c1ae - Added Special to enum TOK.
aziz
parents: 309
diff changeset
96 break;
338
63c6afb915f7 - Made some corrections to the message catalogs.
aziz
parents: 334
diff changeset
97 case "?", "help":
63c6afb915f7 - Made some corrections to the message catalogs.
aziz
parents: 334
diff changeset
98 if (args.length == 3)
63c6afb915f7 - Made some corrections to the message catalogs.
aziz
parents: 334
diff changeset
99 printHelp(args[2]);
63c6afb915f7 - Made some corrections to the message catalogs.
aziz
parents: 334
diff changeset
100 else
391
33b566df6af4 Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 375
diff changeset
101 Stdout(helpMain());
338
63c6afb915f7 - Made some corrections to the message catalogs.
aziz
parents: 334
diff changeset
102 break;
306
9c866aadcb5b - Moved code out of main() to separate functions.
aziz
parents: 305
diff changeset
103 default:
9c866aadcb5b - Moved code out of main() to separate functions.
aziz
parents: 305
diff changeset
104 }
9c866aadcb5b - Moved code out of main() to separate functions.
aziz
parents: 305
diff changeset
105 }
9c866aadcb5b - Moved code out of main() to separate functions.
aziz
parents: 305
diff changeset
106
338
63c6afb915f7 - Made some corrections to the message catalogs.
aziz
parents: 334
diff changeset
107 const char[] COMMANDS =
63c6afb915f7 - Made some corrections to the message catalogs.
aziz
parents: 334
diff changeset
108 " generate (gen)\n"
363
2b387a3c6b58 - Added package cmd.
aziz
parents: 358
diff changeset
109 " help (?)\n"
366
dcbd3bf9bf74 - Added command importgraph/igraph to main.d.
aziz
parents: 364
diff changeset
110 " importgraph (igraph)\n"
363
2b387a3c6b58 - Added package cmd.
aziz
parents: 358
diff changeset
111 " statistics (stats)\n";
338
63c6afb915f7 - Made some corrections to the message catalogs.
aziz
parents: 334
diff changeset
112
375
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 368
diff changeset
113 bool strbeg(char[] str, char[] begin)
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 368
diff changeset
114 {
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 368
diff changeset
115 if (str.length >= begin.length)
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 368
diff changeset
116 {
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 368
diff changeset
117 if (str[0 .. begin.length] == begin)
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 368
diff changeset
118 return true;
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 368
diff changeset
119 }
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 368
diff changeset
120 return false;
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 368
diff changeset
121 }
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 368
diff changeset
122
338
63c6afb915f7 - Made some corrections to the message catalogs.
aziz
parents: 334
diff changeset
123 char[] helpMain()
63c6afb915f7 - Made some corrections to the message catalogs.
aziz
parents: 334
diff changeset
124 {
391
33b566df6af4 Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 375
diff changeset
125 return FormatMsg(MID.HelpMain, VERSION, COMMANDS, COMPILED_WITH, COMPILED_VERSION, COMPILED_DATE);
338
63c6afb915f7 - Made some corrections to the message catalogs.
aziz
parents: 334
diff changeset
126 }
63c6afb915f7 - Made some corrections to the message catalogs.
aziz
parents: 334
diff changeset
127
63c6afb915f7 - Made some corrections to the message catalogs.
aziz
parents: 334
diff changeset
128 void printHelp(char[] command)
63c6afb915f7 - Made some corrections to the message catalogs.
aziz
parents: 334
diff changeset
129 {
63c6afb915f7 - Made some corrections to the message catalogs.
aziz
parents: 334
diff changeset
130 char[] msg;
63c6afb915f7 - Made some corrections to the message catalogs.
aziz
parents: 334
diff changeset
131 switch (command)
63c6afb915f7 - Made some corrections to the message catalogs.
aziz
parents: 334
diff changeset
132 {
63c6afb915f7 - Made some corrections to the message catalogs.
aziz
parents: 334
diff changeset
133 case "gen", "generate":
63c6afb915f7 - Made some corrections to the message catalogs.
aziz
parents: 334
diff changeset
134 msg = GetMsg(MID.HelpGenerate);
63c6afb915f7 - Made some corrections to the message catalogs.
aziz
parents: 334
diff changeset
135 break;
375
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 368
diff changeset
136 case "importgraph", "igraph":
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 368
diff changeset
137 msg = GetMsg(MID.HelpImportGraph);
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 368
diff changeset
138 break;
338
63c6afb915f7 - Made some corrections to the message catalogs.
aziz
parents: 334
diff changeset
139 default:
63c6afb915f7 - Made some corrections to the message catalogs.
aziz
parents: 334
diff changeset
140 msg = helpMain();
63c6afb915f7 - Made some corrections to the message catalogs.
aziz
parents: 334
diff changeset
141 }
391
33b566df6af4 Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 375
diff changeset
142 Stdout(msg).newline;
338
63c6afb915f7 - Made some corrections to the message catalogs.
aziz
parents: 334
diff changeset
143 }
63c6afb915f7 - Made some corrections to the message catalogs.
aziz
parents: 334
diff changeset
144
306
9c866aadcb5b - Moved code out of main() to separate functions.
aziz
parents: 305
diff changeset
145 void parse(string fileName)
9c866aadcb5b - Moved code out of main() to separate functions.
aziz
parents: 305
diff changeset
146 {
358
0faf57d99c1c - Replaced calls to std.file.read() with dil.loadFile().
aziz
parents: 351
diff changeset
147 auto sourceText = loadFile(fileName);
306
9c866aadcb5b - Moved code out of main() to separate functions.
aziz
parents: 305
diff changeset
148 auto parser = new Parser(sourceText, fileName);
368
2adf808343d6 - Renamed method start() to init() in Parser.
aziz
parents: 366
diff changeset
149 auto root = parser.start();
299
559d5d62e0c1 - Added checks for null before adding member to Node.children.
aziz
parents: 249
diff changeset
150
559d5d62e0c1 - Added checks for null before adding member to Node.children.
aziz
parents: 249
diff changeset
151 void print(Node[] decls, char[] indent)
559d5d62e0c1 - Added checks for null before adding member to Node.children.
aziz
parents: 249
diff changeset
152 {
559d5d62e0c1 - Added checks for null before adding member to Node.children.
aziz
parents: 249
diff changeset
153 foreach(decl; decls)
559d5d62e0c1 - Added checks for null before adding member to Node.children.
aziz
parents: 249
diff changeset
154 {
559d5d62e0c1 - Added checks for null before adding member to Node.children.
aziz
parents: 249
diff changeset
155 assert(decl !is null);
375
0bd21b746a04 - Added code to main() for recognizing options to the importgraph command.
aziz
parents: 368
diff changeset
156 // 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
157 print(decl.children, indent ~ " ");
559d5d62e0c1 - Added checks for null before adding member to Node.children.
aziz
parents: 249
diff changeset
158 }
559d5d62e0c1 - Added checks for null before adding member to Node.children.
aziz
parents: 249
diff changeset
159 }
305
df237b3b5f09 - Added class Declarations.
aziz
parents: 303
diff changeset
160 print(root.children, "");
213
b0ebde88d2d4 - Added members trying, errorCount and prev (for debugging) to Parser.
aziz
parents: 112
diff changeset
161 foreach (error; parser.errors)
b0ebde88d2d4 - Added members trying, errorCount and prev (for debugging) to Parser.
aziz
parents: 112
diff changeset
162 {
391
33b566df6af4 Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 375
diff changeset
163 Stdout.format(`{0}({1})P: {2}`, parser.lx.fileName, error.loc, error.getMsg);
213
b0ebde88d2d4 - Added members trying, errorCount and prev (for debugging) to Parser.
aziz
parents: 112
diff changeset
164 }
306
9c866aadcb5b - Moved code out of main() to separate functions.
aziz
parents: 305
diff changeset
165 }