comparison trunk/src/dil/lexer/Lexer.d @ 776:580d4ca9f1ff

Added new module dil.Time.
author Aziz K?ksal <aziz.koeksal@gmail.com>
date Tue, 19 Feb 2008 19:30:04 +0100
parents 5e3ef1b2011c
children c1d5cfd7aa44
comparison
equal deleted inserted replaced
775:e76c9fdb83a3 776:580d4ca9f1ff
12 import dil.Messages; 12 import dil.Messages;
13 import dil.HtmlEntities; 13 import dil.HtmlEntities;
14 import dil.CompilerInfo; 14 import dil.CompilerInfo;
15 import dil.Unicode; 15 import dil.Unicode;
16 import dil.SourceText; 16 import dil.SourceText;
17 import dil.Time;
17 import common; 18 import common;
18 19
19 import tango.stdc.stdlib : strtof, strtod, strtold; 20 import tango.stdc.stdlib : strtof, strtod, strtold;
20 import tango.stdc.errno : errno, ERANGE; 21 import tango.stdc.errno : errno, ERANGE;
21 import tango.stdc.time : time_t, time, ctime;
22 import tango.stdc.string : strlen;
23 22
24 public import dil.lexer.Funcs; 23 public import dil.lexer.Funcs;
25 24
26 /++ 25 /// The Lexer analyzes the characters of a source text and
27 The Lexer analyzes the characters of a source text and 26 /// produces a doubly-linked list of tokens.
28 produces a doubly-linked list of tokens.
29 +/
30 class Lexer 27 class Lexer
31 { 28 {
32 SourceText srcText; /// The source text. 29 SourceText srcText; /// The source text.
33 char* p; /// Points to the current character in the source text. 30 char* p; /// Points to the current character in the source text.
34 char* end; /// Points one character past the end of the source text. 31 char* end; /// Points one character past the end of the source text.
46 uint lineNum = 1; /// Current, actual source text line number. 43 uint lineNum = 1; /// Current, actual source text line number.
47 uint lineNum_hline; /// Line number set by #line. 44 uint lineNum_hline; /// Line number set by #line.
48 uint inTokenString; /// > 0 if inside q{ } 45 uint inTokenString; /// > 0 if inside q{ }
49 NewlineData.FilePaths* filePaths; 46 NewlineData.FilePaths* filePaths;
50 47
51 /++ 48 /// Construct a Lexer object.
52 Construct a Lexer object. 49 /// Params:
53 Params: 50 /// srcText = the UTF-8 source code.
54 srcText = the UTF-8 source code. 51 /// infoMan = used for collecting error messages.
55 infoMan = used for collecting error messages.
56 +/
57 this(SourceText srcText, InfoManager infoMan = null) 52 this(SourceText srcText, InfoManager infoMan = null)
58 { 53 {
59 this.srcText = srcText; 54 this.srcText = srcText;
60 this.infoMan = infoMan; 55 this.infoMan = infoMan;
61 56
101 char[] text() 96 char[] text()
102 { 97 {
103 return srcText.data; 98 return srcText.data;
104 } 99 }
105 100
106 /++ 101 /// The "shebang" may optionally appear once at the beginning of a file.
107 The "shebang" may optionally appear once at the beginning of a file. 102 /// Regexp: #![^\EndOfLine]*
108 Regexp: #![^\EndOfLine]*
109 +/
110 void scanShebang() 103 void scanShebang()
111 { 104 {
112 if (*p == '#' && p[1] == '!') 105 if (*p == '#' && p[1] == '!')
113 { 106 {
114 auto t = new Token; 107 auto t = new Token;
136 t.uint_ = this.errorLineNumber(this.lineNum); 129 t.uint_ = this.errorLineNumber(this.lineNum);
137 break; 130 break;
138 case TOK.DATE, 131 case TOK.DATE,
139 TOK.TIME, 132 TOK.TIME,
140 TOK.TIMESTAMP: 133 TOK.TIMESTAMP:
141 time_t time_val; 134 auto time_str = Time.toString();
142 time(&time_val);
143 char* str = ctime(&time_val);
144 char[] time_str = str[0 .. strlen(str)-1]; // -1 removes trailing '\n'.
145 switch (t.kind) 135 switch (t.kind)
146 { 136 {
147 case TOK.DATE: 137 case TOK.DATE:
148 time_str = time_str[4..11] ~ time_str[20..24] ~ \0; break; 138 time_str = Time.month_day(time_str) ~ ' ' ~ Time.year(time_str); break;
149 case TOK.TIME: 139 case TOK.TIME:
150 time_str = time_str[11..19] ~ \0; break; 140 time_str = Time.time(time_str); break;
151 case TOK.TIMESTAMP: 141 case TOK.TIMESTAMP:
152 time_str = time_str[0..24] ~ \0; break; 142 break; // time_str is the timestamp.
153 default: assert(0); 143 default: assert(0);
154 } 144 }
145 time_str ~= '\0'; // Terminate with a zero.
155 t.str = time_str; 146 t.str = time_str;
156 break; 147 break;
157 case TOK.VENDOR: 148 case TOK.VENDOR:
158 t.str = VENDOR; 149 t.str = VENDOR;
159 break; 150 break;