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