Mercurial > projects > dil
annotate trunk/src/Token.d @ 210:b7bde6583d3e
- Made toString() static.
author | aziz |
---|---|
date | Sun, 22 Jul 2007 17:25:05 +0000 |
parents | 0a9bccf74046 |
children | 7911f6a92e6e |
rev | line source |
---|---|
0 | 1 /++ |
2 Author: Aziz Köksal | |
3 License: GPL2 | |
4 +/ | |
5 module Token; | |
6 | |
7 struct Position | |
8 { | |
9 size_t loc; | |
10 size_t col; | |
11 } | |
12 | |
3 | 13 enum TOK |
14 { | |
95 | 15 Invalid, |
16 | |
3 | 17 Identifier, |
4 | 18 Comment, |
9
5d6968cc751e
- Parsing string and character literals now (rudimentary implementation.)
aziz
parents:
4
diff
changeset
|
19 String, |
82
fc645fb2fe72
- scanEscapeSequences() doesn't return 0xFFFF as an error value anymore, because it is a valid codepoint usable by the user.
aziz
parents:
71
diff
changeset
|
20 CharLiteral, WCharLiteral, DCharLiteral, |
58 | 21 |
22 // Numbers | |
23 Int32, Int64, Uint32, Uint64, | |
97
1a83e5070a84
- Added code for parsing IntNumber- and RealNumberExpressions.
aziz
parents:
95
diff
changeset
|
24 // Floating point number scanner relies on this order. (FloatXY + 3 == ImaginaryXY) |
62
96af5653acef
- Fixed loop of hex number scanner. Moved checks under the switch block.
aziz
parents:
58
diff
changeset
|
25 Float32, Float64, Float80, |
96af5653acef
- Fixed loop of hex number scanner. Moved checks under the switch block.
aziz
parents:
58
diff
changeset
|
26 Imaginary32, Imaginary64, Imaginary80, |
58 | 27 |
36
3c7210a722f7
- Added code for parsing LorEorG, LorG, LessEqual, LessThan, LShiftAssign and LShift tokens.
aziz
parents:
35
diff
changeset
|
28 |
3c7210a722f7
- Added code for parsing LorEorG, LorG, LessEqual, LessThan, LShiftAssign and LShift tokens.
aziz
parents:
35
diff
changeset
|
29 // Brackets |
20 | 30 LParen, |
31 RParen, | |
32 LBracket, | |
33 RBracket, | |
34 LBrace, | |
35 RBrace, | |
21
c785c122e4e6
- Added code for parsing Colon, Semicolon, Question, Comma and Dollar.
aziz
parents:
20
diff
changeset
|
36 |
22
b05fff8e2ce4
- Added code for parsing Dot, Slice and Ellipses tokens.
aziz
parents:
21
diff
changeset
|
37 Dot, Slice, Ellipses, |
b05fff8e2ce4
- Added code for parsing Dot, Slice and Ellipses tokens.
aziz
parents:
21
diff
changeset
|
38 |
131 | 39 // Floating point number operators |
35
c470b9356e35
- Added code for parsing Unordered, UorE, UorG, UorGorE, UorL, UorLorE, NotEqual and Not tokens.
aziz
parents:
32
diff
changeset
|
40 Unordered, |
c470b9356e35
- Added code for parsing Unordered, UorE, UorG, UorGorE, UorL, UorLorE, NotEqual and Not tokens.
aziz
parents:
32
diff
changeset
|
41 UorE, |
c470b9356e35
- Added code for parsing Unordered, UorE, UorG, UorGorE, UorL, UorLorE, NotEqual and Not tokens.
aziz
parents:
32
diff
changeset
|
42 UorG, |
c470b9356e35
- Added code for parsing Unordered, UorE, UorG, UorGorE, UorL, UorLorE, NotEqual and Not tokens.
aziz
parents:
32
diff
changeset
|
43 UorGorE, |
c470b9356e35
- Added code for parsing Unordered, UorE, UorG, UorGorE, UorL, UorLorE, NotEqual and Not tokens.
aziz
parents:
32
diff
changeset
|
44 UorL, |
c470b9356e35
- Added code for parsing Unordered, UorE, UorG, UorGorE, UorL, UorLorE, NotEqual and Not tokens.
aziz
parents:
32
diff
changeset
|
45 UorLorE, |
36
3c7210a722f7
- Added code for parsing LorEorG, LorG, LessEqual, LessThan, LShiftAssign and LShift tokens.
aziz
parents:
35
diff
changeset
|
46 LorEorG, |
3c7210a722f7
- Added code for parsing LorEorG, LorG, LessEqual, LessThan, LShiftAssign and LShift tokens.
aziz
parents:
35
diff
changeset
|
47 LorG, |
35
c470b9356e35
- Added code for parsing Unordered, UorE, UorG, UorGorE, UorL, UorLorE, NotEqual and Not tokens.
aziz
parents:
32
diff
changeset
|
48 |
36
3c7210a722f7
- Added code for parsing LorEorG, LorG, LessEqual, LessThan, LShiftAssign and LShift tokens.
aziz
parents:
35
diff
changeset
|
49 // Normal operators |
35
c470b9356e35
- Added code for parsing Unordered, UorE, UorG, UorGorE, UorL, UorLorE, NotEqual and Not tokens.
aziz
parents:
32
diff
changeset
|
50 Assign, Equal, NotEqual, Not, |
38
640c45aaaaee
- Added code for parsing GreaterEqual, Greater, RShiftAssign, RShift, URShiftAssign and URShift tokens.
aziz
parents:
36
diff
changeset
|
51 LessEqual, Less, |
640c45aaaaee
- Added code for parsing GreaterEqual, Greater, RShiftAssign, RShift, URShiftAssign and URShift tokens.
aziz
parents:
36
diff
changeset
|
52 GreaterEqual, Greater, |
36
3c7210a722f7
- Added code for parsing LorEorG, LorG, LessEqual, LessThan, LShiftAssign and LShift tokens.
aziz
parents:
35
diff
changeset
|
53 LShiftAssign, LShift, |
38
640c45aaaaee
- Added code for parsing GreaterEqual, Greater, RShiftAssign, RShift, URShiftAssign and URShift tokens.
aziz
parents:
36
diff
changeset
|
54 RShiftAssign,RShift, |
640c45aaaaee
- Added code for parsing GreaterEqual, Greater, RShiftAssign, RShift, URShiftAssign and URShift tokens.
aziz
parents:
36
diff
changeset
|
55 URShiftAssign, URShift, |
23
1a7903701a3d
- Added code for parsing OrAssign, OrLogical and OrBinary tokens.
aziz
parents:
22
diff
changeset
|
56 OrAssign, OrLogical, OrBinary, |
24
903f91163f23
- Added code for parsing AndAssign, AndLogical and AndBinary tokens.
aziz
parents:
23
diff
changeset
|
57 AndAssign, AndLogical, AndBinary, |
25
9c866208b3f6
- Added code for parsing PlusAssign, PlusPlus, Plus, MinusAssign, MinusMinus, Minus tokens.
aziz
parents:
24
diff
changeset
|
58 PlusAssign, PlusPlus, Plus, |
9c866208b3f6
- Added code for parsing PlusAssign, PlusPlus, Plus, MinusAssign, MinusMinus, Minus tokens.
aziz
parents:
24
diff
changeset
|
59 MinusAssign, MinusMinus, Minus, |
32
d7011daa4740
- Added missing commas after the items in the messages table.
aziz
parents:
31
diff
changeset
|
60 DivAssign, Div, |
29
ef83eea26bbd
- Added code for parsing MulAssign, Mul, ModAssign, Mod, XorAssign and Xor tokens.
aziz
parents:
28
diff
changeset
|
61 MulAssign, Mul, |
ef83eea26bbd
- Added code for parsing MulAssign, Mul, ModAssign, Mod, XorAssign and Xor tokens.
aziz
parents:
28
diff
changeset
|
62 ModAssign, Mod, |
ef83eea26bbd
- Added code for parsing MulAssign, Mul, ModAssign, Mod, XorAssign and Xor tokens.
aziz
parents:
28
diff
changeset
|
63 XorAssign, Xor, |
27 | 64 CatAssign, Catenate, |
39
69b940398d7b
- Added unittest to test correct parsing of operator tokens.
aziz
parents:
38
diff
changeset
|
65 Tilde, |
71 | 66 Identity, NotIdentity, |
23
1a7903701a3d
- Added code for parsing OrAssign, OrLogical and OrBinary tokens.
aziz
parents:
22
diff
changeset
|
67 |
21
c785c122e4e6
- Added code for parsing Colon, Semicolon, Question, Comma and Dollar.
aziz
parents:
20
diff
changeset
|
68 Colon, |
c785c122e4e6
- Added code for parsing Colon, Semicolon, Question, Comma and Dollar.
aziz
parents:
20
diff
changeset
|
69 Semicolon, |
c785c122e4e6
- Added code for parsing Colon, Semicolon, Question, Comma and Dollar.
aziz
parents:
20
diff
changeset
|
70 Question, |
c785c122e4e6
- Added code for parsing Colon, Semicolon, Question, Comma and Dollar.
aziz
parents:
20
diff
changeset
|
71 Comma, |
c785c122e4e6
- Added code for parsing Colon, Semicolon, Question, Comma and Dollar.
aziz
parents:
20
diff
changeset
|
72 Dollar, |
c785c122e4e6
- Added code for parsing Colon, Semicolon, Question, Comma and Dollar.
aziz
parents:
20
diff
changeset
|
73 |
28 | 74 /* Keywords: |
75 NB.: Token.isKeyword() depends on this list being contiguous. | |
76 */ | |
77 Abstract,Alias,Align,Asm,Assert,Auto,Body, | |
78 Bool,Break,Byte,Case,Cast,Catch,Cdouble, | |
79 Cent,Cfloat,Char,Class,Const,Continue,Creal, | |
80 Dchar,Debug,Default,Delegate,Delete,Deprecated,Do, | |
81 Double,Else,Enum,Export,Extern,False,Final, | |
82 Finally,Float,For,Foreach,Foreach_reverse,Function,Goto, | |
83 Idouble,If,Ifloat,Import,In,Inout,Int, | |
84 Interface,Invariant,Ireal,Is,Lazy,Long,Macro, | |
85 Mixin,Module,New,Null,Out,Override,Package, | |
86 Pragma,Private,Protected,Public,Real,Ref,Return, | |
87 Scope,Short,Static,Struct,Super,Switch,Synchronized, | |
88 Template,This,Throw,True,Try,Typedef,Typeid, | |
89 Typeof,Ubyte,Ucent,Uint,Ulong,Union,Unittest, | |
90 Ushort,Version,Void,Volatile,Wchar,While,With, | |
91 | |
4 | 92 EOF |
3 | 93 } |
94 | |
28 | 95 alias TOK.Abstract KeywordsBegin; |
96 alias TOK.With KeywordsEnd; | |
97 | |
0 | 98 struct Token |
99 { | |
3 | 100 TOK type; |
84
ac8d961d10d1
- Added code for parsing This-,Super-,Null-,Bool-,Dollar-,CharLiteral- and StringLiteralExpression.
aziz
parents:
82
diff
changeset
|
101 // Position pos; |
0 | 102 |
131 | 103 // Token* next, prev; |
104 | |
4 | 105 char* start; |
106 char* end; | |
107 | |
0 | 108 union |
109 { | |
31
94f09f4e988e
- Added struct for strings to Token with 'pf' = postfix.
aziz
parents:
30
diff
changeset
|
110 struct |
94f09f4e988e
- Added struct for strings to Token with 'pf' = postfix.
aziz
parents:
30
diff
changeset
|
111 { |
94f09f4e988e
- Added struct for strings to Token with 'pf' = postfix.
aziz
parents:
30
diff
changeset
|
112 string str; |
94f09f4e988e
- Added struct for strings to Token with 'pf' = postfix.
aziz
parents:
30
diff
changeset
|
113 char pf; |
94f09f4e988e
- Added struct for strings to Token with 'pf' = postfix.
aziz
parents:
30
diff
changeset
|
114 } |
62
96af5653acef
- Fixed loop of hex number scanner. Moved checks under the switch block.
aziz
parents:
58
diff
changeset
|
115 dchar dchar_; |
96af5653acef
- Fixed loop of hex number scanner. Moved checks under the switch block.
aziz
parents:
58
diff
changeset
|
116 long long_; |
96af5653acef
- Fixed loop of hex number scanner. Moved checks under the switch block.
aziz
parents:
58
diff
changeset
|
117 ulong ulong_; |
96af5653acef
- Fixed loop of hex number scanner. Moved checks under the switch block.
aziz
parents:
58
diff
changeset
|
118 int int_; |
96af5653acef
- Fixed loop of hex number scanner. Moved checks under the switch block.
aziz
parents:
58
diff
changeset
|
119 uint uint_; |
96af5653acef
- Fixed loop of hex number scanner. Moved checks under the switch block.
aziz
parents:
58
diff
changeset
|
120 float float_; |
96af5653acef
- Fixed loop of hex number scanner. Moved checks under the switch block.
aziz
parents:
58
diff
changeset
|
121 double double_; |
96af5653acef
- Fixed loop of hex number scanner. Moved checks under the switch block.
aziz
parents:
58
diff
changeset
|
122 real real_; |
0 | 123 } |
28 | 124 |
107
722c05bbd5eb
- Implemented parseEnumDeclaration() and added class EnumDeclaration.
aziz
parents:
97
diff
changeset
|
125 alias srcText identifier; |
722c05bbd5eb
- Implemented parseEnumDeclaration() and added class EnumDeclaration.
aziz
parents:
97
diff
changeset
|
126 |
65 | 127 string srcText() |
28 | 128 { |
30 | 129 assert(start && end); |
28 | 130 return start[0 .. end - start]; |
131 } | |
132 | |
210 | 133 static string toString(TOK tok) |
208 | 134 { |
135 return tokToString[tok]; | |
136 } | |
137 | |
28 | 138 bool isKeyword() |
139 { | |
163
f27a98bb17c7
- Fix: when parsing Declarator fails, type and ident is set to null.
aziz
parents:
131
diff
changeset
|
140 return KeywordsBegin <= type && type <= KeywordsEnd; |
28 | 141 } |
208 | 142 |
163
f27a98bb17c7
- Fix: when parsing Declarator fails, type and ident is set to null.
aziz
parents:
131
diff
changeset
|
143 int opEquals(TOK type2) |
f27a98bb17c7
- Fix: when parsing Declarator fails, type and ident is set to null.
aziz
parents:
131
diff
changeset
|
144 { |
f27a98bb17c7
- Fix: when parsing Declarator fails, type and ident is set to null.
aziz
parents:
131
diff
changeset
|
145 return type == type2; |
f27a98bb17c7
- Fix: when parsing Declarator fails, type and ident is set to null.
aziz
parents:
131
diff
changeset
|
146 } |
208 | 147 } |
148 | |
149 string[] tokToString = [ | |
150 "Invalid", | |
151 | |
152 "Identifier", | |
153 "Comment", | |
154 "String", | |
155 "CharLiteral", "WCharLiteral", "DCharLiteral", | |
156 | |
157 "Int32", "Int64", "Uint32", "Uint64", | |
158 "Float32", "Float64", "Float80", | |
159 "Imaginary32", "Imaginary64", "Imaginary80", | |
160 | |
161 "(", | |
162 ")", | |
163 "[", | |
164 "]", | |
165 "{", | |
166 "}", | |
167 | |
168 ".", "..", "...", | |
169 | |
170 "Unordered", | |
171 "UorE", | |
172 "UorG", | |
173 "UorGorE", | |
174 "UorL", | |
175 "UorLorE", | |
176 "LorEorG", | |
177 "LorG", | |
178 | |
179 "=", "==", "!=", "!", | |
180 "<=", "<", | |
181 ">=", ">", | |
182 "<<=", "<<", | |
183 ">>=",">>", | |
184 ">>>=", ">>>", | |
185 "|=", "||", "|", | |
186 "&=", "&&", "&", | |
187 "+=", "++", "+", | |
188 "-=", "--", "-", | |
189 "/=", "/", | |
190 "*=", "*", | |
191 "%=", "%", | |
192 "^=", "^", | |
193 "~=", "~", | |
194 "~", | |
195 "is", "!is", | |
196 | |
197 ":", | |
198 ";", | |
199 "?", | |
200 ",", | |
201 "$", | |
202 | |
203 "abstract","alias","align","asm","assert","auto","body", | |
204 "bool","break","byte","case","cast","catch","cdouble", | |
205 "cent","cfloat","char","class","const","continue","creal", | |
206 "dchar","debug","default","delegate","delete","deprecated","do", | |
207 "double","else","enum","export","extern","false","final", | |
208 "finally","float","for","foreach","foreach_reverse","function","goto", | |
209 "idouble","if","ifloat","import","in","inout","int", | |
210 "interface","invariant","ireal","is","lazy","long","macro", | |
211 "mixin","module","new","null","out","override","package", | |
212 "pragma","private","protected","public","real","ref","return", | |
213 "scope","short","static","struct","super","switch","synchronized", | |
214 "template","this","throw","true","try","typedef","typeid", | |
215 "typeof","ubyte","ucent","uint","ulong","union","unittest", | |
216 "ushort","version","void","volatile","wchar","while","with", | |
217 | |
218 "EOF" | |
219 ]; |