Mercurial > projects > dil
annotate trunk/src/main.d @ 54:e55bd2270f94
- Relocated messages table to a separate module.
author | aziz |
---|---|
date | Wed, 27 Jun 2007 17:58:00 +0000 |
parents | cadd2bfe686c |
children | 50bb7fc9db44 |
rev | line source |
---|---|
0 | 1 /++ |
2 Author: Aziz Köksal | |
3 License: GPL2 | |
4 +/ | |
5 module dparser; | |
3 | 6 import Lexer; |
7 import Token; | |
54 | 8 import Messages; |
3 | 9 import std.stdio; |
4 | 10 import std.file; |
0 | 11 |
5 | 12 char[] xmlescape(char[] text) |
13 { | |
14 char[] result; | |
15 foreach(c; text) | |
16 switch(c) | |
17 { | |
18 case '<': result ~= "<"; break; | |
19 case '>': result ~= ">"; break; | |
20 case '&': result ~= "&"; break; | |
21 default: result ~= c; | |
22 } | |
23 return result; | |
24 } | |
25 | |
0 | 26 void main(char[][] args) |
27 { | |
4 | 28 auto srctext = cast(char[]) std.file.read(args[1]); |
30 | 29 auto lx = new Lexer(srctext, args[1]); |
0 | 30 |
5 | 31 auto tokens = lx.getTokens(); |
32 char* end = lx.text.ptr; | |
33 | |
34 writef(`<?xml version="1.0"?> | |
35 <?xml-stylesheet href="format.css" type="text/css"?> | |
51 | 36 <root> |
37 <compilerinfo>`\n); | |
38 foreach (error; lx.errors) | |
39 { | |
40 writefln(`<error t="%s">%s(%d): %s</error>`, "l", lx.fileName, error.loc, messages[error.id]); | |
41 } | |
42 writef(`</compilerinfo> | |
5 | 43 <sourcetext>`); |
51 | 44 foreach (ref token; tokens) |
4 | 45 { |
5 | 46 if (end != token.start) |
47 writef("%s", xmlescape(end[0 .. token.start - end])); | |
28 | 48 string span = xmlescape(token.span); |
5 | 49 switch(token.type) |
50 { | |
51 case TOK.Identifier: | |
52 writef("<i>%s</i>", span); | |
53 break; | |
54 case TOK.Comment: | |
44
5055947e0f98
- Specific operators and comments can be formatted with CSS now.
aziz
parents:
39
diff
changeset
|
55 string c; |
5055947e0f98
- Specific operators and comments can be formatted with CSS now.
aziz
parents:
39
diff
changeset
|
56 switch (token.start[1]) |
5055947e0f98
- Specific operators and comments can be formatted with CSS now.
aziz
parents:
39
diff
changeset
|
57 { |
5055947e0f98
- Specific operators and comments can be formatted with CSS now.
aziz
parents:
39
diff
changeset
|
58 case '/': c = "lc"; break; |
5055947e0f98
- Specific operators and comments can be formatted with CSS now.
aziz
parents:
39
diff
changeset
|
59 case '*': c = "bc"; break; |
5055947e0f98
- Specific operators and comments can be formatted with CSS now.
aziz
parents:
39
diff
changeset
|
60 case '+': c = "nc"; break; |
5055947e0f98
- Specific operators and comments can be formatted with CSS now.
aziz
parents:
39
diff
changeset
|
61 } |
5055947e0f98
- Specific operators and comments can be formatted with CSS now.
aziz
parents:
39
diff
changeset
|
62 writef(`<c c="%s">%s</c>`, c, span); |
5 | 63 break; |
9
5d6968cc751e
- Parsing string and character literals now (rudimentary implementation.)
aziz
parents:
5
diff
changeset
|
64 case TOK.String: |
5d6968cc751e
- Parsing string and character literals now (rudimentary implementation.)
aziz
parents:
5
diff
changeset
|
65 writef("<sl>%s</sl>", span); |
5d6968cc751e
- Parsing string and character literals now (rudimentary implementation.)
aziz
parents:
5
diff
changeset
|
66 break; |
5d6968cc751e
- Parsing string and character literals now (rudimentary implementation.)
aziz
parents:
5
diff
changeset
|
67 case TOK.Character: |
5d6968cc751e
- Parsing string and character literals now (rudimentary implementation.)
aziz
parents:
5
diff
changeset
|
68 writef("<cl>%s</cl>", span); |
5d6968cc751e
- Parsing string and character literals now (rudimentary implementation.)
aziz
parents:
5
diff
changeset
|
69 break; |
44
5055947e0f98
- Specific operators and comments can be formatted with CSS now.
aziz
parents:
39
diff
changeset
|
70 case TOK.Assign, TOK.Equal, |
39
69b940398d7b
- Added unittest to test correct parsing of operator tokens.
aziz
parents:
35
diff
changeset
|
71 TOK.Less, TOK.Greater, |
69b940398d7b
- Added unittest to test correct parsing of operator tokens.
aziz
parents:
35
diff
changeset
|
72 TOK.LShiftAssign, TOK.LShift, |
69b940398d7b
- Added unittest to test correct parsing of operator tokens.
aziz
parents:
35
diff
changeset
|
73 TOK.RShiftAssign, TOK.RShift, |
69b940398d7b
- Added unittest to test correct parsing of operator tokens.
aziz
parents:
35
diff
changeset
|
74 TOK.URShiftAssign, TOK.URShift, |
33 | 75 TOK.OrAssign, TOK.OrBinary, |
76 TOK.AndAssign, TOK.AndBinary, | |
77 TOK.PlusAssign, TOK.PlusPlus, TOK.Plus, | |
78 TOK.MinusAssign, TOK.MinusMinus, TOK.Minus, | |
79 TOK.DivAssign, TOK.Div, | |
80 TOK.MulAssign, TOK.Mul, | |
81 TOK.ModAssign, TOK.Mod, | |
82 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
|
83 TOK.CatAssign, TOK.Catenate, |
39
69b940398d7b
- Added unittest to test correct parsing of operator tokens.
aziz
parents:
35
diff
changeset
|
84 TOK.Tilde, |
35
c470b9356e35
- Added code for parsing Unordered, UorE, UorG, UorGorE, UorL, UorLorE, NotEqual and Not tokens.
aziz
parents:
33
diff
changeset
|
85 TOK.Unordered, |
c470b9356e35
- Added code for parsing Unordered, UorE, UorG, UorGorE, UorL, UorLorE, NotEqual and Not tokens.
aziz
parents:
33
diff
changeset
|
86 TOK.UorE, |
c470b9356e35
- Added code for parsing Unordered, UorE, UorG, UorGorE, UorL, UorLorE, NotEqual and Not tokens.
aziz
parents:
33
diff
changeset
|
87 TOK.UorG, |
c470b9356e35
- Added code for parsing Unordered, UorE, UorG, UorGorE, UorL, UorLorE, NotEqual and Not tokens.
aziz
parents:
33
diff
changeset
|
88 TOK.UorGorE, |
c470b9356e35
- Added code for parsing Unordered, UorE, UorG, UorGorE, UorL, UorLorE, NotEqual and Not tokens.
aziz
parents:
33
diff
changeset
|
89 TOK.UorL, |
39
69b940398d7b
- Added unittest to test correct parsing of operator tokens.
aziz
parents:
35
diff
changeset
|
90 TOK.UorLorE, |
69b940398d7b
- Added unittest to test correct parsing of operator tokens.
aziz
parents:
35
diff
changeset
|
91 TOK.LorEorG: |
14 | 92 writef("<op>%s</op>", span); |
93 break; | |
39
69b940398d7b
- Added unittest to test correct parsing of operator tokens.
aziz
parents:
35
diff
changeset
|
94 case TOK.LorG: |
44
5055947e0f98
- Specific operators and comments can be formatted with CSS now.
aziz
parents:
39
diff
changeset
|
95 writef(`<op c="lg"><></op>`); |
39
69b940398d7b
- Added unittest to test correct parsing of operator tokens.
aziz
parents:
35
diff
changeset
|
96 break; |
69b940398d7b
- Added unittest to test correct parsing of operator tokens.
aziz
parents:
35
diff
changeset
|
97 case TOK.LessEqual: |
44
5055947e0f98
- Specific operators and comments can be formatted with CSS now.
aziz
parents:
39
diff
changeset
|
98 writef(`<op c="le"><=</op>`); |
39
69b940398d7b
- Added unittest to test correct parsing of operator tokens.
aziz
parents:
35
diff
changeset
|
99 break; |
69b940398d7b
- Added unittest to test correct parsing of operator tokens.
aziz
parents:
35
diff
changeset
|
100 case TOK.GreaterEqual: |
44
5055947e0f98
- Specific operators and comments can be formatted with CSS now.
aziz
parents:
39
diff
changeset
|
101 writef(`<op c="ge">>=</op>`); |
39
69b940398d7b
- Added unittest to test correct parsing of operator tokens.
aziz
parents:
35
diff
changeset
|
102 break; |
31
94f09f4e988e
- Added struct for strings to Token with 'pf' = postfix.
aziz
parents:
30
diff
changeset
|
103 case TOK.AndLogical: |
44
5055947e0f98
- Specific operators and comments can be formatted with CSS now.
aziz
parents:
39
diff
changeset
|
104 writef(`<op c="aa">&&</op>`); |
31
94f09f4e988e
- Added struct for strings to Token with 'pf' = postfix.
aziz
parents:
30
diff
changeset
|
105 break; |
94f09f4e988e
- Added struct for strings to Token with 'pf' = postfix.
aziz
parents:
30
diff
changeset
|
106 case TOK.OrLogical: |
44
5055947e0f98
- Specific operators and comments can be formatted with CSS now.
aziz
parents:
39
diff
changeset
|
107 writef(`<op c="oo">||</op>`); |
31
94f09f4e988e
- Added struct for strings to Token with 'pf' = postfix.
aziz
parents:
30
diff
changeset
|
108 break; |
35
c470b9356e35
- Added code for parsing Unordered, UorE, UorG, UorGorE, UorL, UorLorE, NotEqual and Not tokens.
aziz
parents:
33
diff
changeset
|
109 case TOK.NotEqual: |
44
5055947e0f98
- Specific operators and comments can be formatted with CSS now.
aziz
parents:
39
diff
changeset
|
110 writef(`<op c="ne">!=</op>`); |
5055947e0f98
- Specific operators and comments can be formatted with CSS now.
aziz
parents:
39
diff
changeset
|
111 break; |
5055947e0f98
- Specific operators and comments can be formatted with CSS now.
aziz
parents:
39
diff
changeset
|
112 case TOK.Not: |
5055947e0f98
- Specific operators and comments can be formatted with CSS now.
aziz
parents:
39
diff
changeset
|
113 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
|
114 break; |
15 | 115 case TOK.Number: |
116 writef("<n>%s</n>", span); | |
117 break; | |
20 | 118 case TOK.LParen, TOK.RParen, TOK.LBracket, |
119 TOK.RBracket, TOK.LBrace, TOK.RBrace: | |
120 writef("<br>%s</br>", span); | |
121 break; | |
22
b05fff8e2ce4
- Added code for parsing Dot, Slice and Ellipses tokens.
aziz
parents:
21
diff
changeset
|
122 case TOK.EOF: break; |
5 | 123 default: |
28 | 124 if (token.isKeyword()) |
125 writef("<k>%s</k>", span); | |
126 else | |
127 writef("%s", span); | |
5 | 128 } |
129 end = token.end; | |
4 | 130 } |
51 | 131 writef("\n</sourcetext>\n</root>"); |
0 | 132 } |