Mercurial > projects > dil
annotate trunk/src/main.d @ 308:3b9fc1e72210
- Indented some cases. Removed case TOK.Catenate.
author | aziz |
---|---|
date | Tue, 14 Aug 2007 18:35:02 +0000 |
parents | 5f6a173d4ad3 |
children | b4d842b0d2c7 |
rev | line source |
---|---|
0 | 1 /++ |
2 Author: Aziz Köksal | |
249 | 3 License: GPL3 |
0 | 4 +/ |
5 module dparser; | |
69
24db7c5522d5
- Added module Information for compiler messages like warnings, info and errors to the user.
aziz
parents:
66
diff
changeset
|
6 import Parser; |
3 | 7 import Lexer; |
8 import Token; | |
54 | 9 import Messages; |
3 | 10 import std.stdio; |
4 | 11 import std.file; |
306 | 12 import std.metastrings; |
0 | 13 |
306 | 14 import Declarations, SyntaxTree; |
15 | |
16 version(D2) | |
17 { | |
18 const VERSION_MAJOR = 2; | |
19 const VERSION_MINOR = 0; | |
20 } | |
21 else | |
5 | 22 { |
306 | 23 const VERSION_MAJOR = 1; |
24 const VERSION_MINOR = 0; | |
5 | 25 } |
306 | 26 const string VERSION = Format!("%s.%s", VERSION_MAJOR, VERSION_MINOR); |
27 | |
28 const char[] usageHighlight = "highlight (hl) file.d"; | |
29 const string helpMain = `dil v`~VERSION~` | |
30 Copyright (c) 2007 by Aziz Köksal | |
31 | |
32 Subcommands: | |
33 `~usageHighlight~` | |
34 | |
35 Type 'dil help <subcommand>' for more help on a particular subcommand. | |
36 | |
37 Compiled with `~__VENDOR__~` `~Format!("v%s.%s", __VERSION__/1000, __VERSION__%1000)~` on `~__TIMESTAMP__~`. | |
38 `; | |
39 | |
0 | 40 void main(char[][] args) |
41 { | |
306 | 42 if (args.length <= 1) |
43 return writefln(helpMain); | |
44 | |
45 string command = args[1]; | |
46 switch (command) | |
47 { | |
48 case "hl", "highlight": | |
49 if (args.length == 3) | |
50 highlightTokens(args[2]); | |
51 break; | |
52 default: | |
53 } | |
54 } | |
55 | |
56 void parse(string fileName) | |
57 { | |
58 auto sourceText = cast(char[]) std.file.read(fileName); | |
59 auto parser = new Parser(sourceText, fileName); | |
213
b0ebde88d2d4
- Added members trying, errorCount and prev (for debugging) to Parser.
aziz
parents:
112
diff
changeset
|
60 parser.start(); |
305 | 61 auto root = parser.parseModule(); |
299
559d5d62e0c1
- Added checks for null before adding member to Node.children.
aziz
parents:
249
diff
changeset
|
62 |
559d5d62e0c1
- Added checks for null before adding member to Node.children.
aziz
parents:
249
diff
changeset
|
63 void print(Node[] decls, char[] indent) |
559d5d62e0c1
- Added checks for null before adding member to Node.children.
aziz
parents:
249
diff
changeset
|
64 { |
559d5d62e0c1
- Added checks for null before adding member to Node.children.
aziz
parents:
249
diff
changeset
|
65 foreach(decl; decls) |
559d5d62e0c1
- Added checks for null before adding member to Node.children.
aziz
parents:
249
diff
changeset
|
66 { |
559d5d62e0c1
- Added checks for null before adding member to Node.children.
aziz
parents:
249
diff
changeset
|
67 assert(decl !is null); |
303 | 68 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
|
69 print(decl.children, indent ~ " "); |
559d5d62e0c1
- Added checks for null before adding member to Node.children.
aziz
parents:
249
diff
changeset
|
70 } |
559d5d62e0c1
- Added checks for null before adding member to Node.children.
aziz
parents:
249
diff
changeset
|
71 } |
305 | 72 print(root.children, ""); |
213
b0ebde88d2d4
- Added members trying, errorCount and prev (for debugging) to Parser.
aziz
parents:
112
diff
changeset
|
73 foreach (error; parser.errors) |
b0ebde88d2d4
- Added members trying, errorCount and prev (for debugging) to Parser.
aziz
parents:
112
diff
changeset
|
74 { |
b0ebde88d2d4
- Added members trying, errorCount and prev (for debugging) to Parser.
aziz
parents:
112
diff
changeset
|
75 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
|
76 } |
306 | 77 } |
213
b0ebde88d2d4
- Added members trying, errorCount and prev (for debugging) to Parser.
aziz
parents:
112
diff
changeset
|
78 |
306 | 79 char[] xml_escape(char[] text) |
80 { | |
81 char[] result; | |
82 foreach(c; text) | |
83 switch(c) | |
84 { | |
85 case '<': result ~= "<"; break; | |
86 case '>': result ~= ">"; break; | |
87 case '&': result ~= "&"; break; | |
88 default: result ~= c; | |
89 } | |
90 return result; | |
91 } | |
0 | 92 |
306 | 93 void highlightTokens(string fileName) |
94 { | |
95 auto sourceText = cast(char[]) std.file.read(fileName); | |
96 auto lx = new Lexer(sourceText, fileName); | |
97 | |
98 auto token = lx.getTokens(); | |
5 | 99 char* end = lx.text.ptr; |
100 | |
306 | 101 writef(`<?xml version="1.0"?>` |
102 `<?xml-stylesheet href="format.css" type="text/css"?>` | |
103 `<root>`); | |
104 if (lx.errors.length) | |
51 | 105 { |
306 | 106 writefln("<compilerinfo>"); |
107 foreach (error; lx.errors) | |
108 { | |
109 writefln(`<error t="%s">%s(%d): %s</error>`, "l", lx.fileName, error.loc, xml_escape(error.getMsg)); | |
110 } | |
111 writefln("</compilerinfo>"); | |
51 | 112 } |
306 | 113 writef(`<sourcetext>`); |
114 | |
115 // Traverse linked list and print tokens. | |
116 while (token.type != TOK.EOF) | |
4 | 117 { |
306 | 118 token = token.next; |
119 | |
120 // Print whitespace between previous and current token. | |
5 | 121 if (end != token.start) |
306 | 122 writef("%s", xml_escape(end[0 .. token.start - end])); |
123 | |
124 string srcText = xml_escape(token.srcText); | |
125 | |
5 | 126 switch(token.type) |
127 { | |
306 | 128 case TOK.Identifier: |
129 writef("<i>%s</i>", srcText); | |
5 | 130 break; |
306 | 131 case TOK.Comment: |
132 string c; | |
133 switch (token.start[1]) | |
134 { | |
135 case '/': c = "l"; break; | |
136 case '*': c = "b"; break; | |
137 case '+': c = "n"; break; | |
138 default: | |
139 assert(0); | |
140 } | |
141 writef(`<c c="%s">%s</c>`, c, srcText); | |
5 | 142 break; |
306 | 143 case TOK.String: |
144 writef("<sl>%s</sl>", srcText); | |
9
5d6968cc751e
- Parsing string and character literals now (rudimentary implementation.)
aziz
parents:
5
diff
changeset
|
145 break; |
306 | 146 case TOK.CharLiteral, TOK.WCharLiteral, TOK.DCharLiteral: |
147 writef("<cl>%s</cl>", srcText); | |
9
5d6968cc751e
- Parsing string and character literals now (rudimentary implementation.)
aziz
parents:
5
diff
changeset
|
148 break; |
308 | 149 case TOK.Assign, TOK.Equal, |
150 TOK.Less, TOK.Greater, | |
151 TOK.LShiftAssign, TOK.LShift, | |
152 TOK.RShiftAssign, TOK.RShift, | |
306 | 153 TOK.URShiftAssign, TOK.URShift, |
308 | 154 TOK.OrAssign, TOK.OrBinary, |
155 TOK.AndAssign, TOK.AndBinary, | |
156 TOK.PlusAssign, TOK.PlusPlus, TOK.Plus, | |
157 TOK.MinusAssign, TOK.MinusMinus, TOK.Minus, | |
158 TOK.DivAssign, TOK.Div, | |
159 TOK.MulAssign, TOK.Mul, | |
160 TOK.ModAssign, TOK.Mod, | |
161 TOK.XorAssign, TOK.Xor, | |
162 TOK.CatAssign, | |
306 | 163 TOK.Tilde, |
164 TOK.Unordered, | |
165 TOK.UorE, | |
166 TOK.UorG, | |
167 TOK.UorGorE, | |
168 TOK.UorL, | |
169 TOK.UorLorE, | |
170 TOK.LorEorG: | |
171 writef("<op>%s</op>", srcText); | |
14 | 172 break; |
306 | 173 case TOK.LorG: |
174 writef(`<op c="lg"><></op>`); | |
39
69b940398d7b
- Added unittest to test correct parsing of operator tokens.
aziz
parents:
35
diff
changeset
|
175 break; |
306 | 176 case TOK.LessEqual: |
177 writef(`<op c="le"><=</op>`); | |
39
69b940398d7b
- Added unittest to test correct parsing of operator tokens.
aziz
parents:
35
diff
changeset
|
178 break; |
306 | 179 case TOK.GreaterEqual: |
180 writef(`<op c="ge">>=</op>`); | |
39
69b940398d7b
- Added unittest to test correct parsing of operator tokens.
aziz
parents:
35
diff
changeset
|
181 break; |
306 | 182 case TOK.AndLogical: |
183 writef(`<op c="aa">&&</op>`); | |
31
94f09f4e988e
- Added struct for strings to Token with 'pf' = postfix.
aziz
parents:
30
diff
changeset
|
184 break; |
306 | 185 case TOK.OrLogical: |
186 writef(`<op c="oo">||</op>`); | |
31
94f09f4e988e
- Added struct for strings to Token with 'pf' = postfix.
aziz
parents:
30
diff
changeset
|
187 break; |
306 | 188 case TOK.NotEqual: |
189 writef(`<op c="ne">!=</op>`); | |
44
5055947e0f98
- Specific operators and comments can be formatted with CSS now.
aziz
parents:
39
diff
changeset
|
190 break; |
306 | 191 case TOK.Not: |
307
5f6a173d4ad3
- Don't consider '\!' as an operator when it begins a template argument list.
aziz
parents:
306
diff
changeset
|
192 // Check if this is part of a template instantiation. |
5f6a173d4ad3
- Don't consider '\!' as an operator when it begins a template argument list.
aziz
parents:
306
diff
changeset
|
193 // TODO: comments aren't skipped. |
5f6a173d4ad3
- Don't consider '\!' as an operator when it begins a template argument list.
aziz
parents:
306
diff
changeset
|
194 if (token.prev.type == TOK.Identifier && token.next.type == TOK.LParen) |
5f6a173d4ad3
- Don't consider '\!' as an operator when it begins a template argument list.
aziz
parents:
306
diff
changeset
|
195 goto default; |
306 | 196 writef(`<op c="n">!</op>`); |
35
c470b9356e35
- Added code for parsing Unordered, UorE, UorG, UorGorE, UorL, UorLorE, NotEqual and Not tokens.
aziz
parents:
33
diff
changeset
|
197 break; |
306 | 198 case TOK.Int32, TOK.Int64, TOK.Uint32, TOK.Uint64, |
308 | 199 TOK.Float32, TOK.Float64, TOK.Float80, |
200 TOK.Imaginary32, TOK.Imaginary64, TOK.Imaginary80: | |
306 | 201 writef("<n>%s</n>", srcText); |
15 | 202 break; |
306 | 203 case TOK.LParen, TOK.RParen, TOK.LBracket, |
308 | 204 TOK.RBracket, TOK.LBrace, TOK.RBrace: |
306 | 205 writef("<br>%s</br>", srcText); |
20 | 206 break; |
306 | 207 default: |
208 if (token.isKeyword()) | |
209 writef("<k>%s</k>", srcText); | |
210 else | |
211 writef("%s", srcText); | |
5 | 212 } |
213 end = token.end; | |
4 | 214 } |
51 | 215 writef("\n</sourcetext>\n</root>"); |
0 | 216 } |