comparison lexer/Token.d @ 189:75d0544ddc45

Better error handling on unexpected EOF.
author Anders Johnsen <skabet@gmail.com>
date Fri, 25 Jul 2008 13:50:01 +0200
parents dc9bf56b7ace
children 08f68d684047
comparison
equal deleted inserted replaced
188:b3e0729c8524 189:75d0544ddc45
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
245 static this() 248 static this()
246 { 249 {
247 typeToString = 250 typeToString =
248 [ 251 [
249 Tok.EOF:"EOF"[], 252 Tok.EOF:"EOF"[],
250 Tok.Identifier:"Identifier", 253 Tok.Identifier:"identifier",
251 Tok.Byte:"Byte", 254 Tok.Byte:"byte",
252 Tok.Short:"Short", 255 Tok.Short:"short",
253 Tok.Int:"Int", 256 Tok.Int:"int",
254 Tok.Long:"Long", 257 Tok.Long:"long",
255 Tok.Char:"Char", 258 Tok.Char:"char",
256 Tok.Wchar:"Wchar", 259 Tok.Wchar:"wchar",
257 Tok.Dchar:"Dchar", 260 Tok.Dchar:"dchar",
258 Tok.Bool:"Bool", 261 Tok.Bool:"bool",
259 Tok.Void:"Void", 262 Tok.Void:"void",
260 Tok.Function:"Function", 263 Tok.Function:"function",
261 Tok.Eq:"Eq", 264 Tok.Eq:"==",
262 Tok.Ne:"Ne", 265 Tok.Ne:"!=",
263 Tok.Lt:"Lt", 266 Tok.Lt:"<",
264 Tok.Le:"Le", 267 Tok.Le:"<=",
265 Tok.Gt:"Gt", 268 Tok.Gt:">",
266 Tok.Ge:"Ge", 269 Tok.Ge:">=",
267 Tok.OpenParentheses:"OpenParentheses", 270 Tok.OpenParentheses:"(",
268 Tok.CloseParentheses:"CloseParentheses", 271 Tok.CloseParentheses:")",
269 Tok.OpenBrace:"OpenBrace", 272 Tok.OpenBrace:"{",
270 Tok.CloseBrace:"CloseBrace", 273 Tok.CloseBrace:"}",
271 Tok.OpenBracket:"OpenBracket", 274 Tok.OpenBracket:"[",
272 Tok.CloseBracket:"CloseBracket", 275 Tok.CloseBracket:"]",
273 Tok.Dot:"Dot", 276 Tok.Dot:"-",
274 Tok.Assign:"Assign", 277 Tok.Assign:"=",
275 Tok.Plus:"Plus", 278 Tok.Plus:"+",
276 Tok.PlusAssign:"PlusAssign", 279 Tok.PlusAssign:"+=",
277 Tok.Minus:"Minus", 280 Tok.Minus:"-",
278 Tok.MinusAssign:"MinusAssign", 281 Tok.MinusAssign:"-=",
279 Tok.Star:"Star", 282 Tok.Star:"*",
280 Tok.StarAssign:"StarAssign", 283 Tok.StarAssign:"*=",
281 Tok.Slash:"Slash", 284 Tok.Slash:"/",
282 Tok.SlashAssign:"SlashAssign", 285 Tok.SlashAssign:"/=",
283 Tok.Percent:"Percent", 286 Tok.Percent:"%",
284 Tok.PercentAssign:"PercentAssign", 287 Tok.PercentAssign:"%=",
285 Tok.LeftShift:"LeftShift", 288 Tok.LeftShift:"<<",
286 Tok.RightShift:"RightShift", 289 Tok.RightShift:">>",
287 Tok.UnsignedRightShift:"UnsignedRightShift", 290 Tok.UnsignedRightShift:">>>",
288 Tok.Integer:"Integer", 291 Tok.Integer:"int",
289 Tok.If:"If", 292 Tok.If:"if",
290 Tok.While:"While", 293 Tok.While:"while",
291 Tok.For:"For", 294 Tok.For:"for",
292 Tok.Switch:"Switch", 295 Tok.Switch:"switch",
293 Tok.Case:"Case", 296 Tok.Case:"case",
294 Tok.Default:"Default", 297 Tok.Default:"default",
295 Tok.Comma:"Comma", 298 Tok.Comma:",",
296 Tok.Return:"Return", 299 Tok.Return:"return",
297 Tok.Struct:"Struct", 300 Tok.Struct:"struct",
298 Tok.Class:"Class", 301 Tok.Class:"class",
299 Tok.This:"This", 302 Tok.This:"this",
300 Tok.Colon:"Colon", 303 Tok.Colon:":",
301 Tok.Seperator:"Seperator", 304 Tok.Seperator:";",
302 Tok.Cast:"Cast", 305 Tok.And:"&",
303 Tok.Module:"Module", 306 Tok.Cast:"cast",
304 Tok.Import:"Import", 307 Tok.Module:"module",
308 Tok.Import:"import",
305 Tok.String:"String", 309 Tok.String:"String",
306 Tok.Public:"Public", 310 Tok.Public:"public",
307 Tok.Private:"Private", 311 Tok.Private:"private",
308 Tok.Protected:"Protected", 312 Tok.Protected:"protected",
309 Tok.Package:"Package", 313 Tok.Package:"package",
310 Tok.Export:"Export", 314 Tok.Export:"export",
311 Tok.Static:"Static", 315 Tok.Static:"static",
312 Tok.Final:"Finale", 316 Tok.Final:"finale",
313 Tok.Public:"Public", 317 Tok.Public:"public",
314 Tok.Const:"Const", 318 Tok.Const:"const",
315 Tok.Abstract:"Abstract", 319 Tok.Abstract:"abstract",
316 Tok.Override:"Override", 320 Tok.Override:"override",
317 Tok.Deprecated:"Deprecated", 321 Tok.Deprecated:"deprecated",
318 Tok.Auto:"Auto", 322 Tok.Auto:"auto",
319 Tok.Extern:"Extern", 323 Tok.Extern:"extern",
320 Tok.New:"New", 324 Tok.New:"new"
321 Tok.And:"And"
322 ]; 325 ];
323 } 326 }