Mercurial > projects > dil
annotate trunk/src/main.d @ 299:559d5d62e0c1
- Added checks for null before adding member to Node.children.
- Added finishConstruction() to FunctionBody.
author | aziz |
---|---|
date | Thu, 09 Aug 2007 21:39:03 +0000 |
parents | 32d354584b28 |
children | bc1112f7933d |
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; |
0 | 12 |
5 | 13 char[] xmlescape(char[] text) |
14 { | |
15 char[] result; | |
16 foreach(c; text) | |
17 switch(c) | |
18 { | |
19 case '<': result ~= "<"; break; | |
20 case '>': result ~= ">"; break; | |
21 case '&': result ~= "&"; break; | |
22 default: result ~= c; | |
23 } | |
24 return result; | |
25 } | |
299
559d5d62e0c1
- Added checks for null before adding member to Node.children.
aziz
parents:
249
diff
changeset
|
26 import Declarations, SyntaxTree; |
0 | 27 void main(char[][] args) |
28 { | |
4 | 29 auto srctext = cast(char[]) std.file.read(args[1]); |
213
b0ebde88d2d4
- Added members trying, errorCount and prev (for debugging) to Parser.
aziz
parents:
112
diff
changeset
|
30 auto parser = new Parser(srctext, args[1]); |
b0ebde88d2d4
- Added members trying, errorCount and prev (for debugging) to Parser.
aziz
parents:
112
diff
changeset
|
31 parser.start(); |
b0ebde88d2d4
- Added members trying, errorCount and prev (for debugging) to Parser.
aziz
parents:
112
diff
changeset
|
32 auto decls = parser.parseModule(); |
299
559d5d62e0c1
- Added checks for null before adding member to Node.children.
aziz
parents:
249
diff
changeset
|
33 |
559d5d62e0c1
- Added checks for null before adding member to Node.children.
aziz
parents:
249
diff
changeset
|
34 void print(Node[] decls, char[] indent) |
559d5d62e0c1
- Added checks for null before adding member to Node.children.
aziz
parents:
249
diff
changeset
|
35 { |
559d5d62e0c1
- Added checks for null before adding member to Node.children.
aziz
parents:
249
diff
changeset
|
36 foreach(decl; decls) |
559d5d62e0c1
- Added checks for null before adding member to Node.children.
aziz
parents:
249
diff
changeset
|
37 { |
559d5d62e0c1
- Added checks for null before adding member to Node.children.
aziz
parents:
249
diff
changeset
|
38 assert(decl !is null); |
559d5d62e0c1
- Added checks for null before adding member to Node.children.
aziz
parents:
249
diff
changeset
|
39 writefln(indent, decl.classinfo.name, ":", decl.children.length); |
559d5d62e0c1
- Added checks for null before adding member to Node.children.
aziz
parents:
249
diff
changeset
|
40 print(decl.children, indent ~ " "); |
559d5d62e0c1
- Added checks for null before adding member to Node.children.
aziz
parents:
249
diff
changeset
|
41 } |
559d5d62e0c1
- Added checks for null before adding member to Node.children.
aziz
parents:
249
diff
changeset
|
42 } |
559d5d62e0c1
- Added checks for null before adding member to Node.children.
aziz
parents:
249
diff
changeset
|
43 print(decls, ""); |
213
b0ebde88d2d4
- Added members trying, errorCount and prev (for debugging) to Parser.
aziz
parents:
112
diff
changeset
|
44 foreach (error; parser.errors) |
b0ebde88d2d4
- Added members trying, errorCount and prev (for debugging) to Parser.
aziz
parents:
112
diff
changeset
|
45 { |
b0ebde88d2d4
- Added members trying, errorCount and prev (for debugging) to Parser.
aziz
parents:
112
diff
changeset
|
46 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
|
47 } |
b0ebde88d2d4
- Added members trying, errorCount and prev (for debugging) to Parser.
aziz
parents:
112
diff
changeset
|
48 std.c.stdlib.exit(0); |
b0ebde88d2d4
- Added members trying, errorCount and prev (for debugging) to Parser.
aziz
parents:
112
diff
changeset
|
49 |
30 | 50 auto lx = new Lexer(srctext, args[1]); |
0 | 51 |
5 | 52 auto tokens = lx.getTokens(); |
53 char* end = lx.text.ptr; | |
54 | |
55 writef(`<?xml version="1.0"?> | |
56 <?xml-stylesheet href="format.css" type="text/css"?> | |
51 | 57 <root> |
58 <compilerinfo>`\n); | |
59 foreach (error; lx.errors) | |
60 { | |
66
8e84db78ad55
- Added support for variadic arguments in error messages.
aziz
parents:
65
diff
changeset
|
61 writefln(`<error t="%s">%s(%d): %s</error>`, "l", lx.fileName, error.loc, error.getMsg); |
51 | 62 } |
63 writef(`</compilerinfo> | |
5 | 64 <sourcetext>`); |
51 | 65 foreach (ref token; tokens) |
4 | 66 { |
5 | 67 if (end != token.start) |
68 writef("%s", xmlescape(end[0 .. token.start - end])); | |
65 | 69 string srcText = xmlescape(token.srcText); |
5 | 70 switch(token.type) |
71 { | |
72 case TOK.Identifier: | |
65 | 73 writef("<i>%s</i>", srcText); |
5 | 74 break; |
75 case TOK.Comment: | |
44
5055947e0f98
- Specific operators and comments can be formatted with CSS now.
aziz
parents:
39
diff
changeset
|
76 string c; |
5055947e0f98
- Specific operators and comments can be formatted with CSS now.
aziz
parents:
39
diff
changeset
|
77 switch (token.start[1]) |
5055947e0f98
- Specific operators and comments can be formatted with CSS now.
aziz
parents:
39
diff
changeset
|
78 { |
5055947e0f98
- Specific operators and comments can be formatted with CSS now.
aziz
parents:
39
diff
changeset
|
79 case '/': c = "lc"; break; |
5055947e0f98
- Specific operators and comments can be formatted with CSS now.
aziz
parents:
39
diff
changeset
|
80 case '*': c = "bc"; break; |
5055947e0f98
- Specific operators and comments can be formatted with CSS now.
aziz
parents:
39
diff
changeset
|
81 case '+': c = "nc"; break; |
112 | 82 default: |
83 assert(0); | |
44
5055947e0f98
- Specific operators and comments can be formatted with CSS now.
aziz
parents:
39
diff
changeset
|
84 } |
65 | 85 writef(`<c c="%s">%s</c>`, c, srcText); |
5 | 86 break; |
9
5d6968cc751e
- Parsing string and character literals now (rudimentary implementation.)
aziz
parents:
5
diff
changeset
|
87 case TOK.String: |
65 | 88 writef("<sl>%s</sl>", srcText); |
9
5d6968cc751e
- Parsing string and character literals now (rudimentary implementation.)
aziz
parents:
5
diff
changeset
|
89 break; |
82
fc645fb2fe72
- scanEscapeSequences() doesn't return 0xFFFF as an error value anymore, because it is a valid codepoint usable by the user.
aziz
parents:
69
diff
changeset
|
90 case TOK.CharLiteral, TOK.WCharLiteral, TOK.DCharLiteral: |
65 | 91 writef("<cl>%s</cl>", srcText); |
9
5d6968cc751e
- Parsing string and character literals now (rudimentary implementation.)
aziz
parents:
5
diff
changeset
|
92 break; |
44
5055947e0f98
- Specific operators and comments can be formatted with CSS now.
aziz
parents:
39
diff
changeset
|
93 case TOK.Assign, TOK.Equal, |
39
69b940398d7b
- Added unittest to test correct parsing of operator tokens.
aziz
parents:
35
diff
changeset
|
94 TOK.Less, TOK.Greater, |
69b940398d7b
- Added unittest to test correct parsing of operator tokens.
aziz
parents:
35
diff
changeset
|
95 TOK.LShiftAssign, TOK.LShift, |
69b940398d7b
- Added unittest to test correct parsing of operator tokens.
aziz
parents:
35
diff
changeset
|
96 TOK.RShiftAssign, TOK.RShift, |
69b940398d7b
- Added unittest to test correct parsing of operator tokens.
aziz
parents:
35
diff
changeset
|
97 TOK.URShiftAssign, TOK.URShift, |
33 | 98 TOK.OrAssign, TOK.OrBinary, |
99 TOK.AndAssign, TOK.AndBinary, | |
100 TOK.PlusAssign, TOK.PlusPlus, TOK.Plus, | |
101 TOK.MinusAssign, TOK.MinusMinus, TOK.Minus, | |
102 TOK.DivAssign, TOK.Div, | |
103 TOK.MulAssign, TOK.Mul, | |
104 TOK.ModAssign, TOK.Mod, | |
105 TOK.XorAssign, TOK.Xor, | |
35
c470b9356e35
- Added code for parsing Unordered, UorE, UorG, UorGorE, UorL, UorLorE, NotEqual and Not tokens.
aziz
parents:
33
diff
changeset
|
106 TOK.CatAssign, TOK.Catenate, |
39
69b940398d7b
- Added unittest to test correct parsing of operator tokens.
aziz
parents:
35
diff
changeset
|
107 TOK.Tilde, |
35
c470b9356e35
- Added code for parsing Unordered, UorE, UorG, UorGorE, UorL, UorLorE, NotEqual and Not tokens.
aziz
parents:
33
diff
changeset
|
108 TOK.Unordered, |
c470b9356e35
- Added code for parsing Unordered, UorE, UorG, UorGorE, UorL, UorLorE, NotEqual and Not tokens.
aziz
parents:
33
diff
changeset
|
109 TOK.UorE, |
c470b9356e35
- Added code for parsing Unordered, UorE, UorG, UorGorE, UorL, UorLorE, NotEqual and Not tokens.
aziz
parents:
33
diff
changeset
|
110 TOK.UorG, |
c470b9356e35
- Added code for parsing Unordered, UorE, UorG, UorGorE, UorL, UorLorE, NotEqual and Not tokens.
aziz
parents:
33
diff
changeset
|
111 TOK.UorGorE, |
c470b9356e35
- Added code for parsing Unordered, UorE, UorG, UorGorE, UorL, UorLorE, NotEqual and Not tokens.
aziz
parents:
33
diff
changeset
|
112 TOK.UorL, |
39
69b940398d7b
- Added unittest to test correct parsing of operator tokens.
aziz
parents:
35
diff
changeset
|
113 TOK.UorLorE, |
69b940398d7b
- Added unittest to test correct parsing of operator tokens.
aziz
parents:
35
diff
changeset
|
114 TOK.LorEorG: |
65 | 115 writef("<op>%s</op>", srcText); |
14 | 116 break; |
39
69b940398d7b
- Added unittest to test correct parsing of operator tokens.
aziz
parents:
35
diff
changeset
|
117 case TOK.LorG: |
44
5055947e0f98
- Specific operators and comments can be formatted with CSS now.
aziz
parents:
39
diff
changeset
|
118 writef(`<op c="lg"><></op>`); |
39
69b940398d7b
- Added unittest to test correct parsing of operator tokens.
aziz
parents:
35
diff
changeset
|
119 break; |
69b940398d7b
- Added unittest to test correct parsing of operator tokens.
aziz
parents:
35
diff
changeset
|
120 case TOK.LessEqual: |
44
5055947e0f98
- Specific operators and comments can be formatted with CSS now.
aziz
parents:
39
diff
changeset
|
121 writef(`<op c="le"><=</op>`); |
39
69b940398d7b
- Added unittest to test correct parsing of operator tokens.
aziz
parents:
35
diff
changeset
|
122 break; |
69b940398d7b
- Added unittest to test correct parsing of operator tokens.
aziz
parents:
35
diff
changeset
|
123 case TOK.GreaterEqual: |
44
5055947e0f98
- Specific operators and comments can be formatted with CSS now.
aziz
parents:
39
diff
changeset
|
124 writef(`<op c="ge">>=</op>`); |
39
69b940398d7b
- Added unittest to test correct parsing of operator tokens.
aziz
parents:
35
diff
changeset
|
125 break; |
31
94f09f4e988e
- Added struct for strings to Token with 'pf' = postfix.
aziz
parents:
30
diff
changeset
|
126 case TOK.AndLogical: |
44
5055947e0f98
- Specific operators and comments can be formatted with CSS now.
aziz
parents:
39
diff
changeset
|
127 writef(`<op c="aa">&&</op>`); |
31
94f09f4e988e
- Added struct for strings to Token with 'pf' = postfix.
aziz
parents:
30
diff
changeset
|
128 break; |
94f09f4e988e
- Added struct for strings to Token with 'pf' = postfix.
aziz
parents:
30
diff
changeset
|
129 case TOK.OrLogical: |
44
5055947e0f98
- Specific operators and comments can be formatted with CSS now.
aziz
parents:
39
diff
changeset
|
130 writef(`<op c="oo">||</op>`); |
31
94f09f4e988e
- Added struct for strings to Token with 'pf' = postfix.
aziz
parents:
30
diff
changeset
|
131 break; |
35
c470b9356e35
- Added code for parsing Unordered, UorE, UorG, UorGorE, UorL, UorLorE, NotEqual and Not tokens.
aziz
parents:
33
diff
changeset
|
132 case TOK.NotEqual: |
44
5055947e0f98
- Specific operators and comments can be formatted with CSS now.
aziz
parents:
39
diff
changeset
|
133 writef(`<op c="ne">!=</op>`); |
5055947e0f98
- Specific operators and comments can be formatted with CSS now.
aziz
parents:
39
diff
changeset
|
134 break; |
5055947e0f98
- Specific operators and comments can be formatted with CSS now.
aziz
parents:
39
diff
changeset
|
135 case TOK.Not: |
5055947e0f98
- Specific operators and comments can be formatted with CSS now.
aziz
parents:
39
diff
changeset
|
136 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
|
137 break; |
62
96af5653acef
- Fixed loop of hex number scanner. Moved checks under the switch block.
aziz
parents:
58
diff
changeset
|
138 case TOK.Int32, TOK.Int64, TOK.Uint32, TOK.Uint64, |
96af5653acef
- Fixed loop of hex number scanner. Moved checks under the switch block.
aziz
parents:
58
diff
changeset
|
139 TOK.Float32, TOK.Float64, TOK.Float80, |
96af5653acef
- Fixed loop of hex number scanner. Moved checks under the switch block.
aziz
parents:
58
diff
changeset
|
140 TOK.Imaginary32, TOK.Imaginary64, TOK.Imaginary80: |
65 | 141 writef("<n>%s</n>", srcText); |
15 | 142 break; |
20 | 143 case TOK.LParen, TOK.RParen, TOK.LBracket, |
144 TOK.RBracket, TOK.LBrace, TOK.RBrace: | |
65 | 145 writef("<br>%s</br>", srcText); |
20 | 146 break; |
22
b05fff8e2ce4
- Added code for parsing Dot, Slice and Ellipses tokens.
aziz
parents:
21
diff
changeset
|
147 case TOK.EOF: break; |
5 | 148 default: |
28 | 149 if (token.isKeyword()) |
65 | 150 writef("<k>%s</k>", srcText); |
28 | 151 else |
65 | 152 writef("%s", srcText); |
5 | 153 } |
154 end = token.end; | |
4 | 155 } |
51 | 156 writef("\n</sourcetext>\n</root>"); |
0 | 157 } |