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