# HG changeset patch # User aziz # Date 1187878204 0 # Node ID 95f1b6e43214383dc1d5d176abc924c743a3aa6c # Parent f13d551d7c4fd7d2565ac425dae0bfe2d8ff224c - Removed TOK.Special and added an own entry for each special token. - Added method finalizeSpecialToken() which assigns a token a value according to its semantics. - Added VENDOR constant. diff -r f13d551d7c4f -r 95f1b6e43214 trunk/src/dil/Keywords.d --- a/trunk/src/dil/Keywords.d Thu Aug 23 12:14:02 2007 +0000 +++ b/trunk/src/dil/Keywords.d Thu Aug 23 14:10:04 2007 +0000 @@ -107,11 +107,11 @@ {TOK.While, "while"}, {TOK.With, "with"}, // Special tokens: - {TOK.Special, "__FILE__"}, - {TOK.Special, "__LINE__"}, - {TOK.Special, "__DATE__"}, - {TOK.Special, "__TIME__"}, - {TOK.Special, "__TIMESTAMP__"}, - {TOK.Special, "__VENDOR__"}, - {TOK.Special, "__VERSION__"}, + {TOK.FILE, "__FILE__"}, + {TOK.LINE, "__LINE__"}, + {TOK.DATE, "__DATE__"}, + {TOK.TIME, "__TIME__"}, + {TOK.TIMESTAMP, "__TIMESTAMP__"}, + {TOK.VENDOR, "__VENDOR__"}, + {TOK.VERSION, "__VERSION__"}, ]; diff -r f13d551d7c4f -r 95f1b6e43214 trunk/src/dil/Lexer.d --- a/trunk/src/dil/Lexer.d Thu Aug 23 12:14:02 2007 +0000 +++ b/trunk/src/dil/Lexer.d Thu Aug 23 14:10:04 2007 +0000 @@ -9,10 +9,13 @@ import dil.Identifier; import dil.Messages; import dil.HtmlEntities; +import dil.Settings; import std.stdio; import std.utf; import std.uni; -import std.c.stdlib; +import std.c.stdlib : strtof, strtod, strtold, getErrno, ERANGE; +import std.c.time : time_t, time, ctime; +import std.c.string : strlen; import std.string; const char[3] LS = \u2028; @@ -106,6 +109,47 @@ } } + void finalizeSpecialToken(ref Token t) + { + assert(t.srcText[0..2] == "__"); + switch (t.type) + { + case TOK.FILE: + t.str = this.fileName; + break; + case TOK.LINE: + t.uint_ = this.loc; + break; + case TOK.DATE, + TOK.TIME, + TOK.TIMESTAMP: + time_t time_val; + time(&time_val); + char* str = ctime(&time_val); + char[] time_str = str[0 .. strlen(str)]; + switch (t.type) + { + case TOK.DATE: + time_str = time_str[4..10] ~ time_str[20..24] ~ \0; break; + case TOK.TIME: + time_str = time_str[11..19] ~ \0; break; + case TOK.TIMESTAMP: + time_str = time_str[0..24] ~ \0; break; + default: assert(0); + } + t.str = time_str; + break; + case TOK.VENDOR: + t.str = VENDOR; + break; + case TOK.VERSION: + t.uint_ = VERSION_MAJOR*1000 + VERSION_MINOR; + break; + default: + assert(0); + } + } + public void scan(out Token t) in { @@ -176,6 +220,8 @@ } assert(id); t.type = id.type; + if (t.isSpecialToken) + finalizeSpecialToken(t); return; } diff -r f13d551d7c4f -r 95f1b6e43214 trunk/src/dil/Parser.d --- a/trunk/src/dil/Parser.d Thu Aug 23 12:14:02 2007 +0000 +++ b/trunk/src/dil/Parser.d Thu Aug 23 14:10:04 2007 +0000 @@ -3490,12 +3490,14 @@ e = new TraitsExpression(id, args); break; } - case T.Special: - e = new SpecialTokenExpression(token); - nT(); - break; default: - // TODO: issue error msg. + if (token.isSpecialToken) + { + e = new SpecialTokenExpression(token); + nT(); + break; + } + error(MID.ExpectedButFound, "Expression", token.srcText); e = new EmptyExpression(); } diff -r f13d551d7c4f -r 95f1b6e43214 trunk/src/dil/Settings.d --- a/trunk/src/dil/Settings.d Thu Aug 23 12:14:02 2007 +0000 +++ b/trunk/src/dil/Settings.d Thu Aug 23 14:10:04 2007 +0000 @@ -32,6 +32,7 @@ } const string VERSION = Format!("%s.%s", VERSION_MAJOR, Pad!(VERSION_MINOR, 3)); +const VENDOR = "dil"; /// Used in main help message. const COMPILED_WITH = __VENDOR__; diff -r f13d551d7c4f -r 95f1b6e43214 trunk/src/dil/Token.d --- a/trunk/src/dil/Token.d Thu Aug 23 12:14:02 2007 +0000 +++ b/trunk/src/dil/Token.d Thu Aug 23 14:10:04 2007 +0000 @@ -25,9 +25,17 @@ Identifier = 5, String, - Special, CharLiteral, WCharLiteral, DCharLiteral, + // Special tokens + FILE, + LINE, + DATE, + TIME, + TIMESTAMP, + VENDOR, + VERSION, + // Numbers Int32, Int64, Uint32, Uint64, // Floating point number scanner relies on this order. (FloatXY + 3 == ImaginaryXY) @@ -160,6 +168,11 @@ return !!(type & TOK.Whitespace); } + bool isSpecialToken() + { + return *start == '_' && type != TOK.Identifier; + } + int opEquals(TOK type2) { return type == type2; diff -r f13d551d7c4f -r 95f1b6e43214 trunk/src/main.d --- a/trunk/src/main.d Thu Aug 23 12:14:02 2007 +0000 +++ b/trunk/src/main.d Thu Aug 23 14:10:04 2007 +0000 @@ -553,9 +553,6 @@ TOK.RBracket, TOK.LBrace, TOK.RBrace: writef(tags[DP.Bracket], srcText); break; - case TOK.Special: - writef(tags[DP.SpecialToken], srcText); - break; case TOK.Shebang: writef(tags[DP.Shebang], srcText); break; @@ -589,6 +586,8 @@ default: if (token.isKeyword()) writef(tags[DP.Keyword], srcText); + else if (token.isSpecialToken) + writef(tags[DP.SpecialToken], srcText); else writef("%s", srcText); }