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