2006-10-26 Marek Safar <marek.safar@gmail.com>
[mono.git] / mcs / mcs / cs-tokenizer.cs
1 //
2 // cs-tokenizer.cs: The Tokenizer for the C# compiler
3 //                  This also implements the preprocessor
4 //
5 // Author: Miguel de Icaza (miguel@gnu.org)
6 //         Marek Safar (marek.safar@seznam.cz)
7 //
8 // Licensed under the terms of the GNU GPL
9 //
10 // (C) 2001, 2002 Ximian, Inc (http://www.ximian.com)
11 // (C) 2004 Novell, Inc
12 //
13
14 /*
15  * TODO:
16  *   Make sure we accept the proper Unicode ranges, per the spec.
17  *   Report error 1032
18 */
19
20 using System;
21 using System.Text;
22 using System.Collections;
23 using System.IO;
24 using System.Globalization;
25 using System.Reflection;
26
27 namespace Mono.CSharp
28 {
29         /// <summary>
30         ///    Tokenizer for C# source code. 
31         /// </summary>
32
33         public class Tokenizer : yyParser.yyInput
34         {
35                 SeekableStreamReader reader;
36                 SourceFile ref_name;
37                 SourceFile file_name;
38                 int ref_line = 1;
39                 int line = 1;
40                 int col = 0;
41                 int previous_col;
42                 int current_token;
43                 bool handle_get_set = false;
44                 bool handle_remove_add = false;
45                 bool handle_assembly = false;
46                 Location current_location;
47                 Location current_comment_location = Location.Null;
48                 ArrayList escapedIdentifiers = new ArrayList ();
49
50                 //
51                 // XML documentation buffer. The save point is used to divide
52                 // comments on types and comments on members.
53                 //
54                 StringBuilder xml_comment_buffer;
55
56                 //
57                 // See comment on XmlCommentState enumeration.
58                 //
59                 XmlCommentState xmlDocState = XmlCommentState.Allowed;
60
61                 //
62                 // Whether tokens have been seen on this line
63                 //
64                 bool tokens_seen = false;
65
66                 //
67                 // Whether a token has been seen on the file
68                 // This is needed because `define' is not allowed to be used
69                 // after a token has been seen.
70                 //
71                 bool any_token_seen = false;
72
73                 static Hashtable tokenValues;
74
75                 private static Hashtable TokenValueName
76                 {
77                         get {
78                                 if (tokenValues == null)
79                                         tokenValues = GetTokenValueNameHash ();
80
81                                 return tokenValues;
82                         }
83                 }
84
85                 private static Hashtable GetTokenValueNameHash ()
86                 {
87                         Type t = typeof (Token);
88                         FieldInfo [] fields = t.GetFields ();
89                         Hashtable hash = new Hashtable ();
90                         foreach (FieldInfo field in fields) {
91                                 if (field.IsLiteral && field.IsStatic && field.FieldType == typeof (int))
92                                         hash.Add (field.GetValue (null), field.Name);
93                         }
94                         return hash;
95                 }
96                 
97                 //
98                 // Returns a verbose representation of the current location
99                 //
100                 public string location {
101                         get {
102                                 string det;
103
104                                 if (current_token == Token.ERROR)
105                                         det = "detail: " + error_details;
106                                 else
107                                         det = "";
108                                 
109                                 // return "Line:     "+line+" Col: "+col + "\n" +
110                                 //       "VirtLine: "+ref_line +
111                                 //       " Token: "+current_token + " " + det;
112                                 string current_token_name = TokenValueName [current_token] as string;
113                                 if (current_token_name == null)
114                                         current_token_name = current_token.ToString ();
115
116                                 return String.Format ("{0} ({1},{2}), Token: {3} {4}", ref_name.Name,
117                                                                                        ref_line,
118                                                                                        col,
119                                                                                        current_token_name,
120                                                                                        det);
121                         }
122                 }
123
124                 public bool PropertyParsing {
125                         get {
126                                 return handle_get_set;
127                         }
128
129                         set {
130                                 handle_get_set = value;
131                         }
132                 }
133
134                 public bool AssemblyTargetParsing {
135                         get {
136                                 return handle_assembly;
137                         }
138
139                         set {
140                                 handle_assembly = value;
141                         }
142                 }
143
144                 public bool EventParsing {
145                         get {
146                                 return handle_remove_add;
147                         }
148
149                         set {
150                                 handle_remove_add = value;
151                         }
152                 }
153
154                 public XmlCommentState doc_state {
155                         get { return xmlDocState; }
156                         set {
157                                 if (value == XmlCommentState.Allowed) {
158                                         check_incorrect_doc_comment ();
159                                         reset_doc_comment ();
160                                 }
161                                 xmlDocState = value;
162                         }
163                 }
164
165                 public bool IsEscapedIdentifier (Location loc)
166                 {
167                         foreach (LocatedToken lt in escapedIdentifiers)
168                                 if (lt.Location.Equals (loc))
169                                         return true;
170                         return false;
171                 }
172
173                 
174                 //
175                 // Class variables
176                 // 
177                 static CharArrayHashtable[] keywords;
178                 static Hashtable keywordStrings = new Hashtable ();
179                 static NumberStyles styles;
180                 static NumberFormatInfo csharp_format_info;
181                 
182                 //
183                 // Values for the associated token returned
184                 //
185                 int putback_char;
186                 Object val;
187
188                 //
189                 // Pre-processor
190                 //
191                 Hashtable defines;
192
193                 const int TAKING        = 1;
194                 const int TAKEN_BEFORE  = 2;
195                 const int ELSE_SEEN     = 4;
196                 const int PARENT_TAKING = 8;
197                 const int REGION        = 16;           
198
199                 //
200                 // pre-processor if stack state:
201                 //
202                 Stack ifstack;
203
204                 static System.Text.StringBuilder string_builder;
205
206                 const int max_id_size = 512;
207                 static char [] id_builder = new char [max_id_size];
208
209                 static CharArrayHashtable [] identifiers = new CharArrayHashtable [max_id_size + 1];
210
211                 const int max_number_size = 512;
212                 static char [] number_builder = new char [max_number_size];
213                 static int number_pos;
214                 
215                 //
216                 // Details about the error encoutered by the tokenizer
217                 //
218                 string error_details;
219                 
220                 public string error {
221                         get {
222                                 return error_details;
223                         }
224                 }
225                 
226                 public int Line {
227                         get {
228                                 return ref_line;
229                         }
230                 }
231
232                 public int Col {
233                         get {
234                                 return col;
235                         }
236                 }
237
238                 static void AddKeyword (string kw, int token) {
239                         keywordStrings.Add (kw, kw);
240                         if (keywords [kw.Length] == null) {
241                                 keywords [kw.Length] = new CharArrayHashtable (kw.Length);
242                         }
243                         keywords [kw.Length] [kw.ToCharArray ()] = token;
244                 }
245
246                 static void InitTokens ()
247                 {
248                         keywords = new CharArrayHashtable [64];
249
250                         AddKeyword ("__arglist", Token.ARGLIST);
251                         AddKeyword ("abstract", Token.ABSTRACT);
252                         AddKeyword ("as", Token.AS);
253                         AddKeyword ("add", Token.ADD);
254                         AddKeyword ("assembly", Token.ASSEMBLY);
255                         AddKeyword ("base", Token.BASE);
256                         AddKeyword ("bool", Token.BOOL);
257                         AddKeyword ("break", Token.BREAK);
258                         AddKeyword ("byte", Token.BYTE);
259                         AddKeyword ("case", Token.CASE);
260                         AddKeyword ("catch", Token.CATCH);
261                         AddKeyword ("char", Token.CHAR);
262                         AddKeyword ("checked", Token.CHECKED);
263                         AddKeyword ("class", Token.CLASS);
264                         AddKeyword ("const", Token.CONST);
265                         AddKeyword ("continue", Token.CONTINUE);
266                         AddKeyword ("decimal", Token.DECIMAL);
267                         AddKeyword ("default", Token.DEFAULT);
268                         AddKeyword ("delegate", Token.DELEGATE);
269                         AddKeyword ("do", Token.DO);
270                         AddKeyword ("double", Token.DOUBLE);
271                         AddKeyword ("else", Token.ELSE);
272                         AddKeyword ("enum", Token.ENUM);
273                         AddKeyword ("event", Token.EVENT);
274                         AddKeyword ("explicit", Token.EXPLICIT);
275                         AddKeyword ("extern", Token.EXTERN);
276                         AddKeyword ("false", Token.FALSE);
277                         AddKeyword ("finally", Token.FINALLY);
278                         AddKeyword ("fixed", Token.FIXED);
279                         AddKeyword ("float", Token.FLOAT);
280                         AddKeyword ("for", Token.FOR);
281                         AddKeyword ("foreach", Token.FOREACH);
282                         AddKeyword ("goto", Token.GOTO);
283                         AddKeyword ("get", Token.GET);
284                         AddKeyword ("if", Token.IF);
285                         AddKeyword ("implicit", Token.IMPLICIT);
286                         AddKeyword ("in", Token.IN);
287                         AddKeyword ("int", Token.INT);
288                         AddKeyword ("interface", Token.INTERFACE);
289                         AddKeyword ("internal", Token.INTERNAL);
290                         AddKeyword ("is", Token.IS);
291                         AddKeyword ("lock", Token.LOCK);
292                         AddKeyword ("long", Token.LONG);
293                         AddKeyword ("namespace", Token.NAMESPACE);
294                         AddKeyword ("new", Token.NEW);
295                         AddKeyword ("null", Token.NULL);
296                         AddKeyword ("object", Token.OBJECT);
297                         AddKeyword ("operator", Token.OPERATOR);
298                         AddKeyword ("out", Token.OUT);
299                         AddKeyword ("override", Token.OVERRIDE);
300                         AddKeyword ("params", Token.PARAMS);
301                         AddKeyword ("private", Token.PRIVATE);
302                         AddKeyword ("protected", Token.PROTECTED);
303                         AddKeyword ("public", Token.PUBLIC);
304                         AddKeyword ("readonly", Token.READONLY);
305                         AddKeyword ("ref", Token.REF);
306                         AddKeyword ("remove", Token.REMOVE);
307                         AddKeyword ("return", Token.RETURN);
308                         AddKeyword ("sbyte", Token.SBYTE);
309                         AddKeyword ("sealed", Token.SEALED);
310                         AddKeyword ("set", Token.SET);
311                         AddKeyword ("short", Token.SHORT);
312                         AddKeyword ("sizeof", Token.SIZEOF);
313                         AddKeyword ("stackalloc", Token.STACKALLOC);
314                         AddKeyword ("static", Token.STATIC);
315                         AddKeyword ("string", Token.STRING);
316                         AddKeyword ("struct", Token.STRUCT);
317                         AddKeyword ("switch", Token.SWITCH);
318                         AddKeyword ("this", Token.THIS);
319                         AddKeyword ("throw", Token.THROW);
320                         AddKeyword ("true", Token.TRUE);
321                         AddKeyword ("try", Token.TRY);
322                         AddKeyword ("typeof", Token.TYPEOF);
323                         AddKeyword ("uint", Token.UINT);
324                         AddKeyword ("ulong", Token.ULONG);
325                         AddKeyword ("unchecked", Token.UNCHECKED);
326                         AddKeyword ("unsafe", Token.UNSAFE);
327                         AddKeyword ("ushort", Token.USHORT);
328                         AddKeyword ("using", Token.USING);
329                         AddKeyword ("virtual", Token.VIRTUAL);
330                         AddKeyword ("void", Token.VOID);
331                         AddKeyword ("volatile", Token.VOLATILE);
332                         AddKeyword ("while", Token.WHILE);
333                         AddKeyword ("partial", Token.PARTIAL);
334                 }
335
336                 //
337                 // Class initializer
338                 // 
339                 static Tokenizer ()
340                 {
341                         InitTokens ();
342                         csharp_format_info = NumberFormatInfo.InvariantInfo;
343                         styles = NumberStyles.Float;
344                         
345                         string_builder = new System.Text.StringBuilder ();
346                 }
347
348                 int GetKeyword (char[] id, int id_len)
349                 {
350                         /*
351                          * Keywords are stored in an array of hashtables grouped by their
352                          * length.
353                          */
354
355                         if ((id_len >= keywords.Length) || (keywords [id_len] == null))
356                                 return -1;
357                         object o = keywords [id_len] [id];
358
359                         if (o == null)
360                                 return -1;
361                         
362                         int res = (int) o;
363
364                         if (handle_get_set == false && (res == Token.GET || res == Token.SET))
365                                 return -1;
366                         if (handle_remove_add == false && (res == Token.REMOVE || res == Token.ADD))
367                                 return -1;
368                         if (handle_assembly == false && res == Token.ASSEMBLY)
369                                 return -1;
370
371                         return res;
372                         
373                 }
374
375                 public Location Location {
376                         get { return current_location; }
377                 }
378
379                 void define (string def)
380                 {
381                         if (!RootContext.AllDefines.Contains (def)){
382                                 RootContext.AllDefines [def] = true;
383                         }
384                         if (defines.Contains (def))
385                                 return;
386                         defines [def] = true;
387                 }
388                 
389                 public Tokenizer (SeekableStreamReader input, SourceFile file, ArrayList defs)
390                 {
391                         this.ref_name = file;
392                         this.file_name = file;
393                         reader = input;
394                         
395                         putback_char = -1;
396
397                         if (defs != null){
398                                 defines = new Hashtable ();
399                                 foreach (string def in defs)
400                                         define (def);
401                         }
402
403                         xml_comment_buffer = new StringBuilder ();
404
405                         //
406                         // FIXME: This could be `Location.Push' but we have to
407                         // find out why the MS compiler allows this
408                         //
409                         Mono.CSharp.Location.Push (file);
410                 }
411
412                 static bool is_identifier_start_character (char c)
413                 {
414                         return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || c == '_' || Char.IsLetter (c);
415                 }
416
417                 static bool is_identifier_part_character (char c)
418                 {
419                         return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_' || (c >= '0' && c <= '9') || Char.IsLetter (c);
420                 }
421
422                 public static bool IsKeyword (string s)
423                 {
424                         return keywordStrings [s] != null;
425                 }
426
427                 public static bool IsValidIdentifier (string s)
428                 {
429                         if (s == null || s.Length == 0)
430                                 return false;
431
432                         if (!is_identifier_start_character (s [0]))
433                                 return false;
434                         
435                         for (int i = 1; i < s.Length; i ++)
436                                 if (! is_identifier_part_character (s [i]))
437                                         return false;
438                         
439                         return true;
440                 }
441
442                 int is_punct (char c, ref bool doread)
443                 {
444                         int d;
445                         int t;
446
447                         doread = false;
448
449                         switch (c){
450                         case '{':
451                                 val = Location;
452                                 return Token.OPEN_BRACE;
453                         case '}':
454                                 val = Location;
455                                 return Token.CLOSE_BRACE;
456                         case '[':
457                                 // To block doccomment inside attribute declaration.
458                                 if (doc_state == XmlCommentState.Allowed)
459                                         doc_state = XmlCommentState.NotAllowed;
460                                 return Token.OPEN_BRACKET;
461                         case ']':
462                                 return Token.CLOSE_BRACKET;
463                         case '(':
464                                 return Token.OPEN_PARENS;
465                         case ')': {
466                                 if (deambiguate_close_parens == 0)
467                                         return Token.CLOSE_PARENS;
468
469                                 --deambiguate_close_parens;
470
471                                 // Save current position and parse next token.
472                                 int old = reader.Position;
473                                 int old_ref_line = ref_line;
474                                 int old_col = col;
475
476                                 // disable preprocessing directives when peeking
477                                 process_directives = false;
478                                 int new_token = token ();
479                                 process_directives = true;
480                                 reader.Position = old;
481                                 ref_line = old_ref_line;
482                                 col = old_col;
483                                 putback_char = -1;
484
485                                 if (new_token == Token.OPEN_PARENS)
486                                         return Token.CLOSE_PARENS_OPEN_PARENS;
487                                 else if (new_token == Token.MINUS)
488                                         return Token.CLOSE_PARENS_MINUS;
489                                 else if (IsCastToken (new_token))
490                                         return Token.CLOSE_PARENS_CAST;
491                                 else
492                                         return Token.CLOSE_PARENS_NO_CAST;
493                         }
494
495                         case ',':
496                                 return Token.COMMA;
497                         case ';':
498                                 val = Location;
499                                 return Token.SEMICOLON;
500                         case '~':
501                                 val = Location;
502                                 return Token.TILDE;
503                         case '?':
504                                 return Token.INTERR;
505                         }
506
507                         d = peekChar ();
508                         if (c == '+'){
509                                 
510                                 if (d == '+') {
511                                         val = Location;
512                                         t = Token.OP_INC;
513                                 }
514                                 else if (d == '=')
515                                         t = Token.OP_ADD_ASSIGN;
516                                 else {
517                                         val = Location;
518                                         return Token.PLUS;
519                                 }
520                                 doread = true;
521                                 return t;
522                         }
523                         if (c == '-'){
524                                 if (d == '-') {
525                                         val = Location;
526                                         t = Token.OP_DEC;
527                                 }
528                                 else if (d == '=')
529                                         t = Token.OP_SUB_ASSIGN;
530                                 else if (d == '>')
531                                         t = Token.OP_PTR;
532                                 else {
533                                         val = Location;
534                                         return Token.MINUS;
535                                 }
536                                 doread = true;
537                                 return t;
538                         }
539
540                         if (c == '!'){
541                                 if (d == '='){
542                                         doread = true;
543                                         return Token.OP_NE;
544                                 }
545                                 val = Location;
546                                 return Token.BANG;
547                         }
548
549                         if (c == '='){
550                                 if (d == '='){
551                                         doread = true;
552                                         return Token.OP_EQ;
553                                 }
554                                 return Token.ASSIGN;
555                         }
556
557                         if (c == '&'){
558                                 if (d == '&'){
559                                         doread = true;
560                                         return Token.OP_AND;
561                                 } else if (d == '='){
562                                         doread = true;
563                                         return Token.OP_AND_ASSIGN;
564                                 }
565                                 val = Location;
566                                 return Token.BITWISE_AND;
567                         }
568
569                         if (c == '|'){
570                                 if (d == '|'){
571                                         doread = true;
572                                         return Token.OP_OR;
573                                 } else if (d == '='){
574                                         doread = true;
575                                         return Token.OP_OR_ASSIGN;
576                                 }
577                                 return Token.BITWISE_OR;
578                         }
579
580                         if (c == '*'){
581                                 if (d == '='){
582                                         doread = true;
583                                         return Token.OP_MULT_ASSIGN;
584                                 }
585                                 val = Location;
586                                 return Token.STAR;
587                         }
588
589                         if (c == '/'){
590                                 if (d == '='){
591                                         doread = true;
592                                         return Token.OP_DIV_ASSIGN;
593                                 }
594                                 return Token.DIV;
595                         }
596
597                         if (c == '%'){
598                                 if (d == '='){
599                                         doread = true;
600                                         return Token.OP_MOD_ASSIGN;
601                                 }
602                                 return Token.PERCENT;
603                         }
604
605                         if (c == '^'){
606                                 if (d == '='){
607                                         doread = true;
608                                         return Token.OP_XOR_ASSIGN;
609                                 }
610                                 return Token.CARRET;
611                         }
612
613                         if (c == '<'){
614                                 if (d == '<'){
615                                         getChar ();
616                                         d = peekChar ();
617
618                                         if (d == '='){
619                                                 doread = true;
620                                                 return Token.OP_SHIFT_LEFT_ASSIGN;
621                                         }
622                                         return Token.OP_SHIFT_LEFT;
623                                 } else if (d == '='){
624                                         doread = true;
625                                         return Token.OP_LE;
626                                 }
627                                 return Token.OP_LT;
628                         }
629
630                         if (c == '>'){
631                                 if (d == '>'){
632                                         getChar ();
633                                         d = peekChar ();
634
635                                         if (d == '='){
636                                                 doread = true;
637                                                 return Token.OP_SHIFT_RIGHT_ASSIGN;
638                                         }
639                                         return Token.OP_SHIFT_RIGHT;
640                                 } else if (d == '='){
641                                         doread = true;
642                                         return Token.OP_GE;
643                                 }
644                                 return Token.OP_GT;
645                         }
646                         if (c == ':'){
647                                 if (d == ':'){
648                                         doread = true;
649                                         return Token.DOUBLE_COLON;
650                                 }
651                                 val = Location;
652                                 return Token.COLON;
653                         }
654
655                         return Token.ERROR;
656                 }
657
658                 int deambiguate_close_parens = 0;
659
660                 public void Deambiguate_CloseParens (object expression)
661                 {
662                         putback (')');
663
664                         // When any binary operation is used we are sure it is not a cast
665                         if (expression is Binary)
666                                 return;
667
668                         deambiguate_close_parens++;
669                 }
670
671                 void Error_NumericConstantTooLong ()
672                 {
673                         Report.Error (1021, Location, "Numeric constant too long");                     
674                 }
675                 
676                 bool decimal_digits (int c)
677                 {
678                         int d;
679                         bool seen_digits = false;
680                         
681                         if (c != -1){
682                                 if (number_pos == max_number_size)
683                                         Error_NumericConstantTooLong ();
684                                 number_builder [number_pos++] = (char) c;
685                         }
686                         
687                         //
688                         // We use peekChar2, because decimal_digits needs to do a 
689                         // 2-character look-ahead (5.ToString for example).
690                         //
691                         while ((d = peekChar2 ()) != -1){
692                                 if (d >= '0' && d <= '9'){
693                                         if (number_pos == max_number_size)
694                                                 Error_NumericConstantTooLong ();
695                                         number_builder [number_pos++] = (char) d;
696                                         getChar ();
697                                         seen_digits = true;
698                                 } else
699                                         break;
700                         }
701                         
702                         return seen_digits;
703                 }
704
705                 static bool is_hex (int e)
706                 {
707                         return (e >= '0' && e <= '9') || (e >= 'A' && e <= 'F') || (e >= 'a' && e <= 'f');
708                 }
709                                 
710                 static int real_type_suffix (int c)
711                 {
712                         int t;
713
714                         switch (c){
715                         case 'F': case 'f':
716                                 t =  Token.LITERAL_FLOAT;
717                                 break;
718                         case 'D': case 'd':
719                                 t = Token.LITERAL_DOUBLE;
720                                 break;
721                         case 'M': case 'm':
722                                  t= Token.LITERAL_DECIMAL;
723                                 break;
724                         default:
725                                 return Token.NONE;
726                         }
727                         return t;
728                 }
729
730                 int integer_type_suffix (ulong ul, int c)
731                 {
732                         bool is_unsigned = false;
733                         bool is_long = false;
734
735                         if (c != -1){
736                                 bool scanning = true;
737                                 do {
738                                         switch (c){
739                                         case 'U': case 'u':
740                                                 if (is_unsigned)
741                                                         scanning = false;
742                                                 is_unsigned = true;
743                                                 getChar ();
744                                                 break;
745
746                                         case 'l':
747                                                 if (!is_unsigned && (RootContext.WarningLevel >= 4)){
748                                                         //
749                                                         // if we have not seen anything in between
750                                                         // report this error
751                                                         //
752                                                         Report.Warning (78, 4, Location, "The 'l' suffix is easily confused with the digit '1' (use 'L' for clarity)");
753                                                 }
754                                                 //
755                                                 // This goto statement causes the MS CLR 2.0 beta 1 csc to report an error, so
756                                                 // work around that.
757                                                 //
758                                                 //goto case 'L';
759                                                 if (is_long)
760                                                         scanning = false;
761                                                 is_long = true;
762                                                 getChar ();
763                                                 break;
764
765                                         case 'L': 
766                                                 if (is_long)
767                                                         scanning = false;
768                                                 is_long = true;
769                                                 getChar ();
770                                                 break;
771                                                 
772                                         default:
773                                                 scanning = false;
774                                                 break;
775                                         }
776                                         c = peekChar ();
777                                 } while (scanning);
778                         }
779
780                         if (is_long && is_unsigned){
781                                 val = ul;
782                                 return Token.LITERAL_INTEGER;
783                         } else if (is_unsigned){
784                                 // uint if possible, or ulong else.
785
786                                 if ((ul & 0xffffffff00000000) == 0)
787                                         val = (uint) ul;
788                                 else
789                                         val = ul;
790                         } else if (is_long){
791                                 // long if possible, ulong otherwise
792                                 if ((ul & 0x8000000000000000) != 0)
793                                         val = ul;
794                                 else
795                                         val = (long) ul;
796                         } else {
797                                 // int, uint, long or ulong in that order
798                                 if ((ul & 0xffffffff00000000) == 0){
799                                         uint ui = (uint) ul;
800                                         
801                                         if ((ui & 0x80000000) != 0)
802                                                 val = ui;
803                                         else
804                                                 val = (int) ui;
805                                 } else {
806                                         if ((ul & 0x8000000000000000) != 0)
807                                                 val = ul;
808                                         else
809                                                 val = (long) ul;
810                                 }
811                         }
812                         return Token.LITERAL_INTEGER;
813                 }
814                                 
815                 //
816                 // given `c' as the next char in the input decide whether
817                 // we need to convert to a special type, and then choose
818                 // the best representation for the integer
819                 //
820                 int adjust_int (int c)
821                 {
822                         try {
823                                 if (number_pos > 9){
824                                         ulong ul = (uint) (number_builder [0] - '0');
825
826                                         for (int i = 1; i < number_pos; i++){
827                                                 ul = checked ((ul * 10) + ((uint)(number_builder [i] - '0')));
828                                         }
829                                         return integer_type_suffix (ul, c);
830                                 } else {
831                                         uint ui = (uint) (number_builder [0] - '0');
832
833                                         for (int i = 1; i < number_pos; i++){
834                                                 ui = checked ((ui * 10) + ((uint)(number_builder [i] - '0')));
835                                         }
836                                         return integer_type_suffix (ui, c);
837                                 }
838                         } catch (OverflowException) {
839                                 error_details = "Integral constant is too large";
840                                 Report.Error (1021, Location, error_details);
841                                 val = 0ul;
842                                 return Token.LITERAL_INTEGER;
843                         }
844                         catch (FormatException) {
845                                 Report.Error (1013, Location, "Invalid number");
846                                 val = 0ul;
847                                 return Token.LITERAL_INTEGER;
848                         }
849                 }
850                 
851                 int adjust_real (int t)
852                 {
853                         string s = new String (number_builder, 0, number_pos);
854                         const string error_details = "Floating-point constant is outside the range of type `{0}'";
855
856                         switch (t){
857                         case Token.LITERAL_DECIMAL:
858                                 try {
859                                         val = System.Decimal.Parse (s, styles, csharp_format_info);
860                                 } catch (OverflowException) {
861                                         val = 0m;     
862                                         Report.Error (594, Location, error_details, "decimal");
863                                 }
864                                 break;
865                         case Token.LITERAL_FLOAT:
866                                 try {
867                                         val = float.Parse (s, styles, csharp_format_info);
868                                 } catch (OverflowException) {
869                                         val = 0.0f;     
870                                         Report.Error (594, Location, error_details, "float");
871                                 }
872                                 break;
873                                 
874                         case Token.LITERAL_DOUBLE:
875                         case Token.NONE:
876                                 t = Token.LITERAL_DOUBLE;
877                                 try {
878                                         val = System.Double.Parse (s, styles, csharp_format_info);
879                                 } catch (OverflowException) {
880                                         val = 0.0;     
881                                         Report.Error (594, Location, error_details, "double");
882                                 }
883                                 break;
884                         }
885                         return t;
886                 }
887
888                 int handle_hex ()
889                 {
890                         int d;
891                         ulong ul;
892                         
893                         getChar ();
894                         while ((d = peekChar ()) != -1){
895                                 if (is_hex (d)){
896                                         number_builder [number_pos++] = (char) d;
897                                         getChar ();
898                                 } else
899                                         break;
900                         }
901                         
902                         string s = new String (number_builder, 0, number_pos);
903                         try {
904                                 if (number_pos <= 8)
905                                         ul = System.UInt32.Parse (s, NumberStyles.HexNumber);
906                                 else
907                                         ul = System.UInt64.Parse (s, NumberStyles.HexNumber);
908                         } catch (OverflowException){
909                                 error_details = "Integral constant is too large";
910                                 Report.Error (1021, Location, error_details);
911                                 val = 0ul;
912                                 return Token.LITERAL_INTEGER;
913                         }
914                         catch (FormatException) {
915                                 Report.Error (1013, Location, "Invalid number");
916                                 val = 0ul;
917                                 return Token.LITERAL_INTEGER;
918                         }
919                         
920                         return integer_type_suffix (ul, peekChar ());
921                 }
922
923                 //
924                 // Invoked if we know we have .digits or digits
925                 //
926                 int is_number (int c)
927                 {
928                         bool is_real = false;
929                         int type;
930
931                         number_pos = 0;
932
933                         if (c >= '0' && c <= '9'){
934                                 if (c == '0'){
935                                         int peek = peekChar ();
936
937                                         if (peek == 'x' || peek == 'X')
938                                                 return handle_hex ();
939                                 }
940                                 decimal_digits (c);
941                                 c = getChar ();
942                         }
943
944                         //
945                         // We need to handle the case of
946                         // "1.1" vs "1.string" (LITERAL_FLOAT vs NUMBER DOT IDENTIFIER)
947                         //
948                         if (c == '.'){
949                                 if (decimal_digits ('.')){
950                                         is_real = true;
951                                         c = getChar ();
952                                 } else {
953                                         putback ('.');
954                                         number_pos--;
955                                         return adjust_int (-1);
956                                 }
957                         }
958                         
959                         if (c == 'e' || c == 'E'){
960                                 is_real = true;
961                                 if (number_pos == max_number_size)
962                                         Error_NumericConstantTooLong ();
963                                 number_builder [number_pos++] = 'e';
964                                 c = getChar ();
965                                 
966                                 if (c == '+'){
967                                         if (number_pos == max_number_size)
968                                                 Error_NumericConstantTooLong ();
969                                         number_builder [number_pos++] = '+';
970                                         c = -1;
971                                 } else if (c == '-') {
972                                         if (number_pos == max_number_size)
973                                                 Error_NumericConstantTooLong ();
974                                         number_builder [number_pos++] = '-';
975                                         c = -1;
976                                 } else {
977                                         if (number_pos == max_number_size)
978                                                 Error_NumericConstantTooLong ();
979                                         number_builder [number_pos++] = '+';
980                                 }
981                                         
982                                 decimal_digits (c);
983                                 c = getChar ();
984                         }
985
986                         type = real_type_suffix (c);
987                         if (type == Token.NONE && !is_real){
988                                 putback (c);
989                                 return adjust_int (c);
990                         } else 
991                                 is_real = true;
992
993                         if (type == Token.NONE){
994                                 putback (c);
995                         }
996                         
997                         if (is_real)
998                                 return adjust_real (type);
999
1000                         Console.WriteLine ("This should not be reached");
1001                         throw new Exception ("Is Number should never reach this point");
1002                 }
1003
1004                 //
1005                 // Accepts exactly count (4 or 8) hex, no more no less
1006                 //
1007                 int getHex (int count, out bool error)
1008                 {
1009                         int i;
1010                         int total = 0;
1011                         int c;
1012                         int top = count != -1 ? count : 4;
1013                         
1014                         getChar ();
1015                         error = false;
1016                         for (i = 0; i < top; i++){
1017                                 c = getChar ();
1018                                 
1019                                 if (c >= '0' && c <= '9')
1020                                         c = (int) c - (int) '0';
1021                                 else if (c >= 'A' && c <= 'F')
1022                                         c = (int) c - (int) 'A' + 10;
1023                                 else if (c >= 'a' && c <= 'f')
1024                                         c = (int) c - (int) 'a' + 10;
1025                                 else {
1026                                         error = true;
1027                                         return 0;
1028                                 }
1029                                 
1030                                 total = (total * 16) + c;
1031                                 if (count == -1){
1032                                         int p = peekChar ();
1033                                         if (p == -1)
1034                                                 break;
1035                                         if (!is_hex ((char)p))
1036                                                 break;
1037                                 }
1038                         }
1039                         return total;
1040                 }
1041
1042                 int escape (int c)
1043                 {
1044                         bool error;
1045                         int d;
1046                         int v;
1047
1048                         d = peekChar ();
1049                         if (c != '\\')
1050                                 return c;
1051                         
1052                         switch (d){
1053                         case 'a':
1054                                 v = '\a'; break;
1055                         case 'b':
1056                                 v = '\b'; break;
1057                         case 'n':
1058                                 v = '\n'; break;
1059                         case 't':
1060                                 v = '\t'; break;
1061                         case 'v':
1062                                 v = '\v'; break;
1063                         case 'r':
1064                                 v = '\r'; break;
1065                         case '\\':
1066                                 v = '\\'; break;
1067                         case 'f':
1068                                 v = '\f'; break;
1069                         case '0':
1070                                 v = 0; break;
1071                         case '"':
1072                                 v = '"'; break;
1073                         case '\'':
1074                                 v = '\''; break;
1075                         case 'x':
1076                                 v = getHex (-1, out error);
1077                                 if (error)
1078                                         goto default;
1079                                 return v;
1080                         case 'u':
1081                                 v = getHex (4, out error);
1082                                 if (error)
1083                                         goto default;
1084                                 return v;
1085                         case 'U':
1086                                 v = getHex (8, out error);
1087                                 if (error)
1088                                         goto default;
1089                                 return v;
1090                         default:
1091                                 Report.Error (1009, Location, "Unrecognized escape sequence `\\{0}'", ((char)d).ToString ());
1092                                 return d;
1093                         }
1094                         getChar ();
1095                         return v;
1096                 }
1097
1098                 int getChar ()
1099                 {
1100                         int x;
1101                         if (putback_char != -1) {
1102                                 x = putback_char;
1103                                 putback_char = -1;
1104                         }
1105                         else
1106                                 x = reader.Read ();
1107                         if (x == '\n') {
1108                                 line++;
1109                                 ref_line++;
1110                                 previous_col = col;
1111                                 col = 0;
1112                         }
1113                         else
1114                                 col++;
1115                         return x;
1116                 }
1117
1118                 int peekChar ()
1119                 {
1120                         if (putback_char != -1)
1121                                 return putback_char;
1122                         putback_char = reader.Read ();
1123                         return putback_char;
1124                 }
1125
1126                 int peekChar2 ()
1127                 {
1128                         if (putback_char != -1)
1129                                 return putback_char;
1130                         return reader.Peek ();
1131                 }
1132                 
1133                 void putback (int c)
1134                 {
1135                         if (putback_char != -1){
1136                                 Console.WriteLine ("Col: " + col);
1137                                 Console.WriteLine ("Row: " + line);
1138                                 Console.WriteLine ("Name: " + ref_name.Name);
1139                                 Console.WriteLine ("Current [{0}] putting back [{1}]  ", putback_char, c);
1140                                 throw new Exception ("This should not happen putback on putback");
1141                         }
1142                         if (c == '\n' || col == 0) {
1143                                 // It won't happen though.
1144                                 line--;
1145                                 ref_line--;
1146                                 col = previous_col;
1147                         }
1148                         else
1149                                 col--;
1150                         putback_char = c;
1151                 }
1152
1153                 public bool advance ()
1154                 {
1155                         return peekChar () != -1;
1156                 }
1157
1158                 public Object Value {
1159                         get {
1160                                 return val;
1161                         }
1162                 }
1163
1164                 public Object value ()
1165                 {
1166                         return val;
1167                 }
1168
1169                 static bool IsCastToken (int token)
1170                 {
1171                         switch (token) {
1172                         case Token.BANG:
1173                         case Token.TILDE:
1174                         case Token.IDENTIFIER:
1175                         case Token.LITERAL_INTEGER:
1176                         case Token.LITERAL_FLOAT:
1177                         case Token.LITERAL_DOUBLE:
1178                         case Token.LITERAL_DECIMAL:
1179                         case Token.LITERAL_CHARACTER:
1180                         case Token.LITERAL_STRING:
1181                         case Token.BASE:
1182                         case Token.CHECKED:
1183                         case Token.FALSE:
1184                         case Token.FIXED:
1185                         case Token.NEW:
1186                         case Token.NULL:
1187                         case Token.SIZEOF:
1188                         case Token.THIS:
1189                         case Token.THROW:
1190                         case Token.TRUE:
1191                         case Token.TYPEOF:
1192                         case Token.UNCHECKED:
1193                         case Token.UNSAFE:
1194
1195                                 //
1196                                 // These can be part of a member access
1197                                 //
1198                         case Token.INT:
1199                         case Token.UINT:
1200                         case Token.SHORT:
1201                         case Token.USHORT:
1202                         case Token.LONG:
1203                         case Token.ULONG:
1204                         case Token.DOUBLE:
1205                         case Token.FLOAT:
1206                         case Token.CHAR:
1207                                 return true;
1208
1209                         default:
1210                                 return false;
1211                         }
1212                 }
1213
1214                 public int token ()
1215                 {
1216                         current_token = xtoken ();
1217                         return current_token;
1218                 }
1219
1220                 static StringBuilder static_cmd_arg = new System.Text.StringBuilder ();
1221                 
1222                 void get_cmd_arg (out string cmd, out string arg)
1223                 {
1224                         int c;
1225                         
1226                         tokens_seen = false;
1227                         arg = "";
1228                         static_cmd_arg.Length = 0;
1229
1230                         // skip over white space
1231                         while ((c = getChar ()) != -1 && (c != '\n') && ((c == '\r') || (c == ' ') || (c == '\t')))
1232                                 ;
1233
1234
1235                         while ((c != -1) && (c != '\n') && (c != ' ') && (c != '\t') && (c != '\r')){
1236                                 if (is_identifier_part_character ((char) c)){
1237                                         static_cmd_arg.Append ((char) c);
1238                                         c = getChar ();
1239                                 } else {
1240                                         putback (c);
1241                                         break;
1242                                 }
1243                         }
1244
1245                         cmd = static_cmd_arg.ToString ();
1246
1247                         if (c == '\n'){
1248                                 return;
1249                         }
1250
1251                         // skip over white space
1252                         while ((c = getChar ()) != -1 && (c != '\n') && ((c == '\r') || (c == ' ') || (c == '\t')))
1253                                 ;
1254
1255                         if (c == '\n'){
1256                                 return;
1257                         } else if (c == '\r'){
1258                                 return;
1259                         } else if (c == -1){
1260                                 arg = "";
1261                                 return;
1262                         }
1263                         
1264                         static_cmd_arg.Length = 0;
1265                         static_cmd_arg.Append ((char) c);
1266                         
1267                         while ((c = getChar ()) != -1 && (c != '\n') && (c != '\r')){
1268                                 static_cmd_arg.Append ((char) c);
1269                         }
1270
1271                         arg = static_cmd_arg.ToString ().Trim ();
1272                 }
1273
1274                 //
1275                 // Handles the #line directive
1276                 //
1277                 bool PreProcessLine (string arg)
1278                 {
1279                         if (arg.Length == 0)
1280                                 return false;
1281
1282                         if (arg == "default"){
1283                                 ref_line = line;
1284                                 ref_name = file_name;
1285                                 Location.Push (ref_name);
1286                                 return true;
1287                         } else if (arg == "hidden"){
1288                                 //
1289                                 // We ignore #line hidden
1290                                 //
1291                                 return true;
1292                         }
1293
1294                         try {
1295                                 int pos;
1296
1297                                 if ((pos = arg.IndexOf (' ')) != -1 && pos != 0){
1298                                         ref_line = System.Int32.Parse (arg.Substring (0, pos));
1299                                         pos++;
1300                                         
1301                                         char [] quotes = { '\"' };
1302                                         
1303                                         string name = arg.Substring (pos). Trim (quotes);
1304                                         ref_name = Location.LookupFile (name);
1305                                         file_name.HasLineDirective = true;
1306                                         ref_name.HasLineDirective = true;
1307                                         Location.Push (ref_name);
1308                                 } else {
1309                                         ref_line = System.Int32.Parse (arg);
1310                                 }
1311                         } catch {
1312                                 return false;
1313                         }
1314                         
1315                         return true;
1316                 }
1317
1318                 //
1319                 // Handles #define and #undef
1320                 //
1321                 void PreProcessDefinition (bool is_define, string arg)
1322                 {
1323                         if (arg.Length == 0 || arg == "true" || arg == "false"){
1324                                 Report.Error (1001, Location, "Missing identifer to pre-processor directive");
1325                                 return;
1326                         }
1327
1328                         char[] whitespace = { ' ', '\t' };
1329                         if (arg.IndexOfAny (whitespace) != -1){
1330                                 Report.Error (1025, Location, "Single-line comment or end-of-line expected");
1331                                 return;
1332                         }
1333
1334                         if (!is_identifier_start_character (arg [0]))
1335                                 Report.Error (1001, Location, "Identifier expected: " + arg);
1336                         
1337                         foreach (char c in arg.Substring (1)){
1338                                 if (!is_identifier_part_character (c)){
1339                                         Report.Error (1001, Location, "Identifier expected: " + arg);
1340                                         return;
1341                                 }
1342                         }
1343
1344                         if (is_define){
1345                                 if (defines == null)
1346                                         defines = new Hashtable ();
1347                                 define (arg);
1348                         } else {
1349                                 if (defines == null)
1350                                         return;
1351                                 if (defines.Contains (arg))
1352                                         defines.Remove (arg);
1353                         }
1354                 }
1355
1356                 /// <summary>
1357                 /// Handles #pragma directive
1358                 /// </summary>
1359                 void PreProcessPragma (string arg)
1360                 {
1361                         const string warning = "warning";
1362                         const string w_disable = "warning disable";
1363                         const string w_restore = "warning restore";
1364
1365                         if (arg == w_disable) {
1366                                 Report.RegisterWarningRegion (Location).WarningDisable (line);
1367                                 return;
1368                         }
1369
1370                         if (arg == w_restore) {
1371                                 Report.RegisterWarningRegion (Location).WarningEnable (line);
1372                                 return;
1373                         }
1374
1375                         if (arg.StartsWith (w_disable)) {
1376                                 int[] codes = ParseNumbers (arg.Substring (w_disable.Length));
1377                                 foreach (int code in codes) {
1378                                         if (code != 0)
1379                                                 Report.RegisterWarningRegion (Location).WarningDisable (Location, code);
1380                                 }
1381                                 return;
1382                         }
1383
1384                         if (arg.StartsWith (w_restore)) {
1385                                 int[] codes = ParseNumbers (arg.Substring (w_restore.Length));
1386                                 Hashtable w_table = Report.warning_ignore_table;
1387                                 foreach (int code in codes) {
1388                                         if (w_table != null && w_table.Contains (code))
1389                                                 Report.Warning (1635, 1, Location, String.Format ("Cannot restore warning `CS{0:0000}' because it was disabled globally", code));
1390                                         Report.RegisterWarningRegion (Location).WarningEnable (Location, code);
1391                                 }
1392                                 return;
1393                         }
1394
1395                         if (arg.StartsWith (warning)) {
1396                                 Report.Warning (1634, 1, Location, "Expected disable or restore");
1397                                 return;
1398                         }
1399
1400                         Report.Warning (1633, 1, Location, "Unrecognized #pragma directive");
1401                 }
1402
1403                 int[] ParseNumbers (string text)
1404                 {
1405                         string[] string_array = text.Split (',');
1406                         int[] values = new int [string_array.Length];
1407                         int index = 0;
1408                         foreach (string string_code in string_array) {
1409                                 try {
1410                                         values[index++] = int.Parse (string_code, System.Globalization.CultureInfo.InvariantCulture);
1411                                 }
1412                                 catch (FormatException) {
1413                                         Report.Warning (1692, 1, Location, "Invalid number");
1414                                 }
1415                         }
1416                         return values;
1417                 }
1418
1419                 bool eval_val (string s)
1420                 {
1421                         if (s == "true")
1422                                 return true;
1423                         if (s == "false")
1424                                 return false;
1425                         
1426                         if (defines == null)
1427                                 return false;
1428                         if (defines.Contains (s))
1429                                 return true;
1430
1431                         return false;
1432                 }
1433
1434                 bool pp_primary (ref string s)
1435                 {
1436                         s = s.Trim ();
1437                         int len = s.Length;
1438
1439                         if (len > 0){
1440                                 char c = s [0];
1441                                 
1442                                 if (c == '('){
1443                                         s = s.Substring (1);
1444                                         bool val = pp_expr (ref s);
1445                                         if (s.Length > 0 && s [0] == ')'){
1446                                                 s = s.Substring (1);
1447                                                 return val;
1448                                         }
1449                                         Error_InvalidDirective ();
1450                                         return false;
1451                                 }
1452                                 
1453                                 if (is_identifier_start_character (c)){
1454                                         int j = 1;
1455
1456                                         while (j < len){
1457                                                 c = s [j];
1458                                                 
1459                                                 if (is_identifier_part_character (c)){
1460                                                         j++;
1461                                                         continue;
1462                                                 }
1463                                                 bool v = eval_val (s.Substring (0, j));
1464                                                 s = s.Substring (j);
1465                                                 return v;
1466                                         }
1467                                         bool vv = eval_val (s);
1468                                         s = "";
1469                                         return vv;
1470                                 }
1471                         }
1472                         Error_InvalidDirective ();
1473                         return false;
1474                 }
1475                 
1476                 bool pp_unary (ref string s)
1477                 {
1478                         s = s.Trim ();
1479                         int len = s.Length;
1480
1481                         if (len > 0){
1482                                 if (s [0] == '!'){
1483                                         if (len > 1 && s [1] == '='){
1484                                                 Error_InvalidDirective ();
1485                                                 return false;
1486                                         }
1487                                         s = s.Substring (1);
1488                                         return ! pp_primary (ref s);
1489                                 } else
1490                                         return pp_primary (ref s);
1491                         } else {
1492                                 Error_InvalidDirective ();
1493                                 return false;
1494                         }
1495                 }
1496                 
1497                 bool pp_eq (ref string s)
1498                 {
1499                         bool va = pp_unary (ref s);
1500
1501                         s = s.Trim ();
1502                         int len = s.Length;
1503                         if (len > 0){
1504                                 if (s [0] == '='){
1505                                         if (len > 2 && s [1] == '='){
1506                                                 s = s.Substring (2);
1507                                                 return va == pp_unary (ref s);
1508                                         } else {
1509                                                 Error_InvalidDirective ();
1510                                                 return false;
1511                                         }
1512                                 } else if (s [0] == '!' && len > 1 && s [1] == '='){
1513                                         s = s.Substring (2);
1514
1515                                         return va != pp_unary (ref s);
1516
1517                                 } 
1518                         }
1519
1520                         return va;
1521                                 
1522                 }
1523                 
1524                 bool pp_and (ref string s)
1525                 {
1526                         bool va = pp_eq (ref s);
1527
1528                         s = s.Trim ();
1529                         int len = s.Length;
1530                         if (len > 0){
1531                                 if (s [0] == '&'){
1532                                         if (len > 2 && s [1] == '&'){
1533                                                 s = s.Substring (2);
1534                                                 return (va & pp_and (ref s));
1535                                         } else {
1536                                                 Error_InvalidDirective ();
1537                                                 return false;
1538                                         }
1539                                 } 
1540                         }
1541                         return va;
1542                 }
1543                 
1544                 //
1545                 // Evaluates an expression for `#if' or `#elif'
1546                 //
1547                 bool pp_expr (ref string s)
1548                 {
1549                         bool va = pp_and (ref s);
1550                         s = s.Trim ();
1551                         int len = s.Length;
1552                         if (len > 0){
1553                                 char c = s [0];
1554                                 
1555                                 if (c == '|'){
1556                                         if (len > 2 && s [1] == '|'){
1557                                                 s = s.Substring (2);
1558                                                 return va | pp_expr (ref s);
1559                                         } else {
1560                                                 Error_InvalidDirective ();
1561                                                 return false;
1562                                         }
1563                                 } 
1564                         }
1565                         
1566                         return va;
1567                 }
1568
1569                 bool eval (string s)
1570                 {
1571                         bool v = pp_expr (ref s);
1572                         s = s.Trim ();
1573                         if (s.Length != 0){
1574                                 return false;
1575                         }
1576
1577                         return v;
1578                 }
1579                 
1580                 void Error_InvalidDirective ()
1581                 {
1582                         Report.Error (1517, Location, "Invalid preprocessor directive");
1583                 }
1584
1585                 void Error_UnexpectedDirective (string extra)
1586                 {
1587                         Report.Error (
1588                                 1028, Location,
1589                                 "Unexpected processor directive (" + extra + ")");
1590                 }
1591
1592                 void Error_TokensSeen ()
1593                 {
1594                         Report.Error (1032, Location,
1595                                 "Cannot define or undefine preprocessor symbols after first token in file");
1596                 }
1597                 
1598                 //
1599                 // Set to false to stop handling preprocesser directives
1600                 // 
1601                 bool process_directives = true;
1602
1603                 //
1604                 // if true, then the code continues processing the code
1605                 // if false, the code stays in a loop until another directive is
1606                 // reached.
1607                 //
1608                 bool handle_preprocessing_directive (bool caller_is_taking)
1609                 {
1610                         string cmd, arg;
1611                         bool region_directive = false;
1612
1613                         get_cmd_arg (out cmd, out arg);
1614
1615                         // Eat any trailing whitespaces and single-line comments
1616                         if (arg.IndexOf ("//") != -1)
1617                                 arg = arg.Substring (0, arg.IndexOf ("//"));
1618                         arg = arg.TrimEnd (' ', '\t');
1619
1620                         //
1621                         // The first group of pre-processing instructions is always processed
1622                         //
1623                         switch (cmd){
1624                         case "pragma":
1625                                 if (RootContext.Version == LanguageVersion.ISO_1) {
1626                                         Report.FeatureIsNotStandardized (Location, "#pragma");
1627                                         return caller_is_taking;
1628                                 }
1629
1630                                 PreProcessPragma (arg);
1631                                 return caller_is_taking;
1632
1633                         case "line":
1634                                 if (!PreProcessLine (arg))
1635                                         Report.Error (
1636                                                 1576, Location,
1637                                                 "The line number specified for #line directive is missing or invalid");
1638                                 return caller_is_taking;
1639
1640                         case "region":
1641                                 region_directive = true;
1642                                 arg = "true";
1643                                 goto case "if";
1644
1645                         case "endregion":
1646                                 region_directive = true;
1647                                 goto case "endif";
1648                                 
1649                         case "if":
1650                                 if (arg.Length == 0){
1651                                         Error_InvalidDirective ();
1652                                         return true;
1653                                 }
1654                                 bool taking = false;
1655                                 if (ifstack == null)
1656                                         ifstack = new Stack (2);
1657
1658                                 if (ifstack.Count == 0){
1659                                         taking = true;
1660                                 } else {
1661                                         int state = (int) ifstack.Peek ();
1662                                         if ((state & TAKING) != 0)
1663                                                 taking = true;
1664                                 }
1665
1666                                 if (eval (arg) && taking){
1667                                         int push = TAKING | TAKEN_BEFORE | PARENT_TAKING;
1668                                         if (region_directive)
1669                                                 push |= REGION;
1670                                         ifstack.Push (push);
1671                                         return true;
1672                                 } else {
1673                                         int push = (taking ? PARENT_TAKING : 0);
1674                                         if (region_directive)
1675                                                 push |= REGION;
1676                                         ifstack.Push (push);
1677                                         return false;
1678                                 }
1679                                 
1680                         case "endif":
1681                                 if (ifstack == null || ifstack.Count == 0){
1682                                         Error_UnexpectedDirective ("no #if for this #endif");
1683                                         return true;
1684                                 } else {
1685                                         int pop = (int) ifstack.Pop ();
1686                                         
1687                                         if (region_directive && ((pop & REGION) == 0))
1688                                                 Report.Error (1027, Location, "Expected `#endif' directive");
1689                                         else if (!region_directive && ((pop & REGION) != 0))
1690                                                 Report.Error (1038, Location, "#endregion directive expected");
1691                                         
1692                                         if (!region_directive && arg.Length != 0) {
1693                                                 Report.Error (1025, Location, "Single-line comment or end-of-line expected");
1694                                         }
1695                                         
1696                                         if (ifstack.Count == 0)
1697                                                 return true;
1698                                         else {
1699                                                 int state = (int) ifstack.Peek ();
1700
1701                                                 if ((state & TAKING) != 0)
1702                                                         return true;
1703                                                 else
1704                                                         return false;
1705                                         }
1706                                 }
1707
1708                         case "elif":
1709                                 if (ifstack == null || ifstack.Count == 0){
1710                                         Error_UnexpectedDirective ("no #if for this #elif");
1711                                         return true;
1712                                 } else {
1713                                         int state = (int) ifstack.Peek ();
1714
1715                                         if ((state & REGION) != 0) {
1716                                                 Report.Error (1038, Location, "#endregion directive expected");
1717                                                 return true;
1718                                         }
1719
1720                                         if ((state & ELSE_SEEN) != 0){
1721                                                 Error_UnexpectedDirective ("#elif not valid after #else");
1722                                                 return true;
1723                                         }
1724
1725                                         if ((state & (TAKEN_BEFORE | TAKING)) != 0)
1726                                                 return false;
1727
1728                                         if (eval (arg) && ((state & PARENT_TAKING) != 0)){
1729                                                 state = (int) ifstack.Pop ();
1730                                                 ifstack.Push (state | TAKING | TAKEN_BEFORE);
1731                                                 return true;
1732                                         } else 
1733                                                 return false;
1734                                 }
1735
1736                         case "else":
1737                                 if (ifstack == null || ifstack.Count == 0){
1738                                         Error_UnexpectedDirective ("no #if for this #else");
1739                                         return true;
1740                                 } else {
1741                                         int state = (int) ifstack.Peek ();
1742
1743                                         if ((state & REGION) != 0) {
1744                                                 Report.Error (1038, Location, "#endregion directive expected");
1745                                                 return true;
1746                                         }
1747
1748                                         if ((state & ELSE_SEEN) != 0){
1749                                                 Error_UnexpectedDirective ("#else within #else");
1750                                                 return true;
1751                                         }
1752
1753                                         ifstack.Pop ();
1754
1755                                         bool ret;
1756                                         if ((state & TAKEN_BEFORE) == 0){
1757                                                 ret = ((state & PARENT_TAKING) != 0);
1758                                         } else
1759                                                 ret = false;
1760                                         
1761                                         if (ret)
1762                                                 state |= TAKING;
1763                                         else
1764                                                 state &= ~TAKING;
1765                                         
1766                                         ifstack.Push (state | ELSE_SEEN);
1767                                         
1768                                         return ret;
1769                                 }
1770                         }
1771
1772                         //
1773                         // These are only processed if we are in a `taking' block
1774                         //
1775                         if (!caller_is_taking)
1776                                 return false;
1777                                         
1778                         switch (cmd){
1779                         case "define":
1780                                 if (any_token_seen){
1781                                         Error_TokensSeen ();
1782                                         return true;
1783                                 }
1784                                 PreProcessDefinition (true, arg);
1785                                 return true;
1786
1787                         case "undef":
1788                                 if (any_token_seen){
1789                                         Error_TokensSeen ();
1790                                         return true;
1791                                 }
1792                                 PreProcessDefinition (false, arg);
1793                                 return true;
1794
1795                         case "error":
1796                                 Report.Error (1029, Location, "#error: '" + arg + "'");
1797                                 return true;
1798
1799                         case "warning":
1800                                 Report.Warning (1030, 1, Location, "#warning: `{0}'", arg);
1801                                 return true;
1802                         }
1803
1804                         Report.Error (1024, Location, "Wrong preprocessor directive");
1805                         return true;
1806
1807                 }
1808
1809                 private int consume_string (bool quoted)
1810                 {
1811                         int c;
1812                         string_builder.Length = 0;
1813                                                                 
1814                         while ((c = getChar ()) != -1){
1815                                 if (c == '"'){
1816                                         if (quoted && peekChar () == '"'){
1817                                                 string_builder.Append ((char) c);
1818                                                 getChar ();
1819                                                 continue;
1820                                         } else {
1821                                                 val = string_builder.ToString ();
1822                                                 return Token.LITERAL_STRING;
1823                                         }
1824                                 }
1825
1826                                 if (c == '\n'){
1827                                         if (!quoted)
1828                                                 Report.Error (1010, Location, "Newline in constant");
1829                                 }
1830
1831                                 if (!quoted){
1832                                         c = escape (c);
1833                                         if (c == -1)
1834                                                 return Token.ERROR;
1835                                 }
1836                                 string_builder.Append ((char) c);
1837                         }
1838
1839                         Report.Error (1039, Location, "Unterminated string literal");
1840                         return Token.EOF;
1841                 }
1842
1843                 private int consume_identifier (int s)
1844                 {
1845                         int res = consume_identifier (s, false);
1846
1847                         if (doc_state == XmlCommentState.Allowed)
1848                                 doc_state = XmlCommentState.NotAllowed;
1849                         switch (res) {
1850                         case Token.USING:
1851                         case Token.NAMESPACE:
1852                                 check_incorrect_doc_comment ();
1853                                 break;
1854                         }
1855
1856                         if (res == Token.PARTIAL) {
1857                                 // Save current position and parse next token.
1858                                 int old = reader.Position;
1859                                 int old_putback = putback_char;
1860                                 int old_ref_line = ref_line;
1861                                 int old_col = col;
1862
1863                                 putback_char = -1;
1864
1865                                 int next_token = token ();
1866                                 bool ok = (next_token == Token.CLASS) ||
1867                                         (next_token == Token.STRUCT) ||
1868                                         (next_token == Token.INTERFACE) ||
1869                                         (next_token == Token.ENUM); // "partial" is a keyword in 'partial enum', even though it's not valid
1870
1871                                 reader.Position = old;
1872                                 ref_line = old_ref_line;
1873                                 col = old_col;
1874                                 putback_char = old_putback;
1875
1876                                 if (ok)
1877                                         return res;
1878                                 else {
1879                                         val = new LocatedToken (Location, "partial");
1880                                         return Token.IDENTIFIER;
1881                                 }
1882                         }
1883
1884                         return res;
1885                 }
1886
1887                 private int consume_identifier (int s, bool quoted) 
1888                 {
1889                         int pos = 1;
1890                         int c = -1;
1891                         
1892                         id_builder [0] = (char) s;
1893
1894                         current_location = new Location (ref_line, Col);
1895
1896                         while ((c = getChar ()) != -1) {
1897                         loop:
1898                                 if (is_identifier_part_character ((char) c)){
1899                                         if (pos == max_id_size){
1900                                                 Report.Error (645, Location, "Identifier too long (limit is 512 chars)");
1901                                                 return Token.ERROR;
1902                                         }
1903                                         
1904                                         id_builder [pos++] = (char) c;
1905 //                                      putback_char = -1;
1906                                 } else if (c == '\\') {
1907                                         c = escape (c);
1908                                         goto loop;
1909                                 } else {
1910 //                                      putback_char = c;
1911                                         putback (c);
1912                                         break;
1913                                 }
1914                         }
1915
1916                         //
1917                         // Optimization: avoids doing the keyword lookup
1918                         // on uppercase letters and _
1919                         //
1920                         if (!quoted && (s >= 'a' || s == '_')){
1921                                 int keyword = GetKeyword (id_builder, pos);
1922                                 if (keyword != -1) {
1923                                         val = Location;
1924                                         return keyword;
1925                                 }
1926                         }
1927
1928                         //
1929                         // Keep identifiers in an array of hashtables to avoid needless
1930                         // allocations
1931                         //
1932
1933                         if (identifiers [pos] != null) {
1934                                 val = identifiers [pos][id_builder];
1935                                 if (val != null) {
1936                                         val = new LocatedToken (Location, (string) val);
1937                                         if (quoted)
1938                                                 escapedIdentifiers.Add (val);
1939                                         return Token.IDENTIFIER;
1940                                 }
1941                         }
1942                         else
1943                                 identifiers [pos] = new CharArrayHashtable (pos);
1944
1945                         val = new String (id_builder, 0, pos);
1946                         if (RootContext.Version == LanguageVersion.ISO_1) {
1947                                 for (int i = 1; i < id_builder.Length; i += 3) {
1948                                         if (id_builder [i] == '_' && (id_builder [i - 1] == '_' || id_builder [i + 1] == '_')) {
1949                                                 Report.Error (1638, Location, 
1950                                                         "`{0}': Any identifier with double underscores cannot be used when ISO language version mode is specified", val.ToString ());
1951                                                 break;
1952                                         }
1953                                 }
1954                         }
1955
1956                         char [] chars = new char [pos];
1957                         Array.Copy (id_builder, chars, pos);
1958
1959                         identifiers [pos] [chars] = val;
1960
1961                         val = new LocatedToken (Location, (string) val);
1962                         if (quoted)
1963                                 escapedIdentifiers.Add (val);
1964                         return Token.IDENTIFIER;
1965                 }
1966                 
1967                 public int xtoken ()
1968                 {
1969                         int t;
1970                         bool doread = false;
1971                         int c;
1972
1973                         // Whether we have seen comments on the current line
1974                         bool comments_seen = false;
1975                         
1976                         val = null;
1977                         // optimization: eliminate col and implement #directive semantic correctly.
1978                         for (;(c = getChar ()) != -1;) {
1979                                 if (c == ' ')
1980                                         continue;
1981                                 
1982                                 if (c == '\t') {
1983                                         continue;
1984                                 }
1985                                 
1986                                 if (c == ' ' || c == '\f' || c == '\v' || c == 0xa0)
1987                                         continue;
1988
1989                                 if (c == '\r') {
1990                                         if (peekChar () == '\n')
1991                                                 getChar ();
1992
1993                                         any_token_seen |= tokens_seen;
1994                                         tokens_seen = false;
1995                                         comments_seen = false;
1996                                         continue;
1997                                 }
1998
1999                                 // Handle double-slash comments.
2000                                 if (c == '/'){
2001                                         int d = peekChar ();
2002                                 
2003                                         if (d == '/'){
2004                                                 getChar ();
2005                                                 if (RootContext.Documentation != null && peekChar () == '/') {
2006                                                         getChar ();
2007                                                         // Don't allow ////.
2008                                                         if ((d = peekChar ()) != '/') {
2009                                                                 update_comment_location ();
2010                                                                 if (doc_state == XmlCommentState.Allowed)
2011                                                                         handle_one_line_xml_comment ();
2012                                                                 else if (doc_state == XmlCommentState.NotAllowed)
2013                                                                         warn_incorrect_doc_comment ();
2014                                                         }
2015                                                 }
2016                                                 while ((d = getChar ()) != -1 && (d != '\n') && d != '\r')
2017                                                 if (d == '\n'){
2018                                                 }
2019                                                 any_token_seen |= tokens_seen;
2020                                                 tokens_seen = false;
2021                                                 comments_seen = false;
2022                                                 continue;
2023                                         } else if (d == '*'){
2024                                                 getChar ();
2025                                                 bool docAppend = false;
2026                                                 if (RootContext.Documentation != null && peekChar () == '*') {
2027                                                         getChar ();
2028                                                         update_comment_location ();
2029                                                         // But when it is /**/, just do nothing.
2030                                                         if (peekChar () == '/') {
2031                                                                 getChar ();
2032                                                                 continue;
2033                                                         }
2034                                                         if (doc_state == XmlCommentState.Allowed)
2035                                                                 docAppend = true;
2036                                                         else if (doc_state == XmlCommentState.NotAllowed)
2037                                                                 warn_incorrect_doc_comment ();
2038                                                 }
2039
2040                                                 int current_comment_start = 0;
2041                                                 if (docAppend) {
2042                                                         current_comment_start = xml_comment_buffer.Length;
2043                                                         xml_comment_buffer.Append (Environment.NewLine);
2044                                                 }
2045
2046                                                 Location start_location = Location;
2047
2048                                                 while ((d = getChar ()) != -1){
2049                                                         if (d == '*' && peekChar () == '/'){
2050                                                                 getChar ();
2051                                                                 comments_seen = true;
2052                                                                 break;
2053                                                         }
2054                                                         if (docAppend)
2055                                                                 xml_comment_buffer.Append ((char) d);
2056                                                         
2057                                                         if (d == '\n'){
2058                                                                 any_token_seen |= tokens_seen;
2059                                                                 tokens_seen = false;
2060                                                                 // 
2061                                                                 // Reset 'comments_seen' just to be consistent.
2062                                                                 // It doesn't matter either way, here.
2063                                                                 //
2064                                                                 comments_seen = false;
2065                                                         }
2066                                                 }
2067                                                 if (!comments_seen)
2068                                                         Report.Error (1035, start_location, "End-of-file found, '*/' expected");
2069
2070                                                 if (docAppend)
2071                                                         update_formatted_doc_comment (current_comment_start);
2072                                                 continue;
2073                                         }
2074                                         goto is_punct_label;
2075                                 }
2076
2077                                 
2078                                 if (c == '\\' || is_identifier_start_character ((char)c)){
2079                                         tokens_seen = true;
2080                                         return consume_identifier (c);
2081                                 }
2082
2083                         is_punct_label:
2084                                 current_location = new Location (ref_line, Col);
2085                                 if ((t = is_punct ((char)c, ref doread)) != Token.ERROR){
2086                                         tokens_seen = true;
2087                                         if (doread){
2088                                                 getChar ();
2089                                         }
2090                                         return t;
2091                                 }
2092
2093                                 // white space
2094                                 if (c == '\n'){
2095                                         any_token_seen |= tokens_seen;
2096                                         tokens_seen = false;
2097                                         comments_seen = false;
2098                                         continue;
2099                                 }
2100
2101                                 if (c >= '0' && c <= '9'){
2102                                         tokens_seen = true;
2103                                         return is_number (c);
2104                                 }
2105
2106                                 if (c == '.'){
2107                                         tokens_seen = true;
2108                                         int peek = peekChar ();
2109                                         if (peek >= '0' && peek <= '9')
2110                                                 return is_number (c);
2111                                         return Token.DOT;
2112                                 }
2113                                 
2114                                 /* For now, ignore pre-processor commands */
2115                                 // FIXME: In C# the '#' is not limited to appear
2116                                 // on the first column.
2117                                 if (c == '#') {
2118                                         // return NONE if we're not processing directives (during token peeks)
2119                                         if (!process_directives)
2120                                                 return Token.NONE;
2121
2122                                         bool cont = true;
2123                                         if (tokens_seen || comments_seen) {
2124                                                error_details = "Preprocessor directives must appear as the first" +
2125                                                " non-whitespace character on a line.";
2126
2127                                                Report.Error (1040, Location, error_details);
2128
2129                                                return Token.ERROR;
2130                                        }
2131                                         
2132                                 start_again:
2133                                         
2134                                         cont = handle_preprocessing_directive (cont);
2135
2136                                         if (cont){
2137                                                 continue;
2138                                         }
2139
2140                                         bool skipping = false;
2141                                         for (;(c = getChar ()) != -1;){
2142                                                 if (c == '\n'){
2143                                                         skipping = false;
2144                                                 } else if (c == ' ' || c == '\t' || c == '\v' || c == '\r' || c == 0xa0)
2145                                                         continue;
2146                                                 else if (c != '#')
2147                                                         skipping = true;
2148                                                 if (c == '#' && !skipping)
2149                                                         goto start_again;
2150                                         }
2151                                         any_token_seen |= tokens_seen;
2152                                         tokens_seen = false;
2153                                         if (c == -1)
2154                                                 Report.Error (1027, Location, "Expected `#endif' directive");
2155                                         continue;
2156                                 }
2157                                 
2158                                 if (c == '"') 
2159                                         return consume_string (false);
2160
2161                                 if (c == '\''){
2162                                         c = getChar ();
2163                                         tokens_seen = true;
2164                                         if (c == '\''){
2165                                                 error_details = "Empty character literal";
2166                                                 Report.Error (1011, Location, error_details);
2167                                                 return Token.ERROR;
2168                                         }
2169                                         if (c == '\r' || c == '\n') {
2170                                                 Report.Error (1010, Location, "Newline in constant");
2171                                                 return Token.ERROR;
2172                                         }
2173                                         c = escape (c);
2174                                         if (c == -1)
2175                                                 return Token.ERROR;
2176                                         val = new System.Char ();
2177                                         val = (char) c;
2178                                         c = getChar ();
2179
2180                                         if (c != '\''){
2181                                                 error_details = "Too many characters in character literal";
2182                                                 Report.Error (1012, Location, error_details);
2183
2184                                                 // Try to recover, read until newline or next "'"
2185                                                 while ((c = getChar ()) != -1){
2186                                                         if (c == '\n'){
2187                                                                 break;
2188                                                         }
2189                                                         else if (c == '\'')
2190                                                                 break;
2191                                                 }
2192                                                 return Token.ERROR;
2193                                         }
2194                                         return Token.LITERAL_CHARACTER;
2195                                 }
2196                                 
2197                                 if (c == '@') {
2198                                         c = getChar ();
2199                                         if (c == '"') {
2200                                                 tokens_seen = true;
2201                                                 return consume_string (true);
2202                                         } else if (is_identifier_start_character ((char) c)){
2203                                                 return consume_identifier (c, true);
2204                                         } else {
2205                                                 Report.Error (1646, Location, "Keyword, identifier, or string expected after verbatim specifier: @");
2206                                         }
2207                                 }
2208
2209                                 error_details = ((char)c).ToString ();
2210                                 
2211                                 return Token.ERROR;
2212                         }
2213
2214                         return Token.EOF;
2215                 }
2216
2217                 //
2218                 // Handles one line xml comment
2219                 //
2220                 private void handle_one_line_xml_comment ()
2221                 {
2222                         int c;
2223                         while ((c = peekChar ()) == ' ')
2224                                 getChar (); // skip heading whitespaces.
2225                         while ((c = peekChar ()) != -1 && c != '\n' && c != '\r') {
2226                                 xml_comment_buffer.Append ((char) getChar ());
2227                         }
2228                         if (c == '\r' || c == '\n')
2229                                 xml_comment_buffer.Append (Environment.NewLine);
2230                 }
2231
2232                 //
2233                 // Remove heading "*" in Javadoc-like xml documentation.
2234                 //
2235                 private void update_formatted_doc_comment (int current_comment_start)
2236                 {
2237                         int length = xml_comment_buffer.Length - current_comment_start;
2238                         string [] lines = xml_comment_buffer.ToString (
2239                                 current_comment_start,
2240                                 length).Replace ("\r", "").Split ('\n');
2241                         
2242                         // The first line starts with /**, thus it is not target
2243                         // for the format check.
2244                         for (int i = 1; i < lines.Length; i++) {
2245                                 string s = lines [i];
2246                                 int idx = s.IndexOf ('*');
2247                                 string head = null;
2248                                 if (idx < 0) {
2249                                         if (i < lines.Length - 1)
2250                                                 return;
2251                                         head = s;
2252                                 } else
2253                                         head = s.Substring (0, idx);
2254                                 foreach (char c in head)
2255                                         if (c != ' ')
2256                                                 return;
2257                                 lines [i] = s.Substring (idx + 1);
2258                         }
2259                         xml_comment_buffer.Remove (current_comment_start, length);
2260                         xml_comment_buffer.Insert (current_comment_start, String.Join (Environment.NewLine, lines));
2261                 }
2262
2263                 //
2264                 // Updates current comment location.
2265                 //
2266                 private void update_comment_location ()
2267                 {
2268                         if (current_comment_location.IsNull) {
2269                                 // "-2" is for heading "//" or "/*"
2270                                 current_comment_location =
2271                                         new Location (ref_line, col - 2);
2272                         }
2273                 }
2274
2275                 //
2276                 // Checks if there was incorrect doc comments and raise
2277                 // warnings.
2278                 //
2279                 public void check_incorrect_doc_comment ()
2280                 {
2281                         if (xml_comment_buffer.Length > 0)
2282                                 warn_incorrect_doc_comment ();
2283                 }
2284
2285                 //
2286                 // Raises a warning when tokenizer found incorrect doccomment
2287                 // markup.
2288                 //
2289                 private void warn_incorrect_doc_comment ()
2290                 {
2291                         if (doc_state != XmlCommentState.Error) {
2292                                 doc_state = XmlCommentState.Error;
2293                                 // in csc, it is 'XML comment is not placed on 
2294                                 // a valid language element'. But that does not
2295                                 // make sense.
2296                                 Report.Warning (1587, 2, Location, "XML comment is not placed on a valid language element");
2297                         }
2298                 }
2299
2300                 //
2301                 // Consumes the saved xml comment lines (if any)
2302                 // as for current target member or type.
2303                 //
2304                 public string consume_doc_comment ()
2305                 {
2306                         if (xml_comment_buffer.Length > 0) {
2307                                 string ret = xml_comment_buffer.ToString ();
2308                                 reset_doc_comment ();
2309                                 return ret;
2310                         }
2311                         return null;
2312                 }
2313
2314                 void reset_doc_comment ()
2315                 {
2316                         xml_comment_buffer.Length = 0;
2317                         current_comment_location = Location.Null;
2318                 }
2319
2320                 public void cleanup ()
2321                 {
2322                         if (ifstack != null && ifstack.Count >= 1) {
2323                                 int state = (int) ifstack.Pop ();
2324                                 if ((state & REGION) != 0)
2325                                         Report.Error (1038, Location, "#endregion directive expected");
2326 //                              else 
2327 //                                      Report.Error (1027, Location, "Expected `#endif' directive");
2328                         }
2329                 }
2330         }
2331
2332         //
2333         // Indicates whether it accepts XML documentation or not.
2334         //
2335         public enum XmlCommentState {
2336                 // comment is allowed in this state.
2337                 Allowed,
2338                 // comment is not allowed in this state.
2339                 NotAllowed,
2340                 // once comments appeared when it is NotAllowed, then the
2341                 // state is changed to it, until the state is changed to
2342                 // .Allowed.
2343                 Error
2344         }
2345 }