Mercurial > projects > dil
annotate trunk/src/main.d @ 31:94f09f4e988e
- Added struct for strings to Token with 'pf' = postfix.
- Added code for parsing hex string literals.
- Added stub for parsing raw strings literals.
author | aziz |
---|---|
date | Mon, 25 Jun 2007 13:34:03 +0000 |
parents | 426767b94635 |
children | d7011daa4740 |
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"?> | |
35 <sourcetext>`); | |
36 foreach(ref token; tokens) | |
4 | 37 { |
5 | 38 if (end != token.start) |
39 writef("%s", xmlescape(end[0 .. token.start - end])); | |
28 | 40 string span = xmlescape(token.span); |
5 | 41 switch(token.type) |
42 { | |
43 case TOK.Identifier: | |
44 writef("<i>%s</i>", span); | |
45 break; | |
46 case TOK.Comment: | |
47 writef("<c>%s</c>", span); | |
48 break; | |
9
5d6968cc751e
- Parsing string and character literals now (rudimentary implementation.)
aziz
parents:
5
diff
changeset
|
49 case TOK.String: |
5d6968cc751e
- Parsing string and character literals now (rudimentary implementation.)
aziz
parents:
5
diff
changeset
|
50 writef("<sl>%s</sl>", span); |
5d6968cc751e
- Parsing string and character literals now (rudimentary implementation.)
aziz
parents:
5
diff
changeset
|
51 break; |
5d6968cc751e
- Parsing string and character literals now (rudimentary implementation.)
aziz
parents:
5
diff
changeset
|
52 case TOK.Character: |
5d6968cc751e
- Parsing string and character literals now (rudimentary implementation.)
aziz
parents:
5
diff
changeset
|
53 writef("<cl>%s</cl>", span); |
5d6968cc751e
- Parsing string and character literals now (rudimentary implementation.)
aziz
parents:
5
diff
changeset
|
54 break; |
14 | 55 case TOK.DivisionAssign: |
56 writef("<op>%s</op>", span); | |
57 break; | |
31
94f09f4e988e
- Added struct for strings to Token with 'pf' = postfix.
aziz
parents:
30
diff
changeset
|
58 case TOK.AndLogical: |
94f09f4e988e
- Added struct for strings to Token with 'pf' = postfix.
aziz
parents:
30
diff
changeset
|
59 writef("<op>∧</op>"); |
94f09f4e988e
- Added struct for strings to Token with 'pf' = postfix.
aziz
parents:
30
diff
changeset
|
60 break; |
94f09f4e988e
- Added struct for strings to Token with 'pf' = postfix.
aziz
parents:
30
diff
changeset
|
61 case TOK.OrLogical: |
94f09f4e988e
- Added struct for strings to Token with 'pf' = postfix.
aziz
parents:
30
diff
changeset
|
62 writef("<op>∨</op>"); |
94f09f4e988e
- Added struct for strings to Token with 'pf' = postfix.
aziz
parents:
30
diff
changeset
|
63 break; |
94f09f4e988e
- Added struct for strings to Token with 'pf' = postfix.
aziz
parents:
30
diff
changeset
|
64 // case TOK.NotEqual: |
94f09f4e988e
- Added struct for strings to Token with 'pf' = postfix.
aziz
parents:
30
diff
changeset
|
65 // writef("<op>≠</op>"); |
94f09f4e988e
- Added struct for strings to Token with 'pf' = postfix.
aziz
parents:
30
diff
changeset
|
66 // break; |
15 | 67 case TOK.Number: |
68 writef("<n>%s</n>", span); | |
69 break; | |
20 | 70 case TOK.LParen, TOK.RParen, TOK.LBracket, |
71 TOK.RBracket, TOK.LBrace, TOK.RBrace: | |
72 writef("<br>%s</br>", span); | |
73 break; | |
22
b05fff8e2ce4
- Added code for parsing Dot, Slice and Ellipses tokens.
aziz
parents:
21
diff
changeset
|
74 case TOK.EOF: break; |
5 | 75 default: |
28 | 76 if (token.isKeyword()) |
77 writef("<k>%s</k>", span); | |
78 else | |
79 writef("%s", span); | |
5 | 80 } |
81 end = token.end; | |
4 | 82 } |
10 | 83 writef("\n</sourcetext>"); |
0 | 84 } |