~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to contrib/NRefactory/Project/Src/Lexer/VBNet/Lexer.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
ImportĀ upstreamĀ versionĀ 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
// <file>
2
 
//     <copyright see="prj:///doc/copyright.txt"/>
3
 
//     <license see="prj:///doc/license.txt"/>
4
 
//     <owner name="Andrea Paatz" email="andrea@icsharpcode.net"/>
5
 
//     <version>$Revision: 4482 $</version>
6
 
// </file>
7
 
 
8
 
using System;
9
 
using System.Globalization;
10
 
using System.IO;
11
 
using System.Text;
12
 
 
13
 
namespace ICSharpCode.OldNRefactory.Parser.VB
14
 
{
15
 
        internal sealed class Lexer : AbstractLexer
16
 
        {
17
 
                bool lineEnd = true;
18
 
                bool isAtLineBegin = false; // TODO: handle line begin, if neccessarry
19
 
                
20
 
                public Lexer(TextReader reader) : base(reader)
21
 
                {
22
 
                }
23
 
                
24
 
                public override Token NextToken()
25
 
                {
26
 
                        if (curToken == null) { // first call of NextToken()
27
 
                                curToken = Next();
28
 
                                specialTracker.InformToken(curToken.kind);
29
 
                                //Console.WriteLine("Tok:" + Tokens.GetTokenString(curToken.kind) + " --- " + curToken.val);
30
 
                                return curToken;
31
 
                        }
32
 
                        
33
 
                        lastToken = curToken;
34
 
                        
35
 
                        if (curToken.next == null) {
36
 
                                curToken.next = Next();
37
 
                                specialTracker.InformToken(curToken.next.kind);
38
 
                        }
39
 
                        
40
 
                        curToken = curToken.next;
41
 
                        
42
 
                        if (curToken.kind == Tokens.EOF && !(lastToken.kind == Tokens.EOL)) { // be sure that before EOF there is an EOL token
43
 
                                curToken = new Token(Tokens.EOL, curToken.col, curToken.line, "\n");
44
 
                                specialTracker.InformToken(curToken.kind);
45
 
                                curToken.next = new Token(Tokens.EOF, curToken.col, curToken.line, "\n");
46
 
                                specialTracker.InformToken(curToken.next.kind);
47
 
                        }
48
 
                        //Console.WriteLine("Tok:" + Tokens.GetTokenString(curToken.kind) + " --- " + curToken.val);
49
 
                        return curToken;
50
 
                }
51
 
                
52
 
                bool misreadExclamationMarkAsTypeCharacter;
53
 
                
54
 
                protected override Token Next()
55
 
                {
56
 
                        if (misreadExclamationMarkAsTypeCharacter) {
57
 
                                misreadExclamationMarkAsTypeCharacter = false;
58
 
                                return new Token(Tokens.ExclamationMark, Col - 1, Line);
59
 
                        }
60
 
                        unchecked {
61
 
                                while (true) {
62
 
                                        Location startLocation = new Location(Col, Line);
63
 
                                        int nextChar = ReaderRead();
64
 
                                        if (nextChar == -1)
65
 
                                                return new Token(Tokens.EOF);
66
 
                                        char ch = (char)nextChar;
67
 
                                        if (Char.IsWhiteSpace(ch)) {
68
 
                                                if (HandleLineEnd(ch)) {
69
 
                                                        if (lineEnd) {
70
 
                                                                // second line end before getting to a token
71
 
                                                                // -> here was a blank line
72
 
                                                                specialTracker.AddEndOfLine(startLocation);
73
 
                                                        } else {
74
 
                                                                lineEnd = true;
75
 
                                                                return new Token(Tokens.EOL, startLocation, new Location(Col, Line), null, null, LiteralFormat.None);
76
 
                                                        }
77
 
                                                }
78
 
                                                continue;
79
 
                                        }
80
 
                                        if (ch == '_') {
81
 
                                                if (ReaderPeek() == -1) {
82
 
                                                        errors.Error(Line, Col, String.Format("No EOF expected after _"));
83
 
                                                        return new Token(Tokens.EOF);
84
 
                                                }
85
 
                                                if (!Char.IsWhiteSpace((char)ReaderPeek())) {
86
 
                                                        int x = Col - 1;
87
 
                                                        int y = Line;
88
 
                                                        string s = ReadIdent('_');
89
 
                                                        lineEnd = false;
90
 
                                                        return new Token(Tokens.Identifier, x, y, s);
91
 
                                                }
92
 
                                                ch = (char)ReaderRead();
93
 
                                                
94
 
                                                bool oldLineEnd = lineEnd;
95
 
                                                lineEnd = false;
96
 
                                                while (Char.IsWhiteSpace(ch)) {
97
 
                                                        if (HandleLineEnd(ch)) {
98
 
                                                                lineEnd = true;
99
 
                                                                break;
100
 
                                                        }
101
 
                                                        if (ReaderPeek() != -1) {
102
 
                                                                ch = (char)ReaderRead();
103
 
                                                        } else {
104
 
                                                                errors.Error(Line, Col, String.Format("No EOF expected after _"));
105
 
                                                                return new Token(Tokens.EOF);
106
 
                                                        }
107
 
                                                }
108
 
                                                if (!lineEnd) {
109
 
                                                        errors.Error(Line, Col, String.Format("Return expected"));
110
 
                                                }
111
 
                                                lineEnd = oldLineEnd;
112
 
                                                continue;
113
 
                                        }
114
 
                                        
115
 
                                        if (ch == '#') {
116
 
                                                while (Char.IsWhiteSpace((char)ReaderPeek())) {
117
 
                                                        ReaderRead();
118
 
                                                }
119
 
                                                if (Char.IsDigit((char)ReaderPeek())) {
120
 
                                                        int x = Col - 1;
121
 
                                                        int y = Line;
122
 
                                                        string s = ReadDate();
123
 
                                                        DateTime time = new DateTime(1, 1, 1, 0, 0, 0);
124
 
                                                        try {
125
 
                                                                time = DateTime.Parse(s, System.Globalization.CultureInfo.InvariantCulture, DateTimeStyles.NoCurrentDateDefault);
126
 
                                                        } catch (Exception e) {
127
 
                                                                errors.Error(Line, Col, String.Format("Invalid date time {0}", e));
128
 
                                                        }
129
 
                                                        return new Token(Tokens.LiteralDate, x, y, s, time, LiteralFormat.DateTimeLiteral);
130
 
                                                } else {
131
 
                                                        ReadPreprocessorDirective();
132
 
                                                        continue;
133
 
                                                }
134
 
                                        }
135
 
                                        
136
 
                                        if (ch == '[') { // Identifier
137
 
                                                lineEnd = false;
138
 
                                                if (ReaderPeek() == -1) {
139
 
                                                        errors.Error(Line, Col, String.Format("Identifier expected"));
140
 
                                                }
141
 
                                                ch = (char)ReaderRead();
142
 
                                                if (ch == ']' || Char.IsWhiteSpace(ch)) {
143
 
                                                        errors.Error(Line, Col, String.Format("Identifier expected"));
144
 
                                                }
145
 
                                                int x = Col - 1;
146
 
                                                int y = Line;
147
 
                                                string s = ReadIdent(ch);
148
 
                                                if (ReaderPeek() == -1) {
149
 
                                                        errors.Error(Line, Col, String.Format("']' expected"));
150
 
                                                }
151
 
                                                ch = (char)ReaderRead();
152
 
                                                if (!(ch == ']')) {
153
 
                                                        errors.Error(Line, Col, String.Format("']' expected"));
154
 
                                                }
155
 
                                                return new Token(Tokens.Identifier, x, y, s);
156
 
                                        }
157
 
                                        if (Char.IsLetter(ch)) {
158
 
                                                int x = Col - 1;
159
 
                                                int y = Line;
160
 
                                                char typeCharacter;
161
 
                                                string s = ReadIdent(ch, out typeCharacter);
162
 
                                                if (typeCharacter == '\0') {
163
 
                                                        int keyWordToken = Keywords.GetToken(s);
164
 
                                                        if (keyWordToken >= 0) {
165
 
                                                                // handle 'REM' comments
166
 
                                                                if (keyWordToken == Tokens.Rem) {
167
 
                                                                        ReadComment();
168
 
                                                                        if (!lineEnd) {
169
 
                                                                                lineEnd = true;
170
 
                                                                                return new Token(Tokens.EOL, Col, Line, "\n");
171
 
                                                                        }
172
 
                                                                        continue;
173
 
                                                                }
174
 
                                                                
175
 
                                                                lineEnd = false;
176
 
                                                                return new Token(keyWordToken, x, y, s);
177
 
                                                        }
178
 
                                                }
179
 
                                                
180
 
                                                lineEnd = false;
181
 
                                                return new Token(Tokens.Identifier, x, y, s);
182
 
                                                
183
 
                                        }
184
 
                                        if (Char.IsDigit(ch)) {
185
 
                                                lineEnd = false;
186
 
                                                return ReadDigit(ch, Col - 1);
187
 
                                        }
188
 
                                        if (ch == '&') {
189
 
                                                lineEnd = false;
190
 
                                                if (ReaderPeek() == -1) {
191
 
                                                        return ReadOperator('&');
192
 
                                                }
193
 
                                                ch = (char)ReaderPeek();
194
 
                                                if (Char.ToUpper(ch, CultureInfo.InvariantCulture) == 'H' || Char.ToUpper(ch, CultureInfo.InvariantCulture) == 'O') {
195
 
                                                        return ReadDigit('&', Col - 1);
196
 
                                                }
197
 
                                                return ReadOperator('&');
198
 
                                        }
199
 
                                        if (ch == '\'' || ch == '\u2018' || ch == '\u2019') {
200
 
                                                int x = Col - 1;
201
 
                                                int y = Line;
202
 
                                                ReadComment();
203
 
                                                if (!lineEnd) {
204
 
                                                        lineEnd = true;
205
 
                                                        return new Token(Tokens.EOL, x, y, "\n");
206
 
                                                }
207
 
                                                continue;
208
 
                                        }
209
 
                                        if (ch == '"') {
210
 
                                                lineEnd = false;
211
 
                                                int x = Col - 1;
212
 
                                                int y = Line;
213
 
                                                string s = ReadString();
214
 
                                                if (ReaderPeek() != -1 && (ReaderPeek() == 'C' || ReaderPeek() == 'c')) {
215
 
                                                        ReaderRead();
216
 
                                                        if (s.Length != 1) {
217
 
                                                                errors.Error(Line, Col, String.Format("Chars can only have Length 1 "));
218
 
                                                        }
219
 
                                                        if (s.Length == 0) {
220
 
                                                                s = "\0";
221
 
                                                        }
222
 
                                                        return new Token(Tokens.LiteralCharacter, x, y, '"' + s  + "\"C", s[0], LiteralFormat.CharLiteral);
223
 
                                                }
224
 
                                                return new Token(Tokens.LiteralString, x, y, '"' + s + '"', s, LiteralFormat.StringLiteral);
225
 
                                        }
226
 
                                        Token token = ReadOperator(ch);
227
 
                                        if (token != null) {
228
 
                                                lineEnd = false;
229
 
                                                return token;
230
 
                                        }
231
 
                                        errors.Error(Line, Col, String.Format("Unknown char({0}) which can't be read", ch));
232
 
                                }
233
 
                        }
234
 
                }
235
 
                
236
 
                string ReadIdent(char ch)
237
 
                {
238
 
                        char typeCharacter;
239
 
                        return ReadIdent(ch, out typeCharacter);
240
 
                }
241
 
                
242
 
                string ReadIdent(char ch, out char typeCharacter)
243
 
                {
244
 
                        typeCharacter = '\0';
245
 
                        
246
 
                        sb.Length = 0;
247
 
                        sb.Append(ch);
248
 
                        int peek;
249
 
                        while ((peek = ReaderPeek()) != -1 && (Char.IsLetterOrDigit(ch = (char)peek) || ch == '_')) {
250
 
                                ReaderRead();
251
 
                                sb.Append(ch.ToString());
252
 
                        }
253
 
                        if (peek == -1) {
254
 
                                return sb.ToString();
255
 
                        }
256
 
                        
257
 
                        if ("%&@!#$".IndexOf((char)peek) != -1) {
258
 
                                typeCharacter = (char)peek;
259
 
                                ReaderRead();
260
 
                                if (typeCharacter == '!') {
261
 
                                        peek = ReaderPeek();
262
 
                                        if (peek != -1 && (peek == '_' || peek == '[' || char.IsLetter((char)peek))) {
263
 
                                                misreadExclamationMarkAsTypeCharacter = true;
264
 
                                        }
265
 
                                }
266
 
                        }
267
 
                        return sb.ToString();
268
 
                }
269
 
                
270
 
                char PeekUpperChar()
271
 
                {
272
 
                        return Char.ToUpper((char)ReaderPeek(), CultureInfo.InvariantCulture);
273
 
                }
274
 
                
275
 
                [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1818:DoNotConcatenateStringsInsideLoops")]
276
 
                Token ReadDigit(char ch, int x)
277
 
                {
278
 
                        sb.Length = 0;
279
 
                        sb.Append(ch);
280
 
                        
281
 
                        int y = Line;
282
 
                        string digit = "";
283
 
                        if (ch != '&') {
284
 
                                digit += ch;
285
 
                        }
286
 
                        
287
 
                        bool ishex      = false;
288
 
                        bool isokt      = false;
289
 
                        bool issingle   = false;
290
 
                        bool isdouble   = false;
291
 
                        bool isdecimal  = false;
292
 
                        
293
 
                        if (ReaderPeek() == -1) {
294
 
                                if (ch == '&') {
295
 
                                        errors.Error(Line, Col, String.Format("digit expected"));
296
 
                                }
297
 
                                return new Token(Tokens.LiteralInteger, x, y, sb.ToString() ,ch - '0', LiteralFormat.DecimalNumber);
298
 
                        }
299
 
                        if (ch == '.') {
300
 
                                if (Char.IsDigit((char)ReaderPeek())) {
301
 
                                        isdouble = true; // double is default
302
 
                                        if (ishex || isokt) {
303
 
                                                errors.Error(Line, Col, String.Format("No hexadecimal or oktadecimal floating point values allowed"));
304
 
                                        }
305
 
                                        while (ReaderPeek() != -1 && Char.IsDigit((char)ReaderPeek())){ // read decimal digits beyond the dot
306
 
                                                digit += (char)ReaderRead();
307
 
                                        }
308
 
                                }
309
 
                        } else if (ch == '&' && PeekUpperChar() == 'H') {
310
 
                                const string hex = "0123456789ABCDEF";
311
 
                                sb.Append((char)ReaderRead()); // skip 'H'
312
 
                                while (ReaderPeek() != -1 && hex.IndexOf(PeekUpperChar()) != -1) {
313
 
                                        ch = (char)ReaderRead();
314
 
                                        sb.Append(ch);
315
 
                                        digit += Char.ToUpper(ch, CultureInfo.InvariantCulture);
316
 
                                }
317
 
                                ishex = true;
318
 
                        } else if (ReaderPeek() != -1 && ch == '&' && PeekUpperChar() == 'O') {
319
 
                                const string okt = "01234567";
320
 
                                sb.Append((char)ReaderRead()); // skip 'O'
321
 
                                while (ReaderPeek() != -1 && okt.IndexOf(PeekUpperChar()) != -1) {
322
 
                                        ch = (char)ReaderRead();
323
 
                                        sb.Append(ch);
324
 
                                        digit += Char.ToUpper(ch, CultureInfo.InvariantCulture);
325
 
                                }
326
 
                                isokt = true;
327
 
                        } else {
328
 
                                while (ReaderPeek() != -1 && Char.IsDigit((char)ReaderPeek())) {
329
 
                                        ch = (char)ReaderRead();;
330
 
                                        digit += ch;
331
 
                                        sb.Append(ch);
332
 
                                }
333
 
                        }
334
 
                        
335
 
                        if (digit.Length == 0) {
336
 
                                errors.Error(Line, Col, String.Format("digit expected"));
337
 
                                return new Token(Tokens.LiteralInteger, x, y, sb.ToString(), 0, LiteralFormat.DecimalNumber);
338
 
                        }
339
 
                        
340
 
                        if (ReaderPeek() != -1 && "%&SILU".IndexOf(PeekUpperChar()) != -1 || ishex || isokt) {
341
 
                                bool unsigned = false;
342
 
                                if (ReaderPeek() != -1) {
343
 
                                        ch = (char)ReaderPeek();
344
 
                                        sb.Append(ch);
345
 
                                        ch = Char.ToUpper(ch, CultureInfo.InvariantCulture);
346
 
                                        unsigned = ch == 'U';
347
 
                                        if (unsigned) {
348
 
                                                ReaderRead(); // read the U
349
 
                                                ch = (char)ReaderPeek();
350
 
                                                sb.Append(ch);
351
 
                                                ch = Char.ToUpper(ch, CultureInfo.InvariantCulture);
352
 
                                                if (ch != 'I' && ch != 'L' && ch != 'S') {
353
 
                                                        errors.Error(Line, Col, "Invalid type character: U" + ch);
354
 
                                                }
355
 
                                        }
356
 
                                }
357
 
                                try {
358
 
                                        if (isokt) {
359
 
                                                ReaderRead();
360
 
                                                ulong number = 0L;
361
 
                                                for (int i = 0; i < digit.Length; ++i) {
362
 
                                                        number = number * 8 + digit[i] - '0';
363
 
                                                }
364
 
                                                if (ch == 'S') {
365
 
                                                        if (unsigned)
366
 
                                                                return new Token(Tokens.LiteralInteger, x, y, sb.ToString(), (ushort)number, LiteralFormat.OctalNumber);
367
 
                                                        else
368
 
                                                                return new Token(Tokens.LiteralInteger, x, y, sb.ToString(), (short)number, LiteralFormat.OctalNumber);
369
 
                                                } else if (ch == '%' || ch == 'I') {
370
 
                                                        if (unsigned)
371
 
                                                                return new Token(Tokens.LiteralInteger, x, y, sb.ToString(), (uint)number, LiteralFormat.OctalNumber);
372
 
                                                        else
373
 
                                                                return new Token(Tokens.LiteralInteger, x, y, sb.ToString(), (int)number, LiteralFormat.OctalNumber);
374
 
                                                } else if (ch == '&' || ch == 'L') {
375
 
                                                        if (unsigned)
376
 
                                                                return new Token(Tokens.LiteralInteger, x, y, sb.ToString(), (ulong)number, LiteralFormat.OctalNumber);
377
 
                                                        else
378
 
                                                                return new Token(Tokens.LiteralInteger, x, y, sb.ToString(), (long)number, LiteralFormat.OctalNumber);
379
 
                                                } else {
380
 
                                                        if (number > uint.MaxValue) {
381
 
                                                                return new Token(Tokens.LiteralInteger, x, y, sb.ToString(), unchecked((long)number), LiteralFormat.OctalNumber);
382
 
                                                        } else {
383
 
                                                                return new Token(Tokens.LiteralInteger, x, y, sb.ToString(), unchecked((int)number), LiteralFormat.OctalNumber);
384
 
                                                        }
385
 
                                                }
386
 
                                        }
387
 
                                        LiteralFormat literalFormat = ishex ? LiteralFormat.HexadecimalNumber : LiteralFormat.DecimalNumber;
388
 
                                        if (ch == 'S') {
389
 
                                                ReaderRead();
390
 
                                                if (unsigned)
391
 
                                                        return new Token(Tokens.LiteralInteger, x, y, sb.ToString(), UInt16.Parse(digit, ishex ? NumberStyles.HexNumber : NumberStyles.Number), literalFormat);
392
 
                                                else
393
 
                                                        return new Token(Tokens.LiteralInteger, x, y, sb.ToString(), Int16.Parse(digit, ishex ? NumberStyles.HexNumber : NumberStyles.Number), literalFormat);
394
 
                                        } else if (ch == '%' || ch == 'I') {
395
 
                                                ReaderRead();
396
 
                                                if (unsigned)
397
 
                                                        return new Token(Tokens.LiteralInteger, x, y, sb.ToString(), UInt32.Parse(digit, ishex ? NumberStyles.HexNumber : NumberStyles.Number), literalFormat);
398
 
                                                else
399
 
                                                        return new Token(Tokens.LiteralInteger, x, y, sb.ToString(), Int32.Parse(digit, ishex ? NumberStyles.HexNumber : NumberStyles.Number), literalFormat);
400
 
                                        } else if (ch == '&' || ch == 'L') {
401
 
                                                ReaderRead();
402
 
                                                if (unsigned)
403
 
                                                        return new Token(Tokens.LiteralInteger, x, y, sb.ToString(), UInt64.Parse(digit, ishex ? NumberStyles.HexNumber : NumberStyles.Number), literalFormat);
404
 
                                                else
405
 
                                                        return new Token(Tokens.LiteralInteger, x, y, sb.ToString(), Int64.Parse(digit, ishex ? NumberStyles.HexNumber : NumberStyles.Number), literalFormat);
406
 
                                        } else if (ishex) {
407
 
                                                ulong number = UInt64.Parse(digit, NumberStyles.HexNumber);
408
 
                                                if (number > uint.MaxValue) {
409
 
                                                        return new Token(Tokens.LiteralInteger, x, y, sb.ToString(), unchecked((long)number), literalFormat);
410
 
                                                } else {
411
 
                                                        return new Token(Tokens.LiteralInteger, x, y, sb.ToString(), unchecked((int)number), literalFormat);
412
 
                                                }
413
 
                                        }
414
 
                                } catch (OverflowException ex) {
415
 
                                        errors.Error(Line, Col, ex.Message);
416
 
                                        return new Token(Tokens.LiteralInteger, x, y, sb.ToString(), 0, LiteralFormat.None);
417
 
                                }
418
 
                        }
419
 
                        Token nextToken = null; // if we accedently read a 'dot'
420
 
                        if (!isdouble && ReaderPeek() == '.') { // read floating point number
421
 
                                ReaderRead();
422
 
                                if (ReaderPeek() != -1 && Char.IsDigit((char)ReaderPeek())) {
423
 
                                        isdouble = true; // double is default
424
 
                                        if (ishex || isokt) {
425
 
                                                errors.Error(Line, Col, String.Format("No hexadecimal or oktadecimal floating point values allowed"));
426
 
                                        }
427
 
                                        digit += '.';
428
 
                                        while (ReaderPeek() != -1 && Char.IsDigit((char)ReaderPeek())){ // read decimal digits beyond the dot
429
 
                                                digit += (char)ReaderRead();
430
 
                                        }
431
 
                                } else {
432
 
                                        nextToken = new Token(Tokens.Dot, Col - 1, Line);
433
 
                                }
434
 
                        }
435
 
                        
436
 
                        if (ReaderPeek() != -1 && PeekUpperChar() == 'E') { // read exponent
437
 
                                isdouble = true;
438
 
                                digit +=  (char)ReaderRead();
439
 
                                if (ReaderPeek() != -1 && (ReaderPeek() == '-' || ReaderPeek() == '+')) {
440
 
                                        digit += (char)ReaderRead();
441
 
                                }
442
 
                                while (ReaderPeek() != -1 && Char.IsDigit((char)ReaderPeek())) { // read exponent value
443
 
                                        digit += (char)ReaderRead();
444
 
                                }
445
 
                        }
446
 
                        
447
 
                        if (ReaderPeek() != -1) {
448
 
                                switch (PeekUpperChar()) {
449
 
                                        case 'R':
450
 
                                        case '#':
451
 
                                                ReaderRead();
452
 
                                                isdouble = true;
453
 
                                                break;
454
 
                                        case 'D':
455
 
                                        case '@':
456
 
                                                ReaderRead();
457
 
                                                isdecimal = true;
458
 
                                                break;
459
 
                                        case 'F':
460
 
                                        case '!':
461
 
                                                ReaderRead();
462
 
                                                issingle = true;
463
 
                                                break;
464
 
                                }
465
 
                        }
466
 
                        
467
 
                        try {
468
 
                                if (issingle) {
469
 
                                        return new Token(Tokens.LiteralSingle, x, y, sb.ToString(), Single.Parse(digit, CultureInfo.InvariantCulture), LiteralFormat.DecimalNumber);
470
 
                                }
471
 
                                if (isdecimal) {
472
 
                                        return new Token(Tokens.LiteralDecimal, x, y, sb.ToString(), Decimal.Parse(digit, NumberStyles.Currency | NumberStyles.AllowExponent, CultureInfo.InvariantCulture), LiteralFormat.DecimalNumber);
473
 
                                }
474
 
                                if (isdouble) {
475
 
                                        return new Token(Tokens.LiteralDouble, x, y, sb.ToString(), Double.Parse(digit, CultureInfo.InvariantCulture), LiteralFormat.DecimalNumber);
476
 
                                }
477
 
                        } catch (FormatException) {
478
 
                                errors.Error(Line, Col, String.Format("{0} is not a parseable number", digit));
479
 
                                if (issingle)
480
 
                                        return new Token(Tokens.LiteralSingle, x, y, sb.ToString(), 0f, LiteralFormat.DecimalNumber);
481
 
                                if (isdecimal)
482
 
                                        return new Token(Tokens.LiteralDecimal, x, y, sb.ToString(), 0m, LiteralFormat.DecimalNumber);
483
 
                                if (isdouble)
484
 
                                        return new Token(Tokens.LiteralDouble, x, y, sb.ToString(), 0.0, LiteralFormat.DecimalNumber);
485
 
                        }
486
 
                        Token token;
487
 
                        try {
488
 
                                token = new Token(Tokens.LiteralInteger, x, y, sb.ToString(), Int32.Parse(digit, ishex ? NumberStyles.HexNumber : NumberStyles.Number), ishex ? LiteralFormat.HexadecimalNumber : LiteralFormat.DecimalNumber);
489
 
                        } catch (Exception) {
490
 
                                try {
491
 
                                        token = new Token(Tokens.LiteralInteger, x, y, sb.ToString(), Int64.Parse(digit, ishex ? NumberStyles.HexNumber : NumberStyles.Number), ishex ? LiteralFormat.HexadecimalNumber : LiteralFormat.DecimalNumber);
492
 
                                } catch (FormatException) {
493
 
                                        errors.Error(Line, Col, String.Format("{0} is not a parseable number", digit));
494
 
                                        // fallback, when nothing helps :)
495
 
                                        token = new Token(Tokens.LiteralInteger, x, y, sb.ToString(), 0, LiteralFormat.DecimalNumber);
496
 
                                } catch (OverflowException) {
497
 
                                        errors.Error(Line, Col, String.Format("{0} is too long for a integer literal", digit));
498
 
                                        // fallback, when nothing helps :)
499
 
                                        token = new Token(Tokens.LiteralInteger, x, y, sb.ToString(), 0, LiteralFormat.DecimalNumber);
500
 
                                }
501
 
                        }
502
 
                        token.next = nextToken;
503
 
                        return token;
504
 
                }
505
 
                
506
 
                void ReadPreprocessorDirective()
507
 
                {
508
 
                        Location start = new Location(Col - 1, Line);
509
 
                        string directive = ReadIdent('#');
510
 
                        string argument  = ReadToEndOfLine();
511
 
                        this.specialTracker.AddPreprocessingDirective(new PreprocessingDirective(directive, argument.Trim(), start, new Location(start.Column + directive.Length + argument.Length, start.Line)));
512
 
                }
513
 
                
514
 
                string ReadDate()
515
 
                {
516
 
                        char ch = '\0';
517
 
                        sb.Length = 0;
518
 
                        int nextChar;
519
 
                        while ((nextChar = ReaderRead()) != -1) {
520
 
                                ch = (char)nextChar;
521
 
                                if (ch == '#') {
522
 
                                        break;
523
 
                                } else if (ch == '\n') {
524
 
                                        errors.Error(Line, Col, String.Format("No return allowed inside Date literal"));
525
 
                                } else {
526
 
                                        sb.Append(ch);
527
 
                                }
528
 
                        }
529
 
                        if (ch != '#') {
530
 
                                errors.Error(Line, Col, String.Format("End of File reached before Date literal terminated"));
531
 
                        }
532
 
                        return sb.ToString();
533
 
                }
534
 
                
535
 
                string ReadString()
536
 
                {
537
 
                        char ch = '\0';
538
 
                        sb.Length = 0;
539
 
                        int nextChar;
540
 
                        while ((nextChar = ReaderRead()) != -1) {
541
 
                                ch = (char)nextChar;
542
 
                                if (ch == '"') {
543
 
                                        if (ReaderPeek() != -1 && ReaderPeek() == '"') {
544
 
                                                sb.Append('"');
545
 
                                                ReaderRead();
546
 
                                        } else {
547
 
                                                break;
548
 
                                        }
549
 
                                } else if (ch == '\n') {
550
 
                                        errors.Error(Line, Col, String.Format("No return allowed inside String literal"));
551
 
                                } else {
552
 
                                        sb.Append(ch);
553
 
                                }
554
 
                        }
555
 
                        if (ch != '"') {
556
 
                                errors.Error(Line, Col, String.Format("End of File reached before String terminated "));
557
 
                        }
558
 
                        return sb.ToString();
559
 
                }
560
 
                
561
 
                void ReadComment()
562
 
                {
563
 
                        Location startPos = new Location(Col, Line);
564
 
                        sb.Length = 0;
565
 
                        StringBuilder curWord = specialCommentHash != null ? new StringBuilder() : null;
566
 
                        int missingApostrophes = 2; // no. of ' missing until it is a documentation comment
567
 
                        int nextChar;
568
 
                        while ((nextChar = ReaderRead()) != -1) {
569
 
                                char ch = (char)nextChar;
570
 
                                
571
 
                                if (HandleLineEnd(ch)) {
572
 
                                        break;
573
 
                                }
574
 
                                
575
 
                                sb.Append(ch);
576
 
                                
577
 
                                if (missingApostrophes > 0) {
578
 
                                        if (ch == '\'' || ch == '\u2018' || ch == '\u2019') {
579
 
                                                if (--missingApostrophes == 0) {
580
 
                                                        specialTracker.StartComment(CommentType.Documentation, isAtLineBegin, startPos);
581
 
                                                        sb.Length = 0;
582
 
                                                }
583
 
                                        } else {
584
 
                                                specialTracker.StartComment(CommentType.SingleLine, isAtLineBegin, startPos);
585
 
                                                missingApostrophes = 0;
586
 
                                        }
587
 
                                }
588
 
                                
589
 
                                if (specialCommentHash != null) {
590
 
                                        if (Char.IsLetter(ch)) {
591
 
                                                curWord.Append(ch);
592
 
                                        } else {
593
 
                                                string tag = curWord.ToString();
594
 
                                                curWord.Length = 0;
595
 
                                                if (specialCommentHash.ContainsKey(tag)) {
596
 
                                                        Location p = new Location(Col, Line);
597
 
                                                        string comment = ch + ReadToEndOfLine();
598
 
                                                        this.TagComments.Add(new TagComment(tag, comment, isAtLineBegin, p, new Location(Col, Line)));
599
 
                                                        sb.Append(comment);
600
 
                                                        break;
601
 
                                                }
602
 
                                        }
603
 
                                }
604
 
                        }
605
 
                        if (missingApostrophes > 0) {
606
 
                                specialTracker.StartComment(CommentType.SingleLine, isAtLineBegin, startPos);
607
 
                        }
608
 
                        specialTracker.AddString(sb.ToString());
609
 
                        specialTracker.FinishComment(new Location(Col, Line));
610
 
                }
611
 
                
612
 
                Token ReadOperator(char ch)
613
 
                {
614
 
                        int x = Col - 1;
615
 
                        int y = Line;
616
 
                        switch(ch) {
617
 
                                case '+':
618
 
                                        switch (ReaderPeek()) {
619
 
                                                case '=':
620
 
                                                        ReaderRead();
621
 
                                                        return new Token(Tokens.PlusAssign, x, y);
622
 
                                                default:
623
 
                                                        break;
624
 
                                        }
625
 
                                        return new Token(Tokens.Plus, x, y);
626
 
                                case '-':
627
 
                                        switch (ReaderPeek()) {
628
 
                                                case '=':
629
 
                                                        ReaderRead();
630
 
                                                        return new Token(Tokens.MinusAssign, x, y);
631
 
                                                default:
632
 
                                                        break;
633
 
                                        }
634
 
                                        return new Token(Tokens.Minus, x, y);
635
 
                                case '*':
636
 
                                        switch (ReaderPeek()) {
637
 
                                                case '=':
638
 
                                                        ReaderRead();
639
 
                                                        return new Token(Tokens.TimesAssign, x, y);
640
 
                                                default:
641
 
                                                        break;
642
 
                                        }
643
 
                                        return new Token(Tokens.Times, x, y, "*");
644
 
                                case '/':
645
 
                                        switch (ReaderPeek()) {
646
 
                                                case '=':
647
 
                                                        ReaderRead();
648
 
                                                        return new Token(Tokens.DivAssign, x, y);
649
 
                                                default:
650
 
                                                        break;
651
 
                                        }
652
 
                                        return new Token(Tokens.Div, x, y);
653
 
                                case '\\':
654
 
                                        switch (ReaderPeek()) {
655
 
                                                case '=':
656
 
                                                        ReaderRead();
657
 
                                                        return new Token(Tokens.DivIntegerAssign, x, y);
658
 
                                                default:
659
 
                                                        break;
660
 
                                        }
661
 
                                        return new Token(Tokens.DivInteger, x, y);
662
 
                                case '&':
663
 
                                        switch (ReaderPeek()) {
664
 
                                                case '=':
665
 
                                                        ReaderRead();
666
 
                                                        return new Token(Tokens.ConcatStringAssign, x, y);
667
 
                                                default:
668
 
                                                        break;
669
 
                                        }
670
 
                                        return new Token(Tokens.ConcatString, x, y);
671
 
                                case '^':
672
 
                                        switch (ReaderPeek()) {
673
 
                                                case '=':
674
 
                                                        ReaderRead();
675
 
                                                        return new Token(Tokens.PowerAssign, x, y);
676
 
                                                default:
677
 
                                                        break;
678
 
                                        }
679
 
                                        return new Token(Tokens.Power, x, y);
680
 
                                case ':':
681
 
                                        return new Token(Tokens.Colon, x, y);
682
 
                                case '=':
683
 
                                        return new Token(Tokens.Assign, x, y);
684
 
                                case '<':
685
 
                                        switch (ReaderPeek()) {
686
 
                                                case '=':
687
 
                                                        ReaderRead();
688
 
                                                        return new Token(Tokens.LessEqual, x, y);
689
 
                                                case '>':
690
 
                                                        ReaderRead();
691
 
                                                        return new Token(Tokens.NotEqual, x, y);
692
 
                                                case '<':
693
 
                                                        ReaderRead();
694
 
                                                        switch (ReaderPeek()) {
695
 
                                                                case '=':
696
 
                                                                        ReaderRead();
697
 
                                                                        return new Token(Tokens.ShiftLeftAssign, x, y);
698
 
                                                                default:
699
 
                                                                        break;
700
 
                                                        }
701
 
                                                        return new Token(Tokens.ShiftLeft, x, y);
702
 
                                        }
703
 
                                        return new Token(Tokens.LessThan, x, y);
704
 
                                case '>':
705
 
                                        switch (ReaderPeek()) {
706
 
                                                case '=':
707
 
                                                        ReaderRead();
708
 
                                                        return new Token(Tokens.GreaterEqual, x, y);
709
 
                                                case '>':
710
 
                                                        ReaderRead();
711
 
                                                        if (ReaderPeek() != -1) {
712
 
                                                                switch (ReaderPeek()) {
713
 
                                                                        case '=':
714
 
                                                                                ReaderRead();
715
 
                                                                                return new Token(Tokens.ShiftRightAssign, x, y);
716
 
                                                                        default:
717
 
                                                                                break;
718
 
                                                                }
719
 
                                                        }
720
 
                                                        return new Token(Tokens.ShiftRight, x, y);
721
 
                                        }
722
 
                                        return new Token(Tokens.GreaterThan, x, y);
723
 
                                case ',':
724
 
                                        return new Token(Tokens.Comma, x, y);
725
 
                                case '.':
726
 
                                        // Prevent OverflowException when Peek returns -1
727
 
                                        int tmp = ReaderPeek();
728
 
                                        if (tmp > 0 && Char.IsDigit((char)tmp)) {
729
 
                                                return ReadDigit('.', Col);
730
 
                                        }
731
 
                                        return new Token(Tokens.Dot, x, y);
732
 
                                case '(':
733
 
                                        return new Token(Tokens.OpenParenthesis, x, y);
734
 
                                case ')':
735
 
                                        return new Token(Tokens.CloseParenthesis, x, y);
736
 
                                case '{':
737
 
                                        return new Token(Tokens.OpenCurlyBrace, x, y);
738
 
                                case '}':
739
 
                                        return new Token(Tokens.CloseCurlyBrace, x, y);
740
 
                                case '?':
741
 
                                        return new Token(Tokens.QuestionMark, x, y);
742
 
                                case '!':
743
 
                                        return new Token(Tokens.ExclamationMark, x, y);
744
 
                        }
745
 
                        return null;
746
 
                }
747
 
                
748
 
                public override void SkipCurrentBlock(int targetToken)
749
 
                {
750
 
                        int lastKind = -1;
751
 
                        int kind = base.lastToken.kind;
752
 
                        while (kind != Tokens.EOF &&
753
 
                               !(lastKind == Tokens.End && kind == targetToken))
754
 
                        {
755
 
                                lastKind = kind;
756
 
                                NextToken();
757
 
                                kind = lastToken.kind;
758
 
                        }
759
 
                }
760
 
        }
761
 
}