Mercurial > projects > dil
annotate trunk/src/main.d @ 213:b0ebde88d2d4
- Added members trying, errorCount and prev (for debugging) to Parser.
- Fix: try_ didn't save token member.
- Added method parseDeclarationDefinitionsBlock() and replaced some code with calls to this method.
- Fixed case T.Colon in parseDeclarationsBlock().
- Fixed parseNonVoidInitializer().
- Fixed parseFunctionBody().
- Fixes to parseStatement(): going to T.Dot in case T.Identifier; added case T.LBrace.
- Fixed parseReturnStatement().
author | aziz |
---|---|
date | Tue, 24 Jul 2007 20:01:02 +0000 |
parents | 004d98df65af |
children | 32d354584b28 |
rev | line source |
---|---|
0 | 1 /++ |
2 Author: Aziz Köksal | |
3 License: GPL2 | |
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 } | |
26 | |
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(); |
b0ebde88d2d4
- Added members trying, errorCount and prev (for debugging) to Parser.
aziz
parents:
112
diff
changeset
|
33 foreach (error; parser.errors) |
b0ebde88d2d4
- Added members trying, errorCount and prev (for debugging) to Parser.
aziz
parents:
112
diff
changeset
|
34 { |
b0ebde88d2d4
- Added members trying, errorCount and prev (for debugging) to Parser.
aziz
parents:
112
diff
changeset
|
35 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
|
36 } |
b0ebde88d2d4
- Added members trying, errorCount and prev (for debugging) to Parser.
aziz
parents:
112
diff
changeset
|
37 std.c.stdlib.exit(0); |
b0ebde88d2d4
- Added members trying, errorCount and prev (for debugging) to Parser.
aziz
parents:
112
diff
changeset
|
38 |
30 | 39 auto lx = new Lexer(srctext, args[1]); |
0 | 40 |
5 | 41 auto tokens = lx.getTokens(); |
42 char* end = lx.text.ptr; | |
43 | |
44 writef(`<?xml version="1.0"?> | |
45 <?xml-stylesheet href="format.css" type="text/css"?> | |
51 | 46 <root> |
47 <compilerinfo>`\n); | |
48 foreach (error; lx.errors) | |
49 { | |
66
8e84db78ad55
- Added support for variadic arguments in error messages.
aziz
parents:
65
diff
changeset
|
50 writefln(`<error t="%s">%s(%d): %s</error>`, "l", lx.fileName, error.loc, error.getMsg); |
51 | 51 } |
52 writef(`</compilerinfo> | |
5 | 53 <sourcetext>`); |
51 | 54 foreach (ref token; tokens) |
4 | 55 { |
5 | 56 if (end != token.start) |
57 writef("%s", xmlescape(end[0 .. token.start - end])); | |
65 | 58 string srcText = xmlescape(token.srcText); |
5 | 59 switch(token.type) |
60 { | |
61 case TOK.Identifier: | |
65 | 62 writef("<i>%s</i>", srcText); |
5 | 63 break; |
64 case TOK.Comment: | |
44
5055947e0f98
- Specific operators and comments can be formatted with CSS now.
aziz
parents:
39
diff
changeset
|
65 string c; |
5055947e0f98
- Specific operators and comments can be formatted with CSS now.
aziz
parents:
39
diff
changeset
|
66 switch (token.start[1]) |
5055947e0f98
- Specific operators and comments can be formatted with CSS now.
aziz
parents:
39
diff
changeset
|
67 { |
5055947e0f98
- Specific operators and comments can be formatted with CSS now.
aziz
parents:
39
diff
changeset
|
68 case '/': c = "lc"; break; |
5055947e0f98
- Specific operators and comments can be formatted with CSS now.
aziz
parents:
39
diff
changeset
|
69 case '*': c = "bc"; break; |
5055947e0f98
- Specific operators and comments can be formatted with CSS now.
aziz
parents:
39
diff
changeset
|
70 case '+': c = "nc"; break; |
112 | 71 default: |
72 assert(0); | |
44
5055947e0f98
- Specific operators and comments can be formatted with CSS now.
aziz
parents:
39
diff
changeset
|
73 } |
65 | 74 writef(`<c c="%s">%s</c>`, c, srcText); |
5 | 75 break; |
9
5d6968cc751e
- Parsing string and character literals now (rudimentary implementation.)
aziz
parents:
5
diff
changeset
|
76 case TOK.String: |
65 | 77 writef("<sl>%s</sl>", srcText); |
9
5d6968cc751e
- Parsing string and character literals now (rudimentary implementation.)
aziz
parents:
5
diff
changeset
|
78 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
|
79 case TOK.CharLiteral, TOK.WCharLiteral, TOK.DCharLiteral: |
65 | 80 writef("<cl>%s</cl>", srcText); |
9
5d6968cc751e
- Parsing string and character literals now (rudimentary implementation.)
aziz
parents:
5
diff
changeset
|
81 break; |
44
5055947e0f98
- Specific operators and comments can be formatted with CSS now.
aziz
parents:
39
diff
changeset
|
82 case TOK.Assign, TOK.Equal, |
39
69b940398d7b
- Added unittest to test correct parsing of operator tokens.
aziz
parents:
35
diff
changeset
|
83 TOK.Less, TOK.Greater, |
69b940398d7b
- Added unittest to test correct parsing of operator tokens.
aziz
parents:
35
diff
changeset
|
84 TOK.LShiftAssign, TOK.LShift, |
69b940398d7b
- Added unittest to test correct parsing of operator tokens.
aziz
parents:
35
diff
changeset
|
85 TOK.RShiftAssign, TOK.RShift, |
69b940398d7b
- Added unittest to test correct parsing of operator tokens.
aziz
parents:
35
diff
changeset
|
86 TOK.URShiftAssign, TOK.URShift, |
33 | 87 TOK.OrAssign, TOK.OrBinary, |
88 TOK.AndAssign, TOK.AndBinary, | |
89 TOK.PlusAssign, TOK.PlusPlus, TOK.Plus, | |
90 TOK.MinusAssign, TOK.MinusMinus, TOK.Minus, | |
91 TOK.DivAssign, TOK.Div, | |
92 TOK.MulAssign, TOK.Mul, | |
93 TOK.ModAssign, TOK.Mod, | |
94 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
|
95 TOK.CatAssign, TOK.Catenate, |
39
69b940398d7b
- Added unittest to test correct parsing of operator tokens.
aziz
parents:
35
diff
changeset
|
96 TOK.Tilde, |
35
c470b9356e35
- Added code for parsing Unordered, UorE, UorG, UorGorE, UorL, UorLorE, NotEqual and Not tokens.
aziz
parents:
33
diff
changeset
|
97 TOK.Unordered, |
c470b9356e35
- Added code for parsing Unordered, UorE, UorG, UorGorE, UorL, UorLorE, NotEqual and Not tokens.
aziz
parents:
33
diff
changeset
|
98 TOK.UorE, |
c470b9356e35
- Added code for parsing Unordered, UorE, UorG, UorGorE, UorL, UorLorE, NotEqual and Not tokens.
aziz
parents:
33
diff
changeset
|
99 TOK.UorG, |
c470b9356e35
- Added code for parsing Unordered, UorE, UorG, UorGorE, UorL, UorLorE, NotEqual and Not tokens.
aziz
parents:
33
diff
changeset
|
100 TOK.UorGorE, |
c470b9356e35
- Added code for parsing Unordered, UorE, UorG, UorGorE, UorL, UorLorE, NotEqual and Not tokens.
aziz
parents:
33
diff
changeset
|
101 TOK.UorL, |
39
69b940398d7b
- Added unittest to test correct parsing of operator tokens.
aziz
parents:
35
diff
changeset
|
102 TOK.UorLorE, |
69b940398d7b
- Added unittest to test correct parsing of operator tokens.
aziz
parents:
35
diff
changeset
|
103 TOK.LorEorG: |
65 | 104 writef("<op>%s</op>", srcText); |
14 | 105 break; |
39
69b940398d7b
- Added unittest to test correct parsing of operator tokens.
aziz
parents:
35
diff
changeset
|
106 case TOK.LorG: |
44
5055947e0f98
- Specific operators and comments can be formatted with CSS now.
aziz
parents:
39
diff
changeset
|
107 writef(`<op c="lg"><></op>`); |
39
69b940398d7b
- Added unittest to test correct parsing of operator tokens.
aziz
parents:
35
diff
changeset
|
108 break; |
69b940398d7b
- Added unittest to test correct parsing of operator tokens.
aziz
parents:
35
diff
changeset
|
109 case TOK.LessEqual: |
44
5055947e0f98
- Specific operators and comments can be formatted with CSS now.
aziz
parents:
39
diff
changeset
|
110 writef(`<op c="le"><=</op>`); |
39
69b940398d7b
- Added unittest to test correct parsing of operator tokens.
aziz
parents:
35
diff
changeset
|
111 break; |
69b940398d7b
- Added unittest to test correct parsing of operator tokens.
aziz
parents:
35
diff
changeset
|
112 case TOK.GreaterEqual: |
44
5055947e0f98
- Specific operators and comments can be formatted with CSS now.
aziz
parents:
39
diff
changeset
|
113 writef(`<op c="ge">>=</op>`); |
39
69b940398d7b
- Added unittest to test correct parsing of operator tokens.
aziz
parents:
35
diff
changeset
|
114 break; |
31
94f09f4e988e
- Added struct for strings to Token with 'pf' = postfix.
aziz
parents:
30
diff
changeset
|
115 case TOK.AndLogical: |
44
5055947e0f98
- Specific operators and comments can be formatted with CSS now.
aziz
parents:
39
diff
changeset
|
116 writef(`<op c="aa">&&</op>`); |
31
94f09f4e988e
- Added struct for strings to Token with 'pf' = postfix.
aziz
parents:
30
diff
changeset
|
117 break; |
94f09f4e988e
- Added struct for strings to Token with 'pf' = postfix.
aziz
parents:
30
diff
changeset
|
118 case TOK.OrLogical: |
44
5055947e0f98
- Specific operators and comments can be formatted with CSS now.
aziz
parents:
39
diff
changeset
|
119 writef(`<op c="oo">||</op>`); |
31
94f09f4e988e
- Added struct for strings to Token with 'pf' = postfix.
aziz
parents:
30
diff
changeset
|
120 break; |
35
c470b9356e35
- Added code for parsing Unordered, UorE, UorG, UorGorE, UorL, UorLorE, NotEqual and Not tokens.
aziz
parents:
33
diff
changeset
|
121 case TOK.NotEqual: |
44
5055947e0f98
- Specific operators and comments can be formatted with CSS now.
aziz
parents:
39
diff
changeset
|
122 writef(`<op c="ne">!=</op>`); |
5055947e0f98
- Specific operators and comments can be formatted with CSS now.
aziz
parents:
39
diff
changeset
|
123 break; |
5055947e0f98
- Specific operators and comments can be formatted with CSS now.
aziz
parents:
39
diff
changeset
|
124 case TOK.Not: |
5055947e0f98
- Specific operators and comments can be formatted with CSS now.
aziz
parents:
39
diff
changeset
|
125 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
|
126 break; |
62
96af5653acef
- Fixed loop of hex number scanner. Moved checks under the switch block.
aziz
parents:
58
diff
changeset
|
127 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
|
128 TOK.Float32, TOK.Float64, TOK.Float80, |
96af5653acef
- Fixed loop of hex number scanner. Moved checks under the switch block.
aziz
parents:
58
diff
changeset
|
129 TOK.Imaginary32, TOK.Imaginary64, TOK.Imaginary80: |
65 | 130 writef("<n>%s</n>", srcText); |
15 | 131 break; |
20 | 132 case TOK.LParen, TOK.RParen, TOK.LBracket, |
133 TOK.RBracket, TOK.LBrace, TOK.RBrace: | |
65 | 134 writef("<br>%s</br>", srcText); |
20 | 135 break; |
22
b05fff8e2ce4
- Added code for parsing Dot, Slice and Ellipses tokens.
aziz
parents:
21
diff
changeset
|
136 case TOK.EOF: break; |
5 | 137 default: |
28 | 138 if (token.isKeyword()) |
65 | 139 writef("<k>%s</k>", srcText); |
28 | 140 else |
65 | 141 writef("%s", srcText); |
5 | 142 } |
143 end = token.end; | |
4 | 144 } |
51 | 145 writef("\n</sourcetext>\n</root>"); |
0 | 146 } |