comparison src/lexer/Token.d @ 207:e0551773a005

Added the correct version.
author Anders Johnsen <skabet@gmail.com>
date Tue, 12 Aug 2008 18:19:34 +0200
parents d3c148ca429b
children
comparison
equal deleted inserted replaced
206:d3c148ca429b 207:e0551773a005
1 module lexer.Token; 1 module lexer.Token;
2 2
3 public 3 public
4 import basic.SourceLocation; 4 import basic.SourceLocation,
5 basic.SourceManager;
5 6
6 import Integer = tango.text.convert.Integer; 7 import Integer = tango.text.convert.Integer;
7 8
8 /** 9 /**
9 The Token struct will be used through the Lexer, Parser and other 10 The Token struct will be used through the Lexer, Parser and other
32 } 33 }
33 34
34 /** 35 /**
35 Get the type of the Token as a string 36 Get the type of the Token as a string
36 */ 37 */
37 char[] getType () 38 char[] get (SourceManager sm)
38 { 39 {
40 if (isIdentifier)
41 return sm.getText(asRange);
39 return typeToString[this.type]; 42 return typeToString[this.type];
40 } 43 }
41 44
42 /** 45 /**
43 A human readable dump of a Token 46 A human readable dump of a Token
44 */ 47 */
45 char[] toString () 48 char[] toString ()
46 { 49 {
47 return this.getType()~": Len: "~Integer.toString(this.length); 50 return typeToString[this.type];
48 } 51 }
49 52
50 /// Get the range of this token 53 /// Get the range of this token
51 SourceRange asRange() { return SourceRange(location, location + length); } 54 SourceRange asRange() { return SourceRange(location, location + length); }
52 55
62 /** 65 /**
63 Returns true for all the various assignments (=, +=, *= ...) 66 Returns true for all the various assignments (=, +=, *= ...)
64 */ 67 */
65 bool isAssignment() 68 bool isAssignment()
66 { 69 {
67 return type == Tok.Assign; 70 return type >= Tok.Assign && type <= Tok.PercentAssign;
71 }
72
73 /**
74 Returns true for all attributes( public, static, private...)
75 */
76 bool isAttribute()
77 {
78 return type >= Tok.Public && type <= Tok.Extern;
79 }
80
81 /**
82 Returns true for all attributes( public, static, private...)
83 */
84 bool isBaseClassProtection()
85 {
86 return type >= Tok.Public && type <= Tok.Export;
87 }
88
89 /**
90 just a shortcut to avoid `token.type == tok.Switch`.
91 */
92 bool isSwitch()
93 {
94 return type == Tok.Switch;
95 }
96
97 /**
98 just a shortcut to avoid `token.type == tok.While`.
99 */
100 bool isWhile()
101 {
102 return type == Tok.While;
103 }
104
105 /**
106 just a shortcut to avoid `token.type == tok.For`.
107 */
108 bool isFor()
109 {
110 return type == Tok.For;
111 }
112
113 /**
114 just a shortcut to avoid `token.type == tok.If`.
115 */
116 bool isIf()
117 {
118 return type == Tok.If;
119 }
120
121 /**
122 just a shortcut to avoid `token.type == tok.Return`.
123 */
124 bool isReturn()
125 {
126 return type == Tok.Return;
68 } 127 }
69 128
70 /** 129 /**
71 Just a shortcut to avoid `token.type == Tok.Identifier`. 130 Just a shortcut to avoid `token.type == Tok.Identifier`.
72 */ 131 */
88 EOF, 147 EOF,
89 148
90 /* Basic types */ 149 /* Basic types */
91 Identifier, 150 Identifier,
92 Integer, 151 Integer,
152 String,
93 153
94 /* Basic operators */ 154 /* Basic operators */
95 Assign, 155 Assign,
96 Plus, Minus, 156 PlusAssign,
97 Star, Slash, Percent, 157 MinusAssign,
158 StarAssign,
159 SlashAssign,
160 PercentAssign,
161 Plus,
162 Minus,
163 Star,
164 Slash,
165 Percent,
166 LeftShift, RightShift, UnsignedRightShift,
98 Comma, 167 Comma,
168 And,
99 169
100 /* Symbols */ 170 /* Symbols */
101 OpenParentheses, 171 OpenParentheses,
102 CloseParentheses, 172 CloseParentheses,
103 OpenBrace, 173 OpenBrace,
113 Lt, Gt, 183 Lt, Gt,
114 Le, Ge, 184 Le, Ge,
115 185
116 Not, 186 Not,
117 187
188
118 /* Keywords */ 189 /* Keywords */
119 Byte, Ubyte, 190 Byte, Ubyte,
120 Short, Ushort, 191 Short, Ushort,
121 Int, Uint, 192 Int, Uint,
122 Long, Ulong, 193 Long, Ulong,
127 198
128 Bool, 199 Bool,
129 200
130 Void, 201 Void,
131 202
132 Struct, 203 Struct, Function, Delegate, Class, This,
204 Interface, Union, Typedef, Typeid,
205 Typeof, Sizeof, Alias,
133 206
134 If, Else, 207 If, Else,
135 While, 208 While,
136 Switch, Case, Default, 209 For,
137 Return, Cast, 210 Switch, Case, Default, Break,
138 211 Return, Cast,
139 String,
140 212
141 Module, Import, 213 Module, Import,
214
215 New, Null,
216
217 /* Attributes */
218 Public, Private, Package, Export, Protected,
219 Static,
220 Final,
221 Const,
222 Abstract,
223 Override,
224 Deprecated,
225 Auto,
226 Extern,
227
228 Align,
229
230 Asm,
231
232 In, Out, Body,
233
234 Assert, Throw, Try, Catch, Finally,
235
236
237
142 238
143 } 239 }
144 240
145 /** 241 /**
146 An associative array to supply a Tok to String function. 242 An associative array to supply a Tok to String function.
152 static this() 248 static this()
153 { 249 {
154 typeToString = 250 typeToString =
155 [ 251 [
156 Tok.EOF:"EOF"[], 252 Tok.EOF:"EOF"[],
157 Tok.Identifier:"Identifier", 253 Tok.Identifier:"identifier",
158 Tok.Byte:"Byte", 254 Tok.Byte:"byte",
159 Tok.Short:"Short", 255 Tok.Short:"short",
160 Tok.Int:"Int", 256 Tok.Int:"int",
161 Tok.Long:"Long", 257 Tok.Long:"long",
162 Tok.Char:"Char", 258 Tok.Char:"char",
163 Tok.Wchar:"Wchar", 259 Tok.Wchar:"wchar",
164 Tok.Dchar:"Dchar", 260 Tok.Dchar:"dchar",
165 Tok.Bool:"Bool", 261 Tok.Bool:"bool",
166 Tok.Void:"Void", 262 Tok.Void:"void",
167 Tok.Eq:"Eq", 263 Tok.Function:"function",
168 Tok.Ne:"Ne", 264 Tok.Eq:"==",
169 Tok.Lt:"Lt", 265 Tok.Ne:"!=",
170 Tok.Le:"Le", 266 Tok.Lt:"<",
171 Tok.Gt:"Gt", 267 Tok.Le:"<=",
172 Tok.Ge:"Ge", 268 Tok.Gt:">",
173 Tok.OpenParentheses:"OpenParentheses", 269 Tok.Ge:">=",
174 Tok.CloseParentheses:"CloseParentheses", 270 Tok.OpenParentheses:"(",
175 Tok.OpenBrace:"OpenBrace", 271 Tok.CloseParentheses:")",
176 Tok.CloseBrace:"CloseBrace", 272 Tok.OpenBrace:"{",
177 Tok.OpenBracket:"OpenBracket", 273 Tok.CloseBrace:"}",
178 Tok.CloseBracket:"CloseBracket", 274 Tok.OpenBracket:"[",
179 Tok.Dot:"Dot", 275 Tok.CloseBracket:"]",
180 Tok.Assign:"Assign", 276 Tok.Dot:"-",
181 Tok.Plus:"Plus", 277 Tok.Assign:"=",
182 Tok.Minus:"Minus", 278 Tok.Plus:"+",
183 Tok.Star:"Star", 279 Tok.PlusAssign:"+=",
184 Tok.Slash:"Slash", 280 Tok.Minus:"-",
185 Tok.Percent:"Percent", 281 Tok.MinusAssign:"-=",
186 Tok.Integer:"Integer", 282 Tok.Star:"*",
187 Tok.If:"If", 283 Tok.StarAssign:"*=",
188 Tok.While:"While", 284 Tok.Slash:"/",
189 Tok.Switch:"Switch", 285 Tok.SlashAssign:"/=",
190 Tok.Case:"Case", 286 Tok.Percent:"%",
191 Tok.Default:"Default", 287 Tok.PercentAssign:"%=",
192 Tok.Comma:"Comma", 288 Tok.LeftShift:"<<",
193 Tok.Return:"Return", 289 Tok.RightShift:">>",
194 Tok.Struct:"Struct", 290 Tok.UnsignedRightShift:">>>",
195 Tok.Colon:"Colon", 291 Tok.Integer:"int",
196 Tok.Seperator:"Seperator", 292 Tok.If:"if",
197 Tok.Cast:"Cast", 293 Tok.While:"while",
198 Tok.Module:"Module", 294 Tok.For:"for",
199 Tok.Import:"Import", 295 Tok.Switch:"switch",
200 Tok.String:"String" 296 Tok.Case:"case",
297 Tok.Default:"default",
298 Tok.Comma:",",
299 Tok.Return:"return",
300 Tok.Struct:"struct",
301 Tok.Class:"class",
302 Tok.This:"this",
303 Tok.Colon:":",
304 Tok.Seperator:";",
305 Tok.And:"&",
306 Tok.Cast:"cast",
307 Tok.Module:"module",
308 Tok.Import:"import",
309 Tok.String:"String",
310 Tok.Public:"public",
311 Tok.Private:"private",
312 Tok.Protected:"protected",
313 Tok.Package:"package",
314 Tok.Export:"export",
315 Tok.Static:"static",
316 Tok.Final:"finale",
317 Tok.Public:"public",
318 Tok.Const:"const",
319 Tok.Abstract:"abstract",
320 Tok.Override:"override",
321 Tok.Deprecated:"deprecated",
322 Tok.Auto:"auto",
323 Tok.Extern:"extern",
324 Tok.New:"new",
325 Tok.Null:"null",
326 Tok.Alias:"alias"
201 ]; 327 ];
202 } 328 }