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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
8ba2570de175 Initial import.
aziz
parents:
diff changeset
1 /++
8ba2570de175 Initial import.
aziz
parents:
diff changeset
2 Author: Aziz Köksal
8ba2570de175 Initial import.
aziz
parents:
diff changeset
3 License: GPL2
8ba2570de175 Initial import.
aziz
parents:
diff changeset
4 +/
8ba2570de175 Initial import.
aziz
parents:
diff changeset
5 module dparser;
3
4bbce78bfb1e - Added TOK enum.
aziz
parents: 0
diff changeset
6 import Lexer;
4bbce78bfb1e - Added TOK enum.
aziz
parents: 0
diff changeset
7 import Token;
4bbce78bfb1e - Added TOK enum.
aziz
parents: 0
diff changeset
8 import std.stdio;
4
92df59b1ec4a - Started implementation of scan().
aziz
parents: 3
diff changeset
9 import std.file;
0
8ba2570de175 Initial import.
aziz
parents:
diff changeset
10
5
79b4e8848794 - Started writing XML generator.
aziz
parents: 4
diff changeset
11 char[] xmlescape(char[] text)
79b4e8848794 - Started writing XML generator.
aziz
parents: 4
diff changeset
12 {
79b4e8848794 - Started writing XML generator.
aziz
parents: 4
diff changeset
13 char[] result;
79b4e8848794 - Started writing XML generator.
aziz
parents: 4
diff changeset
14 foreach(c; text)
79b4e8848794 - Started writing XML generator.
aziz
parents: 4
diff changeset
15 switch(c)
79b4e8848794 - Started writing XML generator.
aziz
parents: 4
diff changeset
16 {
79b4e8848794 - Started writing XML generator.
aziz
parents: 4
diff changeset
17 case '<': result ~= "&lt;"; break;
79b4e8848794 - Started writing XML generator.
aziz
parents: 4
diff changeset
18 case '>': result ~= "&gt;"; break;
79b4e8848794 - Started writing XML generator.
aziz
parents: 4
diff changeset
19 case '&': result ~= "&amp;"; break;
79b4e8848794 - Started writing XML generator.
aziz
parents: 4
diff changeset
20 default: result ~= c;
79b4e8848794 - Started writing XML generator.
aziz
parents: 4
diff changeset
21 }
79b4e8848794 - Started writing XML generator.
aziz
parents: 4
diff changeset
22 return result;
79b4e8848794 - Started writing XML generator.
aziz
parents: 4
diff changeset
23 }
79b4e8848794 - Started writing XML generator.
aziz
parents: 4
diff changeset
24
0
8ba2570de175 Initial import.
aziz
parents:
diff changeset
25 void main(char[][] args)
8ba2570de175 Initial import.
aziz
parents:
diff changeset
26 {
4
92df59b1ec4a - Started implementation of scan().
aziz
parents: 3
diff changeset
27 auto srctext = cast(char[]) std.file.read(args[1]);
30
426767b94635 - Added code for parsing the '#line' special token.
aziz
parents: 28
diff changeset
28 auto lx = new Lexer(srctext, args[1]);
0
8ba2570de175 Initial import.
aziz
parents:
diff changeset
29
5
79b4e8848794 - Started writing XML generator.
aziz
parents: 4
diff changeset
30 auto tokens = lx.getTokens();
79b4e8848794 - Started writing XML generator.
aziz
parents: 4
diff changeset
31 char* end = lx.text.ptr;
79b4e8848794 - Started writing XML generator.
aziz
parents: 4
diff changeset
32
79b4e8848794 - Started writing XML generator.
aziz
parents: 4
diff changeset
33 writef(`<?xml version="1.0"?>
79b4e8848794 - Started writing XML generator.
aziz
parents: 4
diff changeset
34 <?xml-stylesheet href="format.css" type="text/css"?>
51
cadd2bfe686c - Displaying error messages in XML.
aziz
parents: 44
diff changeset
35 <root>
cadd2bfe686c - Displaying error messages in XML.
aziz
parents: 44
diff changeset
36 <compilerinfo>`\n);
cadd2bfe686c - Displaying error messages in XML.
aziz
parents: 44
diff changeset
37 foreach (error; lx.errors)
cadd2bfe686c - Displaying error messages in XML.
aziz
parents: 44
diff changeset
38 {
cadd2bfe686c - Displaying error messages in XML.
aziz
parents: 44
diff changeset
39 writefln(`<error t="%s">%s(%d): %s</error>`, "l", lx.fileName, error.loc, messages[error.id]);
cadd2bfe686c - Displaying error messages in XML.
aziz
parents: 44
diff changeset
40 }
cadd2bfe686c - Displaying error messages in XML.
aziz
parents: 44
diff changeset
41 writef(`</compilerinfo>
5
79b4e8848794 - Started writing XML generator.
aziz
parents: 4
diff changeset
42 <sourcetext>`);
51
cadd2bfe686c - Displaying error messages in XML.
aziz
parents: 44
diff changeset
43 foreach (ref token; tokens)
4
92df59b1ec4a - Started implementation of scan().
aziz
parents: 3
diff changeset
44 {
5
79b4e8848794 - Started writing XML generator.
aziz
parents: 4
diff changeset
45 if (end != token.start)
79b4e8848794 - Started writing XML generator.
aziz
parents: 4
diff changeset
46 writef("%s", xmlescape(end[0 .. token.start - end]));
28
3a9daccf7d96 - Added table for identifiers to Lexer.
aziz
parents: 22
diff changeset
47 string span = xmlescape(token.span);
5
79b4e8848794 - Started writing XML generator.
aziz
parents: 4
diff changeset
48 switch(token.type)
79b4e8848794 - Started writing XML generator.
aziz
parents: 4
diff changeset
49 {
79b4e8848794 - Started writing XML generator.
aziz
parents: 4
diff changeset
50 case TOK.Identifier:
79b4e8848794 - Started writing XML generator.
aziz
parents: 4
diff changeset
51 writef("<i>%s</i>", span);
79b4e8848794 - Started writing XML generator.
aziz
parents: 4
diff changeset
52 break;
79b4e8848794 - Started writing XML generator.
aziz
parents: 4
diff changeset
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
79b4e8848794 - Started writing XML generator.
aziz
parents: 4
diff changeset
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
cf3047cf3cd2 - Added code for parsing back quote and raw strings.
aziz
parents: 32
diff changeset
74 TOK.OrAssign, TOK.OrBinary,
cf3047cf3cd2 - Added code for parsing back quote and raw strings.
aziz
parents: 32
diff changeset
75 TOK.AndAssign, TOK.AndBinary,
cf3047cf3cd2 - Added code for parsing back quote and raw strings.
aziz
parents: 32
diff changeset
76 TOK.PlusAssign, TOK.PlusPlus, TOK.Plus,
cf3047cf3cd2 - Added code for parsing back quote and raw strings.
aziz
parents: 32
diff changeset
77 TOK.MinusAssign, TOK.MinusMinus, TOK.Minus,
cf3047cf3cd2 - Added code for parsing back quote and raw strings.
aziz
parents: 32
diff changeset
78 TOK.DivAssign, TOK.Div,
cf3047cf3cd2 - Added code for parsing back quote and raw strings.
aziz
parents: 32
diff changeset
79 TOK.MulAssign, TOK.Mul,
cf3047cf3cd2 - Added code for parsing back quote and raw strings.
aziz
parents: 32
diff changeset
80 TOK.ModAssign, TOK.Mod,
cf3047cf3cd2 - Added code for parsing back quote and raw strings.
aziz
parents: 32
diff changeset
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
cdf788d8bdaf - Parsing /= now.
aziz
parents: 10
diff changeset
91 writef("<op>%s</op>", span);
cdf788d8bdaf - Parsing /= now.
aziz
parents: 10
diff changeset
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">&lt;&gt;</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">&lt;=</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">&gt;=</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">&amp;&amp;</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
c70c028e47dd - Started implementation of lexing numbers.
aziz
parents: 14
diff changeset
114 case TOK.Number:
c70c028e47dd - Started implementation of lexing numbers.
aziz
parents: 14
diff changeset
115 writef("<n>%s</n>", span);
c70c028e47dd - Started implementation of lexing numbers.
aziz
parents: 14
diff changeset
116 break;
20
d6adfbd7c513 - Added code for parsing braces.
aziz
parents: 17
diff changeset
117 case TOK.LParen, TOK.RParen, TOK.LBracket,
d6adfbd7c513 - Added code for parsing braces.
aziz
parents: 17
diff changeset
118 TOK.RBracket, TOK.LBrace, TOK.RBrace:
d6adfbd7c513 - Added code for parsing braces.
aziz
parents: 17
diff changeset
119 writef("<br>%s</br>", span);
d6adfbd7c513 - Added code for parsing braces.
aziz
parents: 17
diff changeset
120 break;
22
b05fff8e2ce4 - Added code for parsing Dot, Slice and Ellipses tokens.
aziz
parents: 21
diff changeset
121 case TOK.EOF: break;
5
79b4e8848794 - Started writing XML generator.
aziz
parents: 4
diff changeset
122 default:
28
3a9daccf7d96 - Added table for identifiers to Lexer.
aziz
parents: 22
diff changeset
123 if (token.isKeyword())
3a9daccf7d96 - Added table for identifiers to Lexer.
aziz
parents: 22
diff changeset
124 writef("<k>%s</k>", span);
3a9daccf7d96 - Added table for identifiers to Lexer.
aziz
parents: 22
diff changeset
125 else
3a9daccf7d96 - Added table for identifiers to Lexer.
aziz
parents: 22
diff changeset
126 writef("%s", span);
5
79b4e8848794 - Started writing XML generator.
aziz
parents: 4
diff changeset
127 }
79b4e8848794 - Started writing XML generator.
aziz
parents: 4
diff changeset
128 end = token.end;
4
92df59b1ec4a - Started implementation of scan().
aziz
parents: 3
diff changeset
129 }
51
cadd2bfe686c - Displaying error messages in XML.
aziz
parents: 44
diff changeset
130 writef("\n</sourcetext>\n</root>");
0
8ba2570de175 Initial import.
aziz
parents:
diff changeset
131 }